-
Notifications
You must be signed in to change notification settings - Fork 902
/
availability.trend.component.ts
146 lines (118 loc) · 4.3 KB
/
availability.trend.component.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { module } from 'angular';
import { Line, line } from 'd3-shape';
import { scaleLinear, scaleLog } from 'd3-scale';
import { get } from 'lodash';
import { IAvailabilityWindow } from './availability.read.service';
import './availability.less';
interface PopoverContent {
datetime: string;
incidents: string[];
availability: number;
}
interface Dot {
r: number;
cx: string;
cy: string;
score: number;
popoverContent: PopoverContent;
}
export class AvailabilityTrendController implements ng.IComponentController {
public availabilityWindow: IAvailabilityWindow;
public height: number;
public width: number;
public trendLine: string;
public dots: Dot[] = [];
public popoverOpen: boolean[] = [];
public popoverTemplate: string = require('./availability.trend.popover.html');
private margin = 5;
private popoverClose: ng.IPromise<void>[] = [];
static get $inject() { return ['$timeout']; }
constructor(private $timeout: ng.ITimeoutService) {}
// Based on: https://en.wikipedia.org/wiki/High_availability#.22Nines.22
private getNines(availability: number): number {
return -Math.log10((100 - availability) / 100);
}
// Inverse of getNines
private getAvailability (nines: number): number {
return 100 - (Math.pow(10, -nines) * 100);
}
private getScore(availability: number): number {
if (this.getNines(availability) >= this.availabilityWindow.target_nines * 0.95) { return 2; }
return 4;
}
private generateDots(xScale: Function, yScale: Function): Dot[] {
const dots: Dot[] = [];
this.availabilityWindow.ts.is_outage.forEach((isOutage, index) => {
if (isOutage) {
const popoverContent: PopoverContent = {
datetime: get(this.availabilityWindow, ['ts', 'datetime', index], 'Unknown'),
availability: get(this.availabilityWindow, ['ts', 'availability', index], 0),
incidents: get(this.availabilityWindow, ['ts', 'incs', index], <string[]>[])
};
dots.push({
r: 3,
cx: xScale(index),
cy: yScale(this.availabilityWindow.ts.availability[index]),
score: this.getScore(this.availabilityWindow.ts.availability[index]),
popoverContent: popoverContent
});
}
});
return dots;
}
private updateData(): void {
if (this.availabilityWindow && this.availabilityWindow.ts.availability && this.availabilityWindow.ts.availability.length) {
// Set the min value to a large fraction of target nines
const minValue = this.getAvailability(this.availabilityWindow.target_nines) * 0.995;
// Create line function
const xScale = scaleLinear().domain([0, this.availabilityWindow.ts.availability.length]).range([this.margin, this.width]);
const yScale = scaleLog().domain([minValue, 100]).range([this.height - this.margin, this.margin]).clamp(true);
const thisLine: Line<number> = line<number>()
.x((_, i) => xScale(i))
.y((d) => yScale(d));
// Generate line
this.trendLine = thisLine(this.availabilityWindow.ts.availability);
// Generate the dots
this.dots = this.generateDots(xScale, yScale);
}
}
public $onInit(): void {
this.updateData();
}
public $onChanges(): void {
this.updateData();
}
public showPopover(index: number): void {
this.popoverOpen[index] = true;
this.popoverHovered(index);
}
public hidePopover(index: number, defer: boolean): void {
if (defer) {
this.popoverClose[index] = this.$timeout(
() => {
this.popoverOpen[index] = false;
},
500);
} else {
this.popoverOpen[index] = false;
}
}
public popoverHovered(index: number): void {
if (this.popoverClose[index]) {
this.$timeout.cancel(this.popoverClose[index]);
this.popoverClose[index] = null;
}
}
}
class AvailabilityTrendComponent implements ng.IComponentOptions {
public bindings: any = {
availabilityWindow: '<',
height: '<',
width: '<'
};
public controller: any = AvailabilityTrendController;
public templateUrl: string = require('./availability.trend.html');
}
export const AVAILABILITY_TREND_COMPONENT = 'spinnaker.netflix.availability.trend.component';
module(AVAILABILITY_TREND_COMPONENT, [])
.component('availabilityTrend', new AvailabilityTrendComponent());