Skip to content

Commit

Permalink
Merge pull request #1 from Traackr/indices-caching-layer
Browse files Browse the repository at this point in the history
Create a caching layer so that we are not reloading all indices every…
  • Loading branch information
tyrantkhan committed Mar 22, 2022
2 parents ccb6044 + cb74121 commit 84066a2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
44 changes: 43 additions & 1 deletion public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1648,11 +1648,48 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',
$scope.path = '';
$scope.options = [];

var localStorageKey = 'indices';

var success = function(response) {
$scope.response = $sce.trustAsHtml(JSONTree.create(response));
$scope.loadHistory();
};

var setWithExpiry = function(key, value, ttl) {
var now = new Date();

// `item` is an object which contains the original value
// as well as the time when it's supposed to expire
var item = {
value: value,
expiry: now.getTime() + ttl,
};
localStorage.setItem(key, JSON.stringify(item));
};

var getWithExpiry = function(key) {
var itemStr = localStorage.getItem(key);
// if the item doesn't exist, return null
if (!itemStr) {
return null;
}
var item = JSON.parse(itemStr);
var now = new Date();
// compare the expiry time of the item with the current time
if (now.getTime() > item.expiry) {
// If the item is expired, delete the item from storage
// and return null
localStorage.removeItem(key);
return null;
}
return item.value;
};

$scope.deleteFromLocalStorage = function() {
localStorage.removeItem(localStorageKey);
AlertService.info('Indices Cache Successfully Cleared');
};

var failure = function(response) {
$scope.response = $sce.trustAsHtml(JSONTree.create(response));
};
Expand Down Expand Up @@ -1703,9 +1740,14 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',
};

$scope.updateOptions = function(text) {
if ($scope.indices) {
var indices = getWithExpiry(localStorageKey);
if ($scope.indices && indices !== null) {
$scope.options = indices;
} else if ($scope.indices) {
var autocomplete = new URLAutocomplete($scope.indices);
$scope.options = autocomplete.getAlternatives(text);
// Store Indices with a 1 Month TTL
setWithExpiry(localStorageKey, $scope.options, 2629800000);
}
};

Expand Down
5 changes: 5 additions & 0 deletions public/rest/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
</div>
<div class="form-group row">
<div class="col-lg-12 text-right">
<div class="btn-group">
<button type="submit" class="btn btn-default" ng-click="deleteFromLocalStorage()">
<i class="fa fa-trash"></i> Delete Indices Cache
</button>
</div>
<div class="btn-group">
<button type="submit" class="btn btn-default" ng-click="copyAsCURLCommand()">
<i class="fa fa-clipboard"></i> cURL
Expand Down
44 changes: 43 additions & 1 deletion src/app/components/rest/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,48 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',
$scope.path = '';
$scope.options = [];

var localStorageKey = 'indices';

var success = function(response) {
$scope.response = $sce.trustAsHtml(JSONTree.create(response));
$scope.loadHistory();
};

var setWithExpiry = function(key, value, ttl) {
var now = new Date();

// `item` is an object which contains the original value
// as well as the time when it's supposed to expire
var item = {
value: value,
expiry: now.getTime() + ttl,
};
localStorage.setItem(key, JSON.stringify(item));
};

var getWithExpiry = function(key) {
var itemStr = localStorage.getItem(key);
// if the item doesn't exist, return null
if (!itemStr) {
return null;
}
var item = JSON.parse(itemStr);
var now = new Date();
// compare the expiry time of the item with the current time
if (now.getTime() > item.expiry) {
// If the item is expired, delete the item from storage
// and return null
localStorage.removeItem(key);
return null;
}
return item.value;
};

$scope.deleteFromLocalStorage = function() {
localStorage.removeItem(localStorageKey);
AlertService.info('Indices Cache Successfully Cleared');
};

var failure = function(response) {
$scope.response = $sce.trustAsHtml(JSONTree.create(response));
};
Expand Down Expand Up @@ -68,9 +105,14 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http',
};

$scope.updateOptions = function(text) {
if ($scope.indices) {
var indices = getWithExpiry(localStorageKey);
if ($scope.indices && indices !== null) {
$scope.options = indices;
} else if ($scope.indices) {
var autocomplete = new URLAutocomplete($scope.indices);
$scope.options = autocomplete.getAlternatives(text);
// Store Indices with a 1 Month TTL
setWithExpiry(localStorageKey, $scope.options, 2629800000);
}
};

Expand Down

0 comments on commit 84066a2

Please sign in to comment.