2018-02-12 17:29:03 +01:00
|
|
|
package database
|
|
|
|
|
2018-02-13 17:11:22 +01:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
2018-02-12 17:29:03 +01:00
|
|
|
|
|
|
|
// Measurement model
|
|
|
|
type Measurement struct {
|
2018-02-13 17:11:22 +01:00
|
|
|
ID int64 `db:"id"`
|
2018-02-12 17:29:03 +01:00
|
|
|
Name string `db:"name"`
|
2018-02-13 17:11:22 +01:00
|
|
|
StartTime time.Time `db:"start_time"`
|
|
|
|
EndTime time.Time `db:"end_time"`
|
2018-02-12 17:29:03 +01:00
|
|
|
Summary string `db:"summary"` // XXX this should be JSON
|
2018-02-13 17:11:22 +01:00
|
|
|
ASN int64 `db:"asn"`
|
2018-02-12 17:29:03 +01:00
|
|
|
IP string `db:"ip"`
|
|
|
|
CountryCode string `db:"country"`
|
|
|
|
State string `db:"state"`
|
|
|
|
Failure string `db:"failure"`
|
2018-02-13 17:11:22 +01:00
|
|
|
ReportFilePath string `db:"report_file"`
|
|
|
|
ReportID string `db:"report_id"`
|
2018-02-12 17:29:03 +01:00
|
|
|
Input string `db:"input"`
|
2018-02-13 17:11:22 +01:00
|
|
|
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,
|
2018-02-21 16:06:30 +01:00
|
|
|
:asn,:ip,:country,
|
2018-02-13 17:11:22 +01:00
|
|
|
:state,:failure,:report_file,
|
2018-02-21 16:06:30 +01:00
|
|
|
:report_id,:input,
|
2018-02-13 17:11:22 +01:00
|
|
|
: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
|
2018-02-12 17:29:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Result model
|
|
|
|
type Result struct {
|
2018-02-13 17:11:22 +01:00
|
|
|
ID int64 `db:"id"`
|
|
|
|
Name string `db:"name"`
|
|
|
|
StartTime time.Time `db:"start_time"`
|
|
|
|
EndTime time.Time `db:"end_time"`
|
2018-02-12 17:29:03 +01:00
|
|
|
Summary string `db:"summary"` // XXX this should be JSON
|
|
|
|
Done bool `db:"done"`
|
2018-02-13 17:11:22 +01:00
|
|
|
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
|
2018-02-12 17:29:03 +01:00
|
|
|
}
|