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:
		
							parent
							
								
									d6def35286
								
							
						
					
					
						commit
						6b01264373
					
				| @ -6,8 +6,8 @@ import ( | |||||||
| 	"github.com/alecthomas/kingpin" | 	"github.com/alecthomas/kingpin" | ||||||
| 	"github.com/apex/log" | 	"github.com/apex/log" | ||||||
| 	"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root" | 	"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/cmd/ooniprobe/internal/output" | ||||||
|  | 	"github.com/ooni/probe-cli/v3/internal/database" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func init() { | func init() { | ||||||
|  | |||||||
| @ -8,7 +8,7 @@ import ( | |||||||
| 	"github.com/alecthomas/kingpin" | 	"github.com/alecthomas/kingpin" | ||||||
| 	"github.com/apex/log" | 	"github.com/apex/log" | ||||||
| 	"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root" | 	"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" | 	"github.com/upper/db/v4" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -4,8 +4,8 @@ import ( | |||||||
| 	"github.com/alecthomas/kingpin" | 	"github.com/alecthomas/kingpin" | ||||||
| 	"github.com/apex/log" | 	"github.com/apex/log" | ||||||
| 	"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root" | 	"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/cmd/ooniprobe/internal/output" | ||||||
|  | 	"github.com/ooni/probe-cli/v3/internal/database" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func init() { | func init() { | ||||||
|  | |||||||
| @ -8,8 +8,8 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"github.com/apex/log" | 	"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/cmd/ooniprobe/internal/utils" | ||||||
|  | 	"github.com/ooni/probe-cli/v3/internal/database" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func formatSpeed(speed float64) string { | func formatSpeed(speed float64) string { | ||||||
|  | |||||||
| @ -8,9 +8,9 @@ import ( | |||||||
| 
 | 
 | ||||||
| 	"github.com/apex/log" | 	"github.com/apex/log" | ||||||
| 	"github.com/fatih/color" | 	"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/ooni" | ||||||
| 	"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/output" | 	"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" | 	engine "github.com/ooni/probe-cli/v3/internal/engine" | ||||||
| 	"github.com/ooni/probe-cli/v3/internal/model" | 	"github.com/ooni/probe-cli/v3/internal/model" | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
|  | |||||||
| @ -7,8 +7,8 @@ import ( | |||||||
| 	"path" | 	"path" | ||||||
| 	"testing" | 	"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/cmd/ooniprobe/internal/ooni" | ||||||
|  | 	"github.com/ooni/probe-cli/v3/internal/database" | ||||||
| 	"github.com/ooni/probe-cli/v3/internal/model" | 	"github.com/ooni/probe-cli/v3/internal/model" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -7,8 +7,8 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"github.com/apex/log" | 	"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/cmd/ooniprobe/internal/ooni" | ||||||
|  | 	"github.com/ooni/probe-cli/v3/internal/database" | ||||||
| 	"github.com/ooni/probe-cli/v3/internal/model" | 	"github.com/ooni/probe-cli/v3/internal/model" | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
| ) | ) | ||||||
|  | |||||||
| @ -11,10 +11,9 @@ import ( | |||||||
| 
 | 
 | ||||||
| 	"github.com/apex/log" | 	"github.com/apex/log" | ||||||
| 	"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/config" | 	"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/cmd/ooniprobe/internal/utils" | ||||||
| 	"github.com/ooni/probe-cli/v3/internal/atomicx" | 	"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/engine" | ||||||
| 	"github.com/ooni/probe-cli/v3/internal/kvstore" | 	"github.com/ooni/probe-cli/v3/internal/kvstore" | ||||||
| 	"github.com/ooni/probe-cli/v3/internal/legacy/assetsdir" | 	"github.com/ooni/probe-cli/v3/internal/legacy/assetsdir" | ||||||
| @ -26,6 +25,11 @@ import ( | |||||||
| // DefaultSoftwareName is the default software name. | // DefaultSoftwareName is the default software name. | ||||||
| const DefaultSoftwareName = "ooniprobe-cli" | 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. | // ProbeCLI is the OONI Probe CLI context. | ||||||
| type ProbeCLI interface { | type ProbeCLI interface { | ||||||
| 	Config() *config.Config | 	Config() *config.Config | ||||||
| @ -231,7 +235,7 @@ func (p *Probe) NewSession(ctx context.Context, runType model.RunType) (*engine. | |||||||
| 	} | 	} | ||||||
| 	return engine.NewSession(ctx, engine.SessionConfig{ | 	return engine.NewSession(ctx, engine.SessionConfig{ | ||||||
| 		KVStore:         kvstore, | 		KVStore:         kvstore, | ||||||
| 		Logger:          enginex.Logger, | 		Logger:          logger, | ||||||
| 		SoftwareName:    softwareName, | 		SoftwareName:    softwareName, | ||||||
| 		SoftwareVersion: p.softwareVersion, | 		SoftwareVersion: p.softwareVersion, | ||||||
| 		TempDir:         p.tempDir, | 		TempDir:         p.tempDir, | ||||||
|  | |||||||
| @ -8,7 +8,7 @@ import ( | |||||||
| 
 | 
 | ||||||
| 	"github.com/apex/log" | 	"github.com/apex/log" | ||||||
| 	"github.com/mitchellh/go-wordwrap" | 	"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 | // MeasurementJSON prints the JSON of a measurement | ||||||
|  | |||||||
| @ -1,11 +1,9 @@ | |||||||
| package utils | package utils | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"errors" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"time" |  | ||||||
| 
 | 
 | ||||||
| 	"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils/homedir" | 	"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() | 	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 | // GetOONIHome returns the path to the OONI Home | ||||||
| func GetOONIHome() (string, error) { | func GetOONIHome() (string, error) { | ||||||
| 	if ooniHome := os.Getenv("OONI_HOME"); ooniHome != "" { | 	if ooniHome := os.Getenv("OONI_HOME"); ooniHome != "" { | ||||||
| @ -96,8 +74,5 @@ func DidLegacyInformedConsent() bool { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	path := filepath.Join(filepath.Join(home, ".ooni"), "initialized") | 	path := filepath.Join(filepath.Join(home, ".ooni"), "initialized") | ||||||
| 	if FileExists(path) { | 	return FileExists(path) | ||||||
| 		return true |  | ||||||
| 	} |  | ||||||
| 	return false |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -12,8 +12,7 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"github.com/apex/log" | 	"github.com/apex/log" | ||||||
| 	"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/enginex" | 	"github.com/ooni/probe-cli/v3/internal/engine" | ||||||
| 	"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/utils" |  | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
| 	"github.com/upper/db/v4" | 	"github.com/upper/db/v4" | ||||||
| ) | ) | ||||||
| @ -65,7 +64,7 @@ func GetMeasurementJSON(sess db.Session, measurementID int64) (map[string]interf | |||||||
| 		} | 		} | ||||||
| 		query := url.Values{} | 		query := url.Values{} | ||||||
| 		query.Add("report_id", reportID) | 		query.Add("report_id", reportID) | ||||||
| 		if measurement.URL.URL.Valid == true { | 		if measurement.URL.URL.Valid { | ||||||
| 			query.Add("input", measurement.URL.URL.String) | 			query.Add("input", measurement.URL.URL.String) | ||||||
| 		} | 		} | ||||||
| 		measurementURL.RawQuery = query.Encode() | 		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 | 	// MeasurementFilePath might be NULL because the measurement from a | ||||||
| 	// 3.0.0-beta install | 	// 3.0.0-beta install | ||||||
| 	if measurement.Measurement.MeasurementFilePath.Valid == false { | 	if !measurement.Measurement.MeasurementFilePath.Valid { | ||||||
| 		log.Error("invalid measurement_file_path") | 		log.Error("invalid measurement_file_path") | ||||||
| 		log.Error("backup your OONI_HOME and run `ooniprobe reset`") | 		log.Error("backup your OONI_HOME and run `ooniprobe reset`") | ||||||
| 		return nil, errors.New("cannot access measurement file") | 		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) { | func CreateResult(sess db.Session, homePath string, testGroupName string, networkID int64) (*Result, error) { | ||||||
| 	startTime := time.Now().UTC() | 	startTime := time.Now().UTC() | ||||||
| 
 | 
 | ||||||
| 	p, err := utils.MakeResultsDir(homePath, testGroupName, startTime) | 	p, err := makeResultsDir(homePath, testGroupName, startTime) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		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 | // 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{ | 	network := Network{ | ||||||
| 		ASN:         loc.ProbeASN(), | 		ASN:         loc.ProbeASN(), | ||||||
| 		CountryCode: loc.ProbeCC(), | 		CountryCode: loc.ProbeCC(), | ||||||
| @ -351,7 +350,7 @@ func AddTestKeys(sess db.Session, msmt *Measurement, tk interface{}) error { | |||||||
| 	// the IsAnomaly field of bool type. | 	// the IsAnomaly field of bool type. | ||||||
| 	// Maybe generics are not so bad after-all, heh golang? | 	// Maybe generics are not so bad after-all, heh golang? | ||||||
| 	isAnomalyValue := reflect.ValueOf(tk).FieldByName("IsAnomaly") | 	isAnomalyValue := reflect.ValueOf(tk).FieldByName("IsAnomaly") | ||||||
| 	if isAnomalyValue.IsValid() == true && isAnomalyValue.Kind() == reflect.Bool { | 	if isAnomalyValue.IsValid() && isAnomalyValue.Kind() == reflect.Bool { | ||||||
| 		isAnomaly = isAnomalyValue.Bool() | 		isAnomaly = isAnomalyValue.Bool() | ||||||
| 		isAnomalyValid = true | 		isAnomalyValid = true | ||||||
| 	} | 	} | ||||||
| @ -99,7 +99,7 @@ type PerformanceTestKeys struct { | |||||||
| 
 | 
 | ||||||
| // Finished marks the result as done and sets the runtime | // Finished marks the result as done and sets the runtime | ||||||
| func (r *Result) Finished(sess db.Session) error { | 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") | 		return errors.New("Result is already finished") | ||||||
| 	} | 	} | ||||||
| 	r.Runtime = time.Now().UTC().Sub(r.StartTime).Seconds() | 	r.Runtime = time.Now().UTC().Sub(r.StartTime).Seconds() | ||||||
							
								
								
									
										29
									
								
								internal/database/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								internal/database/utils.go
									
									
									
									
									
										Normal 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 | ||||||
|  | } | ||||||
| @ -1,14 +1,4 @@ | |||||||
| // Package enginex contains ooni/probe-engine extensions. | package engine | ||||||
| package enginex |  | ||||||
| 
 |  | ||||||
| import ( |  | ||||||
| 	"github.com/apex/log" |  | ||||||
| ) |  | ||||||
| 
 |  | ||||||
| // Logger is the logger used by the engine. |  | ||||||
| var Logger = log.WithFields(log.Fields{ |  | ||||||
| 	"type": "engine", |  | ||||||
| }) |  | ||||||
| 
 | 
 | ||||||
| // LocationProvider is an interface that returns the current location. The | // LocationProvider is an interface that returns the current location. The | ||||||
| // github.com/ooni/probe-cli/v3/internal/engine/session.Session implements it. | // github.com/ooni/probe-cli/v3/internal/engine/session.Session implements it. | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user