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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
import React, {Component} from 'react';
import './Timeline.scss';
import {
ChartContainer,
ChartRow,
Charts,
LineChart,
MultiBrush,
Resizable,
styler,
YAxis
} from "react-timeseries-charts";
import {TimeRange, TimeSeries} from "pondjs";
import backend from "../backend";
import ChoiceField from "../components/fields/ChoiceField";
import {withRouter} from "react-router-dom";
import log from "../log";
import dispatcher from "../dispatcher";
const minutes = 60 * 1000;
class Timeline extends Component {
state = {
metric: "connections_per_service"
};
constructor() {
super();
this.disableTimeSeriesChanges = false;
this.selectionTimeout = null;
}
filteredPort = () => {
const urlParams = new URLSearchParams(this.props.location.search);
return urlParams.get("service_port");
};
componentDidMount() {
const filteredPort = this.filteredPort();
this.setState({filteredPort});
this.loadStatistics(this.state.metric, filteredPort).then(() => log.debug("Statistics loaded after mount"));
dispatcher.register("connection_updates", payload => {
this.setState({
selection: new TimeRange(payload.from, payload.to),
});
});
dispatcher.register("notifications", payload => {
if (payload.event === "services.edit") {
this.loadServices().then(() => log.debug("Services reloaded after notification update"));
}
});
}
componentDidUpdate(prevProps, prevState, snapshot) {
const filteredPort = this.filteredPort();
if (this.state.filteredPort !== filteredPort) {
this.setState({filteredPort});
this.loadStatistics(this.state.metric, filteredPort).then(() =>
log.debug("Statistics reloaded after filtered port changes"));
}
}
loadStatistics = async (metric, filteredPort) => {
const urlParams = new URLSearchParams();
urlParams.set("metric", metric);
let services = await this.loadServices();
if (filteredPort && services[filteredPort]) {
const service = services[filteredPort];
services = {};
services[filteredPort] = service;
}
const ports = Object.keys(services);
ports.forEach(s => urlParams.append("ports", s));
const metrics = (await backend.get("/api/statistics?" + urlParams)).json;
const zeroFilledMetrics = [];
const toTime = m => new Date(m["range_start"]).getTime();
if (metrics.length > 0) {
let i = 0;
for (let interval = toTime(metrics[0]); interval <= toTime(metrics[metrics.length - 1]); interval += minutes) {
if (interval === toTime(metrics[i])) {
const m = metrics[i++];
m["range_start"] = new Date(m["range_start"]);
zeroFilledMetrics.push(m);
} else {
const m = {};
m["range_start"] = new Date(interval);
m[metric] = {};
ports.forEach(p => m[metric][p] = 0);
zeroFilledMetrics.push(m);
}
}
}
const series = new TimeSeries({
name: "statistics",
columns: ["time"].concat(ports),
points: zeroFilledMetrics.map(m => [m["range_start"]].concat(ports.map(p => m[metric][p] || 0)))
});
const start = series.range().begin();
const end = series.range().end();
start.setTime(start.getTime() - minutes);
end.setTime(end.getTime() + minutes);
this.setState({
metric,
series,
timeRange: new TimeRange(start, end),
start,
end
});
log.debug(`Loaded statistics for metric "${metric}" for services [${ports}]`);
};
loadServices = async () => {
const services = (await backend.get("/api/services")).json;
this.setState({services});
return services;
};
createStyler = () => {
return styler(Object.keys(this.state.services).map(port => {
return {key: port, color: this.state.services[port].color, width: 2};
}));
};
handleTimeRangeChange = (timeRange) => {
if (!this.disableTimeSeriesChanges) {
this.setState({timeRange});
}
};
handleSelectionChange = (timeRange) => {
this.disableTimeSeriesChanges = true;
this.setState({selection: timeRange});
if (this.selectionTimeout) {
clearTimeout(this.selectionTimeout);
}
this.selectionTimeout = setTimeout(() => {
dispatcher.dispatch("timeline_updates", {
from: timeRange.begin(),
to: timeRange.end()
});
this.selectionTimeout = null;
this.disableTimeSeriesChanges = false;
}, 1000);
};
aggregateSeries = (func) => {
const values = this.state.series.columns().map(c => this.state.series[func](c));
return Math[func](...values);
};
render() {
if (!this.state.series) {
return null;
}
return (
<footer className="footer">
<div className="time-line">
<Resizable>
<ChartContainer timeRange={this.state.timeRange} enableDragZoom={false}
paddingTop={5} minDuration={60000}
maxTime={this.state.end}
minTime={this.state.start}
paddingLeft={0} paddingRight={0} paddingBottom={0}
enablePanZoom={true} utc={false}
onTimeRangeChanged={this.handleTimeRangeChange}>
<ChartRow height="125">
<YAxis id="axis1" hideAxisLine
min={this.aggregateSeries("min")}
max={this.aggregateSeries("max")} width="35" type="linear" transition={300}/>
<Charts>
<LineChart axis="axis1" series={this.state.series}
columns={Object.keys(this.state.services)}
style={this.createStyler()} interpolation="curveBasis"/>
<MultiBrush
timeRanges={[this.state.selection]}
allowSelectionClear={false}
allowFreeDrawing={false}
onTimeRangeChanged={this.handleSelectionChange}
/>
</Charts>
</ChartRow>
</ChartContainer>
</Resizable>
<div className="metric-selection">
<ChoiceField inline small
keys={["connections_per_service", "client_bytes_per_service",
"server_bytes_per_service", "duration_per_service"]}
values={["connections_per_service", "client_bytes_per_service",
"server_bytes_per_service", "duration_per_service"]}
onChange={(metric) => this.loadStatistics(metric, this.state.filteredPort)
.then(() => log.debug("Statistics loaded after metric changes"))}
value={this.state.metric}/>
</div>
</div>
</footer>
);
}
}
export default withRouter(Timeline);
|