Visual Console Refactor: fixed the events history model

Former-commit-id: 9228339af7213996fb969ee631a93a3e972e102a
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-03 11:40:40 +02:00
parent eb6757e91c
commit ca4997fd7f
2 changed files with 72 additions and 34 deletions

View File

@ -28,6 +28,33 @@ final class EventsHistory extends Item
protected static $useLinkedVisualConsole = 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.
*
@ -54,7 +81,7 @@ final class EventsHistory extends Item
*
* @return mixed The time in seconds of the graph period or null.
*/
private function extractMaxTime(array $data)
private static function extractMaxTime(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['maxTime', 'period']),
@ -69,15 +96,36 @@ final class EventsHistory extends Item
* @param array $data Unknown input data structure.
*
* @return string The HTML representation in base64 encoding.
* @throws \InvalidArgumentException When an agent Id cannot be found.
*/
private function extractEncodedHtml(array $data): string
private static function extractEncodedHtml(array $data): string
{
if (isset($data['encodedHtml']) === true) {
return $data['encodedHtml'];
} else if (isset($data['html']) === true) {
return \base64_encode($data['html']);
}
}
/**
* 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;
@ -99,13 +147,15 @@ final class EventsHistory extends Item
$moduleId,
(int) $data['width'],
(int) $data['height'],
$this->extractMaxTime($data),
static::extractMaxTime($data),
'',
true
);
$html .= '</div>';
return \base64_encode($html);
$data['encodedHtml'] = \base64_encode($html);
return $data;
}

View File

@ -28,7 +28,7 @@ class EventsHistoryTest extends TestCase
'width' => '600',
'height' => '500',
'maxTime' => null,
'data' => [],
'html' => '<h1>Foo</h1>',
]
)
);
@ -37,15 +37,12 @@ class EventsHistoryTest extends TestCase
EventsHistory::class,
EventsHistory::fromArray(
[
'id' => 14,
'type' => AUTO_SLA_GRAPH,
'width' => '600',
'height' => '500',
'maxTime' => 12800,
'data' => [
'data' => '5',
'time' => 23456788,
],
'id' => 14,
'type' => AUTO_SLA_GRAPH,
'width' => '600',
'height' => '500',
'maxTime' => 12800,
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
]
)
);
@ -60,7 +57,7 @@ class EventsHistoryTest extends TestCase
public function testContainerIsRepresentedAsJson(): void
{
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"data":[],"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","maxTime":null,"moduleId":null,"moduleName":null,"parentId":null,"type":14,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","maxTime":null,"moduleId":null,"moduleName":null,"parentId":null,"type":14,"width":0,"x":-666,"y":76}',
(string) EventsHistory::fromArray(
[
'id' => 7,
@ -75,13 +72,13 @@ class EventsHistoryTest extends TestCase
'x' => -666,
'y' => 76,
'maxTime' => null,
'data' => [],
'html' => '<h1>Foo</h1>',
]
)
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"data":{"time":23456789,"data":15},"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","maxTime":12800,"moduleId":null,"moduleName":null,"parentId":null,"type":14,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","maxTime":12800,"moduleId":null,"moduleName":null,"parentId":null,"type":14,"width":0,"x":-666,"y":76}',
(string) EventsHistory::fromArray(
[
'id' => 7,
@ -96,16 +93,13 @@ class EventsHistoryTest extends TestCase
'x' => -666,
'y' => 76,
'maxTime' => 12800,
'data' => [
'time' => 23456789,
'data' => 15,
],
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
]
)
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"data":[],"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":1,"linkedLayoutStatusType":"default","maxTime":null,"moduleId":null,"moduleName":null,"parentId":null,"type":14,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":1,"linkedLayoutStatusType":"default","maxTime":null,"moduleId":null,"moduleName":null,"parentId":null,"type":14,"width":0,"x":-666,"y":76}',
(string) EventsHistory::fromArray(
[
'id' => 7,
@ -120,14 +114,14 @@ class EventsHistoryTest extends TestCase
'x' => -666,
'y' => 76,
'maxTime' => null,
'data' => [],
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
'id_layout_linked' => 1,
]
)
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"data":{"time":23456789,"data":15},"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":3,"linkedLayoutId":2,"linkedLayoutStatusType":"default","maxTime":12800,"metaconsoleId":5,"moduleId":null,"moduleName":null,"parentId":null,"type":14,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":3,"linkedLayoutId":2,"linkedLayoutStatusType":"default","maxTime":12800,"metaconsoleId":5,"moduleId":null,"moduleName":null,"parentId":null,"type":14,"width":0,"x":-666,"y":76}',
(string) EventsHistory::fromArray(
[
'id' => 7,
@ -142,10 +136,7 @@ class EventsHistoryTest extends TestCase
'x' => -666,
'y' => 76,
'maxTime' => 12800,
'data' => [
'time' => 23456789,
'data' => 15,
],
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
'id_metaconsole' => 5,
'linked_layout_node_id' => 3,
'linkedLayoutId' => 2,
@ -154,7 +145,7 @@ class EventsHistoryTest extends TestCase
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":21,"agentName":null,"data":{"time":23456789,"data":15},"height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":15,"linkedLayoutId":3,"linkedLayoutStatusType":"default","maxTime":12800,"metaconsoleId":2,"moduleId":385,"moduleName":"module_test","parentId":null,"type":14,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":21,"agentName":null,"encodedHtml":"PGgxPkZvbzwvaDE+","height":0,"id":7,"isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":15,"linkedLayoutId":3,"linkedLayoutStatusType":"default","maxTime":12800,"metaconsoleId":2,"moduleId":385,"moduleName":"module_test","parentId":null,"type":14,"width":0,"x":-666,"y":76}',
(string) EventsHistory::fromArray(
[
'id' => 7,
@ -169,10 +160,7 @@ class EventsHistoryTest extends TestCase
'x' => -666,
'y' => 76,
'maxTime' => 12800,
'data' => [
'time' => 23456789,
'data' => 15,
],
'encodedHtml' => 'PGgxPkZvbzwvaDE+',
'id_metaconsole' => 2,
'linked_layout_node_id' => 15,
'linkedLayoutId' => 3,