aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/views/Footer.js
blob: ad1e4f75d814d439517c2f7592a012df4c3a8d22 (plain) (blame)
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
import React, {Component} from 'react';
import './Footer.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 dispatcher from "../globals";
import {withRouter} from "react-router-dom";
import log from "../log";


class Footer 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((payload) => {
            if (payload.actionType === "connections-update") {
                this.setState({
                    selection: new TimeRange(payload.from, payload.to),
                });
            }
        });
    }

    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 backend.get("/api/services")).json;
        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 series = new TimeSeries({
            name: "statistics",
            columns: ["time"].concat(ports),
            points: metrics.map(m => [new Date(m["range_start"])].concat(ports.map(p => m[metric][p] || 0)))
        });
        this.setState({
            metric,
            series,
            services,
            timeRange: series.range(),
        });
        log.debug(`Loaded statistics for metric "${metric}" for services [${ports}]`);
    };

    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({
                actionType: "timeline-update",
                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() {
        return (
            <footer className="footer">
                <div className="time-line">
                    {this.state.series &&
                    <>
                        <Resizable>
                            <ChartContainer timeRange={this.state.timeRange} enableDragZoom={false}
                                            paddingTop={5} utc minDuration={60000}
                                            maxTime={this.state.series.range().end()}
                                            minTime={this.state.series.range().begin()}
                                            paddingLeft={0} paddingRight={0} paddingBottom={0}
                                            enablePanZoom={true}
                                            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(Footer);