Visual Console Refactor: WIP percentile item

Former-commit-id: 1d852fb5dff28411aeda60431d8032e77159ea5d
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-03 12:12:05 +02:00
parent 32ba3aa1d4
commit bd90bbd922
2 changed files with 378 additions and 0 deletions

View File

@ -0,0 +1,223 @@
<?php
declare(strict_types=1);
namespace Models\VisualConsole\Items;
use Models\VisualConsole\Item;
/**
* Model of a percentile item of the Visual Console.
*/
final class Percentile 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;
/**
* Validate the received data structure to ensure if we can extract the
* values required to build the model.
*
* @param array $data Input data.
*
* @return void
*
* @throws \InvalidArgumentException If any input value is considered
* invalid.
*
* @overrides Item::validateData.
*/
protected function validateData(array $data): void
{
parent::validateData($data);
if (static::notEmptyStringOr($data['encodedHtml'], null) === null
&& static::notEmptyStringOr($data['html'], null) === null
) {
throw new \InvalidArgumentException(
'the html property is required and should be string'
);
}
}
/**
* 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'] = PERCENTILE_BAR;
$return['percentileType'] = static::extractPercentileType($data);
$return['valueType'] = static::extractValueType($data);
$return['value'] = static::notEmptyStringOr($data['value'], null);
$return['color'] = static::extractColor($data);
$return['labelColor'] = static::extractLabelColor($data);
return $return;
}
/**
* Extract a percentile type value.
*
* @param array $data Unknown input data structure.
*
* @return string 'progress-bar', 'bubble', 'circular-progress-bar'
* or 'circular-progress-bar-alt'. 'progress-bar' by default.
*/
private static function extractPercentileType(array $data): string
{
if (isset($data['percentileType']) === true) {
switch ($data['percentileType']) {
case 'progress-bar':
case 'bubble':
case 'circular-progress-bar':
case 'circular-progress-bar-alt':
return $data['percentileType'];
default:
return 'progress-bar';
}
}
switch ($data['type']) {
case PERCENTILE_BUBBLE:
return 'bubble';
case CIRCULAR_PROGRESS_BAR:
return 'circular-progress-bar';
case CIRCULAR_INTERIOR_PROGRESS_BAR:
return 'circular-progress-bar-alt';
default:
case PERCENTILE_BAR:
return 'progress-bar';
}
}
/**
* Extract a value type value.
*
* @param array $data Unknown input data structure.
*
* @return string 'percent' or 'value'. 'percent' by default.
*/
private static function extractValueType(array $data): string
{
$rawValueType = static::issetInArray($data, ['valueType', 'image']);
switch ($rawValueType) {
case 'percent':
case 'value':
return $rawValueType;
default:
return 'percent';
}
}
/**
* Extract a color value.
*
* @param array $data Unknown input data structure.
*
* @return mixed The color or null.
*/
private static function extractColor(array $data)
{
return static::notEmptyStringOr(
static::issetInArray($data, ['color', 'border_color']),
null
);
}
/**
* Extract a label color value.
*
* @param array $data Unknown input data structure.
*
* @return mixed The label color or null.
*/
private static function extractLabelColor(array $data)
{
return static::notEmptyStringOr(
static::issetInArray($data, ['labelColor', 'fill_color']),
null
);
}
/**
* 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';
// Get the linked agent and module Ids.
$linkedModule = static::extractLinkedModule($data);
$agentId = static::parseIntOr($linkedModule['agentId'], null);
$moduleId = static::parseIntOr($linkedModule['moduleId'], null);
if ($agentId === null) {
throw new \InvalidArgumentException('missing agent Id');
}
// TODO: Use the same HTML output as the old VC.
$html = '';
$data['html'] = $html;
return $data;
}
}

View File

@ -0,0 +1,155 @@
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use Models\VisualConsole\Items\Percentile;
/**
* Test for the Visual Console Percentile Item model.
*/
class PercentileTest extends TestCase
{
/**
* Test if the instance is created using a valid data structure.
*
* @return void
*/
public function testCanBeCreatedFromValidUserStructure(): void
{
$this->assertInstanceOf(
Percentile::class,
Percentile::fromArray(
[
'id' => 3,
'type' => PERCENTILE_BAR,
'width' => '600',
'height' => '500',
'maxTime' => null,
'valueType' => 'value',
'value' => '123ms',
'html' => '<h1>Foo</h1>',
]
)
);
$this->assertInstanceOf(
Percentile::class,
Percentile::fromArray(
[
'id' => 14,
'type' => PERCENTILE_BUBBLE,
'width' => '600',
'height' => '500',
'maxTime' => 12800,
'valueType' => 'image',
'value' => 'data:image;asdasoih==',
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
]
)
);
}
/**
* Test if the model has a valid JSON representation.
*
* @return void
*/
public function testContainerIsRepresentedAsJson(): void
{
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"color":null,"encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"percentileType":"progress-bar","type":3,"value":null,"valueType":"percent","width":0,"x":-666,"y":76}',
(string) Percentile::fromArray(
[
'id' => 7,
'type' => PERCENTILE_BAR,
'label' => null,
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
'maxTime' => null,
'html' => '<h1>Foo</h1>',
]
)
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"color":null,"encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"percentileType":"bubble","type":3,"value":null,"valueType":"percent","width":0,"x":-666,"y":76}',
(string) Percentile::fromArray(
[
'id' => 7,
'type' => PERCENTILE_BUBBLE,
'label' => null,
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
'maxTime' => 12800,
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
]
)
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"color":null,"encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"percentileType":"circular-progress-bar","type":3,"value":"1","valueType":"value","width":0,"x":-666,"y":76}',
(string) Percentile::fromArray(
[
'id' => 7,
'type' => CIRCULAR_PROGRESS_BAR,
'label' => null,
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
'maxTime' => 12800,
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
'valueType' => 'value',
'value' => '1',
]
)
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"color":"#FFF","encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelColor":"#000","labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"percentileType":"circular-progress-bar","type":3,"value":"80","valueType":"percent","width":0,"x":-666,"y":76}',
(string) Percentile::fromArray(
[
'id' => 7,
'type' => CIRCULAR_PROGRESS_BAR,
'label' => null,
'labelPosition' => 'up',
'isLinkEnabled' => true,
'isOnTop' => false,
'parentId' => null,
'width' => '0',
'height' => '0',
'x' => -666,
'y' => 76,
'maxTime' => 12800,
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
'valueType' => 'percent',
'value' => '80',
'color' => '#FFF',
'labelColor' => '#000',
]
)
);
}
}