From fc075d778aa4c839a3e32ae666359c369663091c Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 3 May 2023 22:55:58 -0400 Subject: [PATCH] fix(striker-ui-api): add get anvil memory summary WIP --- .../NODE_AND_DR_RESERVED_MEMORY_SIZE.ts | 4 +- striker-ui-api/src/lib/consts/index.ts | 3 + .../anvil/buildQueryAnvilDetail.ts | 3 +- .../request_handlers/anvil/getAnvilMemory.ts | 81 +++++++++++++++++++ .../src/lib/request_handlers/anvil/index.ts | 1 + striker-ui-api/src/routes/anvil.ts | 2 + striker-ui-api/src/types/ApiAn.d.ts | 8 ++ 7 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 striker-ui-api/src/lib/request_handlers/anvil/getAnvilMemory.ts diff --git a/striker-ui-api/src/lib/consts/NODE_AND_DR_RESERVED_MEMORY_SIZE.ts b/striker-ui-api/src/lib/consts/NODE_AND_DR_RESERVED_MEMORY_SIZE.ts index 5d24748b..4c3390c3 100644 --- a/striker-ui-api/src/lib/consts/NODE_AND_DR_RESERVED_MEMORY_SIZE.ts +++ b/striker-ui-api/src/lib/consts/NODE_AND_DR_RESERVED_MEMORY_SIZE.ts @@ -1,4 +1,2 @@ // Unit: bytes -const NODE_AND_DR_RESERVED_MEMORY_SIZE = 8589934592; - -export default NODE_AND_DR_RESERVED_MEMORY_SIZE; +export const NODE_AND_DR_RESERVED_MEMORY_SIZE = 8589934592; diff --git a/striker-ui-api/src/lib/consts/index.ts b/striker-ui-api/src/lib/consts/index.ts index 8f37097d..2a859be4 100644 --- a/striker-ui-api/src/lib/consts/index.ts +++ b/striker-ui-api/src/lib/consts/index.ts @@ -5,6 +5,9 @@ export { SERVER_PATHS }; export * from './AN_VARIABLE_NAME_LIST'; export * from './DELETED'; export * from './EXIT_CODE_LIST'; +export * from './LOCAL'; +export * from './NODE_AND_DR_RESERVED_MEMORY_SIZE'; +export * from './OS_LIST'; export * from './PROCESS_OWNER'; export * from './REG_EXP_PATTERNS'; export * from './SERVER_PORT'; diff --git a/striker-ui-api/src/lib/request_handlers/anvil/buildQueryAnvilDetail.ts b/striker-ui-api/src/lib/request_handlers/anvil/buildQueryAnvilDetail.ts index 126196bf..d379e50e 100644 --- a/striker-ui-api/src/lib/request_handlers/anvil/buildQueryAnvilDetail.ts +++ b/striker-ui-api/src/lib/request_handlers/anvil/buildQueryAnvilDetail.ts @@ -1,5 +1,4 @@ -import NODE_AND_DR_RESERVED_MEMORY_SIZE from '../../consts/NODE_AND_DR_RESERVED_MEMORY_SIZE'; -import { OS_LIST } from '../../consts/OS_LIST'; +import { NODE_AND_DR_RESERVED_MEMORY_SIZE, OS_LIST } from '../../consts'; import join from '../../join'; import { stdoutVar } from '../../shell'; diff --git a/striker-ui-api/src/lib/request_handlers/anvil/getAnvilMemory.ts b/striker-ui-api/src/lib/request_handlers/anvil/getAnvilMemory.ts new file mode 100644 index 00000000..1fd242de --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/anvil/getAnvilMemory.ts @@ -0,0 +1,81 @@ +import { RequestHandler } from 'express'; + +import { NODE_AND_DR_RESERVED_MEMORY_SIZE } from '../../consts'; + +import { query } from '../../accessModule'; +import { stderr } from '../../shell'; + +export const getAnvilMemory: RequestHandler< + AnvilDetailParamsDictionary +> = async (request, response) => { + const { + params: { anvilUuid }, + } = request; + + let hostMemoryRows: [ + hostUuid: string, + minMemoryTotal: null | string, + hostMemoryTotal: string, + hostMemoryFree: string, + hostSwapTotal: string, + hostSwapFree: string, + ][]; + + try { + hostMemoryRows = await query( + `SELECT + b.host_uuid, + MIN(c.scan_hardware_ram_total) AS min_memory_total, + c.scan_hardware_ram_total, + c.scan_hardware_memory_free, + c.scan_hardware_swap_total, + c.scan_hardware_swap_free + 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}' + GROUP BY + b.host_uuid, + c.scan_hardware_ram_total, + c.scan_hardware_memory_free, + c.scan_hardware_swap_total, + c.scan_hardware_swap_free + ORDER BY b.host_name;`, + ); + } catch (error) { + stderr(`Failed to get anvil ${anvilUuid} memory info; CAUSE: ${error}`); + + return response.status(500).send(); + } + + const { + 0: { 1: rTotal }, + } = hostMemoryRows; + + if (rTotal === null) return response.status(404).send(); + + const total = Number.parseInt(rTotal); + + const hosts: AnvilDetailHostMemory[] = + hostMemoryRows.map( + ([host_uuid, , mtotal, mfree, stotal, sfree]) => ({ + free: Number.parseInt(mfree), + host_uuid, + swap_free: Number.parseInt(sfree), + swap_total: Number.parseInt(stotal), + total: Number.parseInt(mtotal), + }), + ); + + return response.status(200).send({ + hosts, + reserved: NODE_AND_DR_RESERVED_MEMORY_SIZE, + total, + }); +}; diff --git a/striker-ui-api/src/lib/request_handlers/anvil/index.ts b/striker-ui-api/src/lib/request_handlers/anvil/index.ts index 1a1ef9b0..c0750c82 100644 --- a/striker-ui-api/src/lib/request_handlers/anvil/index.ts +++ b/striker-ui-api/src/lib/request_handlers/anvil/index.ts @@ -1,3 +1,4 @@ export * from './getAnvil'; export * from './getAnvilCpu'; export * from './getAnvilDetail'; +export * from './getAnvilMemory'; diff --git a/striker-ui-api/src/routes/anvil.ts b/striker-ui-api/src/routes/anvil.ts index d893b582..36ec4d0d 100644 --- a/striker-ui-api/src/routes/anvil.ts +++ b/striker-ui-api/src/routes/anvil.ts @@ -4,6 +4,7 @@ import { getAnvil, getAnvilCpu, getAnvilDetail, + getAnvilMemory, } from '../lib/request_handlers/anvil'; const router = express.Router(); @@ -11,6 +12,7 @@ const router = express.Router(); router .get('/', getAnvil) .get('/:anvilUuid/cpu', getAnvilCpu) + .get('/:anvilUuid/memory', getAnvilMemory) .get('/:anvilUuid', getAnvilDetail); export default router; diff --git a/striker-ui-api/src/types/ApiAn.d.ts b/striker-ui-api/src/types/ApiAn.d.ts index 7e7ae3da..91932413 100644 --- a/striker-ui-api/src/types/ApiAn.d.ts +++ b/striker-ui-api/src/types/ApiAn.d.ts @@ -1,3 +1,11 @@ +type AnvilDetailHostMemory = { + free: number; + host_uuid: string; + swap_free: number; + swap_total: number; + total: number; +}; + type AnvilDetailHostSummary = { host_name: string; host_uuid: string;