fix(striker-ui-api): add get anvil CPU summary

main
Tsu-ba-me 2 years ago
parent e35f326573
commit d4eeb2eb89
  1. 74
      striker-ui-api/src/lib/request_handlers/anvil/getAnvilCpu.ts
  2. 1
      striker-ui-api/src/lib/request_handlers/anvil/index.ts
  3. 11
      striker-ui-api/src/routes/anvil.ts
  4. 1
      striker-ui-api/src/types/GetAnvilDataFunction.d.ts

@ -0,0 +1,74 @@
import { RequestHandler } from 'express';
import { query } from '../../accessModule';
import { stderr } from '../../shell';
export const getAnvilCpu: RequestHandler<AnvilDetailParamsDictionary> = async (
request,
response,
) => {
const {
params: { anvilUuid },
} = request;
let rCores: null | string;
let rThreads: null | string;
try {
[[rCores = '', rThreads = '']] = await query<
[[cpuCores: null | string, cpuThreads: null | string]]
>(
`SELECT
MIN(c.scan_hardware_cpu_cores) AS cores,
MIN(c.scan_hardware_cpu_threads) AS threads
FROM anvils AS a
JOIN hosts AS b
ON b.host_uuid IN (
a.anvil_node1_host_uuid,
a.anvil_node2_host_uuid,
a.anvil_dr1_host_uuid
)
JOIN scan_hardware AS c
ON b.host_uuid = c.scan_hardware_host_uuid
WHERE a.anvil_uuid = '${anvilUuid}';`,
);
} catch (error) {
stderr(`Failed to get anvil ${anvilUuid} cpu info; CAUSE: ${error}`);
return response.status(500).send();
}
const cores = Number.parseInt(rCores);
const threads = Number.parseInt(rThreads);
let rAllocated: null | string;
try {
[[rAllocated = '']] = await query<[[cpuAllocated: null | string]]>(
`SELECT
SUM(
CAST(
SUBSTRING(
b.server_definition_xml, 'cores=''([\\d]*)'''
) AS INTEGER
)
) AS allocated
FROM servers AS a
JOIN server_definitions AS b
ON a.server_uuid = b.server_definition_server_uuid
WHERE a.server_anvil_uuid = '${anvilUuid}';`,
);
} catch (error) {
stderr(`Failed to get anvil ${anvilUuid} server cpu info; CAUSE: ${error}`);
return response.status(500).send();
}
const allocated = Number.parseInt(rAllocated);
response.status(200).send({
allocated,
cores,
threads,
});
};

@ -1,2 +1,3 @@
export * from './getAnvil'; export * from './getAnvil';
export * from './getAnvilCpu';
export * from './getAnvilDetail'; export * from './getAnvilDetail';

@ -1,9 +1,16 @@
import express from 'express'; import express from 'express';
import { getAnvil, getAnvilDetail } from '../lib/request_handlers/anvil'; import {
getAnvil,
getAnvilCpu,
getAnvilDetail,
} from '../lib/request_handlers/anvil';
const router = express.Router(); const router = express.Router();
router.get('/', getAnvil).get('/:anvilUuid', getAnvilDetail); router
.get('/', getAnvil)
.get('/:anvilUuid/cpu', getAnvilCpu)
.get('/:anvilUuid', getAnvilDetail);
export default router; export default router;

@ -6,6 +6,7 @@ type AnvilDataAnvilListHash = {
anvil_uuid: { anvil_uuid: {
[uuid: string]: { [uuid: string]: {
anvil_description: string; anvil_description: string;
anvil_dr1_host_uuid?: string;
anvil_node1_host_uuid: string; anvil_node1_host_uuid: string;
anvil_node2_host_uuid: string; anvil_node2_host_uuid: string;
query_time: number; query_time: number;

Loading…
Cancel
Save