Visual Console Refactor: refactored the events history item to return its HTML representation

Former-commit-id: 8b29503a24b1877c645a3b9856d09ee2db947f99
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-02 13:00:49 +02:00
parent 6c53833484
commit 62792c078f
1 changed files with 49 additions and 25 deletions

View File

@ -4,22 +4,13 @@ declare(strict_types=1);
namespace Models\VisualConsole\Items; namespace Models\VisualConsole\Items;
use Models\VisualConsole\Item; use Models\VisualConsole\Item;
use Models\Model;
/** /**
* Model of a group item of the Visual Console. * Model of a events history item of the Visual Console.
*/ */
final class EventsHistory extends Item final class EventsHistory 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 * Used to enable the fetching, validation and extraction of information
* about the linked module. * about the linked module.
@ -28,6 +19,14 @@ final class EventsHistory extends Item
*/ */
protected static $useLinkedModule = true; 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;
/** /**
* Returns a valid representation of the model. * Returns a valid representation of the model.
@ -43,45 +42,70 @@ final class EventsHistory extends Item
$return = parent::decode($data); $return = parent::decode($data);
$return['type'] = AUTO_SLA_GRAPH; $return['type'] = AUTO_SLA_GRAPH;
$return['maxTime'] = $this->extractMaxTime($data); $return['maxTime'] = $this->extractMaxTime($data);
$return['data'] = $this->extractData($data); $return['encodedHtml'] = $this->extractEncodedHtml($data);
return $return; return $return;
} }
/** /**
* Extract the value of maxTime and * Extract a graph period value.
* return a integer or null.
* *
* @param array $data Unknown input data structure. * @param array $data Unknown input data structure.
* *
* @return mixed * @return mixed The time in seconds of the graph period or null.
*/ */
private function extractMaxTime(array $data) private function extractMaxTime(array $data)
{ {
$maxTime = Model::parseIntOr( return static::parseIntOr(
Model::issetInArray($data, ['maxTime', 'period']), static::issetInArray($data, ['maxTime', 'period']),
null null
); );
return $maxTime;
} }
/** /**
* Extract the value of data and * Extract a encoded HTML representation of the item.
* return an array.
* *
* @param array $data Unknown input data structure. * @param array $data Unknown input data structure.
* *
* @return array * @return string The HTML representation in base64 encoding.
* @throws \InvalidArgumentException When an agent Id cannot be found.
*/ */
private function extractData(array $data): array private function extractEncodedHtml(array $data): string
{ {
$array = []; if (isset($data['encodedHtml']) === true) {
if (isset($data['data']) && \is_array($data['data'])) { return $data['encodedHtml'];
$array = $data['data']; } else if (isset($data['html']) === true) {
return \base64_encode($data['html']);
} }
return $array; // Load side libraries.
global $config;
include_once $config['homedir'].'/include/functions_graph.php';
// Get the linked agent and module Ids.
$linkedModule = $this->extractLinkedModule($data);
$agentId = static::parseIntOr($linkedModule['agentId'], null);
$moduleId = static::parseIntOr($linkedModule['moduleId'], null);
if ($agentId === null) {
throw new \InvalidArgumentException('missing agent Id');
}
// Use the same HTML output as the old VC.
$html = '<div style="width:500px;">';
$html .= \graph_graphic_moduleevents(
$agentId,
$moduleId,
(int) $data['width'],
(int) $data['height'],
$this->extractMaxTime($data),
'',
true
);
$html .= '</div>';
return \base64_encode($html);
} }