Get some data into the database
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
_ "github.com/mattn/go-sqlite3" // this is needed to load the sqlite3 driver
|
||||
ooni "github.com/openobservatory/gooni"
|
||||
"github.com/openobservatory/gooni/internal/bindata"
|
||||
"github.com/pkg/errors"
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
// RunMigrations runs the database migrations
|
||||
func RunMigrations(db *sqlx.DB) error {
|
||||
log.Debugf("running migrations")
|
||||
migrations := &migrate.AssetMigrationSource{
|
||||
Asset: bindata.Asset,
|
||||
AssetDir: bindata.AssetDir,
|
||||
@@ -27,19 +28,24 @@ func RunMigrations(db *sqlx.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Connect to the database
|
||||
func Connect(path string) (*sqlx.DB, error) {
|
||||
db, err := sqlx.Connect("sqlite3", path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// XXX RunMigrations(db)
|
||||
err = RunMigrations(db)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// DefaultDatabasePath for the main database
|
||||
func DefaultDatabasePath() (string, error) {
|
||||
home, err := ooni.GetOONIHome()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "default database path")
|
||||
}
|
||||
return filepath.Join(home, "db", "main.db"), nil
|
||||
return filepath.Join(home, "db", "main.sqlite3"), nil
|
||||
}
|
||||
|
||||
+78
-15
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user