From 5a68ea106980a52850cc527da098f481223b07b1 Mon Sep 17 00:00:00 2001
From: Simon Rettberg
Date: Wed, 7 Dec 2016 13:18:23 +0100
Subject: [roomplanner] Tighten up computer list, better search result caching
---
modules-available/roomplanner/clientscript.js | 60 +++++++++++++++++++--------
1 file changed, 43 insertions(+), 17 deletions(-)
(limited to 'modules-available/roomplanner/clientscript.js')
diff --git a/modules-available/roomplanner/clientscript.js b/modules-available/roomplanner/clientscript.js
index cc149392..53e0164a 100644
--- a/modules-available/roomplanner/clientscript.js
+++ b/modules-available/roomplanner/clientscript.js
@@ -5,13 +5,19 @@
* */
/* Map: uuid -> obj */
-machineCache = {};
+var machineCache = {};
-selectMachinInitialized = false;
-
-placedMachines = [];
+var selectMachinInitialized = false;
+var placedMachines = [];
+function makeCombinedField(machineArray)
+{
+ machineArray.forEach(function (v,i,a){
+ machineArray[i].combined = (v.machineuuid + " " + v.hostname + " " + v.clientip + " " + v.macaddr).toLocaleLowerCase();
+ });
+ return machineArray;
+}
function renderMachineEntry(item, escape) {
machineCache[item.machineuuid] = item;
@@ -33,38 +39,58 @@ function renderMachineEntry(item, escape) {
//+ '
'
+ ' '
+ ' '
- + '
'
+ + ' '
+ 'UUID: | ' + escape(item.machineuuid) + ' |
'
- + 'MAC: | ' + escape(item.macaddr) + ' |
'
+ + 'MAC: | ' + escape(item.macaddr) + ' |
'
+ 'IP: | ' + escape(item.clientip) + ' |
'
+ '
'
+ ' '
+ '';
}
+var queryCache = {};
+
+function filterCache(key, query) {
+ return queryCache[key].filter(function (el) {
+ return -1 !== el.combined.indexOf(query);
+ });
+}
+
function loadMachines(query, callback) {
console.log('queryMachines(' + query + ')');
- if (query.length < 2) return callback();
+ if (query.length < 2) {
+ callback();
+ return;
+ }
+ query = query.toLocaleLowerCase();
+ // See if we have a previous query in our cache that is a superset for this one
+ for (var k in queryCache) {
+ if (query.indexOf(k) !== -1) {
+ callback(filterCache(k, query));
+ return;
+ }
+ }
$.ajax({
url: '?do=roomplanner&action=getmachines&query=' + encodeURIComponent(query),
type: 'GET',
+ dataType: 'json',
error: function() {
console.log('error while doing ajax call');
callback();
},
- success: function(res) {
+ success: function(json) {
console.log('success ajax call');
- var json = JSON.parse(res);
- json.machines.forEach(function (v,i,a){
- a[i].combined = v.machineuuid + " " + v.hostname + " " + v.clientip + " " + v.macaddr;
- });
- return callback(json.machines);
+ var machines = makeCombinedField(json.machines);
+ // Server cuts off at 100, so only cache if it contains less entries, as
+ // the new, more specific query could return previously removed results.
+ if (machines.length < 100) {
+ queryCache[query] = machines;
+ }
+ callback(machines);
}
});
}
-
-
function clearSearchBox() {
$selectizeSearch[0].selectize.setValue([], true);
$selectizeSearch[0].selectize.clearCache();
@@ -90,7 +116,7 @@ function initSelectize() {
render : { option : renderMachineEntry, item: renderMachineEntry},
load: loadMachines,
maxItems: 1,
- sortField: 'clientip',
+ sortField: 'hostname',
sortDirection: 'asc',
onChange: clearSubnetBox
});
@@ -106,7 +132,7 @@ function initSelectize() {
create: false,
render : { option : renderMachineEntry, item: renderMachineEntry},
maxItems: 1,
- sortField: 'clientip',
+ sortField: 'hostname',
sortDirection: 'asc',
onChange: clearSearchBox
});
--
cgit v1.2.3-55-g7522