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; namespace Models\VisualConsole\Items;
use Models\VisualConsole\Item; use Models\VisualConsole\Item;
use Models\Model;
/** /**
* Model of a simple value item of the Visual Console. * Model of a simple value item of the Visual Console.
@ -74,41 +73,32 @@ final class SimpleValue extends Item
{ {
$return = parent::decode($data); $return = parent::decode($data);
$return['type'] = SIMPLE_VALUE; $return['type'] = SIMPLE_VALUE;
$return['processValue'] = $this->extractProcessValue($data); $return['processValue'] = static::extractProcessValue($data);
if ($return['processValue'] !== 'none') { 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['value'] = $data['value'];
return $return; return $return;
} }
/** /**
* Extract the value of processValue and * Extract a process value.
* return 'avg', 'max', 'min' or 'none'.
* *
* @param array $data Unknown input data structure. * @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( switch ($data['processValue']) {
Model::issetInArray($data, ['processValue']), case 'none':
null
);
switch ($processValue) {
case 'avg': case 'avg':
return 'avg';
case 'max': case 'max':
return 'max';
case 'min': case 'min':
return 'min'; return $processValue;
default: default:
return 'none'; return 'none';
@ -117,45 +107,32 @@ final class SimpleValue extends Item
/** /**
* Extract the value of period and * Extract the value of period.
* return a integer.
* *
* @param array $data Unknown input data structure. * @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( $period = static::parseIntOr($data['period'], 0);
Model::issetInArray($data, ['period']), return ($period >= 0) ? $period : 0;
0
);
if ($period >= 0) {
return $period;
} else {
return 0;
}
} }
/** /**
* Extract the value of valueType and * Extract a value type.
* return 'image' or 'string'.
* *
* @param array $data Unknown input data structure. * @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( switch ($data['valueType']) {
Model::issetInArray($data, ['valueType']), case 'string':
null
);
switch ($valueType) {
case 'image': case 'image':
return 'image'; return $data['valueType'];
default: default:
return 'string'; 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;
}
} }