Visual Console Refactor: added the module and custom graph model

Former-commit-id: 6756f9f4b9fb9803b7a91acb8e388d19d39a0b6f
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-05 14:16:50 +02:00
parent 55b9c07d63
commit 992209f5bb
1 changed files with 267 additions and 0 deletions

View File

@ -0,0 +1,267 @@
<?php
declare(strict_types=1);
namespace Models\VisualConsole\Items;
use Models\VisualConsole\Item;
/**
* Model of a module graph item of the Visual Console.
*/
final class ModuleGraph extends Item
{
/**
* Used to enable the fetching, validation and extraction of information
* about the linked module.
*
* @var boolean
*/
protected static $useLinkedModule = true;
/**
* Used to enable the fetching, validation and extraction of information
* about the linked visual console.
*
* @var boolean
*/
protected static $useLinkedVisualConsole = 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'] = MODULE_GRAPH;
$return['backgroundType'] = static::extractBackgroundType($data);
$return['period'] = static::extractPeriod($data);
$customGraphId = static::extractCustomGraphId($data);
if (empty($customGraphId) === false) {
$return['customGraphId'] = $customGraphId;
} else {
$return['graphType'] = static::extractGraphType($data);
}
return $return;
}
/**
* Extract a background type value.
*
* @param array $data Unknown input data structure.
*
* @return string 'none', 'white' or 'black'. 'none' by default.
*/
private static function extractBackgroundType(array $data): string
{
$value = static::issetInArray($data, ['backgroundType', 'image']);
switch ($value) {
case 'none':
case 'white':
case 'black':
return $value;
default:
return 'none';
}
}
/**
* Extract a graph period value.
*
* @param array $data Unknown input data structure.
*
* @return mixed The time in seconds of the graph period or null.
*/
private static function extractPeriod(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['period']),
null
);
}
/**
* Extract a custom graph Id value.
*
* @param array $data Unknown input data structure.
*
* @return mixed The custom graph Id (int) or null.
*/
private static function extractCustomGraphId(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['customGraphId', 'id_custom_graph']),
null
);
}
/**
* Extract a graph type value.
*
* @param array $data Unknown input data structure.
*
* @return string 'line' or 'area'. 'line' by default.
*/
private static function extractGraphType(array $data): string
{
$value = static::issetInArray($data, ['graphType', 'type_graph']);
switch ($value) {
case 'line':
case 'area':
return $value;
default:
return 'line';
}
}
/**
* 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';
include_once $config['homedir'].'/include/functions_modules.php';
$imageOnly = false;
$backgroundType = static::extractBackgroundType($data);
$period = static::extractPeriod($data);
$customGraphId = static::extractCustomGraphId($data);
$graphType = static::extractGraphType($data);
$linkedModule = static::extractLinkedModule($data);
$moduleId = $linkedModule['moduleId'];
$metaconsoleId = $linkedModule['metaconsoleId'];
// 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'
);
}
}
// Custom graph.
if (empty($customGraphId) === false) {
// TODO: use a custom size.
$width = 180;
$height = 480;
$customGraph = \db_get_row_filter(
'tgraph',
'id_graph',
$customGraphId
);
$params = [
'period' => $period,
'width' => $width,
'height' => $height,
'title' => '',
'unit_name' => null,
'show_alerts' => false,
'only_image' => $imageOnly,
'vconsole' => true,
'backgroundColor' => $backgroundType,
];
$paramsCombined = [
'id_graph' => $customGraphId,
'stacked' => $graph['stacked'],
'summatory' => $graph['summatory_series'],
'average' => $graph['average_series'],
'modules_series' => $graph['modules_series'],
];
$data['html'] = \graphic_combined_module(
false,
$params,
$paramsCombined
);
} else {
// Module graph.
if ($moduleId === null) {
throw new \InvalidArgumentException('missing module Id');
}
// TODO: use a custom size.
$width = 300;
$height = 180;
$params = [
'agent_module_id' => $moduleId,
'period' => $period,
'show_events' => false,
'width' => $width,
'height' => $height,
'title' => \modules_get_agentmodule_name($moduleId),
'unit' => \modules_get_unit($moduleId),
'only_image' => $imageOnly,
'menu' => false,
'backgroundColor' => $backgroundType,
'type_graph' => $graphType,
'vconsole' => true,
];
$data['html'] = \grafico_modulo_sparse($params);
}
// Restore connection.
if ($nodeConnected === true) {
\metaconsole_restore_db();
}
return $data;
}
}