-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceSlaAggregate.ts
70 lines (63 loc) · 2.28 KB
/
ServiceSlaAggregate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { ServiceSlaAggregateInput } from '../interfaces/ServiceSlaAggregate';
import { ServiceSla } from '../interfaces/ServiceSla';
import { ServiceEntry } from '../interfaces/ServiceEntry';
import { MissingServiceEntriesError } from '../errors/MissingServiceEntriesError';
import { ServiceMissingSlaError } from '../errors/ServiceMissingSlaError';
/**
* @description Factory/builder function to return a new ServiceSlaAggregate.
*/
export function createNewServiceSlaAggregate(
serviceSlaAggregateInput: ServiceSlaAggregateInput
): any {
const { serviceEntries, serviceSlas, serviceNamesList } = serviceSlaAggregateInput;
return new ServiceSlaAggregate(serviceEntries, serviceSlas, serviceNamesList);
}
/**
* @description The ServiceSlaAggregate handles service SLAs and logic around their data and calculations.
*/
class ServiceSlaAggregate {
/**
* Constructed and validated service entries, built from user input and using SLA data.
*/
serviceEntries: ServiceEntry[];
/**
* Services and their SLAs.
*/
serviceSlas: ServiceSla[];
/**
* Plain list of service names as strings.
* Used as a convenience to lookup names when validating.
*/
serviceNamesList: string[];
constructor(
serviceEntries: ServiceEntry[],
serviceSlas: ServiceSla[],
serviceNamesList: string[]
) {
this.serviceEntries = serviceEntries;
this.serviceSlas = serviceSlas;
this.serviceNamesList = serviceNamesList;
}
/**
* @description Utility to get matching service SLA from provided service entries.
*/
private getServiceSlasFromEntries(): number[] {
const serviceEntries = this.serviceEntries;
if (!serviceEntries || serviceEntries.length === 0)
throw new MissingServiceEntriesError('Missing service entries!');
return serviceEntries.map((service: ServiceSla) => {
if (!service.sla) throw new ServiceMissingSlaError('Service is missing SLA!');
return parseFloat(service.sla);
});
}
/**
* @description Calculates maximum SLA from an array of provided numbers.
*/
public calculateMaxSla(): number {
const slas = this.getServiceSlasFromEntries();
const total = slas.reduce(
(prevValue: number, currentValue: number): number => (currentValue * prevValue) / 100
);
return parseFloat(total.toPrecision(5));
}
}