From a56a38f4e6eac05e8fcd5195a45607ddc91caa95 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 5 May 2023 00:02:15 -0400 Subject: [PATCH] fix(striker-ui-api): complete get anvil memory summary --- .../request_handlers/anvil/getAnvilMemory.ts | 62 +++++++++++++++---- striker-ui-api/src/types/ApiAn.d.ts | 8 +-- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/striker-ui-api/src/lib/request_handlers/anvil/getAnvilMemory.ts b/striker-ui-api/src/lib/request_handlers/anvil/getAnvilMemory.ts index 1fd242de..5adb3b4a 100644 --- a/striker-ui-api/src/lib/request_handlers/anvil/getAnvilMemory.ts +++ b/striker-ui-api/src/lib/request_handlers/anvil/getAnvilMemory.ts @@ -1,4 +1,5 @@ import { RequestHandler } from 'express'; +import { DataSizeUnit, dSize } from 'format-data-size'; import { NODE_AND_DR_RESERVED_MEMORY_SIZE } from '../../consts'; @@ -55,27 +56,66 @@ export const getAnvilMemory: RequestHandler< } const { - 0: { 1: rTotal }, + 0: { 1: minTotal }, } = hostMemoryRows; - if (rTotal === null) return response.status(404).send(); - - const total = Number.parseInt(rTotal); + if (minTotal === null) return response.status(404).send(); const hosts: AnvilDetailHostMemory[] = hostMemoryRows.map( - ([host_uuid, , mtotal, mfree, stotal, sfree]) => ({ - free: Number.parseInt(mfree), + ([host_uuid, , total, free, swap_total, swap_free]) => ({ + free, host_uuid, - swap_free: Number.parseInt(sfree), - swap_total: Number.parseInt(stotal), - total: Number.parseInt(mtotal), + swap_free, + swap_total, + total, }), ); + let serverMemoryRows: [serverMemoryValue: string, serverMemoryUnit: string][]; + + try { + serverMemoryRows = await query( + `SELECT + CAST( + SUBSTRING( + a.server_definition_xml, 'memory.*>([\\d]*) 0) { + allocated = String( + serverMemoryRows.reduce((previous, [mvalue, munit]) => { + const serverMemory = + dSize(mvalue, { + fromUnit: munit as DataSizeUnit, + toUnit: 'B', + })?.value ?? '0'; + + return previous + BigInt(serverMemory); + }, BigInt(0)), + ); + } + return response.status(200).send({ + allocated, hosts, - reserved: NODE_AND_DR_RESERVED_MEMORY_SIZE, - total, + reserved: String(NODE_AND_DR_RESERVED_MEMORY_SIZE), + total: minTotal, }); }; diff --git a/striker-ui-api/src/types/ApiAn.d.ts b/striker-ui-api/src/types/ApiAn.d.ts index b05589b7..5e437048 100644 --- a/striker-ui-api/src/types/ApiAn.d.ts +++ b/striker-ui-api/src/types/ApiAn.d.ts @@ -1,9 +1,9 @@ type AnvilDetailHostMemory = { - free: number; + free: string; host_uuid: string; - swap_free: number; - swap_total: number; - total: number; + swap_free: string; + swap_total: string; + total: string; }; type AnvilDetailHostSummary = {