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 @@
inline_status_template = Handlebars.compile(inline_status_source);
function checkComputerStatus(options) {
if ((!options) || (options && !options.news)) {
if (!options || !options.news || !options.news.text) {
return 'ui-btn-no-data';
}
if (options.news.text.startsWith("#access")) {
......@@ -21,37 +21,38 @@
return 'ui-btn-warning';
}
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) {
var message,
computer_partition,
partition_class = 'ui-btn-ok',
error_amount = 0,
total_amount = 0;
computer_partition,
partition_class = 'ui-btn-ok',
error_amount = 0,
total_amount = 0;
if ((!options) || (options && !options.computer_partition_news)) {
if (!options || !options.computer_partition_news) {
return 'ui-btn-no-data';
}
for (computer_partition in options.computer_partition_news) {
message = options.computer_partition_news[computer_partition].text;
if (message.startsWith("#error")) {
partition_class = 'ui-btn-warning';
error_amount++;
}
total_amount++;
if ((error_amount > 0) && (error_amount < total_amount)) {
// No need to continue the result will be a warnning
return partition_class;
if (options.computer_partition_news.hasOwnProperty(computer_partition) &&
options.computer_partition_news[computer_partition].text) {
message = options.computer_partition_news[computer_partition].text;
if (message.startsWith("#error")) {
partition_class = 'ui-btn-warning';
error_amount += 1;
}
total_amount += 1;
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) {
......@@ -74,16 +75,23 @@
status_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') ||
(status_class === 'ui-btn-no-data')) {
right_class = status_class;
} else {
right_class = checkComputerPartitionStatus(
{computer_partition_news: result.news.partition});
if (result && result.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({
monitor_url: monitor_url,
......
......@@ -269,7 +269,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>974.62049.17150.785</string> </value>
<value> <string>978.25610.31873.42666</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -287,7 +287,7 @@
</tuple>
<state>
<tuple>
<float>1555352356.62</float>
<float>1568302726.92</float>
<string>UTC</string>
</tuple>
</state>
......
/*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) {
"use strict";
......@@ -10,7 +10,7 @@
inline_status_template = Handlebars.compile(inline_status_source);
function checkComputerStatus(options) {
if (!options) {
if (!options || !options.text) {
return 'ui-btn-no-data';
}
if (options.text.startsWith("#access")) {
......@@ -21,37 +21,38 @@
return 'ui-btn-warning';
}
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) {
var message,
computer_partition,
partition_class = 'ui-btn-ok',
error_amount = 0,
total_amount = 0;
computer_partition,
partition_class = 'ui-btn-ok',
error_amount = 0,
total_amount = 0;
if (!options) {
return 'ui-btn-no-data';
}
for (computer_partition in options) {
message = options[computer_partition].text;
if (message.startsWith("#error")) {
partition_class = 'ui-btn-warning';
error_amount++;
}
total_amount++;
if (options.hasOwnProperty(computer_partition) &&
options[computer_partition].text) {
message = options[computer_partition].text;
if (message.startsWith("#error")) {
partition_class = 'ui-btn-warning';
error_amount += 1;
}
total_amount += 1;
if ((error_amount > 0) && (error_amount < total_amount)) {
// 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) {
......@@ -66,22 +67,27 @@
function checkProjectStatus(options) {
var previous_status = "START",
status = 'ui-btn-no-data',
i;
status = 'ui-btn-no-data',
i;
if (!options || !options.news || !options.news.computer) {
return status;
}
for (i in options.news.computer) {
status = checkComputerStatus(options.news.computer[i]);
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 (options.news.computer.hasOwnProperty(i)) {
status = checkComputerStatus(options.news.computer[i]);
if (previous_status === "START") {
previous_status = status;
}
if ((status === 'ui-btn-error') && (previous_status === 'ui-btn-ok')) {
return 'ui-btn-warning';
}
if (status === 'ui-btn-no-data') {
status = previous_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')) {
return 'ui-btn-warning';
}
if (status === 'ui-btn-no-data') {
status = previous_status;
}
}
}
}
......@@ -90,27 +96,30 @@
function checkProjectPartitionStatus(options) {
var computer_reference,
status = 'ui-btn-no-data',
previous_status = "START";
status = 'ui-btn-no-data',
previous_status = "START";
for (computer_reference in options.news.partition) {
status = checkComputerPartitionStatus(
options.news.partition[computer_reference]);
if (previous_status === "START") {
previous_status = status;
}
if (status === 'ui-btn-warning') {
// 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 (options.news.partition.hasOwnProperty(computer_reference)) {
status = checkComputerPartitionStatus(
options.news.partition[computer_reference]
);
if (previous_status === "START") {
previous_status = status;
}
if ((status === 'ui-btn-error') && (previous_status === 'ui-btn-ok')) {
return 'ui-btn-warning';
if (status === 'ui-btn-warning') {
// If status is warning, nothing after will change it.
return status;
}
if (status === 'ui-btn-no-data') {
status = previous_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')) {
return 'ui-btn-warning';
}
if (status === 'ui-btn-no-data') {
status = previous_status;
}
}
}
}
......@@ -135,7 +144,8 @@
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({
monitor_url: monitor_url,
status_class: status_class,
......
......@@ -257,7 +257,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>975.3495.41986.50397</string> </value>
<value> <string>978.25609.22500.25258</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -275,7 +275,7 @@
</tuple>
<state>
<tuple>
<float>1555352158.81</float>
<float>1568302679.29</float>
<string>UTC</string>
</tuple>
</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