1
0

optimize stats using "materialized view"

This commit is contained in:
setop 2025-07-07 09:16:52 +02:00
parent 1e291882a6
commit be3023f55d
8 changed files with 31 additions and 58 deletions

View File

@ -1,2 +1,3 @@
.env .env
**/__pycache__ **/__pycache__
**/*.db*

View File

@ -1,6 +1,6 @@
FROM python:3.13-alpine FROM python:3.13-alpine
RUN apk add --no-cache s6 bash wget unzip curl RUN apk add --no-cache s6 bash wget unzip curl sqlite
COPY requirements.txt . COPY requirements.txt .

View File

@ -1,6 +1,6 @@
. .venv/bin/activate . .venv/bin/activate
export VERSION=0.1.0 export VERSION=0.2.0
docker build -t mobilizon-instances:${VERSION} . docker build -t mobilizon-instances:${VERSION} .

5
scripts/refresh_day_stats.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash -eu
CMDD=$(dirname $(realpath $0))
sqlite3 ${SQLPAGE_DATABASE_URL:9} < ${CMDD}/../sqlpage/migrations/003_create_daystats.sql

View File

@ -1,12 +1,4 @@
-- set some pragma CREATE TABLE if not exists instances (
PRAGMA journal_mode = WAL;
PRAGMA busy_timeout = 15000; -- for interactive, 15s for background tasks
PRAGMA synchronous = NORMAL;
PRAGMA cache_size = 1000000000; -- means infinite
PRAGMA foreign_keys = true;
PRAGMA temp_store = memory;
CREATE TABLE instances (
-- PK will be rowid -- PK will be rowid
domain TEXT UNIQUE domain TEXT UNIQUE
, name TEXT , name TEXT
@ -21,7 +13,7 @@ CREATE TABLE instances (
, updatedAt INTEGER -- second since epoch , updatedAt INTEGER -- second since epoch
) strict; ) strict;
create table stats ( create table if not exists stats (
insertedAt INTEGER insertedAt INTEGER
, instance_id INTEGER -- rowid of instance , instance_id INTEGER -- rowid of instance
, users INTEGER , users INTEGER

View File

@ -0,0 +1,2 @@
CREATE INDEX if not exists idx_stats_day ON stats(insertedAt / (3600*24));

View File

@ -0,0 +1,12 @@
DROP table if exists day_stats;
Create table day_stats as
SELECT
count(instance_id) as i
, sum(users) as u
, sum(local_events) as e
, sum(local_groups) as g
, insertedAt as x
, insertedAt / (3600*24) as j
FROM stats
GROUP BY j

View File

@ -19,32 +19,17 @@ select
-- total Users -- total Users
select select
'Users' as title 'Users' as title
, (select sum(users) from ( SELECT , (SELECT u FROM day_stats order by j desc limit 1) as value
instance_id,
users,
MAX(insertedAt)
FROM stats
GROUP BY instance_id)) as value
; ;
-- total Groups -- total Groups
select select
'Groups' as title 'Groups' as title
, (select sum(local_groups) from ( SELECT , (SELECT g FROM day_stats order by j desc limit 1) as value
instance_id,
local_groups,
MAX(insertedAt)
FROM stats
GROUP BY instance_id)) as value
; ;
-- total Events -- total Events
select select
'Events' as title 'Events' as title
, (select sum(local_events) from ( SELECT , (SELECT e FROM day_stats order by j desc limit 1) as value
instance_id,
local_events,
MAX(insertedAt)
FROM stats
GROUP BY instance_id)) as value
; ;
----- over time ---- ----- over time ----
@ -62,13 +47,7 @@ select
--, 5 as marker --, 5 as marker
, TRUE as time , TRUE as time
; ;
SELECT SELECT i as y, x FROM day_stats
count(instance_id) as y
, insertedAt as x
, insertedAt / (3600*24) as j
FROM stats
GROUP BY j
order by j
; ;
-- users -- users
select select
@ -79,13 +58,7 @@ select
--, 5 as marker --, 5 as marker
, TRUE as time , TRUE as time
; ;
SELECT SELECT u as y, x FROM day_stats
sum(users) as y
, insertedAt as x
, insertedAt / (3600*24) as j
FROM stats
GROUP BY j
order by j
; ;
-- events -- events
select select
@ -96,13 +69,7 @@ select
--, 5 as marker --, 5 as marker
, TRUE as time , TRUE as time
; ;
SELECT SELECT e as y, x FROM day_stats
sum(local_events) as y
, insertedAt as x
, insertedAt / (3600*24) as j
FROM stats
GROUP BY j
order by j
; ;
-- groups -- groups
select select
@ -113,11 +80,5 @@ select
--, 5 as marker --, 5 as marker
, TRUE as time , TRUE as time
; ;
SELECT SELECT g as y, x FROM day_stats
sum(local_groups) as y
, insertedAt as x
, insertedAt / (3600*24) as j
FROM stats
GROUP BY j
order by j
; ;