fix(striker-ui-api): complete get anvil memory summary

main
Tsu-ba-me 2 years ago
parent 1978983340
commit a56a38f4e6
  1. 62
      striker-ui-api/src/lib/request_handlers/anvil/getAnvilMemory.ts
  2. 8
      striker-ui-api/src/types/ApiAn.d.ts

@ -1,4 +1,5 @@
import { RequestHandler } from 'express'; import { RequestHandler } from 'express';
import { DataSizeUnit, dSize } from 'format-data-size';
import { NODE_AND_DR_RESERVED_MEMORY_SIZE } from '../../consts'; import { NODE_AND_DR_RESERVED_MEMORY_SIZE } from '../../consts';
@ -55,27 +56,66 @@ export const getAnvilMemory: RequestHandler<
} }
const { const {
0: { 1: rTotal }, 0: { 1: minTotal },
} = hostMemoryRows; } = hostMemoryRows;
if (rTotal === null) return response.status(404).send(); if (minTotal === null) return response.status(404).send();
const total = Number.parseInt(rTotal);
const hosts: AnvilDetailHostMemory[] = const hosts: AnvilDetailHostMemory[] =
hostMemoryRows.map<AnvilDetailHostMemory>( hostMemoryRows.map<AnvilDetailHostMemory>(
([host_uuid, , mtotal, mfree, stotal, sfree]) => ({ ([host_uuid, , total, free, swap_total, swap_free]) => ({
free: Number.parseInt(mfree), free,
host_uuid, host_uuid,
swap_free: Number.parseInt(sfree), swap_free,
swap_total: Number.parseInt(stotal), swap_total,
total: Number.parseInt(mtotal), total,
}), }),
); );
let serverMemoryRows: [serverMemoryValue: string, serverMemoryUnit: string][];
try {
serverMemoryRows = await query(
`SELECT
CAST(
SUBSTRING(
a.server_definition_xml, 'memory.*>([\\d]*)</memory'
) AS BIGINT
) AS server_memory_value,
SUBSTRING(
a.server_definition_xml, 'memory.*unit=''([A-Za-z]*)'''
) AS server_memory_unit
FROM server_definitions AS a
JOIN servers AS b
ON b.server_uuid = a.server_definition_server_uuid
WHERE server_anvil_uuid = '${anvilUuid}';`,
);
} catch (error) {
stderr(`Failed to get anvil ${anvilUuid} server info; CAUSE: ${error}`);
return response.status(500).send();
}
let allocated = '0';
if (serverMemoryRows.length > 0) {
allocated = String(
serverMemoryRows.reduce<bigint>((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({ return response.status(200).send({
allocated,
hosts, hosts,
reserved: NODE_AND_DR_RESERVED_MEMORY_SIZE, reserved: String(NODE_AND_DR_RESERVED_MEMORY_SIZE),
total, total: minTotal,
}); });
}; };

@ -1,9 +1,9 @@
type AnvilDetailHostMemory = { type AnvilDetailHostMemory = {
free: number; free: string;
host_uuid: string; host_uuid: string;
swap_free: number; swap_free: string;
swap_total: number; swap_total: string;
total: number; total: string;
}; };
type AnvilDetailHostSummary = { type AnvilDetailHostSummary = {

Loading…
Cancel
Save