fix(striker-ui-api): add /job

main
Tsu-ba-me 2 years ago
parent 76e5f5f925
commit bc91fcd88b
  1. 82
      striker-ui-api/src/lib/request_handlers/job/getJob.ts
  2. 1
      striker-ui-api/src/lib/request_handlers/job/index.ts
  3. 2
      striker-ui-api/src/routes/index.ts
  4. 9
      striker-ui-api/src/routes/job.ts

@ -0,0 +1,82 @@
import { sanitizeQS } from '../../sanitizeQS';
import { date } from '../../shell';
import buildGetRequestHandler from '../buildGetRequestHandler';
export const getJob = buildGetRequestHandler((request, buildQueryOptions) => {
const { epoch } = request.query;
const sanitizedEpoch = sanitizeQS(epoch, { returnType: 'number' });
let condModifiedDate = '';
try {
const minDate = date('--date', `@${sanitizedEpoch}`, '--rfc-3339', 'ns');
condModifiedDate = `OR (job.job_progress = 100 AND job.modified_date >= '${minDate}')`;
} catch (shellError) {
throw new Error(
`Failed to build date condition for job query; CAUSE: ${shellError}`,
);
}
process.stdout.write(`condModifiedDate=[${condModifiedDate}]\n`);
if (buildQueryOptions) {
buildQueryOptions.afterQueryReturn = (queryStdout) => {
let result = queryStdout;
if (queryStdout instanceof Array) {
result = queryStdout.reduce<{
[jobUUID: string]: {
jobCommand: string;
jobHostName: string;
jobHostUUID: string;
jobName: string;
jobProgress: number;
jobUUID: string;
};
}>(
(
previous,
[
jobUUID,
jobName,
jobHostUUID,
jobHostName,
jobCommand,
rawJobProgress,
],
) => {
previous[jobUUID] = {
jobCommand,
jobHostName,
jobHostUUID,
jobName,
jobProgress: parseFloat(rawJobProgress),
jobUUID,
};
return previous;
},
{},
);
}
return result;
};
}
return `
SELECT
job.job_uuid,
job.job_name,
job.job_host_uuid,
hos.host_name,
job.job_command,
job.job_progress
FROM jobs AS job
JOIN hosts AS hos
ON job.job_host_uuid = hos.host_uuid
WHERE job.job_progress < 100
${condModifiedDate};`;
});

@ -5,6 +5,7 @@ import commandRouter from './command';
import echoRouter from './echo';
import fileRouter from './file';
import hostRouter from './host';
import jobRouter from './job';
import networkInterfaceRouter from './network-interface';
import serverRouter from './server';
@ -14,6 +15,7 @@ const routes: Readonly<Record<string, Router>> = {
echo: echoRouter,
file: fileRouter,
host: hostRouter,
job: jobRouter,
'network-interface': networkInterfaceRouter,
server: serverRouter,
};

@ -0,0 +1,9 @@
import express from 'express';
import { getJob } from '../lib/request_handlers/job';
const router = express.Router();
router.get('/', getJob);
export default router;
Loading…
Cancel
Save