From c9356a419efe267c3c1dac1610eadca20aa2188f Mon Sep 17 00:00:00 2001 From: valentinkolb Date: Wed, 13 Nov 2024 02:51:11 +0100 Subject: [PATCH] fix(analyticsApi): fixed last_30_days_data (final) --- analyticsApi/main.go | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/analyticsApi/main.go b/analyticsApi/main.go index 273336f..43a9b8e 100644 --- a/analyticsApi/main.go +++ b/analyticsApi/main.go @@ -1,6 +1,7 @@ package analyticsApi import ( + "encoding/json" "git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/ldapApi" "git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/logger" "github.com/labstack/echo/v5" @@ -119,11 +120,17 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { Count int `json:"count"` } + type Item struct { + Path string `json:"path"` + Count int `json:"count"` + Last30DaysData []Last30DaysData `json:"last_30_days_data"` + } + // Query paginated items - var Items []struct { - Path string `db:"path" json:"path"` - Count int `db:"count" json:"count"` - Last30DaysData []Last30DaysData `db:"last_30_days_data" json:"last_30_days_data"` + var rawItems []struct { + 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(). @@ -160,19 +167,36 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { "perPage": perPage, "offset": offset, }). - All(&Items) + All(&rawItems) if err != nil { return apis.NewApiError(500, "Failed to query page view data", err) } + // Parse last_30_days_data JSON string into structured data + var items []Item + for _, rawItem := range rawItems { + var last30DaysData []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) + } + } + + items = append(items, Item{ + Path: rawItem.Path, + Count: rawItem.Count, + Last30DaysData: last30DaysData, + }) + } + // Final response structure response := map[string]interface{}{ "page": page, "perPage": perPage, "totalItems": totalItems, "totalPages": totalPages, - "items": Items, + "items": items, } // Return the final JSON response