Refactor events.list.

The SQL has been simplified and the following changes made:

- The `sequence` argument now can only take one individual
  sequence, not a list of sequences.
- A new `sequences` argument is recognised. It takes a list
  of sequences (as a string).
- A new `label` argument is recognised. It takes a label
  name and returns events containing that label.
- A new `jpq` argument is recognised. It takes a JSONPath
  string which is applied to `meta` with jsonb_path_exists(),
  returning any events for which the JSON path expression
  matches.
This commit is contained in:
D. Berge
2023-09-30 01:37:22 +02:00
parent 0bfe54e0c2
commit 8755fe01b6

View File

@@ -10,25 +10,32 @@ async function list (projectId, opts = {}) {
const offset = Math.abs((opts.page-1)*opts.itemsPerPage) || 0; const offset = Math.abs((opts.page-1)*opts.itemsPerPage) || 0;
const limit = Math.abs(Number(opts.itemsPerPage)) || null; const limit = Math.abs(Number(opts.itemsPerPage)) || null;
const filter = opts.sequence const sequence = opts.sequence && Number(opts.sequence) || null;
? String(opts.sequence).includes(";") const sequences = opts.sequences && opts.sequences.split(/[^0-9]+/).map(v => Number(v)) || null;
? [ "sequence = ANY ( $1 )", [ opts.sequence.split(";") ] ] const date0 = opts.date0 ?? null;
: [ "sequence = $1", [ opts.sequence ] ] const date1 = opts.date1 ?? null;
: opts.date0 const jpq = opts.jpq || null;
? opts.date1 const label = opts.label ?? null;
? [ "date(tstamp) BETWEEN SYMMETRIC $1 AND $2", [ opts.date0, opts.date1 ] ]
: [ "date(tstamp) = $1", [ opts.date0 ] ]
: [ "true = true", [] ];
const text = ` const text = `
SELECT * SELECT *
FROM event_log e FROM event_log e
WHERE WHERE
${filter[0]} ($1::numeric IS NULL OR sequence = $1) AND
ORDER BY ${sortKey} ${sortDir}; ($2::numeric[] IS NULL OR sequence = ANY( $2 )) AND
($3::timestamptz IS NULL OR date(tstamp) = $3) AND
($3::timestamptz IS NULL OR
(($4::timestamptz IS NULL AND date(tstamp) = $3) OR
date(tstamp) BETWEEN SYMMETRIC $3 AND $4)) AND
($5::jsonpath IS NULL OR jsonb_path_exists(meta::jsonb, $5::jsonpath)) AND
($6::text IS NULL OR $6 = ANY(labels))
ORDER BY ${sortKey} ${sortDir}
LIMIT ${limit};
`; `;
const res = await client.query(text, filter[1]); const values = [ sequence, sequences, date0, date1, jpq, label ];
const res = await client.query(text, values);
client.release(); client.release();
return res.rows.map(i => replaceMarkers(i)); return res.rows.map(i => replaceMarkers(i));
} }