Added DonutGraph
Former-commit-id: 09b54770531c96a6f5c504f06caaefc10463bba9
This commit is contained in:
parent
b3bcc888df
commit
cb2be4cd6f
|
@ -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.
|
||||
|
|
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Models\VisualConsole\Items;
|
||||
use Models\VisualConsole\Item;
|
||||
|
||||
/**
|
||||
* Model of a Donut Graph item of the Visual Console.
|
||||
*/
|
||||
final class DonutGraph extends Item
|
||||
{
|
||||
|
||||
/**
|
||||
* Used to enable the fetching, validation and extraction of information
|
||||
* about the linked visual console.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected static $useLinkedVisualConsole = true;
|
||||
|
||||
/**
|
||||
* Used to enable the fetching, validation and extraction of information
|
||||
* about the linked module.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected static $useLinkedModule = true;
|
||||
|
||||
/**
|
||||
* Used to enable validation, extraction and encodeing of the HTML output.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected static $useHtmlOutput = true;
|
||||
|
||||
|
||||
/**
|
||||
* Returns a valid representation of the model.
|
||||
*
|
||||
* @param array $data Input data.
|
||||
*
|
||||
* @return array Data structure representing the model.
|
||||
*
|
||||
* @overrides Item::decode.
|
||||
*/
|
||||
protected function decode(array $data): array
|
||||
{
|
||||
$return = parent::decode($data);
|
||||
$return['type'] = DONUT_GRAPH;
|
||||
$return['color'] = $this->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 = '<img src="../../images/console/signes/wrong_donut_graph.png">';
|
||||
} else {
|
||||
$img = '<img src="images/console/signes/wrong_donut_graph.png">';
|
||||
}
|
||||
}
|
||||
|
||||
$data['html'] = $img;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Models\VisualConsole\Items\DonutGraph;
|
||||
|
||||
/**
|
||||
* Test for the Visual Console Donut Graph Item model.
|
||||
*/
|
||||
class DonutGraphTest extends TestCase
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Test if the instance is created using a valid data structure.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCanBeCreatedFromValidUserStructure(): void
|
||||
{
|
||||
$this->assertInstanceOf(
|
||||
DonutGraph::class,
|
||||
DonutGraph::fromArray(
|
||||
[
|
||||
'id' => 3,
|
||||
'type' => DONUT_GRAPH,
|
||||
'width' => '600',
|
||||
'height' => '500',
|
||||
'color' => '#33CCFF',
|
||||
'html' => '<h1>Foo</h1>',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$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' => '<h1>Foo</h1>',
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$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+',
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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,
|
||||
|
|
|
@ -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.");
|
||||
|
||||
|
|
Loading…
Reference in New Issue