🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
package appmetrics
import (
"database/sql"
"encoding/json"
"errors"
"strings"
2021-09-01 12:02:18 +00:00
"time"
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
"github.com/xeipuuv/gojsonschema"
)
type AppMetricEventType string
// Value is `json.RawMessage` so we can send any json shape, including strings
// Validation is handled using JSON schemas defined in validators.go, instead of Golang structs
type AppMetric struct {
2021-09-01 12:02:18 +00:00
ID int ` json:"-" `
MessageID string ` json:"message_id" `
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
Event AppMetricEventType ` json:"event" `
Value json . RawMessage ` json:"value" `
AppVersion string ` json:"app_version" `
OS string ` json:"os" `
2021-04-12 13:46:11 +00:00
SessionID string ` json:"session_id" `
2021-09-01 12:02:18 +00:00
CreatedAt time . Time ` json:"created_at" `
Processed bool ` json:"processed" `
ReceivedAt time . Time ` json:"received_at" `
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
}
type AppMetricValidationError struct {
Metric AppMetric
Errors [ ] gojsonschema . ResultError
}
2021-05-21 08:34:28 +00:00
type Page struct {
AppMetrics [ ] AppMetric
TotalCount int
}
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
const (
// status-react navigation events
2021-04-12 12:25:53 +00:00
NavigateTo AppMetricEventType = "navigate-to"
ScreensOnWillFocus AppMetricEventType = "screens/on-will-focus"
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
)
// EventSchemaMap Every event should have a schema attached
var EventSchemaMap = map [ AppMetricEventType ] interface { } {
2021-04-12 12:25:53 +00:00
NavigateTo : NavigateToCofxSchema ,
ScreensOnWillFocus : NavigateToCofxSchema ,
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
}
func NewDB ( db * sql . DB ) * Database {
return & Database { db : db }
}
// Database sql wrapper for operations with browser objects.
type Database struct {
db * sql . DB
}
// Close closes database.
func ( db Database ) Close ( ) error {
return db . db . Close ( )
}
func jsonschemaErrorsToError ( validationErrors [ ] AppMetricValidationError ) error {
var fieldErrors [ ] string
for _ , appMetricValidationError := range validationErrors {
metric := appMetricValidationError . Metric
errors := appMetricValidationError . Errors
var errorDesc string = "Error in event: " + string ( metric . Event ) + " - "
for _ , e := range errors {
errorDesc = errorDesc + "value." + e . Context ( ) . String ( ) + ":" + e . Description ( )
}
fieldErrors = append ( fieldErrors , errorDesc )
}
return errors . New ( strings . Join ( fieldErrors [ : ] , "/ " ) )
}
func ( db * Database ) ValidateAppMetrics ( appMetrics [ ] AppMetric ) ( err error ) {
var calculatedErrors [ ] AppMetricValidationError
for _ , metric := range appMetrics {
schema := EventSchemaMap [ metric . Event ]
if schema == nil {
return errors . New ( "No schema defined for: " + string ( metric . Event ) )
}
schemaLoader := gojsonschema . NewGoLoader ( schema )
valLoader := gojsonschema . NewStringLoader ( string ( metric . Value ) )
res , err := gojsonschema . Validate ( schemaLoader , valLoader )
if err != nil {
return err
}
// validate all metrics and save errors
if ! res . Valid ( ) {
calculatedErrors = append ( calculatedErrors , AppMetricValidationError { metric , res . Errors ( ) } )
}
}
if len ( calculatedErrors ) > 0 {
return jsonschemaErrorsToError ( calculatedErrors )
}
return
}
2021-04-12 13:46:11 +00:00
func ( db * Database ) SaveAppMetrics ( appMetrics [ ] AppMetric , sessionID string ) ( err error ) {
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
var (
tx * sql . Tx
insert * sql . Stmt
)
// make sure that the shape of the metric is same as expected
err = db . ValidateAppMetrics ( appMetrics )
if err != nil {
return err
}
// start txn
tx , err = db . db . Begin ( )
if err != nil {
return err
}
defer func ( ) {
if err == nil {
err = tx . Commit ( )
return
}
_ = tx . Rollback ( )
} ( )
2021-09-01 12:02:18 +00:00
insert , err = tx . Prepare ( "INSERT INTO app_metrics (event, value, app_version, operating_system, session_id, processed) VALUES (?, ?, ?, ?, ?, ?)" )
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
if err != nil {
return err
}
for _ , metric := range appMetrics {
2021-09-01 12:02:18 +00:00
_ , err = insert . Exec ( metric . Event , metric . Value , metric . AppVersion , metric . OS , sessionID , metric . Processed )
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
if err != nil {
return
}
}
return
}
2021-05-21 08:34:28 +00:00
func ( db * Database ) GetAppMetrics ( limit int , offset int ) ( page Page , err error ) {
countErr := db . db . QueryRow ( "SELECT count(*) FROM app_metrics" ) . Scan ( & page . TotalCount )
if countErr != nil {
return page , countErr
}
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
2021-09-01 12:02:18 +00:00
rows , err := db . db . Query ( "SELECT id, event, value, app_version, operating_system, session_id, created_at, processed FROM app_metrics LIMIT ? OFFSET ?" , limit , offset )
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
if err != nil {
2021-05-21 08:34:28 +00:00
return page , err
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
}
defer rows . Close ( )
2021-05-21 08:34:28 +00:00
2021-09-01 12:02:18 +00:00
page . AppMetrics , err = db . getFromRows ( rows )
return page , err
}
func ( db * Database ) getFromRows ( rows * sql . Rows ) ( appMetrics [ ] AppMetric , err error ) {
var metrics [ ] AppMetric
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
for rows . Next ( ) {
metric := AppMetric { }
2021-09-01 12:02:18 +00:00
err = rows . Scan (
& metric . ID ,
& metric . Event ,
& metric . Value ,
& metric . AppVersion ,
& metric . OS ,
& metric . SessionID ,
& metric . CreatedAt ,
& metric . Processed ,
2021-04-12 13:46:11 +00:00
)
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
if err != nil {
2021-09-01 12:02:18 +00:00
return metrics , err
}
metrics = append ( metrics , metric )
}
return metrics , nil
}
func ( db * Database ) GetUnprocessed ( ) ( [ ] AppMetric , error ) {
rows , err := db . db . Query ( "SELECT id, event, value, app_version, operating_system, session_id, created_at, processed FROM app_metrics WHERE processed IS ? ORDER BY session_id ASC, created_at ASC" , false )
if err != nil {
return nil , err
}
defer rows . Close ( )
return db . getFromRows ( rows )
}
func ( db * Database ) GetUnprocessedGroupedBySession ( ) ( map [ string ] [ ] AppMetric , error ) {
uam , err := db . GetUnprocessed ( )
if err != nil {
return nil , err
}
out := map [ string ] [ ] AppMetric { }
for _ , am := range uam {
out [ am . SessionID ] = append ( out [ am . SessionID ] , am )
}
return out , nil
}
func ( db * Database ) SetToProcessedByIDs ( ids [ ] int ) ( err error ) {
var (
tx * sql . Tx
update * sql . Stmt
)
// start txn
tx , err = db . db . Begin ( )
if err != nil {
return err
}
defer func ( ) {
if err == nil {
err = tx . Commit ( )
return
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
}
2021-09-01 12:02:18 +00:00
_ = tx . Rollback ( )
} ( )
// Generate prepared statement IN list
in := "("
for i := 0 ; i < len ( ids ) ; i ++ {
in += "?,"
}
in = in [ : len ( in ) - 1 ] + ")"
2022-03-28 10:10:40 +00:00
update , err = tx . Prepare ( "UPDATE app_metrics SET processed = 1 WHERE id IN " + in ) // nolint: gosec
2021-09-01 12:02:18 +00:00
if err != nil {
return err
}
// Convert the ids into Stmt.Exec compatible variadic
args := make ( [ ] interface { } , 0 , len ( ids ) )
for _ , id := range ids {
args = append ( args , id )
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
}
2021-09-01 12:02:18 +00:00
_ , err = update . Exec ( args ... )
if err != nil {
return
}
return
}
func ( db * Database ) SetToProcessed ( appMetrics [ ] AppMetric ) ( err error ) {
ids := GetAppMetricsIDs ( appMetrics )
return db . SetToProcessedByIDs ( ids )
}
func ( db * Database ) GetMessagesOlderThan ( date * time . Time ) ( [ ] AppMetric , error ) {
rows , err := db . db . Query ( "SELECT id, event, value, app_version, operating_system, session_id, created_at, processed FROM app_metrics WHERE created_at < ?" , date )
if err != nil {
return nil , err
}
defer rows . Close ( )
return db . getFromRows ( rows )
}
func ( db * Database ) DeleteOlderThan ( date * time . Time ) ( err error ) {
var (
tx * sql . Tx
d * sql . Stmt
)
// start txn
tx , err = db . db . Begin ( )
if err != nil {
return err
}
defer func ( ) {
if err == nil {
err = tx . Commit ( )
return
}
_ = tx . Rollback ( )
} ( )
d , err = tx . Prepare ( "DELETE FROM app_metrics WHERE created_at < ?" )
if err != nil {
return err
}
_ , err = d . Exec ( date )
if err != nil {
return
}
return
}
func GetAppMetricsIDs ( appMetrics [ ] AppMetric ) [ ] int {
var ids [ ] int
for _ , am := range appMetrics {
ids = append ( ids , am . ID )
}
return ids
🎭 📊 Anonymous Metrics V0 (#2170)
* Migrations in place, how to run them?
* Remove down migrations and touch database.go
* Database and Database Test package in place, added functions to get and store app metrics
* make generate output
* Minor bug fix on app metrics insert and select
* Add a validation layer to restrict what can be saved in the database
* Make validation more terse, throw error if schema doesn't exist, expose appmetrics service
* service updates
* Compute all errors before sending them out
* Trying to bring a closjure to appmetrics go
* Expose appmetrics via an api, skip fancy
* Address value as Jason Dawt Rawmasage to ease parsing
* Introduce a buffered chan with magic cap of 8 to minimize writes to DB. Tests for service and API. Also expose GetAppMetrics function.
* Lint issues
* Remove autoincrement, undo waku.json changes, fix error being shadowed, return nil where nil ought to be returned, get rid of buffered channel
* Bump migration number
* Fix API factory usage
* Add comment re:json.RawMessage instead of strings
* Get rid of test vars, throw save error inside the loop
* Update version
Co-authored-by: Samuel Hawksby-Robinson <samuel@samyoul.com>
2021-03-17 12:39:28 +00:00
}