fix(analyticsApi): fixed last_30_days_data (7)
Build and Push Docker image / build-and-push (push) Successful in 2m10s
Details
Build and Push Docker image / build-and-push (push) Successful in 2m10s
Details
This commit is contained in:
parent
6aa51ea129
commit
0d5b976e97
|
@ -72,6 +72,9 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return apis.NewBadRequestError("Invalid start date format, expected ISO 8601", err)
|
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
|
// Pagination and sorting parameters
|
||||||
|
@ -86,8 +89,8 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sort := c.QueryParam("sort")
|
sort := c.QueryParam("sort")
|
||||||
if sort != "DESC" {
|
if sort != "ASC" && sort != "DESC" {
|
||||||
sort = "ASC" // default sorting order
|
sort = "DESC" // default sorting order
|
||||||
}
|
}
|
||||||
|
|
||||||
offset := (page - 1) * perPage
|
offset := (page - 1) * perPage
|
||||||
|
@ -101,7 +104,7 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
|
||||||
WHERE created >= {:startDate}
|
WHERE created >= {:startDate}
|
||||||
`).
|
`).
|
||||||
Bind(dbx.Params{
|
Bind(dbx.Params{
|
||||||
"startDate": startDate,
|
"startDate": startDate.Format("2006-01-02 15:04:05"),
|
||||||
}).
|
}).
|
||||||
Row(&totalItems)
|
Row(&totalItems)
|
||||||
|
|
||||||
|
@ -118,30 +121,23 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
ID int `json:"id"`
|
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Count int `json:"count"`
|
Count int `json:"count"`
|
||||||
Last30DaysData []Last30DaysData `json:"last_30_days_data"`
|
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 {
|
var rawItems []struct {
|
||||||
ID int `json:"id"`
|
Path string `db:"path" json:"path"`
|
||||||
Path string `json:"path"`
|
Count int `db:"count" json:"count"`
|
||||||
Count int `json:"count"`
|
Last30DaysData string `db:"last_30_days_data" json:"last_30_days_data"` // JSON string to parse
|
||||||
Last30DaysData string `json:"last_30_days_data"` // JSON string to parse
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = app.Dao().DB().
|
err = app.Dao().DB().
|
||||||
NewQuery(`
|
NewQuery(`
|
||||||
SELECT
|
SELECT
|
||||||
(ROW_NUMBER() OVER()) AS id,
|
view.path,
|
||||||
view.path AS path,
|
COUNT(view.id) AS count,
|
||||||
(
|
|
||||||
SELECT COUNT(id)
|
|
||||||
FROM analyticsPageViews
|
|
||||||
WHERE path = view.path AND created >= {:startDate}
|
|
||||||
) AS count,
|
|
||||||
(
|
(
|
||||||
SELECT json_group_array(
|
SELECT json_group_array(
|
||||||
json_object(
|
json_object(
|
||||||
|
@ -150,26 +146,24 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
FROM (
|
FROM (
|
||||||
SELECT
|
SELECT
|
||||||
strftime('%Y-%m-%d', created) AS date,
|
strftime('%Y-%m-%d', created) AS date,
|
||||||
COUNT(id) AS daily_count
|
COUNT(id) AS daily_count
|
||||||
FROM analyticsPageViews
|
FROM analyticsPageViews
|
||||||
WHERE
|
WHERE
|
||||||
path = view.path AND
|
path = view.path
|
||||||
created >= datetime('now', '-30 days')
|
AND created >= datetime('now', '-30 days')
|
||||||
GROUP BY date
|
GROUP BY date
|
||||||
) AS daily_data
|
) AS daily_data
|
||||||
) AS last_30_days_data
|
) AS last_30_days_data
|
||||||
FROM
|
FROM analyticsPageViews AS view
|
||||||
analyticsPageViews view
|
WHERE view.created >= {:startDate}
|
||||||
GROUP BY
|
GROUP BY view.path
|
||||||
view.path
|
ORDER BY count ` + sort + `
|
||||||
ORDER BY
|
|
||||||
count ` + sort + `
|
|
||||||
LIMIT {:perPage} OFFSET {:offset}
|
LIMIT {:perPage} OFFSET {:offset}
|
||||||
`).
|
`).
|
||||||
Bind(dbx.Params{
|
Bind(dbx.Params{
|
||||||
"startDate": startDate,
|
"startDate": startDate.Format("2006-01-02 15:04:05"),
|
||||||
"perPage": perPage,
|
"perPage": perPage,
|
||||||
"offset": offset,
|
"offset": offset,
|
||||||
}).
|
}).
|
||||||
|
@ -183,7 +177,6 @@ func initPageViewCount(app *pocketbase.PocketBase, e *core.ServeEvent) {
|
||||||
var items []Item
|
var items []Item
|
||||||
for _, rawItem := range rawItems {
|
for _, rawItem := range rawItems {
|
||||||
var last30DaysData []Last30DaysData
|
var last30DaysData []Last30DaysData
|
||||||
logger.LogInfoF("rawItem.Last30DaysData: %v", rawItem.Last30DaysData)
|
|
||||||
if rawItem.Last30DaysData != "" {
|
if rawItem.Last30DaysData != "" {
|
||||||
if err := json.Unmarshal([]byte(rawItem.Last30DaysData), &last30DaysData); err != nil {
|
if err := json.Unmarshal([]byte(rawItem.Last30DaysData), &last30DaysData); err != nil {
|
||||||
return apis.NewApiError(500, "Failed to parse last_30_days_data", err)
|
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{
|
items = append(items, Item{
|
||||||
ID: rawItem.ID,
|
|
||||||
Path: rawItem.Path,
|
Path: rawItem.Path,
|
||||||
Count: rawItem.Count,
|
Count: rawItem.Count,
|
||||||
Last30DaysData: last30DaysData,
|
Last30DaysData: last30DaysData,
|
||||||
|
|
Loading…
Reference in New Issue