Skip to content

Commit

Permalink
feat: get occurences by day
Browse files Browse the repository at this point in the history
  • Loading branch information
ghivert committed Sep 3, 2024
1 parent aba055a commit 524c8d5
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import backend/data/hex_user.{type HexUser}
import backend/error
import backend/gleam/context
import birl.{type Time}
import birl/duration
import gleam/bool
import gleam/dict.{type Dict}
import gleam/dynamic
Expand Down Expand Up @@ -102,11 +103,19 @@ pub fn upsert_search_analytics_timeseries(
}

pub fn get_timeseries_count(db: pgo.Connection) {
"SELECT count(*), date
FROM analytics_timeseries
WHERE date >= now() - INTERVAL '30 day'
GROUP BY date
ORDER BY date desc"
"SELECT
SUM(at.occurences - COALESCE(
(SELECT att.occurences
FROM analytics_timeseries att
WHERE att.date < at.date
AND att.query = at.query),
0)
) searches,
at.date date
FROM analytics_timeseries at
WHERE at.date >= now() - INTERVAL '30 day'
GROUP BY at.date
ORDER BY date DESC"
|> pgo.execute(db, [], dynamic.tuple2(dynamic.int, helpers.decode_time))
|> result.map_error(error.DatabaseError)
|> result.map(fn(r) { r.rows })
Expand Down

0 comments on commit 524c8d5

Please sign in to comment.