consolidate device status to string logic in DeviceStatusPipe.

Ensure device status takes into account new settings.
This commit is contained in:
Jason Kulatunga
2022-07-29 07:11:57 -07:00
parent 2e768fb491
commit ce2f990eb1
7 changed files with 222 additions and 159 deletions
@@ -1,15 +1,15 @@
<div [ngClass]="{ 'border-green': deviceStatusString(deviceSummary) == 'passed',
'border-red': deviceStatusString(deviceSummary) == 'failed' }"
<div [ngClass]="{ 'border-green': deviceStatusForModelWithThreshold(deviceSummary.device, !!deviceSummary.smart, config.metrics.status_threshold) == 'passed',
'border-red': deviceStatusForModelWithThreshold(deviceSummary.device, !!deviceSummary.smart, config.metrics.status_threshold) == 'failed' }"
class="relative flex flex-col flex-auto p-6 pr-3 pb-3 bg-card rounded border-l-4 shadow-md overflow-hidden">
<div class="absolute bottom-0 right-0 w-24 h-24 -m-6">
<mat-icon class="icon-size-96 opacity-12 text-green"
*ngIf="deviceStatusString(deviceSummary) == 'passed'"
*ngIf="deviceStatusForModelWithThreshold(deviceSummary.device, !!deviceSummary.smart, config.metrics.status_threshold) == 'passed'"
[svgIcon]="'heroicons_outline:check-circle'"></mat-icon>
<mat-icon class="icon-size-96 opacity-12 text-red"
*ngIf="deviceStatusString(deviceSummary) == 'failed'"
*ngIf="deviceStatusForModelWithThreshold(deviceSummary.device, !!deviceSummary.smart, config.metrics.status_threshold) == 'failed'"
[svgIcon]="'heroicons_outline:exclamation-circle'"></mat-icon>
<mat-icon class="icon-size-96 opacity-12 text-yellow"
*ngIf="deviceStatusString(deviceSummary) == 'unknown'"
*ngIf="deviceStatusForModelWithThreshold(deviceSummary.device, !!deviceSummary.smart, config.metrics.status_threshold) == 'unknown'"
[svgIcon]="'heroicons_outline:question-mark-circle'"></mat-icon>
</div>
<div class="flex items-center">
@@ -47,7 +47,7 @@
<div class="flex flex-col mx-6 my-3 xs:w-full">
<div class="font-semibold text-xs text-hint uppercase tracking-wider leading-none">Status</div>
<div class="mt-2 font-medium text-3xl leading-none"
*ngIf="deviceSummary.smart?.collector_date; else unknownStatus">{{ deviceStatusString(deviceSummary) | titlecase}}</div>
*ngIf="deviceSummary.smart?.collector_date; else unknownStatus">{{ deviceStatusForModelWithThreshold(deviceSummary.device, !!deviceSummary.smart, config.metrics.status_threshold) | titlecase}}</div>
<ng-template #unknownStatus><div class="mt-2 font-medium text-3xl leading-none">No Data</div></ng-template>
</div>
<div class="flex flex-col mx-6 my-3 xs:w-full">
@@ -155,115 +155,4 @@ describe('DashboardDeviceComponent', () => {
} as DeviceSummaryModel)).toBe('text-red')
});
})
describe('#deviceStatusString()', () => {
it('if healthy device, should be passing', () => {
httpClientSpy.get.and.returnValue(of({
settings: {
metrics: {
status_threshold: MetricsStatusThreshold.Both,
}
}
}));
component.ngOnInit()
expect(component.deviceStatusString({
device: {
device_status: 0
},
smart: {
collector_date: moment().subtract(13, 'days').toISOString()
},
} as DeviceSummaryModel)).toBe('passed')
});
it('if device with no smart data, should be unknown', () => {
httpClientSpy.get.and.returnValue(of({
settings: {
metrics: {
status_threshold: MetricsStatusThreshold.Both,
}
}
}));
component.ngOnInit()
expect(component.deviceStatusString({
device: {
device_status: 0
},
} as DeviceSummaryModel)).toBe('unknown')
});
const testCases = [
{
'deviceStatus': 1,
'threshold': MetricsStatusThreshold.Smart,
'result': 'failed'
},
{
'deviceStatus': 1,
'threshold': MetricsStatusThreshold.Scrutiny,
'result': 'passed'
},
{
'deviceStatus': 1,
'threshold': MetricsStatusThreshold.Both,
'result': 'failed'
},
{
'deviceStatus': 2,
'threshold': MetricsStatusThreshold.Smart,
'result': 'passed'
},
{
'deviceStatus': 2,
'threshold': MetricsStatusThreshold.Scrutiny,
'result': 'failed'
},
{
'deviceStatus': 2,
'threshold': MetricsStatusThreshold.Both,
'result': 'failed'
},
{
'deviceStatus': 3,
'threshold': MetricsStatusThreshold.Smart,
'result': 'failed'
},
{
'deviceStatus': 3,
'threshold': MetricsStatusThreshold.Scrutiny,
'result': 'failed'
},
{
'deviceStatus': 3,
'threshold': MetricsStatusThreshold.Both,
'result': 'failed'
}
]
testCases.forEach((test, index) => {
it(`if device with status (${test.deviceStatus}) and threshold (${test.threshold}), should be ${test.result}`, () => {
httpClientSpy.get.and.returnValue(of({
settings: {
metrics: {
status_threshold: test.threshold,
}
}
}));
component.ngOnInit()
expect(component.deviceStatusString({
device: {
device_status: test.deviceStatus
},
smart: {
collector_date: moment().subtract(13, 'days').toISOString()
},
} as DeviceSummaryModel)).toBe(test.result)
});
});
})
});
@@ -9,8 +9,7 @@ import {MatDialog} from '@angular/material/dialog';
import {DashboardDeviceDeleteDialogComponent} from 'app/layout/common/dashboard-device-delete-dialog/dashboard-device-delete-dialog.component';
import {DeviceTitlePipe} from 'app/shared/device-title.pipe';
import {DeviceSummaryModel} from 'app/core/models/device-summary-model';
export type deviceStatusName = 'unknown' | 'passed' | 'failed'
import {DeviceStatusPipe} from 'app/shared/device-status.pipe';
@Component({
selector: 'app-dashboard-device',
@@ -37,6 +36,8 @@ export class DashboardDeviceComponent implements OnInit {
readonly humanizeDuration = humanizeDuration;
deviceStatusForModelWithThreshold = DeviceStatusPipe.deviceStatusForModelWithThreshold
ngOnInit(): void {
// Subscribe to config changes
this._configService.config$
@@ -52,7 +53,7 @@ export class DashboardDeviceComponent implements OnInit {
// -----------------------------------------------------------------------------------------------------
classDeviceLastUpdatedOn(deviceSummary: DeviceSummaryModel): string {
const deviceStatus = this.deviceStatusString(deviceSummary)
const deviceStatus = DeviceStatusPipe.deviceStatusForModelWithThreshold(deviceSummary.device, !!deviceSummary.smart, this.config.metrics.status_threshold)
if (deviceStatus === 'failed') {
return 'text-red' // if the device has failed, always highlight in red
} else if (deviceStatus === 'passed') {
@@ -71,24 +72,6 @@ export class DashboardDeviceComponent implements OnInit {
}
}
deviceStatusString(deviceSummary: DeviceSummaryModel): deviceStatusName {
// no smart data, so treat the device status as unknown
if (!deviceSummary.smart) {
return 'unknown'
}
// determine the device status, by comparing it against the allowed threshold
// tslint:disable-next-line:no-bitwise
const deviceStatus = deviceSummary.device.device_status & this.config.metrics.status_threshold
if (deviceStatus === 0) {
return 'passed'
} else {
return 'failed'
}
}
openDeleteDialog(): void {
const dialogRef = this.dialog.open(DashboardDeviceDeleteDialogComponent, {
// width: '250px',