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 9b3df67
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 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,18 @@ 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)
), at.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 9b3df67

Please sign in to comment.