feat(backend): presence stats count time online from sessions in the browser time zone, capture uptime marks hours without data, presence reaches every stream of the account, message volume keeps empty days
This commit is contained in:
1 parent
bb38e23f29
commit
f44597b2dc
14 files changed
+585
-26
No files matched your search
@@ -4,23 +4,31 @@ import asyncpg
|
||||
|
||||
from utils.read.accounts import self_user_id
|
||||
from utils.read.models import ResponseStats, VolumeBucket
|
||||
from utils.read.period import period_bounds
|
||||
|
||||
_VOLUME = """
|
||||
WITH counts AS (
|
||||
SELECT (date AT TIME ZONE $5)::date AS day, count(*) AS total,
|
||||
count(*) FILTER (WHERE sender_id = $3) AS outgoing
|
||||
FROM messages
|
||||
WHERE account_id = $1 AND chat_id = $2 AND date >= $4
|
||||
GROUP BY 1
|
||||
)
|
||||
SELECT local_day::date AS bucket, coalesce(total, 0) AS total,
|
||||
coalesce(outgoing, 0) AS outgoing
|
||||
FROM generate_series(($4 AT TIME ZONE $5)::date, (now() AT TIME ZONE $5)::date,
|
||||
interval '1 day') AS local_day
|
||||
LEFT JOIN counts ON counts.day = local_day::date
|
||||
ORDER BY 1
|
||||
"""
|
||||
|
||||
|
||||
async def message_volume(
|
||||
pool: asyncpg.Pool, account_id: int, chat_id: int, *, days: int = 90
|
||||
pool: asyncpg.Pool, account_id: int, chat_id: int, *, days: int, tz: str
|
||||
) -> list[VolumeBucket]:
|
||||
self_id = await self_user_id(pool, account_id)
|
||||
rows = await pool.fetch(
|
||||
"SELECT date_trunc('day', date) AS bucket, count(*) AS total, "
|
||||
"count(*) FILTER (WHERE sender_id = $3) AS outgoing "
|
||||
"FROM messages "
|
||||
"WHERE account_id = $1 AND chat_id = $2 AND date >= $4 "
|
||||
"GROUP BY bucket ORDER BY bucket",
|
||||
account_id,
|
||||
chat_id,
|
||||
self_id,
|
||||
datetime.now(UTC) - timedelta(days=days),
|
||||
)
|
||||
since, _ = await period_bounds(pool, days, tz)
|
||||
rows = await pool.fetch(_VOLUME, account_id, chat_id, self_id, since, tz)
|
||||
return [
|
||||
VolumeBucket(
|
||||
bucket=row["bucket"],
|
||||
|
||||
Reference in new issue
Block a user