Visual Console Refactor: added a WIP version of two items

Former-commit-id: 817361ea626057d7a23d2584fb1da1defa75b848
This commit is contained in:
Alejandro Gallardo Escobar 2019-03-21 18:42:53 +01:00
parent fa366590ce
commit 107594bf48
3 changed files with 140 additions and 6 deletions

View File

@ -79,6 +79,9 @@ function itemInstanceFrom(data: UnknownObject) {
case ItemType.MODULE_GRAPH:
throw new TypeError("item not found");
case ItemType.SIMPLE_VALUE:
case ItemType.SIMPLE_VALUE_MAX:
case ItemType.SIMPLE_VALUE_MIN:
case ItemType.SIMPLE_VALUE_AVG:
throw new TypeError("item not found");
case ItemType.PERCENTILE_BAR:
throw new TypeError("item not found");
@ -86,12 +89,6 @@ function itemInstanceFrom(data: UnknownObject) {
throw new TypeError("item not found");
case ItemType.ICON:
return new Icon(iconPropsDecoder(data));
case ItemType.SIMPLE_VALUE_MAX:
throw new TypeError("item not found");
case ItemType.SIMPLE_VALUE_MIN:
throw new TypeError("item not found");
case ItemType.SIMPLE_VALUE_AVG:
throw new TypeError("item not found");
case ItemType.PERCENTILE_BUBBLE:
throw new TypeError("item not found");
case ItemType.SERVICE:

View File

@ -0,0 +1,43 @@
import {
LinkedVisualConsoleProps,
UnknownObject,
WithModuleProps
} from "../types";
import { linkedVCPropsDecoder, modulePropsDecoder, parseIntOr } from "../lib";
import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item";
export type EventsHistoryProps = {
type: ItemType.AUTO_SLA_GRAPH;
maxTime: number | null;
data: any[]; // eslint-disable-line @typescript-eslint/no-explicit-any
} & ItemProps &
WithModuleProps &
LinkedVisualConsoleProps;
/**
* Build a valid typed object from a raw object.
* This will allow us to ensure the type safety.
*
* @param data Raw object.
* @return An object representing the simple value props.
* @throws Will throw a TypeError if some property
* is missing from the raw object or have an invalid type.
*/
export function eventsHistoryPropsDecoder(
data: UnknownObject
): EventsHistoryProps | never {
return {
...itemBasePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
type: ItemType.AUTO_SLA_GRAPH,
maxTime: parseIntOr(data.maxTime, null),
data: data.data instanceof Array ? data.data : [],
...modulePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
...linkedVCPropsDecoder(data) // Object spread. It will merge the properties of the two objects.
};
}
export default class EventsHistory extends Item<EventsHistoryProps> {
public createDomElement(): HTMLElement {
throw new Error("not implemented");
}
}

View File

@ -0,0 +1,94 @@
import {
LinkedVisualConsoleProps,
UnknownObject,
WithModuleProps
} from "../types";
import { linkedVCPropsDecoder, parseIntOr, modulePropsDecoder } from "../lib";
import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item";
export type SimpleValueProps = {
type: ItemType.SIMPLE_VALUE;
valueType: "string" | "image";
value: string;
} & (
| {
processValue: "none";
}
| {
processValue: "avg" | "max" | "min";
period: number;
}) &
ItemProps &
WithModuleProps &
LinkedVisualConsoleProps;
/**
* Extract a valid enum value from a raw value type.
* @param valueType Raw value.
*/
const parseValueType = (
valueType: any // eslint-disable-line @typescript-eslint/no-explicit-any
): SimpleValueProps["valueType"] => {
switch (valueType) {
case "string":
case "image":
return valueType;
default:
return "string";
}
};
/**
* Extract a valid enum value from a raw process value.
* @param processValue Raw value.
*/
const parseProcessValue = (
processValue: any // eslint-disable-line @typescript-eslint/no-explicit-any
): SimpleValueProps["processValue"] => {
switch (processValue) {
case "none":
case "avg":
case "max":
case "min":
return processValue;
default:
return "none";
}
};
/**
* Build a valid typed object from a raw object.
* This will allow us to ensure the type safety.
*
* @param data Raw object.
* @return An object representing the simple value props.
* @throws Will throw a TypeError if some property
* is missing from the raw object or have an invalid type.
*/
export function simpleValuePropsDecoder(
data: UnknownObject
): SimpleValueProps | never {
if (typeof data.value !== "string" || data.value.length === 0) {
throw new TypeError("invalid value");
}
const processValue = parseProcessValue(data.processValue);
return {
...itemBasePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
type: ItemType.SIMPLE_VALUE,
valueType: parseValueType(data.valueType),
value: data.value,
...(processValue === "none"
? { processValue }
: { processValue, period: parseIntOr(data.period, 0) }), // Object spread. It will merge the properties of the two objects.
...modulePropsDecoder(data), // Object spread. It will merge the properties of the two objects.
...linkedVCPropsDecoder(data) // Object spread. It will merge the properties of the two objects.
};
}
export default class SimpleValue extends Item<SimpleValueProps> {
public createDomElement(): HTMLElement {
throw new Error("not implemented");
}
}