fix(analyticsApi): fixed last_30_days_data (4)
Build and Push Docker image / build-and-push (push) Successful in 3m38s Details

This commit is contained in:
Valentin Kolb 2024-11-13 02:04:55 +01:00
parent 059f473a38
commit 21908ecfb4
1 changed files with 37 additions and 20 deletions

View File

@ -1,6 +1,7 @@
package analyticsApi package analyticsApi
import ( import (
"encoding/json"
"git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/ldapApi" "git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/ldapApi"
"git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/logger" "git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/logger"
"github.com/labstack/echo/v5" "github.com/labstack/echo/v5"
@ -124,7 +125,13 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
} }
// Query paginated items, sorted by count // Query paginated items, sorted by count
var items []Item 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
}
err = app.Dao().DB(). err = app.Dao().DB().
NewQuery(` NewQuery(`
SELECT SELECT
@ -138,20 +145,15 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
( (
SELECT json_group_array( SELECT json_group_array(
json_object( json_object(
'date', date, 'date', strftime('%Y-%m-%d', created),
'count', daily_count 'count', COUNT(id)
) )
) )
FROM ( FROM analyticsPageViews
SELECT WHERE
strftime('%Y-%m-%d', created) AS date, path = view.path AND
COUNT(id) AS daily_count created >= datetime('now', '-30 days')
FROM analyticsPageViews GROUP BY strftime('%Y-%m-%d', created)
WHERE
path = view.path AND
created >= datetime('now', '-30 days')
GROUP BY date
) AS daily_data
) AS last_30_days_data ) AS last_30_days_data
FROM FROM
analyticsPageViews view analyticsPageViews view
@ -166,23 +168,38 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
"perPage": perPage, "perPage": perPage,
"offset": offset, "offset": offset,
}). }).
All(&items) All(&rawItems)
if err != nil { if err != nil {
return apis.NewApiError(500, "Failed to query page view data", err) return apis.NewApiError(500, "Failed to query page view data", err)
} }
// Final response structure // Parse last_30_days_data JSON string into structured data
response := map[string]interface{}{ 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{
ID: rawItem.ID,
Path: rawItem.Path,
Count: rawItem.Count,
Last30DaysData: last30DaysData,
})
}
// Return the final JSON response
return c.JSON(200, map[string]interface{}{
"page": page, "page": page,
"perPage": perPage, "perPage": perPage,
"totalItems": totalItems, "totalItems": totalItems,
"totalPages": totalPages, "totalPages": totalPages,
"items": items, "items": items,
} })
// Return the final JSON response
return c.JSON(200, response)
}, apis.ActivityLogger(app)) }, apis.ActivityLogger(app))
} }