From 6ae1abada464caef068da3c88bb3f7153ead659c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bahattin=20=C3=87ini=C3=A7?= Date: Sat, 8 Jun 2024 12:19:28 +0300 Subject: [PATCH] fix: Fixed Activity data handling for StartLocation, EndLocation, and Map MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Converted Activity StartLocation and EndLocation from a stringified array to a JSON array for easier parsing. - Decoded the encoded polyline map data in Activity and stored the decoded data in the database for client-side usability. Signed-off-by: Bahattin Çiniç --- database/query.go | 20 ++++++++++++++++++++ database/schema.go | 3 +++ importer/importer.go | 13 ++++++++++--- models/activity.go | 4 ++-- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/database/query.go b/database/query.go index 264d790..7686c6f 100644 --- a/database/query.go +++ b/database/query.go @@ -8,6 +8,21 @@ import ( pkgerrors "github.com/pkg/errors" ) +// compileQuery compiles a SQL query template with predefined date parameters. +// It supports placeholders for various date ranges like today, yesterday, +// this week, last week, this month, and last month. +// +// The supported placeholders in the query template are: +// - {{.Today}}: The current date in "YYYY-MM-DD" format +// - {{.Yesterday}}: The date of the previous day +// - {{.ThisWeekStart}}: The start date of the current week (Monday) +// - {{.ThisWeekEnd}}: The end date of the current week (Sunday) +// - {{.LastWeekStart}}: The start date of the previous week (Monday) +// - {{.LastWeekEnd}}: The end date of the previous week (Sunday) +// - {{.ThisMonthStart}}: The first day of the current month +// - {{.ThisMonthEnd}}: The last day of the current month +// - {{.LastMonthStart}}: The first day of the previous month +// - {{.LastMonthEnd}}: The last day of the previous month func (d *Database) compileQuery(query string) (string, error) { tmpl, err := template.New("query").Parse(query) if err != nil { @@ -48,6 +63,11 @@ func (d *Database) compileQuery(query string) (string, error) { return builder.String(), nil } +// RunQuery runs a SQL query after compiling it with date placeholders. +// It executes the query and returns the results as a slice of maps, where +// each map represents a row with column names as keys and column values as values. +// Parameters: +// - query: The SQL query template containing placeholders. func (d *Database) RunQuery(query string) ([]map[string]interface{}, error) { compiledQuery, err := d.compileQuery(query) if err != nil { diff --git a/database/schema.go b/database/schema.go index 4720284..eabd753 100644 --- a/database/schema.go +++ b/database/schema.go @@ -8,12 +8,15 @@ import ( "gorm.io/gorm/schema" ) +// Schema represents the schema information of a database field. type Schema struct { TableName string `json:"table_name"` DBName string `json:"field_db_name"` Type string `json:"type"` } +// GetModelsSchema retrieves the schema information of the database models. +// It parses the fields of predefined models (Activity, Athlete, Gear) and returns their schema details. func (d *Database) GetModelsSchema() ([]Schema, error) { var resp []Schema diff --git a/importer/importer.go b/importer/importer.go index 49ad618..b91a3cd 100644 --- a/importer/importer.go +++ b/importer/importer.go @@ -12,6 +12,7 @@ import ( pkgerrors "github.com/pkg/errors" client "github.com/strava/go.strava" "go.uber.org/zap" + "gorm.io/datatypes" "gorm.io/gorm" ) @@ -94,7 +95,13 @@ func (im *Importer) updateActivities(tx *gorm.DB, activities []*client.ActivityS var rows []models.Activity for _, activity := range activities { - actMap, err := json.Marshal(activity.Map) + actMap, err := json.Marshal(map[string]interface{}{ + "id": activity.Map.Id, + "polyline": activity.Map.Polyline, + "polyline_decoded": activity.Map.Polyline.Decode(), + "summary_polyline_decoded": activity.Map.SummaryPolyline.Decode(), + "summary_polyline": activity.Map.SummaryPolyline, + }) if err != nil { return pkgerrors.Wrap(err, "Marshal") } @@ -113,8 +120,8 @@ func (im *Importer) updateActivities(tx *gorm.DB, activities []*client.ActivityS StartDate: activity.StartDate, StartDateLocal: activity.StartDateLocal, TimeZone: activity.TimeZone, - StartLocation: activity.StartLocation.String(), - EndLocation: activity.EndLocation.String(), + StartLocation: datatypes.JSON(activity.StartLocation.String()), + EndLocation: datatypes.JSON(activity.EndLocation.String()), City: activity.City, State: activity.State, Country: activity.Country, diff --git a/models/activity.go b/models/activity.go index 5ed72a9..da47561 100644 --- a/models/activity.go +++ b/models/activity.go @@ -19,8 +19,8 @@ type Activity struct { StartDate time.Time `json:"start_date"` StartDateLocal time.Time `json:"start_date_local"` TimeZone string `json:"time_zone"` - StartLocation string `json:"start_lat_lng"` - EndLocation string `json:"end_lat_lng"` + StartLocation datatypes.JSON `json:"start_lat_lng"` + EndLocation datatypes.JSON `json:"end_lat_lng"` City string `json:"location_city"` State string `json:"location_state"` Country string `json:"location_country"`