From 3945af02d1be39ee499e40ebe728df7be895f281 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Fri, 28 Jan 2022 02:22:42 +0100 Subject: [PATCH] feat(app/web): update ratelimit and placeholders (#826) --- source/app/web/instance.mjs | 32 +- source/app/web/statics/about/index.html | 11 +- source/app/web/statics/about/script.js | 11 +- source/app/web/statics/app.js | 25 +- source/app/web/statics/app.placeholder.js | 47 +- source/app/web/statics/index.html | 25 +- .../placeholders/isocalendar.full-year.svg | 1964 +++++++++++++++++ .../placeholders/isocalendar.half-year.svg | 1002 +++++++++ .../web/statics/placeholders/screenshot.png | Bin 0 -> 101296 bytes .../app/web/statics/placeholders/skyline.png | Bin 0 -> 88912 bytes source/app/web/statics/placeholders/stock.svg | 34 + source/app/web/statics/style.css | 1 + 12 files changed, 3124 insertions(+), 28 deletions(-) create mode 100644 source/app/web/statics/placeholders/isocalendar.full-year.svg create mode 100644 source/app/web/statics/placeholders/isocalendar.half-year.svg create mode 100644 source/app/web/statics/placeholders/screenshot.png create mode 100644 source/app/web/statics/placeholders/skyline.png create mode 100644 source/app/web/statics/placeholders/stock.svg diff --git a/source/app/web/instance.mjs b/source/app/web/instance.mjs index 872b1523..0af6cf1b 100644 --- a/source/app/web/instance.mjs +++ b/source/app/web/instance.mjs @@ -12,13 +12,13 @@ import presets from "../metrics/presets.mjs" import setup from "../metrics/setup.mjs" /**App */ -export default async function({sandbox} = {}) { +export default async function({sandbox = false} = {}) { //Load configuration settings const {conf, Plugins, Templates} = await setup({sandbox}) //Sandbox mode if (sandbox) { console.debug("metrics/app > sandbox mode is specified, enabling advanced features") - Object.assign(conf.settings, {optimize: true, cached:0, "plugins.default":true, extras:{default:true}}) + Object.assign(conf.settings, {sandbox:true, optimize: true, cached:0, "plugins.default":true, extras:{default:true}}) } const {token, maxusers = 0, restricted = [], debug = false, cached = 30 * 60 * 1000, port = 3000, ratelimiter = null, plugins = null} = conf.settings const mock = sandbox || conf.settings.mocked @@ -93,17 +93,28 @@ export default async function({sandbox} = {}) { const enabled = Object.entries(metadata).filter(([_name, {category}]) => category !== "core").map(([name]) => ({name, category:metadata[name]?.category ?? "community", enabled:plugins[name]?.enabled ?? false})) const templates = Object.entries(Templates).map(([name]) => ({name, enabled:(conf.settings.templates.enabled.length ? conf.settings.templates.enabled.includes(name) : true) ?? false})) const actions = {flush:new Map()} - let requests = {limit:0, used:0, remaining:0, reset:NaN} + const requests = {rest:{limit:0, used:0, remaining:0, reset:NaN}, graphql:{limit:0, used:0, remaining:0, reset:NaN}} + let _requests_refresh = false if (!conf.settings.notoken) { - requests = (await rest.rateLimit.get()).data.rate - setInterval(async () => { + const refresh = async () => { try { - requests = (await rest.rateLimit.get()).data.rate + const {limit} = await graphql("{ limit:rateLimit {limit remaining reset:resetAt used} }") + Object.assign(requests, { + rest:(await rest.rateLimit.get()).data.rate, + graphql:{...limit, reset:new Date(limit.reset).getTime()} + }) } catch { console.debug("metrics/app > failed to update remaining requests") } - }, 5 * 60 * 1000) + } + await refresh() + setInterval(refresh, 15 * 60 * 1000) + setInterval(() => { + if (_requests_refresh) + refresh() + _requests_refresh = false + }, 15 * 1000) } //Web app.get("/", limiter, (req, res) => res.sendFile(`${conf.paths.statics}/index.html`)) @@ -119,6 +130,8 @@ export default async function({sandbox} = {}) { app.get("/.templates/:template", limiter, (req, res) => req.params.template in conf.templates ? res.status(200).json(conf.templates[req.params.template]) : res.sendStatus(404)) for (const template in conf.templates) app.use(`/.templates/${template}/partials`, express.static(`${conf.paths.templates}/${template}/partials`)) + //Placeholders + app.use("/.placeholders", express.static(`${conf.paths.statics}/placeholders`)) //Styles app.get("/.css/style.css", limiter, (req, res) => res.sendFile(`${conf.paths.statics}/style.css`)) app.get("/.css/style.vars.css", limiter, (req, res) => res.sendFile(`${conf.paths.statics}/style.vars.css`)) @@ -205,6 +218,9 @@ export default async function({sandbox} = {}) { console.error(error) return res.status(500).send("Internal Server Error: failed to process metrics correctly") } + finally { + _requests_refresh = true + } }) //Metrics @@ -309,8 +325,8 @@ export default async function({sandbox} = {}) { } finally { //After rendering - solve?.() + _requests_refresh = true } }) diff --git a/source/app/web/statics/about/index.html b/source/app/web/statics/about/index.html index 61783dbc..035a3a9e 100644 --- a/source/app/web/statics/about/index.html +++ b/source/app/web/statics/about/index.html @@ -1,3 +1,4 @@ +
@@ -28,7 +29,7 @@ Search a GitHub user - {{ requests.remaining }} GitHub requests remaining + Remaining GitHub requests: {{ requests.rest.remaining }} REST / {{ requests.graphql.remaining }} GraphQL Send feedback on GitHub discussions!