diff --git a/analyticsApi/main.go b/analyticsApi/main.go index 43a9b8e..67aa11f 100644 --- a/analyticsApi/main.go +++ b/analyticsApi/main.go @@ -223,33 +223,34 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) { // // { // "device_type": [ -// { "value": "", "count": }, +// { "value": "", "count": , "error_count": }, // ... // ], // "browser_name": [ -// { "value": "", "count": }, +// { "value": "", "count": , "error_count": }, // ... // ], // "operating_system": [ -// { "value": "", "count": }, +// { "value": "", "count": , "error_count": }, // ... // ], // "user_agent": [ -// { "value": "", "count": }, +// { "value": "", "count": , "error_count": }, // ... // ], // "geo_country_code": [ -// { "value": "", "count": }, +// { "value": "", "count": , "error_count": }, // ... // ], // "preferred_language": [ -// { "value": "", "count": }, +// { "value": "", "count": , "error_count": }, // ... // ] // } // -// Each category includes an array of objects where `value` is the specific item (e.g., a device type or browser name) -// and `count` is the number of sessions matching that item since `startDate`. +// Each category includes an array of objects where `value` is the specific item (e.g., a device type or browser name), +// `count` is the total number of sessions matching that item since `startDate`, and `error_count` is the number +// of sessions with errors for that item (where the `error` field is not NULL) since `startDate`. func initAggregateCount(app *pocketbase.PocketBase, e *core.ServeEvent) { e.Router.GET("/api/analytics/aggregateCount", func(c echo.Context) error { @@ -265,8 +266,9 @@ func initAggregateCount(app *pocketbase.PocketBase, e *core.ServeEvent) { } type Data struct { - Value string `db:"value" json:"value"` - Count int `db:"count" json:"count"` + Value string `db:"value" json:"value"` + Count int `db:"count" json:"count"` + ErrorCount int `db:"error_count" json:"error_count"` } // Use a map to store the response data @@ -279,7 +281,10 @@ func initAggregateCount(app *pocketbase.PocketBase, e *core.ServeEvent) { err := app.Dao().DB(). NewQuery(` - SELECT IFNULL(` + field + `, 'N/A') as value, COUNT(id) AS count + SELECT + IFNULL(` + field + `, 'N/A') as value, + COUNT(id) AS count, + SUM(CASE WHEN error IS NOT NULL THEN 1 ELSE 0 END) AS error_count FROM analyticsSessions WHERE created >= {:startDate} GROUP BY ` + field). @@ -299,7 +304,6 @@ func initAggregateCount(app *pocketbase.PocketBase, e *core.ServeEvent) { // Return the final JSON response return c.JSON(200, response) }, apis.ActivityLogger(app)) - } // InitAnalyticsApi initializes analytics api endpoints