Get some data into the database
This commit is contained in:
parent
d3d3ce9d78
commit
e7ee54436e
6 changed files with 137 additions and 65 deletions
|
|
@ -1,34 +1,97 @@
|
|||
package database
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Measurement model
|
||||
type Measurement struct {
|
||||
ID int `db:"id"`
|
||||
ID int64 `db:"id"`
|
||||
Name string `db:"name"`
|
||||
StartTime time.Time `db:"startTime"`
|
||||
EndTime time.Time `db:"endTime"`
|
||||
StartTime time.Time `db:"start_time"`
|
||||
EndTime time.Time `db:"end_time"`
|
||||
Summary string `db:"summary"` // XXX this should be JSON
|
||||
ASN int `db:"asn"`
|
||||
ASN int64 `db:"asn"`
|
||||
IP string `db:"ip"`
|
||||
CountryCode string `db:"country"`
|
||||
State string `db:"state"`
|
||||
Failure string `db:"failure"`
|
||||
ReportFilePath string `db:"reportFile"`
|
||||
ReportID string `db:"reportId"`
|
||||
ReportFilePath string `db:"report_file"`
|
||||
ReportID string `db:"report_id"`
|
||||
Input string `db:"input"`
|
||||
MeasurementID string `db:"measurementId"`
|
||||
ResultID string `db:"resultId"`
|
||||
MeasurementID string `db:"measurement_id"`
|
||||
ResultID string `db:"result_id"`
|
||||
}
|
||||
|
||||
// CreateMeasurement writes the measurement to the database a returns a pointer
|
||||
// to the Measurement
|
||||
func CreateMeasurement(db *sqlx.DB, m Measurement) (*Measurement, error) {
|
||||
res, err := db.NamedExec(`INSERT INTO measurements
|
||||
(name, start_time,
|
||||
summary, asn, ip, country,
|
||||
state, failure, report_file,
|
||||
report_id, input, measurement_id,
|
||||
result_id)
|
||||
VALUES (:name,:start_time,
|
||||
:summary,:asn,:ip,:country,
|
||||
:state,:failure,:report_file,
|
||||
:report_id,:input,:measurement_id,
|
||||
:result_id)`,
|
||||
m)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "creating measurement")
|
||||
}
|
||||
id, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "creating measurement")
|
||||
}
|
||||
m.ID = id
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
// Update the measurement in the database
|
||||
func (r Measurement) Update(db *sqlx.DB) error {
|
||||
// XXX implement me
|
||||
return nil
|
||||
}
|
||||
|
||||
// Result model
|
||||
type Result struct {
|
||||
ID int `db:"id"`
|
||||
Name int `db:"name"`
|
||||
StartTime time.Time `db:"startTime"`
|
||||
EndTime time.Time `db:"endTime"`
|
||||
ID int64 `db:"id"`
|
||||
Name string `db:"name"`
|
||||
StartTime time.Time `db:"start_time"`
|
||||
EndTime time.Time `db:"end_time"`
|
||||
Summary string `db:"summary"` // XXX this should be JSON
|
||||
Done bool `db:"done"`
|
||||
DataUsageUp int `db:"dataUsageUp"`
|
||||
DataUsageDown int `db:"dataUsageDown"`
|
||||
DataUsageUp int64 `db:"data_usage_up"`
|
||||
DataUsageDown int64 `db:"data_usage_down"`
|
||||
}
|
||||
|
||||
// Update the Result in the database
|
||||
func (r Result) Update(db *sqlx.DB) error {
|
||||
// XXX implement me
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateResult writes the Result to the database a returns a pointer
|
||||
// to the Result
|
||||
func CreateResult(db *sqlx.DB, r Result) (*Result, error) {
|
||||
log.Debugf("Creating result %s", r)
|
||||
res, err := db.NamedExec(`INSERT INTO results
|
||||
(name, start_time)
|
||||
VALUES (:name,:start_time)`,
|
||||
r)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "creating result")
|
||||
}
|
||||
id, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "creating measurement")
|
||||
}
|
||||
r.ID = id
|
||||
return &r, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue