fix(striker-ui-api): organize networks in host detail response

main
Tsu-ba-me 2 years ago
parent b24021ebdc
commit c1589eba3c
  1. 71
      striker-ui-api/src/lib/request_handlers/host/buildQueryHostDetail.ts
  2. 3
      striker-ui-api/src/types/Tree.d.ts

@ -1,45 +1,70 @@
import { buildKnownIDCondition } from '../../buildCondition'; import { buildKnownIDCondition } from '../../buildCondition';
import { buildQueryResultModifier } from '../../buildQueryResultModifier'; import { buildQueryResultModifier } from '../../buildQueryResultModifier';
import { cap } from '../../cap'; import { camel } from '../../camel';
import { getShortHostName } from '../../disassembleHostName'; import { getShortHostName } from '../../disassembleHostName';
import { stdout } from '../../shell'; import { stdout } from '../../shell';
type ExtractVariableKeyFunction = (parts: string[]) => string; const CVAR_PREFIX = 'form::config_step';
const MAP_TO_EXTRACTOR: { [prefix: string]: ExtractVariableKeyFunction } = { const MAP_TO_EXTRACTOR: Record<string, (parts: string[]) => string[]> = {
form: ([, part2]) => { form: ([, part2]) => {
const [head, ...rest] = part2.split('_'); const [rHead, ...rest] = part2.split('_');
const head = rHead.toLowerCase();
return rest.reduce<string>( return /^[a-z]+n[0-9]+/.test(head)
(previous, part) => `${previous}${cap(part)}`, ? ['networks', head, camel(...rest)]
head, : [camel(head, ...rest)];
);
}, },
'install-target': () => 'installTarget', 'install-target': () => ['installTarget'],
};
const setCvar = (
keychain: string[],
value: string,
parent: Tree = {},
): Tree | string => {
const { 0: key, length } = keychain;
if (!key) return value;
const next = 1;
const { [key]: xv } = parent;
parent[key] =
next < length && typeof xv !== 'string'
? setCvar(keychain.slice(next), value, xv)
: value;
return parent;
}; };
export const buildQueryHostDetail: BuildQueryDetailFunction = ({ export const buildQueryHostDetail: BuildQueryDetailFunction = ({
keys: hostUUIDs = '*', keys: hostUUIDs = '*',
} = {}) => { } = {}) => {
const condHostUUIDs = buildKnownIDCondition(hostUUIDs, 'AND hos.host_uuid'); const condHostUUIDs = buildKnownIDCondition(hostUUIDs, 'AND b.host_uuid');
stdout(`condHostUUIDs=[${condHostUUIDs}]`); stdout(`condHostUUIDs=[${condHostUUIDs}]`);
const query = ` const query = `
SELECT SELECT
hos.host_name, b.host_name,
hos.host_type, b.host_type,
hos.host_uuid, b.host_uuid,
var.variable_name, a.variable_name,
var.variable_value a.variable_value,
FROM variables AS var SUBSTRING(
JOIN hosts AS hos a.variable_name, '^${CVAR_PREFIX}\\d+::([^:]+)'
ON var.variable_source_uuid = hos.host_uuid ) as cvar_name
FROM variables AS a
JOIN hosts AS b
ON a.variable_source_uuid = b.host_uuid
WHERE ( WHERE (
variable_name LIKE 'form::config_%' variable_name LIKE '${CVAR_PREFIX}%'
OR variable_name = 'install-target::enabled' OR variable_name = 'install-target::enabled'
) )
${condHostUUIDs};`; ${condHostUUIDs}
ORDER BY cvar_name ASC,
a.variable_name ASC;`;
const afterQueryReturn: QueryResultModifierFunction = const afterQueryReturn: QueryResultModifierFunction =
buildQueryResultModifier((output) => { buildQueryResultModifier((output) => {
@ -52,14 +77,14 @@ export const buildQueryHostDetail: BuildQueryDetailFunction = ({
hostType: string; hostType: string;
hostUUID: string; hostUUID: string;
shortHostName: string; shortHostName: string;
} & Record<string, string> } & Tree
>( >(
(previous, [, , , variableName, variableValue]) => { (previous, [, , , variableName, variableValue]) => {
const [variablePrefix, ...restVariableParts] = const [variablePrefix, ...restVariableParts] =
variableName.split('::'); variableName.split('::');
const key = MAP_TO_EXTRACTOR[variablePrefix](restVariableParts); const keychain = MAP_TO_EXTRACTOR[variablePrefix](restVariableParts);
previous[key] = variableValue; setCvar(keychain, variableValue, previous);
return previous; return previous;
}, },

@ -0,0 +1,3 @@
type Tree<T = string> = {
[k: string]: Tree<T> | T;
};
Loading…
Cancel
Save