phase 2 rework
This commit is contained in:
1 parent
20054198f3
commit
b397ea9256
44 files changed
+5528
-1377
No files matched your search
@@ -0,0 +1,61 @@
|
||||
"use strict";
|
||||
|
||||
const { getDb } = require("../lib/mongo");
|
||||
const { config } = require("../lib/config");
|
||||
const { STATUS_READ_PROJECTION, toPublicJob } = require("../lib/jobValidation");
|
||||
const { STATES, QUERY_TIMEOUT_MS, stateQuery, publicGlobal } = require("../lib/statusQuery");
|
||||
const { readActivity } = require("../lib/activity");
|
||||
|
||||
const RECENT_LIMIT = 5;
|
||||
|
||||
async function readSummary(db) {
|
||||
const [totals, record] = await Promise.all([
|
||||
Promise.all(STATES.map(state => {
|
||||
const { collection, filter } = stateQuery(state);
|
||||
return db.collection(collection).countDocuments(filter, { maxTimeMS: QUERY_TIMEOUT_MS });
|
||||
})),
|
||||
db.collection("settings").findOne({ _id: "global" }, {
|
||||
projection: { captcha_blocked: 1, pause_reason: 1 }, maxTimeMS: QUERY_TIMEOUT_MS,
|
||||
}),
|
||||
]);
|
||||
return { counts: Object.fromEntries(STATES.map((state, index) => [state, totals[index]])), global: publicGlobal(record) };
|
||||
}
|
||||
|
||||
async function readRecent(db, state) {
|
||||
const { collection, filter, sort } = stateQuery(state);
|
||||
const records = await db.collection(collection).find(filter, {
|
||||
projection: STATUS_READ_PROJECTION, maxTimeMS: QUERY_TIMEOUT_MS,
|
||||
}).sort(sort).limit(RECENT_LIMIT).toArray();
|
||||
return records.map(toPublicJob);
|
||||
}
|
||||
|
||||
function createDashboardController(dependencies = {}) {
|
||||
const connect = dependencies.getDb || getDb;
|
||||
const settings = dependencies.config || config;
|
||||
const activityReader = dependencies.readActivity || readActivity;
|
||||
|
||||
async function readDashboard() {
|
||||
const db = await connect();
|
||||
const [summary, activity, done, failed] = await Promise.all([
|
||||
readSummary(db), activityReader(db, { settings }), readRecent(db, "done"), readRecent(db, "failed"),
|
||||
]);
|
||||
return { ...summary, activity, recent: { done, failed } };
|
||||
}
|
||||
|
||||
async function dashboard(req, res, next) {
|
||||
res.set("Cache-Control", "no-store");
|
||||
try {
|
||||
return res.render("dashboard", { title: "Dashboard", activePage: "dashboard", ...await readDashboard() });
|
||||
} catch (error) { return next(error); }
|
||||
}
|
||||
|
||||
async function dashboardData(req, res, next) {
|
||||
res.set("Cache-Control", "no-store");
|
||||
try { return res.json(await readDashboard()); }
|
||||
catch (error) { return next(error); }
|
||||
}
|
||||
|
||||
return { dashboard, dashboardData, readDashboard };
|
||||
}
|
||||
|
||||
module.exports = { ...createDashboardController(), createDashboardController, readSummary, RECENT_LIMIT };
|
||||
Reference in new issue
Block a user