Skip to content

Commit

Permalink
fix: Fixed Activity data handling for StartLocation, EndLocation, and…
Browse files Browse the repository at this point in the history
… Map

- 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ç <[email protected]>
  • Loading branch information
bahattincinic committed Jun 8, 2024
1 parent 1edd1c3 commit 6ae1aba
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
20 changes: 20 additions & 0 deletions database/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions database/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 10 additions & 3 deletions importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions models/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 6ae1aba

Please sign in to comment.