]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/device.service.ts
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / device.service.ts
1 import { Injectable } from '@angular/core';
2
3 import * as moment from 'moment';
4
5 import { CdDevice } from '../models/devices';
6
7 @Injectable({
8 providedIn: 'root'
9 })
10 export class DeviceService {
11 constructor() {}
12
13 /**
14 * Calculates additional data and appends them as new attributes to the given device.
15 */
16 calculateAdditionalData(device: CdDevice): CdDevice {
17 if (!device.life_expectancy_min || !device.life_expectancy_max) {
18 device.state = 'unknown';
19 return device;
20 }
21 const hasDate = (float: string): boolean => !!Number.parseFloat(float);
22 const weeks = (isoDate1: string, isoDate2: string): number =>
23 !isoDate1 || !isoDate2 || !hasDate(isoDate1) || !hasDate(isoDate2)
24 ? null
25 : moment.duration(moment(isoDate1).diff(moment(isoDate2))).asWeeks();
26
27 const ageOfStamp = moment
28 .duration(moment(moment.now()).diff(moment(device.life_expectancy_stamp)))
29 .asWeeks();
30 const max = weeks(device.life_expectancy_max, device.life_expectancy_stamp);
31 const min = weeks(device.life_expectancy_min, device.life_expectancy_stamp);
32
33 if (ageOfStamp > 1) {
34 device.state = 'stale';
35 } else if (max !== null && max <= 2) {
36 device.state = 'bad';
37 } else if (min !== null && min <= 4) {
38 device.state = 'warning';
39 } else {
40 device.state = 'good';
41 }
42
43 device.life_expectancy_weeks = {
44 max: max !== null ? Math.round(max) : null,
45 min: min !== null ? Math.round(min) : null
46 };
47
48 return device;
49 }
50
51 readable(device: CdDevice): CdDevice {
52 device.readableDaemons = device.daemons.join(' ');
53 return device;
54 }
55
56 prepareDevice(device: CdDevice): CdDevice {
57 return this.readable(this.calculateAdditionalData(device));
58 }
59 }