From a55e431b462b3b994d87109aa7d0bc272b907f12 Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Thu, 20 Feb 2020 13:25:56 +0100 Subject: [PATCH] Fixed error cache build link VC --- .../javascript/pandora_visual_console.js | 15 ++++++ .../include/rest-api/models/CachedModel.php | 47 +++++++------------ .../models/VisualConsole/Container.php | 2 +- .../rest-api/models/VisualConsole/Item.php | 26 ++++++++-- 4 files changed, 56 insertions(+), 34 deletions(-) diff --git a/pandora_console/include/javascript/pandora_visual_console.js b/pandora_console/include/javascript/pandora_visual_console.js index 5be03ef756..0e0a21659b 100755 --- a/pandora_console/include/javascript/pandora_visual_console.js +++ b/pandora_console/include/javascript/pandora_visual_console.js @@ -45,6 +45,21 @@ function createVisualConsole( visualConsoleId, function(error, data) { if (error) { + //Remove spinner change VC. + document + .getElementById("visual-console-container") + .classList.remove("is-updating"); + + var div = document + .getElementById("visual-console-container") + .querySelector(".div-visual-console-spinner"); + + if (div !== null) { + var parent = div.parentElement; + if (parent !== null) { + parent.removeChild(div); + } + } console.log( "[ERROR]", "[VISUAL-CONSOLE-CLIENT]", diff --git a/pandora_console/include/rest-api/models/CachedModel.php b/pandora_console/include/rest-api/models/CachedModel.php index a3db743f5a..49d209179a 100644 --- a/pandora_console/include/rest-api/models/CachedModel.php +++ b/pandora_console/include/rest-api/models/CachedModel.php @@ -75,39 +75,26 @@ abstract class CachedModel extends Model public static function fromDB(array $filter): Model { global $config; - - // TODO: Remove references to the VC items. This class should be usable with any resource. + $save_cache = false; if ($filter['cache_expiration'] > 0) { - // Obtain the item's data from cache. - $cachedData = static::fetchCachedData($filter); - if ($cachedData === null) { - $userId = (static::$indexCacheByUser === true) ? $config['id_user'] : null; + $data = static::fetchCachedData($filter); + $save_cache = true; + } - // Delete expired data cache. - static::clearCachedData( - [ - 'vc_item_id' => $filter['id'], - 'vc_id' => $filter['id_layout'], - 'user_id' => $userId, - ] - ); - // 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' => $userId, - 'expiration' => $filter['cache_expiration'], - ], - $data - ); - } else { - $data = $cachedData; - } - } else { + if (isset($data) === false) { $data = static::fetchDataFromDB($filter); + } else { + // Retrieved from cache. + $save_cache = false; + } + + if ($save_cache === true) { + // Rebuild cache. + if (static::saveCachedData($filter, $data) !== true) { + throw new \Exception( + $config['dbconnection']->error + ); + } } return static::fromArray($data); diff --git a/pandora_console/include/rest-api/models/VisualConsole/Container.php b/pandora_console/include/rest-api/models/VisualConsole/Container.php index f45fee7651..3074d3feef 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Container.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Container.php @@ -413,7 +413,7 @@ final class Container extends Model try { array_push($items, $class::fromDB($data)); } catch (\Throwable $e) { - // TODO: Log this? + error_log('VC[Container]: '.$e->getMessage()); } } diff --git a/pandora_console/include/rest-api/models/VisualConsole/Item.php b/pandora_console/include/rest-api/models/VisualConsole/Item.php index 2cb365f3f7..ff033b645e 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Item.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Item.php @@ -41,6 +41,13 @@ class Item extends CachedModel */ protected static $useHtmlOutput = false; + /** + * Enable the cache index by user id. + * + * @var boolean + */ + protected static $indexCacheByUser = true; + /** * Validate the received data structure to ensure if we can extract the @@ -815,6 +822,14 @@ class Item extends CachedModel ); if ($data === false) { + // Invalid entry, clean it. + self::clearCachedData( + [ + 'vc_id' => $filter['vc_id'], + 'vc_item_id' => $filter['vc_item_id'], + 'user_id' => $filter['user_id'], + ] + ); return null; } @@ -835,14 +850,19 @@ class Item extends CachedModel */ protected static function saveCachedData(array $filter, array $data): bool { + global $config; + if (static::$indexCacheByUser === true) { + $filter['user_id'] = $config['id_user']; + } + return \db_process_sql_insert( 'tvisual_console_elements_cache', [ - 'vc_id' => $filter['vc_id'], - 'vc_item_id' => $filter['vc_item_id'], + 'vc_id' => $data['id_layout'], + 'vc_item_id' => $data['id'], 'user_id' => $filter['user_id'], 'data' => base64_encode(json_encode($data)), - 'expiration' => $filter['expiration'], + 'expiration' => $data['cache_expiration'], ] ) > 0; }