From 107594bf485e4b2c18b24e45450cf89039fcee0c Mon Sep 17 00:00:00 2001
From: Alejandro Gallardo Escobar <alejandro.gallardo@artica.es>
Date: Thu, 21 Mar 2019 18:42:53 +0100
Subject: [PATCH] Visual Console Refactor: added a WIP version of two items

Former-commit-id: 817361ea626057d7a23d2584fb1da1defa75b848
---
 visual_console/src/VisualConsole.ts       |  9 +--
 visual_console/src/items/EventsHistory.ts | 43 +++++++++++
 visual_console/src/items/SimpleValue.ts   | 94 +++++++++++++++++++++++
 3 files changed, 140 insertions(+), 6 deletions(-)
 create mode 100644 visual_console/src/items/EventsHistory.ts
 create mode 100644 visual_console/src/items/SimpleValue.ts

diff --git a/visual_console/src/VisualConsole.ts b/visual_console/src/VisualConsole.ts
index 9eac4e5d7f..0b834d9ee3 100644
--- a/visual_console/src/VisualConsole.ts
+++ b/visual_console/src/VisualConsole.ts
@@ -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:
diff --git a/visual_console/src/items/EventsHistory.ts b/visual_console/src/items/EventsHistory.ts
new file mode 100644
index 0000000000..25b302c555
--- /dev/null
+++ b/visual_console/src/items/EventsHistory.ts
@@ -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");
+  }
+}
diff --git a/visual_console/src/items/SimpleValue.ts b/visual_console/src/items/SimpleValue.ts
new file mode 100644
index 0000000000..2f0eb5ca9e
--- /dev/null
+++ b/visual_console/src/items/SimpleValue.ts
@@ -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");
+  }
+}