[optim] reduce unnecessary operations before each backend request

This commit is contained in:
Andrea Vos 2021-06-18 01:10:59 +02:00
parent 3f9e1666c0
commit cbbeff6edd
10 changed files with 38 additions and 40 deletions

View File

@ -6,7 +6,7 @@ import cookieParser from 'cookie-parser';
import grant from "grant";
import router from "./routes/user";
import { loadSuml } from './loader';
import {buildLocaleList, isGranted} from "../src/helpers";
import {isGranted} from "../src/helpers";
global.config = loadSuml('config');
@ -25,8 +25,6 @@ app.use(session({
app.use(async function (req, res, next) {
try {
req.config = global.config;
req.locales = buildLocaleList(global.config.locale);
req.rawUser = authenticate(req);
req.user = req.rawUser && req.rawUser.authenticated ? req.rawUser : null;
req.isGranted = (area, locale = global.config.locale) => req.user && isGranted(req.user, locale, area);

View File

@ -2,7 +2,7 @@ import { Router } from 'express';
import SQL from 'sql-template-strings';
import avatar from '../avatar';
import {config as socialLoginConfig} from "../social";
import {buildDict, now, shuffle, handleErrorAsync} from "../../src/helpers";
import {buildDict, now, shuffle, handleErrorAsync, buildLocaleList} from "../../src/helpers";
import locales from '../../src/locales';
import {calculateStats, statsFile} from '../../src/stats';
import fs from 'fs';
@ -22,9 +22,9 @@ router.get('/admin/list', handleErrorAsync(async (req, res) => {
`);
const adminsGroupped = buildDict(function* () {
yield [req.config.locale, []];
yield [global.config.locale, []];
for (let [locale, , , published] of locales) {
if (locale !== req.config.locale && published) {
if (locale !== global.config.locale && published) {
yield [locale, []];
}
}
@ -52,12 +52,12 @@ router.get('/admin/list/footer', handleErrorAsync(async (req, res) => {
SELECT u.username, p.footerName, p.footerAreas, p.locale
FROM users u
LEFT JOIN profiles p ON p.userId = u.id
WHERE p.locale = ${req.config.locale}
WHERE p.locale = ${global.config.locale}
AND p.footerName IS NOT NULL AND p.footerName != ''
AND p.footerAreas IS NOT NULL AND p.footerAreas != ''
`);
const fromConfig = req.config.contact.authors || [];
const fromConfig = global.config.contact.authors || [];
return [...fromDb, ...fromConfig];
})));
@ -112,7 +112,7 @@ router.get('/admin/stats', handleErrorAsync(async (req, res) => {
const stats = fs.existsSync(statsFile)
? JSON.parse(fs.readFileSync(statsFile))
: await calculateStats(req.db, req.locales);
: await calculateStats(req.db, buildLocaleList(global.config.locale));
for (let locale in stats.locales) {
if (stats.locales.hasOwnProperty(locale) && !req.isGranted('panel', locale)) {

View File

@ -88,7 +88,7 @@ router.get('/banner/:pronounName*.png', handleErrorAsync(async (req, res) => {
const logo = await loadImage('node_modules/@fortawesome/fontawesome-pro/svgs/light/tags.svg');
if (!pronoun && pronounName !== req.config.pronouns.any) {
if (!pronoun && pronounName !== global.config.pronouns.any) {
await fallback();
return canvas.toBuffer(mime);
}
@ -97,7 +97,7 @@ router.get('/banner/:pronounName*.png', handleErrorAsync(async (req, res) => {
context.font = 'regular 48pt Quicksand'
context.fillText(translations.pronouns.intro + ':', width / leftRatio + imageSize / 1.5, height / 2 - 36)
const pronounNameOptions = pronounName === req.config.pronouns.any ? [req.config.pronouns.any] : pronoun.nameOptions();
const pronounNameOptions = pronounName === global.config.pronouns.any ? [global.config.pronouns.any] : pronoun.nameOptions();
context.font = `bold ${pronounNameOptions.length <= 2 ? '70' : '36'}pt Quicksand`
context.fillText(pronounNameOptions.join('\n'), width / leftRatio + imageSize / 1.5, height / 2 + (pronounNameOptions.length <= 2 ? 72 : 24));

View File

@ -23,8 +23,8 @@ const hasFinished = async req => {
if (req.user) {
const byUser = await req.db.get(SQL`
SELECT * FROM census
WHERE locale = ${req.config.locale}
AND edition = ${req.config.census.edition}
WHERE locale = ${global.config.locale}
AND edition = ${global.config.census.edition}
AND userId = ${req.user.id}
`);
return !!byUser;
@ -33,8 +33,8 @@ const hasFinished = async req => {
const fingerprint = buildFingerprint(req);
const byFingerprint = await req.db.get(SQL`
SELECT * FROM census
WHERE locale = ${req.config.locale}
AND edition = ${req.config.census.edition}
WHERE locale = ${global.config.locale}
AND edition = ${global.config.census.edition}
AND fingerprint = ${fingerprint}
AND userId IS NULL
`);
@ -53,8 +53,8 @@ router.post('/census/submit', handleErrorAsync(async (req, res) => {
const id = ulid();
await req.db.get(SQL`INSERT INTO census (id, locale, edition, userId, fingerprint, answers, writins, ip, userAgent, acceptLanguage, suspicious) VALUES (
${id},
${req.config.locale},
${req.config.census.edition},
${global.config.locale},
${global.config.census.edition},
${req.user ? req.user.id : null},
${buildFingerprint(req)},
${req.body.answers},
@ -71,8 +71,8 @@ router.post('/census/submit', handleErrorAsync(async (req, res) => {
router.get('/census/count', handleErrorAsync(async (req, res) => {
return res.json((await req.db.get(SQL`
SELECT COUNT(*) as c FROM census
WHERE locale = ${req.config.locale}
AND edition = ${req.config.census.edition}
WHERE locale = ${global.config.locale}
AND edition = ${global.config.census.edition}
`)).c);
}));
@ -84,8 +84,8 @@ router.get('/census/export', handleErrorAsync(async (req, res) => {
const report = [];
for (let {answers, writins} of await req.db.all(SQL`
SELECT answers, writins FROM census
WHERE locale = ${req.config.locale}
AND edition = ${req.config.census.edition}
WHERE locale = ${global.config.locale}
AND edition = ${global.config.census.edition}
AND suspicious = 0
`)) {
answers = JSON.parse(answers);

View File

@ -25,7 +25,7 @@ router.get('/inclusive', handleErrorAsync(async (req, res) => {
return res.json(await req.db.all(SQL`
SELECT i.*, u.username AS author FROM inclusive i
LEFT JOIN users u ON i.author_id = u.id
WHERE i.locale = ${req.config.locale}
WHERE i.locale = ${global.config.locale}
AND i.approved >= ${req.isGranted('inclusive') ? 0 : 1}
AND i.deleted = 0
ORDER BY i.approved, i.insteadOf
@ -37,7 +37,7 @@ router.get('/inclusive/search/:term', handleErrorAsync(async (req, res) => {
return res.json(await req.db.all(SQL`
SELECT i.*, u.username AS author FROM inclusive i
LEFT JOIN users u ON i.author_id = u.id
WHERE i.locale = ${req.config.locale}
WHERE i.locale = ${global.config.locale}
AND i.approved >= ${req.isGranted('inclusive') ? 0 : 1}
AND i.deleted = 0
AND (i.insteadOf like ${term} OR i.say like ${term})
@ -56,7 +56,7 @@ router.post('/inclusive/submit', handleErrorAsync(async (req, res) => {
VALUES (
${id},
${req.body.insteadOf.join('|')}, ${req.body.say.join('|')}, ${req.body.because},
0, ${req.body.base}, ${req.config.locale}, ${req.user ? req.user.id : null},
0, ${req.body.base}, ${global.config.locale}, ${req.user ? req.user.id : null},
${req.body.categories.join(',')}, ${JSON.stringify(req.body.links)}
)
`);

View File

@ -31,7 +31,7 @@ const addVersions = async (req, nouns) => {
const sources = await req.db.all(SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale == ${req.config.locale}
WHERE s.locale == ${global.config.locale}
AND s.deleted = 0
AND s.approved >= ${req.isGranted('sources') ? 0 : 1}
AND s.key IN (`.append([...keys].join(',')).append(SQL`)
@ -72,7 +72,7 @@ router.get('/nouns', handleErrorAsync(async (req, res) => {
return res.json(await addVersions(req, await req.db.all(SQL`
SELECT n.*, u.username AS author FROM nouns n
LEFT JOIN users u ON n.author_id = u.id
WHERE n.locale = ${req.config.locale}
WHERE n.locale = ${global.config.locale}
AND n.deleted = 0
AND n.approved >= ${req.isGranted('nouns') ? 0 : 1}
ORDER BY n.approved, n.masc
@ -84,7 +84,7 @@ router.get('/nouns/search/:term', handleErrorAsync(async (req, res) => {
return res.json(await addVersions(req, await req.db.all(SQL`
SELECT n.*, u.username AS author FROM nouns n
LEFT JOIN users u ON n.author_id = u.id
WHERE n.locale = ${req.config.locale}
WHERE n.locale = ${global.config.locale}
AND n.approved >= ${req.isGranted('nouns') ? 0 : 1}
AND n.deleted = 0
AND (n.masc like ${term} OR n.fem like ${term} OR n.neutr like ${term} OR n.mascPl like ${term} OR n.femPl like ${term} OR n.neutrPl like ${term})
@ -105,7 +105,7 @@ router.post('/nouns/submit', handleErrorAsync(async (req, res) => {
${req.body.masc.join('|')}, ${req.body.fem.join('|')}, ${req.body.neutr.join('|')},
${req.body.mascPl.join('|')}, ${req.body.femPl.join('|')}, ${req.body.neutrPl.join('|')},
${req.body.sources || null},
0, ${req.body.base}, ${req.config.locale}, ${req.user ? req.user.id : null}
0, ${req.body.base}, ${global.config.locale}, ${req.user ? req.user.id : null}
)
`);
@ -171,7 +171,7 @@ router.get('/nouns/:word.png', handleErrorAsync(async (req, res) => {
const term = '%' + query + '%';
const noun = (await req.db.all(SQL`
SELECT * FROM nouns
WHERE locale = ${req.config.locale}
WHERE locale = ${global.config.locale}
AND approved >= ${req.isGranted('nouns') ? 0 : 1}
AND (masc like ${term} OR fem like ${term} OR neutr like ${term} OR mascPl like ${term} OR femPl like ${term} OR neutrPl like ${term})
ORDER BY masc

View File

@ -73,7 +73,7 @@ router.post('/profile/save', handleErrorAsync(async (req, res) => {
}
// TODO just make it a transaction...
const ids = (await req.db.all(SQL`SELECT * FROM profiles WHERE userId = ${req.user.id} AND locale = ${req.config.locale}`)).map(row => row.id);
const ids = (await req.db.all(SQL`SELECT * FROM profiles WHERE userId = ${req.user.id} AND locale = ${global.config.locale}`)).map(row => row.id);
if (ids.length) {
await req.db.get(SQL`UPDATE profiles
SET
@ -92,7 +92,7 @@ router.post('/profile/save', handleErrorAsync(async (req, res) => {
`);
} else {
await req.db.get(SQL`INSERT INTO profiles (id, userId, locale, names, pronouns, description, birthday, links, flags, customFlags, words, active, teamName, footerName, footerAreas)
VALUES (${ulid()}, ${req.user.id}, ${req.config.locale}, ${JSON.stringify(req.body.names)}, ${JSON.stringify(req.body.pronouns)},
VALUES (${ulid()}, ${req.user.id}, ${global.config.locale}, ${JSON.stringify(req.body.names)}, ${JSON.stringify(req.body.pronouns)},
${req.body.description}, ${req.body.birthday || null}, ${JSON.stringify(req.body.links.filter(x => !!x))}, ${JSON.stringify(req.body.flags)}, ${JSON.stringify(req.body.customFlags)},
${JSON.stringify(req.body.words)}, 1,
${req.isGranted('users') ? req.body.teamName || null : ''},

View File

@ -35,7 +35,7 @@ router.get('/pronounce/:voice/:pronoun*', handleErrorAsync(async (req, res) => {
return res.status(404).json({error: 'Not found'});
}
const voice = req.config.pronunciation.voices[req.params.voice];
const voice = global.config.pronunciation.voices[req.params.voice];
if (!voice) {
return res.status(404).json({error: 'Not found'});
}
@ -43,7 +43,7 @@ router.get('/pronounce/:voice/:pronoun*', handleErrorAsync(async (req, res) => {
const s3 = new S3(awsConfig);
const polly = new Polly(awsConfig);
const key = `pronunciation/${req.config.locale}-${req.params.voice}/${pronounString}/${sha1(text)}.mp3`;
const key = `pronunciation/${global.config.locale}-${req.params.voice}/${pronounString}/${sha1(text)}.mp3`;
try {
const s3getResponse = await s3.getObject({Key: key}).promise();

View File

@ -25,7 +25,7 @@ const linkOtherVersions = async (req, sources) => {
const otherVersions = await req.db.all(SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale != ${req.config.locale}
WHERE s.locale != ${global.config.locale}
AND s.deleted = 0
AND s.approved >= ${req.isGranted('sources') ? 0 : 1}
AND s.key IN (`.append([...keys].join(',')).append(SQL`)
@ -51,7 +51,7 @@ router.get('/sources', handleErrorAsync(async (req, res) => {
let sql = SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale = ${req.config.locale}
WHERE s.locale = ${global.config.locale}
AND s.deleted = 0
AND s.approved >= ${req.isGranted('sources') ? 0 : 1}
`;
@ -65,7 +65,7 @@ router.get('/sources/:id', handleErrorAsync(async (req, res) => {
return res.json(await linkOtherVersions(req, await req.db.all(SQL`
SELECT s.*, u.username AS submitter FROM sources s
LEFT JOIN users u ON s.submitter_id = u.id
WHERE s.locale = ${req.config.locale}
WHERE s.locale = ${global.config.locale}
AND s.deleted = 0
AND s.approved >= ${req.isGranted('sources') ? 0 : 1}
AND s.id = ${req.params.id}
@ -77,7 +77,7 @@ router.post('/sources/submit', handleErrorAsync(async (req, res) => {
await req.db.get(SQL`
INSERT INTO sources (id, locale, pronouns, type, author, title, extra, year, fragments, comment, link, key, images, submitter_id, base_id)
VALUES (
${id}, ${req.config.locale}, ${req.body.pronouns.join(';')},
${id}, ${global.config.locale}, ${req.body.pronouns.join(';')},
${req.body.type}, ${req.body.author}, ${req.body.title}, ${req.body.extra}, ${req.body.year},
${req.body.fragments.join('@').replace(/\n/g, '|')}, ${req.body.comment}, ${req.body.link},
${req.body.key || null}, ${req.body.images || null},

View File

@ -27,7 +27,7 @@ router.get('/terms', handleErrorAsync(async (req, res) => {
return req.db.all(SQL`
SELECT i.*, u.username AS author FROM terms i
LEFT JOIN users u ON i.author_id = u.id
WHERE i.locale = ${req.config.locale}
WHERE i.locale = ${global.config.locale}
AND i.approved >= ${req.isGranted('terms') ? 0 : 1}
AND i.deleted = 0
ORDER BY i.term
@ -40,7 +40,7 @@ router.get('/terms/search/:term', handleErrorAsync(async (req, res) => {
return res.json(await req.db.all(SQL`
SELECT i.*, u.username AS author FROM terms i
LEFT JOIN users u ON i.author_id = u.id
WHERE i.locale = ${req.config.locale}
WHERE i.locale = ${global.config.locale}
AND i.approved >= ${req.isGranted('terms') ? 0 : 1}
AND i.deleted = 0
AND (i.term like ${term} OR i.original like ${term})
@ -59,7 +59,7 @@ router.post('/terms/submit', handleErrorAsync(async (req, res) => {
VALUES (
${id},
${req.body.term.join('|')}, ${req.body.original.join('|')}, ${req.body.definition},
0, ${req.body.base}, ${req.config.locale}, ${req.user ? req.user.id : null},
0, ${req.body.base}, ${global.config.locale}, ${req.user ? req.user.id : null},
${req.body.category}, ${JSON.stringify(req.body.flags)}, ${req.body.images}
)
`);