qualiteer/lib/server/database/queries/results.js

94 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-05-17 12:32:04 +00:00
import pg from "../postgres.js";
2022-08-21 20:58:51 +00:00
import { failingMock } from "../mocks/results-mock.js";
2022-05-17 12:32:04 +00:00
// Imports
import {
insertQuery,
selectWhereAnyQuery,
selectWhereAllQuery,
updateWhereAnyQuery,
} from "../pg-query.js";
// Constants
2022-10-08 17:47:46 +00:00
const table = "results";
const recentResultsMax = 5;
2022-08-06 21:21:41 +00:00
const PG_DISABLED = process.env.POSTGRES_DISABLED;
2022-05-17 12:32:04 +00:00
// Queries
export const insertTestResult = (testResult) => {
const {
2022-10-08 17:47:46 +00:00
name,
class: className,
method,
env,
timestamp,
triage,
2022-05-17 12:32:04 +00:00
failed,
2022-10-08 17:47:46 +00:00
message,
screenshot,
console: cs,
2022-05-17 12:32:04 +00:00
} = testResult;
var query = insertQuery(table, {
2022-10-08 17:47:46 +00:00
name,
class: className,
method,
env,
timestamp,
triage,
2022-05-17 12:32:04 +00:00
failed,
2022-10-08 17:47:46 +00:00
message,
screenshot,
console: cs,
2022-05-17 12:32:04 +00:00
});
query += "\n RETURNING *";
return pg.query(query);
};
2022-08-06 21:21:41 +00:00
export const getCurrentlyFailing = async () => {
if (PG_DISABLED) return failingMock();
2022-10-08 17:47:46 +00:00
/* 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}`
);
2022-05-17 12:32:04 +00:00
2022-10-08 17:47:46 +00:00
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;
2022-05-17 12:32:04 +00:00
};
2022-10-08 17:47:46 +00:00
export const ignoreResult = async ({ id }) => {
const query = updateWhereAnyQuery(table, { failed: false }, { id });
2022-05-17 12:32:04 +00:00
return pg.query(query);
};