Commit afeff2aa authored by Thomas Gambier's avatar Thomas Gambier 🚴🏼 Committed by Rafael Monnerat

slapos_jio: fix computer and project status

  * fix JSLint
  * prevent the script from crashing when there is no data (no
partitions or no computer)
parent 3244ebb0
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
inline_status_template = Handlebars.compile(inline_status_source); inline_status_template = Handlebars.compile(inline_status_source);
function checkComputerStatus(options) { function checkComputerStatus(options) {
if ((!options) || (options && !options.news)) { if (!options || !options.news || !options.news.text) {
return 'ui-btn-no-data'; return 'ui-btn-no-data';
} }
if (options.news.text.startsWith("#access")) { if (options.news.text.startsWith("#access")) {
...@@ -21,37 +21,38 @@ ...@@ -21,37 +21,38 @@
return 'ui-btn-warning'; return 'ui-btn-warning';
} }
return 'ui-btn-ok'; return 'ui-btn-ok';
} else {
if (options.news.no_data) {
return 'ui-btn-no-data';
}
return 'ui-btn-error';
} }
if (options.news.no_data) {
return 'ui-btn-no-data';
}
return 'ui-btn-error';
} }
function checkComputerPartitionStatus(options) { function checkComputerPartitionStatus(options) {
var message, var message,
computer_partition, computer_partition,
partition_class = 'ui-btn-ok', partition_class = 'ui-btn-ok',
error_amount = 0, error_amount = 0,
total_amount = 0; total_amount = 0;
if ((!options) || (options && !options.computer_partition_news)) { if (!options || !options.computer_partition_news) {
return 'ui-btn-no-data'; return 'ui-btn-no-data';
} }
for (computer_partition in options.computer_partition_news) { for (computer_partition in options.computer_partition_news) {
message = options.computer_partition_news[computer_partition].text; if (options.computer_partition_news.hasOwnProperty(computer_partition) &&
if (message.startsWith("#error")) { options.computer_partition_news[computer_partition].text) {
partition_class = 'ui-btn-warning'; message = options.computer_partition_news[computer_partition].text;
error_amount++; if (message.startsWith("#error")) {
} partition_class = 'ui-btn-warning';
total_amount++; error_amount += 1;
}
if ((error_amount > 0) && (error_amount < total_amount)) { total_amount += 1;
// No need to continue the result will be a warnning
return partition_class; if ((error_amount > 0) && (error_amount < total_amount)) {
// No need to continue the result will be a warnning
return partition_class;
}
} }
} }
if (total_amount === 0) { if (total_amount === 0) {
...@@ -74,16 +75,23 @@ ...@@ -74,16 +75,23 @@
status_style = '', status_style = '',
right_style = ''; right_style = '';
status_class = checkComputerStatus({news: result.news.computer}); if (result && result.news && result.news.computer) {
status_class = checkComputerStatus({news: result.news.computer});
}
if ((status_class === 'ui-btn-error') || if ((status_class === 'ui-btn-error') ||
(status_class === 'ui-btn-no-data')) { (status_class === 'ui-btn-no-data')) {
right_class = status_class; right_class = status_class;
} else { } else {
right_class = checkComputerPartitionStatus( if (result && result.news && result.news.partition) {
{computer_partition_news: result.news.partition}); right_class = checkComputerPartitionStatus(
{computer_partition_news: result.news.partition}
);
}
} }
monitor_url = 'https://monitor.app.officejs.com/#/?page=ojsm_dispatch&query=portal_type%3A%22Software%20Instance%22%20AND%20aggregate_reference%3A%22' + result.reference + '%22'; monitor_url = 'https://monitor.app.officejs.com/#/' +
'?page=ojsm_dispatch&query=portal_type%3A%22Software%20Instance%22%20' +
'AND%20aggregate_reference%3A%22' + result.reference + '%22';
gadget.element.innerHTML = inline_status_template({ gadget.element.innerHTML = inline_status_template({
monitor_url: monitor_url, monitor_url: monitor_url,
......
...@@ -269,7 +269,7 @@ ...@@ -269,7 +269,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>974.62049.17150.785</string> </value> <value> <string>978.25610.31873.42666</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -287,7 +287,7 @@ ...@@ -287,7 +287,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1555352356.62</float> <float>1568302726.92</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*globals console, window, rJS, RSVP, loopEventListener, i18n, Handlebars, $*/ /*globals console, window, rJS, RSVP, loopEventListener, i18n, Handlebars, $*/
/*jslint indent: 2, nomen: true, maxlen: 80*/ /*jslint indent: 2, nomen: true, maxlen: 90*/
(function (window, rJS, RSVP, Handlebars) { (function (window, rJS, RSVP, Handlebars) {
"use strict"; "use strict";
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
inline_status_template = Handlebars.compile(inline_status_source); inline_status_template = Handlebars.compile(inline_status_source);
function checkComputerStatus(options) { function checkComputerStatus(options) {
if (!options) { if (!options || !options.text) {
return 'ui-btn-no-data'; return 'ui-btn-no-data';
} }
if (options.text.startsWith("#access")) { if (options.text.startsWith("#access")) {
...@@ -21,37 +21,38 @@ ...@@ -21,37 +21,38 @@
return 'ui-btn-warning'; return 'ui-btn-warning';
} }
return 'ui-btn-ok'; return 'ui-btn-ok';
} else {
if (options.no_data) {
return 'ui-btn-no-data';
}
return 'ui-btn-error';
} }
if (options.no_data) {
return 'ui-btn-no-data';
}
return 'ui-btn-error';
} }
function checkComputerPartitionStatus(options) { function checkComputerPartitionStatus(options) {
var message, var message,
computer_partition, computer_partition,
partition_class = 'ui-btn-ok', partition_class = 'ui-btn-ok',
error_amount = 0, error_amount = 0,
total_amount = 0; total_amount = 0;
if (!options) { if (!options) {
return 'ui-btn-no-data'; return 'ui-btn-no-data';
} }
for (computer_partition in options) { for (computer_partition in options) {
message = options[computer_partition].text; if (options.hasOwnProperty(computer_partition) &&
if (message.startsWith("#error")) { options[computer_partition].text) {
partition_class = 'ui-btn-warning'; message = options[computer_partition].text;
error_amount++; if (message.startsWith("#error")) {
} partition_class = 'ui-btn-warning';
total_amount++; error_amount += 1;
}
total_amount += 1;
if ((error_amount > 0) && (error_amount < total_amount)) { if ((error_amount > 0) && (error_amount < total_amount)) {
// No need to continue the result will be a warnning // No need to continue the result will be a warnning
return partition_class; return partition_class;
}
} }
} }
if (!total_amount) { if (!total_amount) {
...@@ -66,22 +67,27 @@ ...@@ -66,22 +67,27 @@
function checkProjectStatus(options) { function checkProjectStatus(options) {
var previous_status = "START", var previous_status = "START",
status = 'ui-btn-no-data', status = 'ui-btn-no-data',
i; i;
if (!options || !options.news || !options.news.computer) {
return status;
}
for (i in options.news.computer) { for (i in options.news.computer) {
status = checkComputerStatus(options.news.computer[i]); if (options.news.computer.hasOwnProperty(i)) {
if (previous_status === "START") { status = checkComputerStatus(options.news.computer[i]);
previous_status = status; if (previous_status === "START") {
} previous_status = status;
if (previous_status !== status) {
if ((previous_status === 'ui-btn-error') && (status === 'ui-btn-ok')) {
return 'ui-btn-warning';
} }
if ((status === 'ui-btn-error') && (previous_status === 'ui-btn-ok')) { if (previous_status !== status) {
return 'ui-btn-warning'; if ((previous_status === 'ui-btn-error') && (status === 'ui-btn-ok')) {
} return 'ui-btn-warning';
if (status === 'ui-btn-no-data') { }
status = previous_status; if ((status === 'ui-btn-error') && (previous_status === 'ui-btn-ok')) {
return 'ui-btn-warning';
}
if (status === 'ui-btn-no-data') {
status = previous_status;
}
} }
} }
} }
...@@ -90,27 +96,30 @@ ...@@ -90,27 +96,30 @@
function checkProjectPartitionStatus(options) { function checkProjectPartitionStatus(options) {
var computer_reference, var computer_reference,
status = 'ui-btn-no-data', status = 'ui-btn-no-data',
previous_status = "START"; previous_status = "START";
for (computer_reference in options.news.partition) { for (computer_reference in options.news.partition) {
status = checkComputerPartitionStatus( if (options.news.partition.hasOwnProperty(computer_reference)) {
options.news.partition[computer_reference]); status = checkComputerPartitionStatus(
if (previous_status === "START") { options.news.partition[computer_reference]
previous_status = status; );
} if (previous_status === "START") {
if (status === 'ui-btn-warning') { previous_status = status;
// If status is warning, nothing after will change it.
return status;
}
if (previous_status !== status) {
if ((previous_status === 'ui-btn-error') && (status === 'ui-btn-ok')) {
return 'ui-btn-warning';
} }
if ((status === 'ui-btn-error') && (previous_status === 'ui-btn-ok')) { if (status === 'ui-btn-warning') {
return 'ui-btn-warning'; // If status is warning, nothing after will change it.
return status;
} }
if (status === 'ui-btn-no-data') { if (previous_status !== status) {
status = previous_status; if ((previous_status === 'ui-btn-error') && (status === 'ui-btn-ok')) {
return 'ui-btn-warning';
}
if ((status === 'ui-btn-error') && (previous_status === 'ui-btn-ok')) {
return 'ui-btn-warning';
}
if (status === 'ui-btn-no-data') {
status = previous_status;
}
} }
} }
} }
...@@ -135,7 +144,8 @@ ...@@ -135,7 +144,8 @@
right_class = checkProjectPartitionStatus(result); right_class = checkProjectPartitionStatus(result);
} }
monitor_url = gadget.props.hateoas_url + gadget.options.value.jio_key + '/Base_redirectToMonitor'; monitor_url = gadget.props.hateoas_url +
gadget.options.value.jio_key + '/Base_redirectToMonitor';
gadget.element.innerHTML = inline_status_template({ gadget.element.innerHTML = inline_status_template({
monitor_url: monitor_url, monitor_url: monitor_url,
status_class: status_class, status_class: status_class,
......
...@@ -257,7 +257,7 @@ ...@@ -257,7 +257,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>975.3495.41986.50397</string> </value> <value> <string>978.25609.22500.25258</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -275,7 +275,7 @@ ...@@ -275,7 +275,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1555352158.81</float> <float>1568302679.29</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment