Visual Console Refactor: refactores the SimpleValue model and added value retrieval

Former-commit-id: 97ef5c92852b8fc5fd7f71e960f947d22222abcd
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-03 10:28:07 +02:00
parent 8e89f07961
commit b7ed6f9a7b
1 changed files with 70 additions and 43 deletions

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Models\VisualConsole\Items;
use Models\VisualConsole\Item;
use Models\Model;
/**
* Model of a simple value item of the Visual Console.
@ -74,41 +73,32 @@ final class SimpleValue extends Item
{
$return = parent::decode($data);
$return['type'] = SIMPLE_VALUE;
$return['processValue'] = $this->extractProcessValue($data);
$return['processValue'] = static::extractProcessValue($data);
if ($return['processValue'] !== 'none') {
$return['period'] = $this->extractPeriod($data);
$return['period'] = static::extractPeriod($data);
}
$return['valueType'] = $this->extractValueType($data);
$return['valueType'] = static::extractValueType($data);
$return['value'] = $data['value'];
return $return;
}
/**
* Extract the value of processValue and
* return 'avg', 'max', 'min' or 'none'.
* Extract a process value.
*
* @param array $data Unknown input data structure.
*
* @return string
* @return string One of 'none', 'avg', 'max' or 'min'. 'none' by default.
*/
private function extractProcessValue(array $data): string
private static function extractProcessValue(array $data): string
{
$processValue = Model::notEmptyStringOr(
Model::issetInArray($data, ['processValue']),
null
);
switch ($processValue) {
switch ($data['processValue']) {
case 'none':
case 'avg':
return 'avg';
case 'max':
return 'max';
case 'min':
return 'min';
return $processValue;
default:
return 'none';
@ -117,45 +107,32 @@ final class SimpleValue extends Item
/**
* Extract the value of period and
* return a integer.
* Extract the value of period.
*
* @param array $data Unknown input data structure.
*
* @return integer
* @return integer The period in seconds. 0 is the minimum value.
*/
private function extractPeriod(array $data): int
private static function extractPeriod(array $data): int
{
$period = Model::parseIntOr(
Model::issetInArray($data, ['period']),
0
);
if ($period >= 0) {
return $period;
} else {
return 0;
}
$period = static::parseIntOr($data['period'], 0);
return ($period >= 0) ? $period : 0;
}
/**
* Extract the value of valueType and
* return 'image' or 'string'.
* Extract a value type.
*
* @param array $data Unknown input data structure.
*
* @return string
* @return string One of 'string' or 'image'. 'string' by default.
*/
private function extractValueType(array $data): string
private static function extractValueType(array $data): string
{
$valueType = Model::notEmptyStringOr(
Model::issetInArray($data, ['valueType']),
null
);
switch ($valueType) {
switch ($data['valueType']) {
case 'string':
case 'image':
return 'image';
return $data['valueType'];
default:
return 'string';
@ -163,4 +140,54 @@ final class SimpleValue extends Item
}
/**
* 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 a module 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);
$moduleId = static::parseIntOr($linkedModule['moduleId'], null);
if ($moduleId === null) {
throw new \InvalidArgumentException('missing module Id');
}
// Get the formatted value.
$value = \visual_map_get_simple_value(
$data['type'],
$moduleId,
static::extractPeriod($data)
);
// Some modules are image based. Extract the base64 image if needed.
$matches = [];
if (\preg_match('/src=\"(data:image.*)"/', $value, $matches) === 1) {
$data['valueType'] = 'image';
$data['value'] = $matches[1];
}
return $data;
}
}