From 8e324b2a1f802a1fdef746c050ef291fa1425d64 Mon Sep 17 00:00:00 2001 From: setop Date: Fri, 4 Jul 2025 00:13:16 +0200 Subject: [PATCH] feat(views): list, add, stats --- DESIGN.md | 27 +++-- add_instance.sql | 31 ++++++ cmd.txt | 1 + index.sql | 41 ++++++++ schema.sql | 1 + scripts/datamigation/create_instances.sql | 10 +- stats.sql | 114 ++++++++++++++++++++++ 7 files changed, 212 insertions(+), 13 deletions(-) create mode 100644 add_instance.sql create mode 100644 cmd.txt create mode 100644 index.sql create mode 100644 stats.sql diff --git a/DESIGN.md b/DESIGN.md index 5cd8116..3f42784 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -11,13 +11,15 @@ # MVP +- [x] chore: data model +- [x] data migration scripts - [ ] homepage - - [ ] form to register an instance + - [x] form to register an instance - [ ] check for duplicates - [ ] grab first stats : version, users, groups, events - [ ] if fail, set failure to 1 - [ ] confirmation page - - [ ] instances list, no pagination + - [x] instances list, no pagination - [ ] abuse link - [ ] cron - [ ] select instances where failure < max_failure @@ -25,13 +27,20 @@ - [ ] if fail, set failure to failure+1 - [ ] if success, set failure = 0 - [ ] stats page - - [ ] instances over time area chart - - [ ] users over time area chart - - [ ] events over time area chart - - [ ] groups over time area chart - - [ ] versions pie chart - - [ ] languages pie chart (user weighted ?) - - [ ] location pie chart + - [x] big numbers + - [x] total Instances + - [x] total Users + - [x] total Groups + - [x] total Events + - [x] over time + - [x] instances over time area chart + - [x] users over time area chart + - [x] events over time area chart + - [x] groups over time area chart + - [ ] repartition + - [ ] versions pie chart + - [ ] languages pie chart (user weighted ?) + - [ ] location pie chart # impl diff --git a/add_instance.sql b/add_instance.sql new file mode 100644 index 0000000..f2fc10c --- /dev/null +++ b/add_instance.sql @@ -0,0 +1,31 @@ +-- TODO check for duplicate to avoid +-- error returned from database: (code: 2067) UNIQUE constraint failed: instances.domain + +select + 'alert' as component + , 'Error' as title + , 'domain must be minimum four characters' as description + , 'alert-circle' as icon + , 'red' as color +where length($domain) <4 +or $domain is null +; + +-- TODO perform better validation of the domain +insert into instances(domain, failure) select $domain, -1 +where length($domain) >3 +and $domain is not null +; + +select + 'empty_state' as component + , 'mood-check' as icon + , 'Thanks you' as title + , 'This domain ' || $domain || ' has been added to the list and will be be displayed after next refresh, usually once a day' as description + , 'Go back to the list' as link_text + , 'home' as link_icon + , '/' as link +where length($domain) >3 +and $domain is not null +; + diff --git a/cmd.txt b/cmd.txt new file mode 100644 index 0000000..77f7c0b --- /dev/null +++ b/cmd.txt @@ -0,0 +1 @@ +. .venv/bin/activate diff --git a/index.sql b/index.sql new file mode 100644 index 0000000..598665a --- /dev/null +++ b/index.sql @@ -0,0 +1,41 @@ +select + 'shell' as component + , 'Mobilizon Instances' as title + , 'social' as icon + , '' as footer +; + +-- add form +select + 'form' as component + , 'GET' as method + , 'add an instance' as validate + , '/add_instance' as action +; +select + 'domain' as name + , 70 as maxlength + , '[0-9.a-z]{4,70}' as pattern + --, 'url' as type + , 'events.example.net' as placeholder + , true as required + , 6 as width +; + +-- instances list +select + 'table' as component + , true as search + , true as small + , true as sort + , true as freeze_headers + , true as striped_rows +; +select + domain as Url + , name + , slogan + , description +from instances +where failure >= 0 +; diff --git a/schema.sql b/schema.sql index 900d1a4..a6cee17 100644 --- a/schema.sql +++ b/schema.sql @@ -3,6 +3,7 @@ PRAGMA foreign_keys = true; CREATE TABLE instances ( -- PK will be rowid domain TEXT UNIQUE + , name TEXT , slogan TEXT , description TEXT , languages TEXT diff --git a/scripts/datamigation/create_instances.sql b/scripts/datamigation/create_instances.sql index a3d59e8..243ba36 100644 --- a/scripts/datamigation/create_instances.sql +++ b/scripts/datamigation/create_instances.sql @@ -3,12 +3,14 @@ create table instances as select id as rowid , host as domain -, config->'description' as slogan -, config->'longDescription' as description +, config->>'name' as name +, '' as slogan +, config->>'description' as description +-- won't caputure long description, not used yet , languages , CASE WHEN config->'registrationsOpen' = 'true' THEN 1 ELSE 0 END as open -, config->'version' as version -, "connectivityStats"->'country' as location +, config->>'version' as version +, "connectivityStats"->>'country' as location , 0 as failure , CAST(extract(epoch from "createdAt") as integer) as "createdAt" , CAST(extract(epoch from "updatedAt") as integer) as "updatedAt" diff --git a/stats.sql b/stats.sql new file mode 100644 index 0000000..6b2da92 --- /dev/null +++ b/stats.sql @@ -0,0 +1,114 @@ +select + 'big_number' as component + , 4 as columns +; +-- total Instances +select + 'Instances' as title + , (select count(*) from instances where failure>=0 and failure<5) as value +; +-- total Users +select + 'Users' as title + , (select sum(users) from ( SELECT + instance_id, + users, + MAX(insertedAt) + FROM stats + GROUP BY instance_id)) as value +; +-- total Groups +select + 'Groups' as title + , (select sum(local_groups) from ( SELECT + instance_id, + local_groups, + MAX(insertedAt) + FROM stats + GROUP BY instance_id)) as value +; +-- total Events +select + 'Events' as title + , (select sum(local_events) from ( SELECT + instance_id, + local_events, + MAX(insertedAt) + FROM stats + GROUP BY instance_id)) as value +; + +----- over time ---- +select + 'title' as component + , 'Over time' as contents +; + +-- instances +select + 'chart' as component + , 'Instances' as title + , 'area' as type + , 'blue-lt' as color + --, 5 as marker + , TRUE as time +; +SELECT + count(instance_id) as y + , insertedAt as x + , insertedAt / (3600*24) as j +FROM stats +GROUP BY j +order by j +; +-- users +select + 'chart' as component + , 'Users' as title + , 'area' as type + , 'blue-lt' as color + --, 5 as marker + , TRUE as time +; +SELECT + sum(users) as y + , insertedAt as x + , insertedAt / (3600*24) as j +FROM stats +GROUP BY j +order by j +; +-- events +select + 'chart' as component + , 'Events' as title + , 'area' as type + , 'blue-lt' as color + --, 5 as marker + , TRUE as time +; +SELECT + sum(local_events) as y + , insertedAt as x + , insertedAt / (3600*24) as j +FROM stats +GROUP BY j +order by j +; +-- groups +select + 'chart' as component + , 'Groups' as title + , 'area' as type + , 'blue-lt' as color + --, 5 as marker + , TRUE as time +; +SELECT + sum(local_groups) as y + , insertedAt as x + , insertedAt / (3600*24) as j +FROM stats +GROUP BY j +order by j +;