refactor(ooniprobe): migrate database to internal (#979)

See https://github.com/ooni/probe/issues/2352

Co-authored-by: decfox <decfox@github.com>
Co-authored-by: Simone Basso <bassosimone@gmail.com>
This commit is contained in:
DecFox 2022-11-15 15:05:30 +05:30 committed by GitHub
parent d6def35286
commit 6b01264373
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 53 additions and 56 deletions

View File

@ -6,8 +6,8 @@ import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/output"
"github.com/ooni/probe-cli/v3/internal/database"
)
func init() {

View File

@ -8,7 +8,7 @@ import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
"github.com/ooni/probe-cli/v3/internal/database"
"github.com/upper/db/v4"
)

View File

@ -4,8 +4,8 @@ import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/output"
"github.com/ooni/probe-cli/v3/internal/database"
)
func init() {

View File

@ -8,8 +8,8 @@ import (
"time"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils"
"github.com/ooni/probe-cli/v3/internal/database"
)
func formatSpeed(speed float64) string {

View File

@ -8,9 +8,9 @@ import (
"github.com/apex/log"
"github.com/fatih/color"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/output"
"github.com/ooni/probe-cli/v3/internal/database"
engine "github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/pkg/errors"

View File

@ -7,8 +7,8 @@ import (
"path"
"testing"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni"
"github.com/ooni/probe-cli/v3/internal/database"
"github.com/ooni/probe-cli/v3/internal/model"
)

View File

@ -7,8 +7,8 @@ import (
"time"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni"
"github.com/ooni/probe-cli/v3/internal/database"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/pkg/errors"
)

View File

@ -11,10 +11,9 @@ import (
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/config"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/enginex"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/database"
"github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/legacy/assetsdir"
@ -26,6 +25,11 @@ import (
// DefaultSoftwareName is the default software name.
const DefaultSoftwareName = "ooniprobe-cli"
// logger is the logger used by the engine.
var logger = log.WithFields(log.Fields{
"type": "engine",
})
// ProbeCLI is the OONI Probe CLI context.
type ProbeCLI interface {
Config() *config.Config
@ -231,7 +235,7 @@ func (p *Probe) NewSession(ctx context.Context, runType model.RunType) (*engine.
}
return engine.NewSession(ctx, engine.SessionConfig{
KVStore: kvstore,
Logger: enginex.Logger,
Logger: logger,
SoftwareName: softwareName,
SoftwareVersion: p.softwareVersion,
TempDir: p.tempDir,

View File

@ -8,7 +8,7 @@ import (
"github.com/apex/log"
"github.com/mitchellh/go-wordwrap"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/database"
"github.com/ooni/probe-cli/v3/internal/database"
)
// MeasurementJSON prints the JSON of a measurement

View File

@ -1,11 +1,9 @@
package utils
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils/homedir"
)
@ -53,26 +51,6 @@ func FileExists(path string) bool {
return err == nil && stat.Mode().IsRegular()
}
// ResultTimestamp is a windows friendly timestamp
const ResultTimestamp = "2006-01-02T150405.999999999Z0700"
// MakeResultsDir creates and returns a directory for the result
func MakeResultsDir(home string, name string, ts time.Time) (string, error) {
p := filepath.Join(home, "msmts",
fmt.Sprintf("%s-%s", name, ts.Format(ResultTimestamp)))
// If the path already exists, this is a problem. It should not clash, because
// we are using nanosecond precision for the starttime.
if _, e := os.Stat(p); e == nil {
return "", errors.New("results path already exists")
}
err := os.MkdirAll(p, 0700)
if err != nil {
return "", err
}
return p, nil
}
// GetOONIHome returns the path to the OONI Home
func GetOONIHome() (string, error) {
if ooniHome := os.Getenv("OONI_HOME"); ooniHome != "" {
@ -96,8 +74,5 @@ func DidLegacyInformedConsent() bool {
}
path := filepath.Join(filepath.Join(home, ".ooni"), "initialized")
if FileExists(path) {
return true
}
return false
return FileExists(path)
}

View File

@ -12,8 +12,7 @@ import (
"time"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/enginex"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils"
"github.com/ooni/probe-cli/v3/internal/engine"
"github.com/pkg/errors"
"github.com/upper/db/v4"
)
@ -65,7 +64,7 @@ func GetMeasurementJSON(sess db.Session, measurementID int64) (map[string]interf
}
query := url.Values{}
query.Add("report_id", reportID)
if measurement.URL.URL.Valid == true {
if measurement.URL.URL.Valid {
query.Add("input", measurement.URL.URL.String)
}
measurementURL.RawQuery = query.Encode()
@ -84,7 +83,7 @@ func GetMeasurementJSON(sess db.Session, measurementID int64) (map[string]interf
}
// MeasurementFilePath might be NULL because the measurement from a
// 3.0.0-beta install
if measurement.Measurement.MeasurementFilePath.Valid == false {
if !measurement.Measurement.MeasurementFilePath.Valid {
log.Error("invalid measurement_file_path")
log.Error("backup your OONI_HOME and run `ooniprobe reset`")
return nil, errors.New("cannot access measurement file")
@ -254,7 +253,7 @@ func CreateMeasurement(sess db.Session, reportID sql.NullString, testName string
func CreateResult(sess db.Session, homePath string, testGroupName string, networkID int64) (*Result, error) {
startTime := time.Now().UTC()
p, err := utils.MakeResultsDir(homePath, testGroupName, startTime)
p, err := makeResultsDir(homePath, testGroupName, startTime)
if err != nil {
return nil, err
}
@ -276,7 +275,7 @@ func CreateResult(sess db.Session, homePath string, testGroupName string, networ
}
// CreateNetwork will create a new network in the network table
func CreateNetwork(sess db.Session, loc enginex.LocationProvider) (*Network, error) {
func CreateNetwork(sess db.Session, loc engine.LocationProvider) (*Network, error) {
network := Network{
ASN: loc.ProbeASN(),
CountryCode: loc.ProbeCC(),
@ -351,7 +350,7 @@ func AddTestKeys(sess db.Session, msmt *Measurement, tk interface{}) error {
// the IsAnomaly field of bool type.
// Maybe generics are not so bad after-all, heh golang?
isAnomalyValue := reflect.ValueOf(tk).FieldByName("IsAnomaly")
if isAnomalyValue.IsValid() == true && isAnomalyValue.Kind() == reflect.Bool {
if isAnomalyValue.IsValid() && isAnomalyValue.Kind() == reflect.Bool {
isAnomaly = isAnomalyValue.Bool()
isAnomalyValid = true
}

View File

@ -99,7 +99,7 @@ type PerformanceTestKeys struct {
// Finished marks the result as done and sets the runtime
func (r *Result) Finished(sess db.Session) error {
if r.IsDone == true || r.Runtime != 0 {
if r.IsDone || r.Runtime != 0 {
return errors.New("Result is already finished")
}
r.Runtime = time.Now().UTC().Sub(r.StartTime).Seconds()

View File

@ -0,0 +1,29 @@
package database
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
)
// resultTimestamp is a windows friendly timestamp
const resultTimestamp = "2006-01-02T150405.999999999Z0700"
// makeResultsDir creates and returns a directory for the result
func makeResultsDir(home string, name string, ts time.Time) (string, error) {
p := filepath.Join(home, "msmts",
fmt.Sprintf("%s-%s", name, ts.Format(resultTimestamp)))
// If the path already exists, this is a problem. It should not clash, because
// we are using nanosecond precision for the starttime.
if _, e := os.Stat(p); e == nil {
return "", errors.New("results path already exists")
}
err := os.MkdirAll(p, 0700)
if err != nil {
return "", err
}
return p, nil
}

View File

@ -1,14 +1,4 @@
// Package enginex contains ooni/probe-engine extensions.
package enginex
import (
"github.com/apex/log"
)
// Logger is the logger used by the engine.
var Logger = log.WithFields(log.Fields{
"type": "engine",
})
package engine
// LocationProvider is an interface that returns the current location. The
// github.com/ooni/probe-cli/v3/internal/engine/session.Session implements it.