From 0d5b976e9731d7868ba940f340ade92233caae91 Mon Sep 17 00:00:00 2001 From: valentinkolb Date: Wed, 13 Nov 2024 02:32:45 +0100 Subject: [PATCH] fix(analyticsApi): fixed last_30_days_data (7) --- analyticsApi/main.go | 54 +++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/analyticsApi/main.go b/analyticsApi/main.go index b37166f..43a9b8e 100644 --- a/analyticsApi/main.go +++ b/analyticsApi/main.go @@ -72,6 +72,9 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { if err != nil { return apis.NewBadRequestError("Invalid start date format, expected ISO 8601", err) } + } else { + // Default to the beginning of time if not provided + startDate = time.Time{} } // Pagination and sorting parameters @@ -86,8 +89,8 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { } sort := c.QueryParam("sort") - if sort != "DESC" { - sort = "ASC" // default sorting order + if sort != "ASC" && sort != "DESC" { + sort = "DESC" // default sorting order } offset := (page - 1) * perPage @@ -101,7 +104,7 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { WHERE created >= {:startDate} `). Bind(dbx.Params{ - "startDate": startDate, + "startDate": startDate.Format("2006-01-02 15:04:05"), }). Row(&totalItems) @@ -118,30 +121,23 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { } type Item struct { - ID int `json:"id"` Path string `json:"path"` Count int `json:"count"` Last30DaysData []Last30DaysData `json:"last_30_days_data"` } - // Query paginated items, retrieving last_30_days_data as a JSON string + // Query paginated items var rawItems []struct { - ID int `json:"id"` - Path string `json:"path"` - Count int `json:"count"` - Last30DaysData string `json:"last_30_days_data"` // JSON string to parse + Path string `db:"path" json:"path"` + Count int `db:"count" json:"count"` + Last30DaysData string `db:"last_30_days_data" json:"last_30_days_data"` // JSON string to parse } err = app.Dao().DB(). NewQuery(` - SELECT - (ROW_NUMBER() OVER()) AS id, - view.path AS path, - ( - SELECT COUNT(id) - FROM analyticsPageViews - WHERE path = view.path AND created >= {:startDate} - ) AS count, + SELECT + view.path, + COUNT(view.id) AS count, ( SELECT json_group_array( json_object( @@ -150,26 +146,24 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { ) ) FROM ( - SELECT + SELECT strftime('%Y-%m-%d', created) AS date, COUNT(id) AS daily_count FROM analyticsPageViews - WHERE - path = view.path AND - created >= datetime('now', '-30 days') + WHERE + path = view.path + AND created >= datetime('now', '-30 days') GROUP BY date ) AS daily_data ) AS last_30_days_data - FROM - analyticsPageViews view - GROUP BY - view.path - ORDER BY - count ` + sort + ` + FROM analyticsPageViews AS view + WHERE view.created >= {:startDate} + GROUP BY view.path + ORDER BY count ` + sort + ` LIMIT {:perPage} OFFSET {:offset} - `). + `). Bind(dbx.Params{ - "startDate": startDate, + "startDate": startDate.Format("2006-01-02 15:04:05"), "perPage": perPage, "offset": offset, }). @@ -183,7 +177,6 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { var items []Item for _, rawItem := range rawItems { var last30DaysData []Last30DaysData - logger.LogInfoF("rawItem.Last30DaysData: %v", rawItem.Last30DaysData) if rawItem.Last30DaysData != "" { if err := json.Unmarshal([]byte(rawItem.Last30DaysData), &last30DaysData); err != nil { return apis.NewApiError(500, "Failed to parse last_30_days_data", err) @@ -191,7 +184,6 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { } items = append(items, Item{ - ID: rawItem.ID, Path: rawItem.Path, Count: rawItem.Count, Last30DaysData: last30DaysData,