ooni-probe-cli/internal/database/models.go

106 lines
2.9 KiB
Go
Raw Normal View History

package database
2018-02-13 17:11:22 +01:00
import (
"time"
"github.com/apex/log"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
// Measurement model
type Measurement struct {
2018-02-13 17:11:22 +01:00
ID int64 `db:"id"`
Name string `db:"name"`
2018-02-13 17:11:22 +01:00
StartTime time.Time `db:"start_time"`
EndTime time.Time `db:"end_time"`
Summary string `db:"summary"` // XXX this should be JSON
2018-02-13 17:11:22 +01:00
ASN int64 `db:"asn"`
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"`
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,
:asn,:ip,:country,
2018-02-13 17:11:22 +01:00
:state,:failure,:report_file,
: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
}
// 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"`
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 {
2018-03-08 11:53:04 +01:00
log.Debugf("Updating result %v", r)
_, err := db.NamedExec(`UPDATE results SET
(name, start_time, end_time, summary, done, data_usage_up, data_usage_down) =
(:name, :start_time, :end_time, :summary, :done, :data_usage_up, :data_usage_down)
WHERE id = :id`, r)
if err != nil {
return errors.Wrap(err, "updating result")
}
2018-02-13 17:11:22 +01:00
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) {
2018-03-08 11:53:04 +01:00
log.Debugf("Creating result %v", r)
2018-02-13 17:11:22 +01:00
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
}