import pg from "../postgres.js"; import { failingMock } from "../mocks/results-mock.js"; // Imports import { insertQuery, selectWhereAnyQuery, selectWhereAllQuery, updateWhereAnyQuery, } from "../pg-query.js"; // Constants const table = "results"; const recentResultsMax = 5; const PG_DISABLED = process.env.POSTGRES_DISABLED; // Queries export const insertTestResult = (testResult) => { const { name, class: className, method, env, timestamp, triage, failed, message, screenshot, console: cs, } = testResult; var query = insertQuery(table, { name, class: className, method, env, timestamp, triage, failed, message, screenshot, console: cs, }); query += "\n RETURNING *"; return pg.query(query); }; export const getCurrentlyFailing = async () => { if (PG_DISABLED) return failingMock(); /* This can probably be changed into a super query, but perhaps faster/smaller */ const recent = `SELECT * FROM ${table} WHERE (timestamp BETWEEN NOW() - INTERVAL '24 HOURS' AND NOW()) AND NOT(failed AND triage)`; const slimCatalog = `SELECT name, crons, class, type, pipeline, env AS enabled_env FROM catalog`; const failing = `SELECT * FROM recent INNER JOIN slim_catalog USING(name) WHERE timestamp = (SELECT MAX(timestamp) FROM recent r2 WHERE recent.name = r2.name) AND failed`; const applicableFailing = `SELECT name, count(*) as fails FROM recent WHERE recent.name IN (SELECT name FROM failing) GROUP BY name`; /*const runHistory = `SELECT name, timestamp, failed FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY name ORDER BY timestamp) as n FROM ${table} WHERE name IN (SELECT name FROM failing)) as ord WHERE n <= ${recentResultsMax} ORDER BY name DESC`;*/ const runHistory = `SELECT name, timestamp, failed FROM results WHERE NOT triage AND name IN (SELECT name FROM failing) ORDER BY timestamp DESC LIMIT ${recentResultsMax}`; // const recentQuery = pg.query(recent); const failingQuery = pg.query( `WITH recent as (${recent}), slim_catalog as (${slimCatalog}) ${failing}` ); const applicableQuery = pg.query( `WITH recent as (${recent}), slim_catalog as (${slimCatalog}), failing as (${failing}) ${applicableFailing}` ); const historyQuery = pg.query( `WITH recent as (${recent}), slim_catalog as (${slimCatalog}), failing as (${failing}) ${runHistory}` ); const [currentlyFailing, applicableFails, failHistory] = await Promise.all([ failingQuery, applicableQuery, historyQuery, ]); for (var i = 0; i < currentlyFailing.length; i++) { currentlyFailing[i].dailyFails = parseInt( applicableFails.find((af) => af.name === currentlyFailing[i].name).fails ); currentlyFailing[i].recentResults = []; currentlyFailing[i].enabledEnv = currentlyFailing[i].enabled_env; currentlyFailing[i].isPipeline = currentlyFailing[i].pipeline; delete currentlyFailing[i].enabled_env; delete currentlyFailing[i].pipeline; for (var fh of failHistory) { if (fh.name !== currentlyFailing[i].name) continue; currentlyFailing[i].recentResults.push(fh); } } return currentlyFailing; }; export const ignoreResult = async ({ id }) => { const query = updateWhereAnyQuery(table, { failed: false }, { id }); return pg.query(query); };