Visual Console Refactor: added the status image to the static graph item

Former-commit-id: 26560bf06dd8a88e8b24beed3244b030a94268b9
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-03 13:19:50 +02:00
parent bd90bbd922
commit 3afe6f8d3a
2 changed files with 47 additions and 5 deletions

View File

@ -6,7 +6,7 @@ namespace Models\VisualConsole\Items;
use Models\VisualConsole\Item; use Models\VisualConsole\Item;
/** /**
* Model of a group item of the Visual Console. * Model of a static graph item of the Visual Console.
*/ */
final class StaticGraph extends Item final class StaticGraph extends Item
{ {
@ -35,7 +35,7 @@ final class StaticGraph extends Item
* *
* @return array Data structure representing the model. * @return array Data structure representing the model.
* *
* @overrides Item::decode. * @overrides Item->decode.
*/ */
protected function decode(array $data): array protected function decode(array $data): array
{ {
@ -43,6 +43,11 @@ final class StaticGraph extends Item
$return['type'] = STATIC_GRAPH; $return['type'] = STATIC_GRAPH;
$return['imageSrc'] = $this->extractImageSrc($data); $return['imageSrc'] = $this->extractImageSrc($data);
$return['showLastValueTooltip'] = $this->extractShowLastValueTooltip($data); $return['showLastValueTooltip'] = $this->extractShowLastValueTooltip($data);
$return['statusImageSrc'] = static::notEmptyStringOr(
$data['statusImageSrc'],
null
);
return $return; return $return;
} }
@ -63,7 +68,7 @@ final class StaticGraph extends Item
null null
); );
if ($imageSrc === null || \strlen($imageSrc) === 0) { if ($imageSrc === null) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'the image src property is required and should be a non empty string' 'the image src property is required and should be a non empty string'
); );
@ -118,4 +123,35 @@ final class StaticGraph extends Item
} }
/**
* Fetch a vc item data structure from the database using a filter.
*
* @param array $filter Filter of the Visual Console Item.
*
* @return array The Visual Console Item data structure stored into the DB.
* @throws \InvalidArgumentException When an agent Id cannot be found.
*
* @override Item::fetchDataFromDB.
*/
protected static function fetchDataFromDB(array $filter): array
{
// Due to this DB call, this function cannot be unit tested without
// a proper mock.
$data = parent::fetchDataFromDB($filter);
/*
* Retrieve extra data.
*/
// Load side libraries.
global $config;
include_once $config['homedir'].'/include/functions_visual_map.php';
// Get the img src.
$data['statusImageSrc'] = \visual_map_get_image_status_element($data);
return $data;
}
} }

View File

@ -4,13 +4,18 @@ import {
UnknownObject UnknownObject
} from "../types"; } from "../types";
import { modulePropsDecoder, linkedVCPropsDecoder } from "../lib"; import {
modulePropsDecoder,
linkedVCPropsDecoder,
notEmptyStringOr
} from "../lib";
import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item"; import Item, { ItemType, ItemProps, itemBasePropsDecoder } from "../Item";
export type StaticGraphProps = { export type StaticGraphProps = {
type: ItemType.STATIC_GRAPH; type: ItemType.STATIC_GRAPH;
imageSrc: string; // URL? imageSrc: string; // URL?
showLastValueTooltip: "default" | "enabled" | "disabled"; showLastValueTooltip: "default" | "enabled" | "disabled";
statusImageSrc: string | null; // URL?
} & ItemProps & } & ItemProps &
(WithModuleProps | LinkedVisualConsoleProps); (WithModuleProps | LinkedVisualConsoleProps);
@ -52,6 +57,7 @@ export function staticGraphPropsDecoder(
type: ItemType.STATIC_GRAPH, type: ItemType.STATIC_GRAPH,
imageSrc: data.imageSrc, imageSrc: data.imageSrc,
showLastValueTooltip: parseShowLastValueTooltip(data.showLastValueTooltip), showLastValueTooltip: parseShowLastValueTooltip(data.showLastValueTooltip),
statusImageSrc: notEmptyStringOr(data.statusImageSrc, null),
...modulePropsDecoder(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.
...linkedVCPropsDecoder(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.
}; };
@ -61,7 +67,7 @@ export default class StaticGraph extends Item<StaticGraphProps> {
public createDomElement(): HTMLElement { public createDomElement(): HTMLElement {
const img: HTMLImageElement = document.createElement("img"); const img: HTMLImageElement = document.createElement("img");
img.className = "static-graph"; img.className = "static-graph";
img.src = this.props.imageSrc; img.src = this.props.statusImageSrc || this.props.imageSrc;
// TODO: Show last value in a tooltip. // TODO: Show last value in a tooltip.