[pl][census] graphs

This commit is contained in:
Andrea Vos 2023-02-04 13:35:35 +01:00
parent 6024f2a668
commit d00de414a1
4 changed files with 31 additions and 2 deletions

View File

@ -1,6 +1,7 @@
<template>
<div class="card">
<div class="card-header">
<p v-if="header" class="h6 d-inline-block me-3"><strong>{{header}}</strong></p>
<div class="form-check form-check-inline"
v-for="(desc, m) in {'': 'Hide chart', 'daily': 'Daily change chart', 'cumulative': 'Cumulative chart'}">
<label class="form-check-label">
@ -22,6 +23,7 @@
name: { required: true },
data: { required: true },
init: { 'default': '' },
header: {},
},
data() {
return {

View File

@ -20,6 +20,11 @@
<li>{{countResponses.usable}} <T>census.repliesUsable</T></li>
<li><nuxt-link :to="`/${config.census.route}/admin`">{{countResponses.awaiting}} <T>census.repliesAwaiting</T></nuxt-link></li>
</ul>
<hr/>
<ChartSet v-for="(editionGraph, edition) in countResponses.graphs"
:header="edition" :name="`useful responses (${edition})`" :data="editionGraph"
class="mb-3"
/>
</div>
</section>

View File

@ -3,8 +3,9 @@ import SQL from 'sql-template-strings';
import sha1 from 'sha1';
import {ulid} from "ulid";
import Papa from 'papaparse';
import {handleErrorAsync} from "../../src/helpers";
import {groupBy, handleErrorAsync, ImmutableArray} from "../../src/helpers";
import {intersection, difference} from "../../src/sets";
import {buildChart} from "../../src/stats";
const getIp = req => {
try {
@ -101,6 +102,7 @@ router.get('/census/count', handleErrorAsync(async (req, res) => {
nonbinary: 0,
usable: 0,
awaiting: 0,
graphs: {},
});
}
@ -131,6 +133,22 @@ router.get('/census/count', handleErrorAsync(async (req, res) => {
AND edition = ${global.config.census.edition}
AND troll IS NULL
`)).c,
graphs: Object.fromEntries(
Object.entries(
groupBy(
await req.db.all(SQL`
SELECT edition, id
FROM census
WHERE locale = ${global.config.locale}
AND relevant = 1
AND troll = 0
`),
r => r.edition
)
).map(([edition, rows]) => {
return [edition, buildChart(rows, false)];
})
),
});
}));

View File

@ -51,7 +51,7 @@ const deepGet = (obj, path) => {
const formatDate = d => `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`;
module.exports.buildChart = (rows) => {
module.exports.buildChart = (rows, cumulative = true) => {
const dates = rows.map(row => new Date(decodeTime(row.id)));
const chart = {};
@ -71,6 +71,10 @@ module.exports.buildChart = (rows) => {
chart[formatDate(date)]++;
}
if (!cumulative) {
return chart;
}
const cumChart = {};
let cum = 0;
for (let [date, count] of Object.entries(chart)) {