From 9e9b84604f238cdcf1b5b97d7ccaf6763bb9b5ff Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 17 Mar 2023 19:34:47 -0400 Subject: [PATCH] fix(striker-ui-api): add /manifest/template --- .../manifest/getManifestTemplate.ts | 107 ++++++++++++++++++ .../lib/request_handlers/manifest/index.ts | 1 + striker-ui-api/src/routes/manifest.ts | 7 +- 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 striker-ui-api/src/lib/request_handlers/manifest/getManifestTemplate.ts diff --git a/striker-ui-api/src/lib/request_handlers/manifest/getManifestTemplate.ts b/striker-ui-api/src/lib/request_handlers/manifest/getManifestTemplate.ts new file mode 100644 index 00000000..ad94ba63 --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/manifest/getManifestTemplate.ts @@ -0,0 +1,107 @@ +import { RequestHandler } from 'express'; + +import { dbQuery, getLocalHostName } from '../../accessModule'; +import { + getHostNameDomain, + getHostNamePrefix, + getShortHostName, +} from '../../disassembleHostName'; +import { stderr } from '../../shell'; + +export const getManifestTemplate: RequestHandler = (request, response) => { + let localHostName = ''; + + try { + localHostName = getLocalHostName(); + } catch (subError) { + stderr(String(subError)); + + response.status(500).send(); + + return; + } + + const localDomain = getHostNameDomain(localHostName); + const localShort = getShortHostName(localHostName); + const localPrefix = getHostNamePrefix(localShort); + + let rawQueryResult: Array< + [fenceUUID: string, fenceName: string, upsUUID: string, upsName: string] + >; + + try { + ({ stdout: rawQueryResult } = dbQuery( + `SELECT + fence_uuid, + fence_name, + ups_uuid, + ups_name + FROM ( + SELECT + ROW_NUMBER() OVER (ORDER BY fence_name), + fence_uuid, + fence_name + FROM fences + ORDER BY fence_name + ) AS a + FULL JOIN ( + SELECT + ROW_NUMBER() OVER (ORDER BY ups_name), + ups_uuid, + ups_name + FROM upses + ORDER BY ups_name + ) AS b ON a.row_number = b.row_number;`, + )); + } catch (queryError) { + stderr(`Failed to execute query; CAUSE: ${queryError}`); + + response.status(500).send(); + + return; + } + + const queryResult = rawQueryResult.reduce<{ + fences: { + [fenceUUID: string]: { + fenceName: string; + fenceUUID: string; + }; + }; + upses: { + [upsUUID: string]: { + upsName: string; + upsUUID: string; + }; + }; + }>( + (previous, [fenceUUID, fenceName, upsUUID, upsName]) => { + const { fences, upses } = previous; + + if (fenceUUID) { + fences[fenceUUID] = { + fenceName, + fenceUUID, + }; + } + + if (upsUUID) { + upses[upsUUID] = { + upsName, + upsUUID, + }; + } + + return previous; + }, + { fences: {}, upses: {} }, + ); + + response.status(200).send({ + localHostName, + localShort, + localPrefix, + localDomain, + ...queryResult, + }); +}; diff --git a/striker-ui-api/src/lib/request_handlers/manifest/index.ts b/striker-ui-api/src/lib/request_handlers/manifest/index.ts index 79e54734..bf41d98e 100644 --- a/striker-ui-api/src/lib/request_handlers/manifest/index.ts +++ b/striker-ui-api/src/lib/request_handlers/manifest/index.ts @@ -1 +1,2 @@ export * from './getManifest'; +export * from './getManifestTemplate'; diff --git a/striker-ui-api/src/routes/manifest.ts b/striker-ui-api/src/routes/manifest.ts index 2502a183..6b3e5e5c 100644 --- a/striker-ui-api/src/routes/manifest.ts +++ b/striker-ui-api/src/routes/manifest.ts @@ -1,9 +1,12 @@ import express from 'express'; -import { getManifest } from '../lib/request_handlers/manifest'; +import { + getManifest, + getManifestTemplate, +} from '../lib/request_handlers/manifest'; const router = express.Router(); -router.get('/', getManifest); +router.get('/', getManifest).get('/template', getManifestTemplate); export default router;