Add support for deleting measurements

This commit is contained in:
Arturo Filastò 2018-09-17 17:30:29 +02:00
parent 1fe8eedfb3
commit 52bfe5df6c
3 changed files with 102 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package database
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"os"
"reflect" "reflect"
"time" "time"
@ -112,24 +113,40 @@ func ListResults(sess sqlbuilder.Database) ([]ResultNetwork, []ResultNetwork, er
incompleteResults := []ResultNetwork{} incompleteResults := []ResultNetwork{}
req := sess.Select( req := sess.Select(
"networks.id AS network_id",
"results.id AS result_id",
db.Raw("networks.*"), db.Raw("networks.*"),
db.Raw("results.*"), db.Raw("results.*"),
).From("results"). ).From("results").
Join("networks").On("results.network_id = networks.id"). Join("networks").On("results.network_id = networks.network_id").
OrderBy("results.start_time") OrderBy("results.result_start_time")
if err := req.Where("is_done = true").All(&doneResults); err != nil { if err := req.Where("result_is_done = true").All(&doneResults); err != nil {
return doneResults, incompleteResults, errors.Wrap(err, "failed to get result done list") return doneResults, incompleteResults, errors.Wrap(err, "failed to get result done list")
} }
if err := req.Where("is_done = false").All(&incompleteResults); err != nil { if err := req.Where("result_is_done = false").All(&incompleteResults); err != nil {
return doneResults, incompleteResults, errors.Wrap(err, "failed to get result done list") return doneResults, incompleteResults, errors.Wrap(err, "failed to get result done list")
} }
return doneResults, incompleteResults, nil return doneResults, incompleteResults, nil
} }
// DeleteResult will delete a particular result and the relative measurement on
// disk.
func DeleteResult(sess sqlbuilder.Database, resultID int64) error {
var result Result
res := sess.Collection("results").Find("result_id", resultID)
if err := res.One(&result); err != nil {
log.WithError(err).Error("error in obtaining the result")
return err
}
if err := res.Delete(); err != nil {
log.WithError(err).Error("failed to delete the result directory")
return err
}
os.RemoveAll(result.MeasurementDir)
return nil
}
// CreateMeasurement writes the measurement to the database a returns a pointer // CreateMeasurement writes the measurement to the database a returns a pointer
// to the Measurement // to the Measurement
func CreateMeasurement(sess sqlbuilder.Database, reportID sql.NullString, testName string, resultID int64, reportFilePath string, urlID sql.NullInt64) (*Measurement, error) { func CreateMeasurement(sess sqlbuilder.Database, reportID sql.NullString, testName string, resultID int64, reportFilePath string, urlID sql.NullInt64) (*Measurement, error) {

View File

@ -84,6 +84,80 @@ func TestMeasurementWorkflow(t *testing.T) {
} }
} }
func TestDeleteResult(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "dbtest")
if err != nil {
t.Fatal(err)
}
fmt.Printf("%s", tmpfile.Name())
//defer os.Remove(tmpfile.Name())
tmpdir, err := ioutil.TempDir("", "oonitest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
sess, err := Connect(tmpfile.Name())
if err != nil {
t.Fatal(err)
}
location := utils.LocationInfo{
ASN: 0,
CountryCode: "IT",
NetworkName: "Unknown",
}
network, err := CreateNetwork(sess, &location)
if err != nil {
t.Fatal(err)
}
result, err := CreateResult(sess, tmpdir, "websites", network.ID)
if err != nil {
t.Fatal(err)
}
reportID := sql.NullString{String: "", Valid: false}
testName := "antani"
resultID := result.ID
reportFilePath := tmpdir
urlID := sql.NullInt64{Int64: 0, Valid: false}
m1, err := CreateMeasurement(sess, reportID, testName, resultID, reportFilePath, urlID)
if err != nil {
t.Fatal(err)
}
var m2 Measurement
err = sess.Collection("measurements").Find("measurement_id", m1.ID).One(&m2)
if err != nil {
t.Fatal(err)
}
if m2.ResultID != m1.ResultID {
t.Error("result_id mismatch")
}
err = DeleteResult(sess, resultID)
if err != nil {
t.Fatal(err)
}
totalResults, err := sess.Collection("results").Find().Count()
if err != nil {
t.Fatal(err)
}
totalMeasurements, err := sess.Collection("measurements").Find().Count()
if err != nil {
t.Fatal(err)
}
if totalResults != 0 {
t.Fatal("results should be zero")
}
if totalMeasurements != 0 {
t.Fatal("measurements should be zero")
}
}
func TestNetworkCreate(t *testing.T) { func TestNetworkCreate(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "dbtest") tmpfile, err := ioutil.TempFile("", "dbtest")
if err != nil { if err != nil {

View File

@ -30,8 +30,13 @@ func RunMigrations(db *sql.DB) error {
func Connect(path string) (db sqlbuilder.Database, err error) { func Connect(path string) (db sqlbuilder.Database, err error) {
settings := sqlite.ConnectionURL{ settings := sqlite.ConnectionURL{
Database: path, Database: path,
Options: map[string]string{"_foreign_keys": "1"},
} }
sess, err := sqlite.Open(settings) sess, err := sqlite.Open(settings)
if err != nil {
log.WithError(err).Error("failed to open the DB")
return nil, err
}
err = RunMigrations(sess.Driver().(*sql.DB)) err = RunMigrations(sess.Driver().(*sql.DB))
if err != nil { if err != nil {