feat(analyticsApi): added error_count to initAggregateCount
Build and Push Docker image / build-and-push (push) Successful in 2m13s Details

This commit is contained in:
Valentin Kolb 2024-11-13 15:16:30 +01:00
parent c9356a419e
commit 099026cd9f
1 changed files with 16 additions and 12 deletions

View File

@ -223,33 +223,34 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
//
// {
// "device_type": [
// { "value": "<device type>", "count": <session count> },
// { "value": "<device type>", "count": <session count>, "error_count": <error count> },
// ...
// ],
// "browser_name": [
// { "value": "<browser name>", "count": <session count> },
// { "value": "<browser name>", "count": <session count>, "error_count": <error count> },
// ...
// ],
// "operating_system": [
// { "value": "<operating system>", "count": <session count> },
// { "value": "<operating system>", "count": <session count>, "error_count": <error count> },
// ...
// ],
// "user_agent": [
// { "value": "<user agent>", "count": <session count> },
// { "value": "<user agent>", "count": <session count>, "error_count": <error count> },
// ...
// ],
// "geo_country_code": [
// { "value": "<country code>", "count": <session count> },
// { "value": "<country code>", "count": <session count>, "error_count": <error count> },
// ...
// ],
// "preferred_language": [
// { "value": "<preferred language>", "count": <session count> },
// { "value": "<preferred language>", "count": <session count>, "error_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 {
@ -267,6 +268,7 @@ func initAggregateCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
type Data struct {
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