mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 10:48:06 -05:00
Every other pane under Problems answers what went WRONG. This one answers what is being DONE: `POST /api/boards` called 34 times by alice last month, 812 times yesterday by nobody with an account. Without it the only record of REST API use is whatever the reverse proxy happens to keep, and most instances keep none. The table is username, endpoint, calls, the window they fall in, and the addresses - sorted by calls, because a usage report's question is "what is used most" where a problem report's is "what happened last". THE NAME IS THE ROUTE PATTERN, NOT THE PATH. `/api/boards/:boardId/lists` is one endpoint; `/api/boards/abc123/lists` and ten thousand of its siblings are that one endpoint being used. Naming rows after paths would put a row per board in the collection - the one-row-per-event cost this whole page exists to remove - and a request that matched NO route is counted under a single "(no route)" name rather than under the path it invented, because a 404 sweep is an attacker walking a wordlist and a row per guess would let them fill the database. ONE ROW PER ACCOUNT AND ENDPOINT, which makes the account part of a row's identity - the one deliberate exception to the rule every other stream follows. For those the question is "what is happening" and the caller would multiply the rows; here "who called what" IS the report, and the cardinality is bounded by real accounts times real endpoints. The account is stored by ID, so a rename does not split its history in two. NOTHING PER REQUEST. Ordinary API traffic is not rare the way a guard firing is rare, so calls are counted in memory and folded on a timer - a thousand requests become one write. A few seconds of counts are lost if the process stops between flushes, which is acceptable here and nowhere else on this page: this report decides nothing and nothing is protected by it. The tracked pairs are capped and the rest counted, not dropped. Counting hooks the middleware chain, not the routes, so a route cannot be added without being counted - and it counts on the response's `finish` event, which is where Express has filled req.route.path and authentication has run. The pane is the SHARED event-stream report with a different column list, not a second table page. The API stream is deliberately NOT in EVENT_STREAMS: it is usage, not a problem, and an instance serving its API would otherwise report thousands of new problems. AND THE IPv4/IPv6 SPLIT IS FINISHED WHILE HERE. The summary rows have had ipv4 and ipv6 fields since the summaries were written, and not one of the four loggers ever filled them - so the columns the design asked for could not have worked. The fold splits the address now, once, for every stream, and the two columns are one shared pair used by both reports rather than a copy each. They fall back to classifying the stored `ip`, so rows written before today display correctly instead of showing two empty columns for all of history. Seven i18n keys in 239 language files, in en.i18n.json's order, English as the placeholder where nothing is translated yet. Thanks to xet7 !
84 lines
3.4 KiB
JavaScript
84 lines
3.4 KiB
JavaScript
// Fold one event into its problem's row (models/lib/eventLogSummary.js).
|
|
//
|
|
// Shared by the security, speed and test loggers, so "one row per problem, not
|
|
// per event" is a property of Admin Panel -> Problems rather than of one logger.
|
|
// All three sit on paths that repeat: a guard under attack, a slow query in a
|
|
// loop, a failing test on every run.
|
|
//
|
|
// Fire-and-forget and never throws into the caller: recording that something
|
|
// happened must never be able to stop it happening.
|
|
|
|
import EventLog from '/models/eventLog';
|
|
|
|
const {
|
|
summaryIdentity, summaryUpdate, actorUpdate,
|
|
} = require('/models/lib/eventLogSummary');
|
|
const { classifyAddress } = require('/models/lib/ipAddress');
|
|
|
|
// Which actor keys each problem row already names, so the per-actor cap can be
|
|
// applied without reading the row on every attempt. Under attack - exactly when
|
|
// this is hot - the same few actors repeat, so the answer is nearly always
|
|
// already known. Bounded, so a long-running server cannot accumulate one entry
|
|
// per problem for ever; dropping an entry costs one extra read.
|
|
const knownActors = new Map();
|
|
const MAX_CACHED_ROWS = 500;
|
|
|
|
export async function foldEvent(doc = {}) {
|
|
const { at, ...evt } = doc;
|
|
const when = at instanceof Date ? at : new Date();
|
|
// THE ADDRESS, SPLIT BY FAMILY, here rather than in each logger.
|
|
//
|
|
// Every report shows IPv4 and IPv6 in columns of their own, because an
|
|
// instance reached over IPv6 and one reached over IPv4 are different
|
|
// situations and a single column that sometimes holds one and sometimes the
|
|
// other cannot be scanned. Doing it in the fold means a logger cannot forget
|
|
// to - and none of the four did it, so the fields existed and were always
|
|
// empty. `::ffff:203.0.113.9` is unwrapped to the IPv4 it is
|
|
// (models/lib/ipAddress.js), or one client would be two addresses depending
|
|
// which listener it reached.
|
|
if (evt.ip && !evt.ipv4 && !evt.ipv6) {
|
|
const { ipv4, ipv6 } = classifyAddress(evt.ip);
|
|
if (ipv4) evt.ipv4 = ipv4;
|
|
if (ipv6) evt.ipv6 = ipv6;
|
|
}
|
|
const identity = summaryIdentity(evt);
|
|
const cacheKey = JSON.stringify(identity);
|
|
|
|
let known = knownActors.get(cacheKey);
|
|
if (!known) {
|
|
const row = await EventLog.findOneAsync(identity, { fields: { actors: 1 } });
|
|
known = new Set(Object.keys((row && row.actors) || {}));
|
|
if (knownActors.size >= MAX_CACHED_ROWS) knownActors.clear();
|
|
knownActors.set(cacheKey, known);
|
|
}
|
|
|
|
const summary = summaryUpdate(evt, when);
|
|
const actors = actorUpdate(evt, when, 1, known);
|
|
await EventLog.upsertAsync(identity, {
|
|
$set: { ...summary.$set, ...actors.$set },
|
|
$setOnInsert: summary.$setOnInsert,
|
|
$inc: { ...summary.$inc, ...actors.$inc },
|
|
});
|
|
|
|
// Remember what this attempt added, so the next one from the same actor
|
|
// increments instead of paying for a read.
|
|
for (const field of Object.keys(actors.$inc)) {
|
|
const m = /^actors\.([0-9a-f]{16})\.count$/.exec(field);
|
|
if (m) known.add(m[1]);
|
|
}
|
|
}
|
|
|
|
// The call every logger makes: fold, and swallow anything that goes wrong.
|
|
export function foldEventFireAndForget(doc, name = 'eventlog') {
|
|
try {
|
|
const p = foldEvent(doc);
|
|
if (p && typeof p.catch === 'function') {
|
|
p.catch(e => {
|
|
if (process.env.DEBUG === 'true') console.warn(`${name} fold failed:`, e && e.message);
|
|
});
|
|
}
|
|
} catch (e) {
|
|
if (process.env.DEBUG === 'true') console.warn(`${name} fold failed:`, e && e.message);
|
|
}
|
|
}
|