Visual Console Refactor: refactored the donut graph model

Former-commit-id: a0dd9abb1473c2f74956509b21e879aea44ce1e2
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-08 17:02:51 +02:00
parent 0f2b840a8f
commit 86c5ead4e9
2 changed files with 75 additions and 54 deletions

View File

@ -48,7 +48,7 @@ final class DonutGraph extends Item
{
$return = parent::decode($data);
$return['type'] = DONUT_GRAPH;
$return['color'] = $this->extractBorderColor($data);
$return['legendBackgroundColor'] = $this->extractLegendBackgroundColor($data);
return $return;
}
@ -60,10 +60,16 @@ final class DonutGraph extends Item
*
* @return mixed String representing the border color (not empty) or null.
*/
private function extractBorderColor(array $data)
private function extractLegendBackgroundColor(array $data)
{
return static::notEmptyStringOr(
static::issetInArray($data, ['color', 'border_color']),
static::issetInArray(
$data,
[
'legendBackgroundColor',
'border_color',
]
),
null
);
}
@ -93,58 +99,73 @@ final class DonutGraph extends Item
global $config;
include_once $config['homedir'].'/include/functions_graph.php';
// Extract needed properties.
$legendBackGroundColor = static::extractLegendBackgroundColor($data);
// Get the linked agent and module Ids.
$linkedModule = static::extractLinkedModule($data);
$agentId = static::parseIntOr($linkedModule['agentId'], null);
$moduleId = static::parseIntOr($linkedModule['moduleId'], null);
$agentId = $linkedModule['agentId'];
$moduleId = $linkedModule['moduleId'];
$metaconsoleId = $linkedModule['metaconsoleId'];
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) {
if ($moduleId === null) {
throw new \InvalidArgumentException('missing module Id');
}
// Maybe connect to node.
$nodeConnected = false;
if (\is_metaconsole() === true && $metaconsoleId !== null) {
$nodeConnected = \metaconsole_connect(
null,
$metaconsoleId
) === NOERR;
if ($nodeConnected === false) {
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,
]
$sql = sprintf(
'SELECT COUNT(tam.id_agente_modulo)
FROM tagente_modulo tam
INNER JOIN ttipo_modulo ttm
ON tam.id_tipo_modulo = ttm.id_tipo
WHERE tam.id_agente = %d
AND tam.id_agente_modulo = %d
AND ttm.nombre LIKE \'%%_string\'',
$agentId,
$moduleId
);
$isString = (bool) \db_get_value_sql($sql);
if (!empty($data['id_metaconsole'])) {
metaconsole_restore_db();
// Restore connection.
if ($nodeConnected === true) {
\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);
if ($isString === true) {
$graphData = \get_donut_module_data($moduleId);
$img = d3_donut_graph(
$data['id'],
$data['width'],
$data['width'],
$donut_data,
$data['border_color']
$data['html'] = \d3_donut_graph(
(int) $data['id'],
(int) $data['width'],
(int) $data['width'],
$graphData,
$legendBackGroundColor
);
} 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">';
$src = 'images/console/signes/wrong_donut_graph.png';
if (\is_metaconsole() === true && $metaconsoleId !== null) {
$src = '../../'.$src;
}
}
$data['html'] = $img;
$data['html'] = '<img src="'.$src.'">';
}
return $data;
}

View File

@ -23,12 +23,12 @@ class DonutGraphTest extends TestCase
DonutGraph::class,
DonutGraph::fromArray(
[
'id' => 3,
'type' => DONUT_GRAPH,
'width' => '600',
'height' => '500',
'color' => '#33CCFF',
'html' => '<h1>Foo</h1>',
'id' => 3,
'type' => DONUT_GRAPH,
'width' => '600',
'height' => '500',
'legendBackgroundColor' => '#33CCFF',
'html' => '<h1>Foo</h1>',
]
)
);
@ -57,7 +57,7 @@ class DonutGraphTest extends TestCase
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}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"legendBackgroundColor":"#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,
@ -78,22 +78,22 @@ class DonutGraphTest extends TestCase
);
$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}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"legendBackgroundColor":"#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+',
'id' => 7,
'type' => DONUT_GRAPH,
'label' => null,
'labelPosition' => 'left',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
'legendBackgroundColor' => '#000000',
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
]
)
);