I'm working on a piece of javascript that sets a color value based on the status of a healthcheck. The string color values tie into the CSS I'm working with, and the string statuses tie into the health checks reported to this script.
This function is designed to set the color value for each healthcheck, AND return an overall status of the system. The overall status should be correspond to the worst condition health check (e.g. if most are "ok", and one is "fatal", overall should be "danger").
The current code works to set individual color values, but not the overall. Is there an "elegant" way to set the overall without having to use lots of nested if statements?
function getStatus(checks) {
var overall = '';
for (j in checks) {
checks[j]['color'] = '';
switch (checks[j].status) {
case 'OK':
checks[j]['color'] = 'success';
break;
case 'INFO':
checks[j]['color'] = 'info';
break;
case 'WARN':
checks[j]['color'] = 'warning';
break;
case 'ERROR':
checks[j]['color'] = 'danger';
break;
case 'FATAL':
checks[j]['color'] = 'danger';
break;
}
}
return overall;
}
I could do something like this:
case 'WARN':
if (overall === "OK" || overall === "INFO") {
overall = 'warning';
}
... but that gets messy, especially when checking for "ERROR" which must be greater than 3 values, but less than one. Is there a more effective way?