From 8fe47a89148b927d2a3ab06005d0b7c8247be962 Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Wed, 10 Apr 2019 18:33:05 +0200 Subject: [PATCH] Added Donutgraph.ts, BarsGraph.ts and ModuleGraph.ts Former-commit-id: b7a224710e18a743a590086a21e22b5ecfe162ca --- visual_console_client/src/VisualConsole.ts | 8 ++- visual_console_client/src/items/BarsGraph.ts | 44 +++++++++++++++ visual_console_client/src/items/DonutGraph.ts | 55 +++++++++++++++++++ .../src/items/EventsHistory.ts | 13 +---- .../src/items/ModuleGraph.ts | 55 +++++++++++++++++++ 5 files changed, 163 insertions(+), 12 deletions(-) create mode 100644 visual_console_client/src/items/BarsGraph.ts create mode 100644 visual_console_client/src/items/DonutGraph.ts create mode 100644 visual_console_client/src/items/ModuleGraph.ts diff --git a/visual_console_client/src/VisualConsole.ts b/visual_console_client/src/VisualConsole.ts index 261d946a19..9cf5e3edf2 100644 --- a/visual_console_client/src/VisualConsole.ts +++ b/visual_console_client/src/VisualConsole.ts @@ -20,6 +20,9 @@ import EventsHistory, { } from "./items/EventsHistory"; import Percentile, { percentilePropsDecoder } from "./items/Percentile"; import TypedEvent, { Disposable, Listener } from "./TypedEvent"; +import DonutGraph, { donutGraphPropsDecoder } from "./items/DonutGraph"; +import BarsGraph, { barsGraphPropsDecoder } from "./items/BarsGraph"; +import ModuleGraph, { moduleGraphPropsDecoder } from "./items/ModuleGraph"; // Base properties. export interface VisualConsoleProps extends Size { @@ -84,7 +87,7 @@ function itemInstanceFrom(data: UnknownObject) { case ItemType.STATIC_GRAPH: return new StaticGraph(staticGraphPropsDecoder(data)); case ItemType.MODULE_GRAPH: - throw new TypeError("item not found"); + return new ModuleGraph(moduleGraphPropsDecoder(data)); case ItemType.SIMPLE_VALUE: case ItemType.SIMPLE_VALUE_MAX: case ItemType.SIMPLE_VALUE_MIN: @@ -110,8 +113,9 @@ function itemInstanceFrom(data: UnknownObject) { case ItemType.AUTO_SLA_GRAPH: return new EventsHistory(eventsHistoryPropsDecoder(data)); case ItemType.DONUT_GRAPH: + return new DonutGraph(donutGraphPropsDecoder(data)); case ItemType.BARS_GRAPH: - throw new TypeError("item not found"); + return new BarsGraph(barsGraphPropsDecoder(data)); case ItemType.CLOCK: return new Clock(clockPropsDecoder(data)); case ItemType.COLOR_CLOUD: diff --git a/visual_console_client/src/items/BarsGraph.ts b/visual_console_client/src/items/BarsGraph.ts new file mode 100644 index 0000000000..c7be5f82a9 --- /dev/null +++ b/visual_console_client/src/items/BarsGraph.ts @@ -0,0 +1,44 @@ +import { UnknownObject, WithModuleProps } from "../types"; +import { modulePropsDecoder, decodeBase64, stringIsEmpty } from "../lib"; +import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item"; + +export type BarsGraphProps = { + type: ItemType.BARS_GRAPH; + html: string; +} & ItemProps & + WithModuleProps; + +/** + * 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 bars graph props. + * @throws Will throw a TypeError if some property + * is missing from the raw object or have an invalid type. + */ +export function barsGraphPropsDecoder( + data: UnknownObject +): BarsGraphProps | never { + if (stringIsEmpty(data.html) && stringIsEmpty(data.encodedHtml)) { + throw new TypeError("missing html content."); + } + + return { + ...itemBasePropsDecoder(data), // Object spread. It will merge the properties of the two objects. + type: ItemType.BARS_GRAPH, + html: !stringIsEmpty(data.html) + ? data.html + : decodeBase64(data.encodedHtml), + ...modulePropsDecoder(data) // Object spread. It will merge the properties of the two objects. + }; +} + +export default class BarsGraph extends Item { + public createDomElement(): HTMLElement { + const element = document.createElement("div"); + element.innerHTML = this.props.html; + + return element; + } +} diff --git a/visual_console_client/src/items/DonutGraph.ts b/visual_console_client/src/items/DonutGraph.ts new file mode 100644 index 0000000000..2d4141bc22 --- /dev/null +++ b/visual_console_client/src/items/DonutGraph.ts @@ -0,0 +1,55 @@ +import { + LinkedVisualConsoleProps, + UnknownObject, + WithModuleProps +} from "../types"; +import { + linkedVCPropsDecoder, + modulePropsDecoder, + decodeBase64, + stringIsEmpty +} from "../lib"; +import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item"; + +export type DonutGraphProps = { + type: ItemType.DONUT_GRAPH; + html: string; +} & 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 donut graph props. + * @throws Will throw a TypeError if some property + * is missing from the raw object or have an invalid type. + */ +export function donutGraphPropsDecoder( + data: UnknownObject +): DonutGraphProps | never { + if (stringIsEmpty(data.html) && stringIsEmpty(data.encodedHtml)) { + throw new TypeError("missing html content."); + } + + return { + ...itemBasePropsDecoder(data), // Object spread. It will merge the properties of the two objects. + type: ItemType.DONUT_GRAPH, + html: !stringIsEmpty(data.html) + ? data.html + : decodeBase64(data.encodedHtml), + ...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 DonutGraph extends Item { + public createDomElement(): HTMLElement { + const element = document.createElement("div"); + element.innerHTML = this.props.html; + + return element; + } +} diff --git a/visual_console_client/src/items/EventsHistory.ts b/visual_console_client/src/items/EventsHistory.ts index dda5b3258d..9b002a31e4 100644 --- a/visual_console_client/src/items/EventsHistory.ts +++ b/visual_console_client/src/items/EventsHistory.ts @@ -1,10 +1,5 @@ +import { UnknownObject, WithModuleProps } from "../types"; import { - LinkedVisualConsoleProps, - UnknownObject, - WithModuleProps -} from "../types"; -import { - linkedVCPropsDecoder, modulePropsDecoder, parseIntOr, decodeBase64, @@ -17,8 +12,7 @@ export type EventsHistoryProps = { maxTime: number | null; html: string; } & ItemProps & - WithModuleProps & - LinkedVisualConsoleProps; + WithModuleProps; /** * Build a valid typed object from a raw object. @@ -43,8 +37,7 @@ export function eventsHistoryPropsDecoder( html: !stringIsEmpty(data.html) ? data.html : decodeBase64(data.encodedHtml), - ...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. + ...modulePropsDecoder(data) // Object spread. It will merge the properties of the two objects. }; } diff --git a/visual_console_client/src/items/ModuleGraph.ts b/visual_console_client/src/items/ModuleGraph.ts new file mode 100644 index 0000000000..e6b47733ea --- /dev/null +++ b/visual_console_client/src/items/ModuleGraph.ts @@ -0,0 +1,55 @@ +import { + LinkedVisualConsoleProps, + UnknownObject, + WithModuleProps +} from "../types"; +import { + linkedVCPropsDecoder, + modulePropsDecoder, + decodeBase64, + stringIsEmpty +} from "../lib"; +import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item"; + +export type ModuleGraphProps = { + type: ItemType.MODULE_GRAPH; + html: string; +} & 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 module graph props. + * @throws Will throw a TypeError if some property + * is missing from the raw object or have an invalid type. + */ +export function moduleGraphPropsDecoder( + data: UnknownObject +): ModuleGraphProps | never { + if (stringIsEmpty(data.html) && stringIsEmpty(data.encodedHtml)) { + throw new TypeError("missing html content."); + } + + return { + ...itemBasePropsDecoder(data), // Object spread. It will merge the properties of the two objects. + type: ItemType.MODULE_GRAPH, + html: !stringIsEmpty(data.html) + ? data.html + : decodeBase64(data.encodedHtml), + ...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 ModuleGraph extends Item { + public createDomElement(): HTMLElement { + const element = document.createElement("div"); + element.innerHTML = this.props.html; + + return element; + } +}