From cb2be4cd6f4f052801c7952d9ebe4bebc018dd09 Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Mon, 8 Apr 2019 16:21:26 +0200 Subject: [PATCH] Added DonutGraph Former-commit-id: 09b54770531c96a6f5c504f06caaefc10463bba9 --- .../models/VisualConsole/Container.php | 3 +- .../models/VisualConsole/Items/DonutGraph.php | 153 ++++++++++++++++++ .../visual_console/visual_console_view.php | 2 +- .../VisualConsole/Items/DonutGraphTest.php | 103 ++++++++++++ .../VisualConsole/Items/PercentileTest.php | 8 +- visual_console_client/src/VisualConsole.ts | 3 - 6 files changed, 262 insertions(+), 10 deletions(-) create mode 100644 pandora_console/include/rest-api/models/VisualConsole/Items/DonutGraph.php create mode 100644 pandora_console/tests/Functional/Models/VisualConsole/Items/DonutGraphTest.php diff --git a/pandora_console/include/rest-api/models/VisualConsole/Container.php b/pandora_console/include/rest-api/models/VisualConsole/Container.php index a9e2f5a274..9c8e18bc6d 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Container.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Container.php @@ -251,8 +251,7 @@ final class Container extends Model return Items\EventsHistory::class; case DONUT_GRAPH: - // TODO: Instance return. - break; + return Items\DonutGraph::class; case BARS_GRAPH: // TODO: Instance return. diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/DonutGraph.php b/pandora_console/include/rest-api/models/VisualConsole/Items/DonutGraph.php new file mode 100644 index 0000000000..ba5485c150 --- /dev/null +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/DonutGraph.php @@ -0,0 +1,153 @@ +extractBorderColor($data); + return $return; + } + + + /** + * Extract a border color value. + * + * @param array $data Unknown input data structure. + * + * @return mixed String representing the border color (not empty) or null. + */ + private function extractBorderColor(array $data) + { + return static::notEmptyStringOr( + static::issetInArray($data, ['color', 'border_color']), + null + ); + } + + + /** + * 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_graph.php'; + + // Get the linked agent and module Ids. + $linkedModule = static::extractLinkedModule($data); + $agentId = static::parseIntOr($linkedModule['agentId'], null); + $moduleId = static::parseIntOr($linkedModule['moduleId'], null); + + if ($agentId === null) { + throw new \InvalidArgumentException('missing agent Id'); + } + + if (!empty($data['id_metaconsole'])) { + $connection = db_get_row_filter('tmetaconsole_setup', $data['id_metaconsole']); + if (metaconsole_load_external_db($connection) != NOERR) { + throw new \InvalidArgumentException( + 'error connecting to the node' + ); + } + } + + $is_string = db_get_value_filter( + 'id_tipo_modulo', + 'tagente_modulo', + [ + 'id_agente' => $agentId, + 'id_agente_modulo' => $moduleId, + ] + ); + + if (!empty($data['id_metaconsole'])) { + metaconsole_restore_db(); + } + + if (($is_string === 17) || ($is_string === 23) || ($is_string === 3) + || ($is_string === 10) || ($is_string === 33) + ) { + $donut_data = get_donut_module_data($moduleId); + + $img = d3_donut_graph( + $data['id'], + $data['width'], + $data['width'], + $donut_data, + $data['border_color'] + ); + } else { + if ($data['id_metaconsole'] !== 0) { + $img = ''; + } else { + $img = ''; + } + } + + $data['html'] = $img; + + return $data; + } + + +} diff --git a/pandora_console/operation/visual_console/visual_console_view.php b/pandora_console/operation/visual_console/visual_console_view.php index b5050b136b..636bc7f237 100644 --- a/pandora_console/operation/visual_console/visual_console_view.php +++ b/pandora_console/operation/visual_console/visual_console_view.php @@ -179,7 +179,7 @@ if (is_dir($dir)) { if (container != null) { try { - var visualConsole = new VisualConsole(container, props, items); + var visualConsole = new VisualConsole(container, props, items); console.log(visualConsole); } catch (error) { console.log("ERROR", error.message); diff --git a/pandora_console/tests/Functional/Models/VisualConsole/Items/DonutGraphTest.php b/pandora_console/tests/Functional/Models/VisualConsole/Items/DonutGraphTest.php new file mode 100644 index 0000000000..94f6997eea --- /dev/null +++ b/pandora_console/tests/Functional/Models/VisualConsole/Items/DonutGraphTest.php @@ -0,0 +1,103 @@ +assertInstanceOf( + DonutGraph::class, + DonutGraph::fromArray( + [ + 'id' => 3, + 'type' => DONUT_GRAPH, + 'width' => '600', + 'height' => '500', + 'color' => '#33CCFF', + 'html' => '

Foo

', + ] + ) + ); + + $this->assertInstanceOf( + DonutGraph::class, + DonutGraph::fromArray( + [ + 'id' => 14, + 'type' => DONUT_GRAPH, + 'width' => '600', + 'height' => '500', + 'border_color' => '#000000', + 'encodedHtml' => 'PGgxPkZvbzwvaDE+', + ] + ) + ); + } + + + /** + * Test if the model has a valid JSON representation. + * + * @return void + */ + public function testContainerIsRepresentedAsJson(): void + { + $this->assertEquals( + '{"aclGroupId":null,"agentId":null,"agentName":null,"color":"#33CCFF","encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"type":17,"width":0,"x":-666,"y":76}', + (string) DonutGraph::fromArray( + [ + 'id' => 7, + 'type' => DONUT_GRAPH, + 'label' => null, + 'labelPosition' => 'up', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'border_color' => '#33CCFF', + 'html' => '

Foo

', + ] + ) + ); + + $this->assertEquals( + '{"aclGroupId":null,"agentId":null,"agentName":null,"color":"#000000","encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"left","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"type":17,"width":0,"x":-666,"y":76}', + (string) DonutGraph::fromArray( + [ + 'id' => 7, + 'type' => DONUT_GRAPH, + 'label' => null, + 'labelPosition' => 'left', + 'isLinkEnabled' => true, + 'isOnTop' => false, + 'parentId' => null, + 'width' => '0', + 'height' => '0', + 'x' => -666, + 'y' => 76, + 'color' => '#000000', + 'encodedHtml' => 'PGgxPkZvbzwvaDE+', + ] + ) + ); + } + + +} diff --git a/pandora_console/tests/Functional/Models/VisualConsole/Items/PercentileTest.php b/pandora_console/tests/Functional/Models/VisualConsole/Items/PercentileTest.php index d9946bc19a..ebe80f6b8c 100644 --- a/pandora_console/tests/Functional/Models/VisualConsole/Items/PercentileTest.php +++ b/pandora_console/tests/Functional/Models/VisualConsole/Items/PercentileTest.php @@ -59,7 +59,7 @@ class PercentileTest extends TestCase public function testContainerIsRepresentedAsJson(): void { $this->assertEquals( - '{"aclGroupId":null,"agentId":null,"agentName":null,"color":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"percentileType":"progress-bar","type":3,"value":null,"valueType":"percent","width":0,"x":-666,"y":76}', + '{"aclGroupId":null,"agentId":null,"agentName":null,"color":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","maxValue":0,"minValue":null,"moduleId":null,"moduleName":null,"parentId":null,"percentileType":"progress-bar","type":3,"unit":null,"value":null,"valueType":"percent","width":0,"x":-666,"y":76}', (string) Percentile::fromArray( [ 'id' => 7, @@ -79,7 +79,7 @@ class PercentileTest extends TestCase ); $this->assertEquals( - '{"aclGroupId":null,"agentId":null,"agentName":null,"color":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"percentileType":"bubble","type":3,"value":null,"valueType":"percent","width":0,"x":-666,"y":76}', + '{"aclGroupId":null,"agentId":null,"agentName":null,"color":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","maxValue":0,"minValue":null,"moduleId":null,"moduleName":null,"parentId":null,"percentileType":"bubble","type":3,"unit":null,"value":null,"valueType":"percent","width":0,"x":-666,"y":76}', (string) Percentile::fromArray( [ 'id' => 7, @@ -99,7 +99,7 @@ class PercentileTest extends TestCase ); $this->assertEquals( - '{"aclGroupId":null,"agentId":null,"agentName":null,"color":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"percentileType":"circular-progress-bar","type":3,"value":"1","valueType":"value","width":0,"x":-666,"y":76}', + '{"aclGroupId":null,"agentId":null,"agentName":null,"color":null,"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","maxValue":0,"minValue":null,"moduleId":null,"moduleName":null,"parentId":null,"percentileType":"circular-progress-bar","type":3,"unit":null,"value":1,"valueType":"value","width":0,"x":-666,"y":76}', (string) Percentile::fromArray( [ 'id' => 7, @@ -121,7 +121,7 @@ class PercentileTest extends TestCase ); $this->assertEquals( - '{"aclGroupId":null,"agentId":null,"agentName":null,"color":"#FFF","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":"#000","labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"percentileType":"circular-progress-bar","type":3,"value":"80","valueType":"percent","width":0,"x":-666,"y":76}', + '{"aclGroupId":null,"agentId":null,"agentName":null,"color":"#FFF","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":"#000","labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","maxValue":0,"minValue":null,"moduleId":null,"moduleName":null,"parentId":null,"percentileType":"circular-progress-bar","type":3,"unit":null,"value":80,"valueType":"percent","width":0,"x":-666,"y":76}', (string) Percentile::fromArray( [ 'id' => 7, diff --git a/visual_console_client/src/VisualConsole.ts b/visual_console_client/src/VisualConsole.ts index a75e401c28..806dbb1224 100644 --- a/visual_console_client/src/VisualConsole.ts +++ b/visual_console_client/src/VisualConsole.ts @@ -76,9 +76,6 @@ export function visualConsolePropsDecoder( // TODO: Document. // eslint-disable-next-line @typescript-eslint/explicit-function-return-type function itemInstanceFrom(data: UnknownObject) { - if (typeof data === "string") { - data = JSON.parse(data); - } const type = parseIntOr(data.type, null); if (type == null) throw new TypeError("missing item type.");