WIP: Visual Console Refactor

Former-commit-id: 7114433022097a8b6b2eb4e1f77c91cf8f8007cf
This commit is contained in:
Alejandro Gallardo Escobar 2019-02-12 16:38:36 +01:00
parent 49aaa91d2e
commit 8a8de49ecc
3 changed files with 124 additions and 8 deletions

View File

@ -4,6 +4,8 @@ import {
UnknownObject
} from "../types";
import { modulePropsDecoder } from "../lib";
import VisualConsoleItem, {
VisualConsoleItemProps,
itemPropsDecoder
@ -49,7 +51,8 @@ export function staticGraphPropsDecoder(
return {
imageSrc: data.imageSrc,
showLastValueTooltip: parseShowLastValueTooltip(data.showLastValueTooltip),
...itemPropsDecoder(data) // Object spread. It will merge the properties of the two objects.
...itemPropsDecoder(data), // Object spread. It will merge the properties of the two objects.
...modulePropsDecoder(data) // Object spread. It will merge the properties of the two objects.
};
}

View File

@ -1,4 +1,12 @@
import { Position, Size, UnknownObject } from "./types";
import {
UnknownObject,
Position,
Size,
WithAgentProps,
WithModuleProps,
LinkedVisualConsoleProps,
LinkedVisualConsolePropsStatus
} from "./types";
/**
* Return a number or a default value from a raw value.
@ -58,3 +66,107 @@ export function sizePropsDecoder(data: UnknownObject): Size | never {
height: parseInt(data.height)
};
}
/**
* Build a valid typed object from a raw object.
* @param data Raw object.
* @return An object representing the agent properties.
*/
export function agentPropsDecoder(data: UnknownObject): WithAgentProps {
// Object destructuring: http://exploringjs.com/es6/ch_destructuring.html
const { metaconsoleId, agentId: id, agentName: name } = data;
const agentProps: WithAgentProps = {
agentId: parseIntOr(id, null),
agentName: typeof name === "string" && name.length > 0 ? name : null
};
return metaconsoleId != null
? {
metaconsoleId,
...agentProps // Object spread: http://exploringjs.com/es6/ch_parameter-handling.html#sec_spread-operator
}
: agentProps;
}
/**
* Build a valid typed object from a raw object.
* @param data Raw object.
* @return An object representing the module and agent properties.
*/
export function modulePropsDecoder(data: UnknownObject): WithModuleProps {
// Object destructuring: http://exploringjs.com/es6/ch_destructuring.html
const { moduleId: id, moduleName: name } = data;
return {
moduleId: parseIntOr(id, null),
moduleName: typeof name === "string" && name.length > 0 ? name : null,
...agentPropsDecoder(data)
};
}
/**
* Build a valid typed object from a raw object.
* @param data Raw object.
* @return An object representing the linked visual console properties.
* @throws Will throw a TypeError if the status calculation properties are invalid.
*/
export function linkedVCPropsDecoder(
data: UnknownObject
): LinkedVisualConsoleProps | never {
// Object destructuring: http://exploringjs.com/es6/ch_destructuring.html
const {
metaconsoleId,
linkedLayoutId: id,
linkedLayoutAgentId: agentId
} = data;
let linkedLayoutStatusProps: LinkedVisualConsolePropsStatus = {
linkedLayoutStatusType: "default"
};
switch (data.linkedLayoutStatusType) {
case "weight":
const weight = parseIntOr(data.linkedLayoutStatusTypeWeight, null);
if (weight == null)
throw new TypeError("invalid status calculation properties.");
if (data.linkedLayoutStatusTypeWeight)
linkedLayoutStatusProps = {
linkedLayoutStatusType: "weight",
linkedLayoutStatusTypeWeight: weight
};
break;
case "service":
const warningThreshold = parseIntOr(
data.linkedLayoutStatusTypeWarningThreshold,
null
);
const criticalThreshold = parseIntOr(
data.linkedLayoutStatusTypeCriticalThreshold,
null
);
if (warningThreshold == null || criticalThreshold == null) {
throw new TypeError("invalid status calculation properties.");
}
linkedLayoutStatusProps = {
linkedLayoutStatusType: "service",
linkedLayoutStatusTypeWarningThreshold: warningThreshold,
linkedLayoutStatusTypeCriticalThreshold: criticalThreshold
};
break;
}
const linkedLayoutBaseProps = {
linkedLayoutId: parseIntOr(id, null),
linkedLayoutAgentId: parseIntOr(agentId, null),
...linkedLayoutStatusProps // Object spread: http://exploringjs.com/es6/ch_parameter-handling.html#sec_spread-operator
};
return metaconsoleId != null
? {
metaconsoleId,
...linkedLayoutBaseProps // Object spread: http://exploringjs.com/es6/ch_parameter-handling.html#sec_spread-operator
}
: linkedLayoutBaseProps;
}

View File

@ -23,11 +23,7 @@ export interface WithModuleProps extends WithAgentProps {
moduleName: string | null;
}
export type LinkedVisualConsoleProps = {
metaconsoleId?: number | null;
linkedLayoutId: number | null;
linkedLayoutAgentId: number | null;
} & (
export type LinkedVisualConsolePropsStatus =
| {
linkedLayoutStatusType: "default";
}
@ -39,4 +35,9 @@ export type LinkedVisualConsoleProps = {
linkedLayoutStatusType: "service";
linkedLayoutStatusTypeWarningThreshold: number;
linkedLayoutStatusTypeCriticalThreshold: number;
});
};
export type LinkedVisualConsoleProps = {
metaconsoleId?: number | null;
linkedLayoutId: number | null;
linkedLayoutAgentId: number | null;
} & LinkedVisualConsolePropsStatus;