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
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// @flow
import React from 'react';
import { debounce, findLast, maxBy, minBy, sortBy } from 'lodash';
import ProjectActivityGraphsHeader from './ProjectActivityGraphsHeader';
import GraphsZoom from './GraphsZoom';
import GraphsHistory from './GraphsHistory';
import {
datesQueryChanged,
generateSeries,
getDisplayedHistoryMetrics,
historyQueryChanged
} from '../utils';
import type { RawQuery } from '../../../helpers/query';
import type { Analysis, MeasureHistory, Metric, Query } from '../types';
import type { Serie } from '../../../components/charts/AdvancedTimeline';
type Props = {
analyses: Array<Analysis>,
leakPeriodDate: Date,
loading: boolean,
measuresHistory: Array<MeasureHistory>,
metrics: Array<Metric>,
metricsType: string,
project: string,
query: Query,
updateQuery: RawQuery => void
};
type State = {
graphStartDate: ?Date,
graphEndDate: ?Date,
series: Array<Serie>
};
export default class ProjectActivityGraphs extends React.PureComponent {
props: Props;
state: State;
constructor(props: Props) {
super(props);
const series = generateSeries(
props.measuresHistory,
props.query.graph,
props.metricsType,
getDisplayedHistoryMetrics(props.query.graph, props.query.customMetrics)
);
this.state = { series, ...this.getStateZoomDates(null, props, series) };
this.updateQueryDateRange = debounce(this.updateQueryDateRange, 500);
}
componentWillReceiveProps(nextProps: Props) {
if (
nextProps.measuresHistory !== this.props.measuresHistory ||
historyQueryChanged(this.props.query, nextProps.query)
) {
const series = generateSeries(
nextProps.measuresHistory,
nextProps.query.graph,
nextProps.metricsType,
getDisplayedHistoryMetrics(nextProps.query.graph, nextProps.query.customMetrics)
);
const newDates = this.getStateZoomDates(this.props, nextProps, series);
if (newDates) {
this.setState({ series, ...newDates });
} else {
this.setState({ series });
}
}
}
getStateZoomDates = (props: ?Props, nextProps: Props, series: Array<Serie>) => {
const newDates = { from: nextProps.query.from || null, to: nextProps.query.to || null };
if (props && datesQueryChanged(props.query, nextProps.query)) {
return { graphEndDate: newDates.to, graphStartDate: newDates.from };
}
if (newDates.to == null && newDates.from == null) {
const firstValid = minBy(series.map(serie => serie.data.find(p => p.y || p.y === 0)), 'x');
const lastValid = maxBy(
series.map(serie => findLast(serie.data, p => p.y || p.y === 0)),
'x'
);
return {
graphEndDate: lastValid ? lastValid.x : newDates.to,
graphStartDate: firstValid ? firstValid.x : newDates.from
};
}
if (!props) {
return { graphEndDate: newDates.to, graphStartDate: newDates.from };
}
};
updateSelectedDate = (selectedDate: ?Date) => this.props.updateQuery({ selectedDate });
updateGraphZoom = (graphStartDate: ?Date, graphEndDate: ?Date) => {
if (graphEndDate != null && graphStartDate != null) {
const msDiff = Math.abs(graphEndDate.valueOf() - graphStartDate.valueOf());
// 12 hours minimum between the two dates
if (msDiff < 1000 * 60 * 60 * 12) {
return;
}
}
this.setState({ graphStartDate, graphEndDate });
this.updateQueryDateRange([graphStartDate, graphEndDate]);
};
updateQueryDateRange = (dates: Array<?Date>) => {
if (dates[0] == null || dates[1] == null) {
this.props.updateQuery({ from: dates[0], to: dates[1] });
} else {
const sortedDates = sortBy(dates);
this.props.updateQuery({ from: sortedDates[0], to: sortedDates[1] });
}
};
render() {
const { leakPeriodDate, loading, metricsType, query } = this.props;
const { series } = this.state;
return (
<div className="project-activity-layout-page-main-inner boxed-group boxed-group-inner">
<ProjectActivityGraphsHeader
graph={query.graph}
metrics={this.props.metrics}
selectedMetrics={this.props.query.customMetrics}
updateQuery={this.props.updateQuery}
/>
<GraphsHistory
analyses={this.props.analyses}
eventFilter={query.category}
graph={query.graph}
graphEndDate={this.state.graphEndDate}
graphStartDate={this.state.graphStartDate}
leakPeriodDate={leakPeriodDate}
loading={loading}
measuresHistory={this.props.measuresHistory}
metricsType={metricsType}
project={this.props.project}
selectedDate={this.props.query.selectedDate}
series={series}
updateGraphZoom={this.updateGraphZoom}
updateSelectedDate={this.updateSelectedDate}
/>
<GraphsZoom
graphEndDate={this.state.graphEndDate}
graphStartDate={this.state.graphStartDate}
leakPeriodDate={leakPeriodDate}
loading={loading}
metricsType={metricsType}
series={series}
showAreas={['coverage', 'duplications'].includes(query.graph)}
updateGraphZoom={this.updateGraphZoom}
/>
</div>
);
}
}
|