Added cache

This commit is contained in:
Daniel Maya 2019-05-20 15:15:54 +02:00
parent f186cd6201
commit 5c0a197b6b
9 changed files with 249 additions and 15 deletions

View File

@ -2,6 +2,26 @@ START TRANSACTION;
DELETE FROM 'tevent_response' WHERE 'name' LIKE 'Create Integria IMS incident from event';
-- ---------------------------------------------------------------------
-- Table `tvisual_console_items_cache`
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tvisual_console_elements_cache` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`vc_id` INTEGER UNSIGNED NOT NULL,
`vc_item_id` INTEGER UNSIGNED NOT NULL,
`user_id` VARCHAR(60) DEFAULT NULL,
`data` TEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(`id`),
FOREIGN KEY(`vc_id`) REFERENCES `tlayout`(`id`)
ON DELETE CASCADE,
FOREIGN KEY(`vc_item_id`) REFERENCES `tlayout_data`(`id`)
ON DELETE CASCADE,
FOREIGN KEY (`user_id`) REFERENCES `tusuario`(`id_user`)
ON DELETE CASCADE
ON UPDATE CASCADE
) engine=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `tlayout_data` ADD COLUMN `cache_expiration` INTEGER UNSIGNED NOT NULL DEFAULT 0;
COMMIT;

View File

@ -2104,3 +2104,23 @@ ALTER TABLE `tnetflow_filter` DROP COLUMN `output`;
-- Update table `tuser_task`
-- ----------------------------------------------------------------------
UPDATE tuser_task set parameters = 'a:5:{i:0;a:6:{s:11:\"description\";s:28:\"Report pending to be created\";s:5:\"table\";s:7:\"treport\";s:8:\"field_id\";s:9:\"id_report\";s:10:\"field_name\";s:4:\"name\";s:4:\"type\";s:3:\"int\";s:9:\"acl_group\";s:8:\"id_group\";}i:1;a:2:{s:11:\"description\";s:46:\"Send to email addresses (separated by a comma)\";s:4:\"type\";s:4:\"text\";}i:2;a:2:{s:11:\"description\";s:7:\"Subject\";s:8:\"optional\";i:1;}i:3;a:3:{s:11:\"description\";s:7:\"Message\";s:4:\"type\";s:4:\"text\";s:8:\"optional\";i:1;}i:4;a:2:{s:11:\"description\";s:11:\"Report Type\";s:4:\"type\";s:11:\"report_type\";}}' where function_name = "cron_task_generate_report";
-- ---------------------------------------------------------------------
-- Table `tvisual_console_items_cache`
-- ---------------------------------------------------------------------
CREATE TABLE `tvisual_console_elements_cache` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`vc_id` INTEGER UNSIGNED NOT NULL,
`vc_item_id` INTEGER UNSIGNED NOT NULL,
`user_id` VARCHAR(60) DEFAULT NULL,
`data` TEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(`id`),
FOREIGN KEY(`vc_id`) REFERENCES `tlayout`(`id`)
ON DELETE CASCADE,
FOREIGN KEY(`vc_item_id`) REFERENCES `tlayout_data`(`id`)
ON DELETE CASCADE,
FOREIGN KEY (`user_id`) REFERENCES `tusuario`(`id_user`)
ON DELETE CASCADE
ON UPDATE CASCADE
) engine=InnoDB DEFAULT CHARSET=utf8;

View File

@ -2589,6 +2589,9 @@ function hiddenFields(item) {
$("#line_case").css("display", "none");
$("#line_case." + item).css("display", "");
$("#cache_expiration_row").css("display", "none");
$("#cache_expiration_row." + item).css("display", "");
// Color cloud rows
$("#color_cloud_diameter_row").hide();
$("#color_cloud_diameter_row." + item).show();

View File

@ -1141,14 +1141,7 @@ function visual_map_editor_print_item_palette($visualConsole_id, $background)
$form_items_advance['cache_expiration_row'] = [];
$form_items_advance['cache_expiration_row']['items'] = [
'group_item',
'static_graph',
'percentile_bar',
'percentile_item',
'module_graph',
'simple_value',
'label',
'icon',
'datos',
'auto_sla_graph',
'bars_graph',
'donut_graph',

View File

@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace Models;
use Models\Model;
/**
* This class should be extended to add functionalities to
* fetch, clear and save item cache.
*/
abstract class CacheModel extends Model
{
/**
* Obtain a data structure from the database using a filter.
*
* @param array $filter Filter to retrieve the modeled element.
*
* @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @abstract
*/
abstract protected static function fetchCachedData(array $filter);
/**
* Stores the data structure obtained.
*
* @param array $filter Filter to retrieve the modeled element.
*
* @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @abstract
*/
abstract protected static function saveCachedData(array $filter, array $data): bool;
/**
* Deletes previous data that are not useful.
*
* @param array $filter Filter to retrieve the modeled element.
*
* @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @abstract
*/
abstract protected static function clearCachedData(array $filter): int;
/**
* Obtain a model's instance from the database using a filter.
*
* @param array $filter Filter to retrieve the modeled element.
*
* @return self A modeled element's instance.
*
* @overrides Model::fromDB.
*/
public static function fromDB(array $filter): Model
{
global $config;
if ($filter['cache_expiration'] != 0) {
// Obtain the item's data from cache.
$cacheData = static::fetchCachedData($filter);
if ($cacheData === null) {
// Delete expired data cache.
static::clearCachedData(
[
'vc_item_id' => $filter['id'],
'vc_id' => $filter['id_layout'],
'user_id' => $config['id_user'],
]
);
// Obtain the item's data from the database.
$data = static::fetchDataFromDB($filter);
// Save the item's data in cache.
static::saveCachedData(
[
'vc_item_id' => $filter['id'],
'vc_id' => $filter['id_layout'],
'user_id' => $config['id_user'],
],
$data
);
return static::fromArray($data);
} else {
$data = \io_safe_output(\json_decode(\base64_decode($cacheData['data']), true));
return static::fromArray($data);
}
} else {
return static::fromArray(static::fetchDataFromDB($filter));
}
}
}

View File

@ -321,6 +321,8 @@ final class Container extends Model
$fields = [
'id',
'type',
'cache_expiration',
'id_layout',
];
// Override the filter if the groups filter is not empty.
@ -359,11 +361,10 @@ final class Container extends Model
foreach ($rows as $data) {
$itemId = (int) $data['id'];
$itemType = (int) $data['type'];
$class = static::getItemClass($itemType);
$class = static::getItemClass((int) $data['type']);
try {
array_push($items, $class::fromDB(['id' => $itemId]));
array_push($items, $class::fromDB($data));
} catch (\Throwable $e) {
// TODO: Log this?
}

View File

@ -3,12 +3,12 @@
declare(strict_types=1);
namespace Models\VisualConsole;
use Models\Model;
use Models\CacheModel;
/**
* Model of a generic Visual Console Item.
*/
class Item extends Model
class Item extends CacheModel
{
/**
@ -699,7 +699,6 @@ class Item extends Model
}
// Load side libraries.
global $config;
include_once $config['homedir'].'/include/functions_io.php';
// Clean up to two levels of HTML entities.
@ -725,6 +724,82 @@ class Item extends Model
}
/**
* Fetch a cache 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 \Exception When the data cannot be retrieved from the DB.
*
* @override CacheModel::fetchCachedData.
*/
protected static function fetchCachedData(array $filter)
{
global $config;
$row = \db_get_row_sql(
'SELECT * FROM tvisual_console_elements_cache
WHERE vc_item_id = '.$filter['id'].'
AND vc_id = '.$filter['id_layout'].'
AND user_id LIKE "'.$config['id_user'].'"
AND (UNIX_TIMESTAMP(`created_at`) +'.$filter['cache_expiration'].') > UNIX_TIMESTAMP()'
);
if ($row === false) {
return null;
}
// $fecha = \date_create();
// $fecha = \date_timestamp_get($fecha);
$row = \io_safe_output(\io_safe_output($row));
return $row;
}
/**
* Stores the data structure obtained.
*
* @param array $filter Filter to retrieve the modeled element.
*
* @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @override CacheModel::saveCachedData.
*/
protected static function saveCachedData(array $filter, array $data): bool
{
db_process_sql_insert(
'tvisual_console_elements_cache',
[
'vc_id' => $filter['vc_id'],
'vc_item_id' => $filter['vc_item_id'],
'user_id' => $filter['user_id'],
'data' => \base64_encode(\json_encode(\io_safe_input($data))),
'expiration' => $filter['expiration'],
]
);
return false;
}
/**
* Deletes previous data that are not useful.
*
* @param array $filter Filter to retrieve the modeled element.
*
* @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB.
*
* @override CacheModel::clearCachedData.
*/
protected static function clearCachedData(array $filter): int
{
hd($filter);
return \db_process_sql_delete('tvisual_console_elements_cache', $filter);
}
/**
* Fetch a data structure of an agent from the database using the
* vs item's data.

View File

@ -124,7 +124,7 @@ if ($aclWrite || $aclManage) {
).'</a>';
}
$options['view']['text'] = '<a href="index.php?sec=network&sec2=operation/visual_console/render_view&id='.$visualConsoleId.'">'.html_print_image(
$options['view']['text'] = '<a href="index.php?sec=network&sec2=operation/visual_console/render_view&id='.$visualConsoleId.'&refr='.$refr.'">'.html_print_image(
'images/operation.png',
true,
['title' => __('View')]
@ -166,7 +166,7 @@ if ($pure === true) {
// Quit fullscreen.
echo '<li class="nomn">';
$urlNoFull = 'index.php?sec=network&sec2=operation/visual_console/render_view&id='.$visualConsoleId;
$urlNoFull = 'index.php?sec=network&sec2=operation/visual_console/render_view&id='.$visualConsoleId.'&refr='.$refr;
echo '<a class="vc-btn-no-fullscreen" href="'.$urlNoFull.'">';
echo html_print_image('images/normal_screen.png', true, ['title' => __('Back to normal mode')]);
echo '</a>';

View File

@ -3563,3 +3563,23 @@ CREATE TABLE `tuser_task_scheduled` (
`id_grupo` int(10) unsigned NOT NULL default 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ---------------------------------------------------------------------
-- Table `tvisual_console_items_cache`
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tvisual_console_elements_cache` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`vc_id` INTEGER UNSIGNED NOT NULL,
`vc_item_id` INTEGER UNSIGNED NOT NULL,
`user_id` VARCHAR(60) DEFAULT NULL,
`data` TEXT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(`id`),
FOREIGN KEY(`vc_id`) REFERENCES `tlayout`(`id`)
ON DELETE CASCADE,
FOREIGN KEY(`vc_item_id`) REFERENCES `tlayout_data`(`id`)
ON DELETE CASCADE,
FOREIGN KEY (`user_id`) REFERENCES `tusuario`(`id_user`)
ON DELETE CASCADE
ON UPDATE CASCADE
) engine=InnoDB DEFAULT CHARSET=utf8;