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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/*
* SonarQube
* Copyright (C) 2009-2020 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.
*/
import { sortBy, uniq } from 'lodash';
import * as React from 'react';
import { parseDate, toNotSoISOString } from 'sonar-ui-common/helpers/dates';
import { isDefined } from 'sonar-ui-common/helpers/types';
import { getApplicationLeak } from '../../../api/application';
import { getMeasuresWithPeriodAndMetrics } from '../../../api/measures';
import { getProjectActivity } from '../../../api/projectActivity';
import { getApplicationQualityGate, getQualityGateProjectStatus } from '../../../api/quality-gates';
import { getTimeMachineData } from '../../../api/time-machine';
import {
getActivityGraph,
getHistoryMetrics,
saveActivityGraph
} from '../../../components/activity-graph/utils';
import {
getBranchLikeDisplayName,
getBranchLikeQuery,
isSameBranchLike
} from '../../../helpers/branch-like';
import { enhanceConditionWithMeasure, enhanceMeasuresWithMetrics } from '../../../helpers/measures';
import {
extractStatusConditionsFromApplicationStatusChildProject,
extractStatusConditionsFromProjectStatus
} from '../../../helpers/qualityGates';
import { ApplicationPeriod } from '../../../types/application';
import { BranchLike } from '../../../types/branch-like';
import { ComponentQualifier } from '../../../types/component';
import { MetricKey } from '../../../types/metrics';
import { GraphType, MeasureHistory } from '../../../types/project-activity';
import { QualityGateStatus, QualityGateStatusCondition } from '../../../types/quality-gates';
import '../styles.css';
import { HISTORY_METRICS_LIST, METRICS } from '../utils';
import BranchOverviewRenderer from './BranchOverviewRenderer';
interface Props {
branchLike?: BranchLike;
component: T.Component;
}
interface State {
analyses?: T.Analysis[];
appLeak?: ApplicationPeriod;
graph: GraphType;
loadingHistory?: boolean;
loadingStatus?: boolean;
measures?: T.MeasureEnhanced[];
measuresHistory?: MeasureHistory[];
metrics?: T.Metric[];
period?: T.Period;
qgStatuses?: QualityGateStatus[];
}
export const BRANCH_OVERVIEW_ACTIVITY_GRAPH = 'sonar_branch_overview.graph';
// Get all history data over the past year.
const FROM_DATE = toNotSoISOString(new Date().setFullYear(new Date().getFullYear() - 1));
export default class BranchOverview extends React.PureComponent<Props, State> {
mounted = false;
state: State;
constructor(props: Props) {
super(props);
const { graph } = getActivityGraph(BRANCH_OVERVIEW_ACTIVITY_GRAPH, props.component.key);
this.state = { graph };
}
componentDidMount() {
this.mounted = true;
this.loadStatus();
this.loadHistory();
}
componentDidUpdate(prevProps: Props) {
if (
this.props.component.key !== prevProps.component.key ||
!isSameBranchLike(this.props.branchLike, prevProps.branchLike)
) {
this.loadStatus();
this.loadHistory();
}
}
componentWillUnmount() {
this.mounted = false;
}
loadStatus = () => {
if (this.props.component.qualifier === ComponentQualifier.Application) {
this.loadApplicationStatus();
} else {
this.loadProjectStatus();
}
};
loadApplicationStatus = async () => {
const { branchLike, component } = this.props;
this.setState({ loadingStatus: true });
// Start by loading the application quality gate info, as well as the meta
// data for the application as a whole.
const appStatus = await getApplicationQualityGate({
application: component.key,
...getBranchLikeQuery(branchLike)
});
const { measures: appMeasures, metrics, period } = await this.loadMeasuresAndMeta(
component.key
);
// We also need to load the application leak periods separately.
getApplicationLeak(component.key, branchLike && getBranchLikeDisplayName(branchLike)).then(
leaks => {
if (this.mounted && leaks && leaks.length) {
const sortedLeaks = sortBy(leaks, leak => {
return new Date(leak.date);
});
this.setState({
appLeak: sortedLeaks[0]
});
}
},
() => {
if (this.mounted) {
this.setState({ appLeak: undefined });
}
}
);
// We need to load the measures for each project in an application
// individually, in order to display all QG conditions correctly. Loading
// them at the parent application level will not get all the necessary
// information, unfortunately, as they are aggregated.
Promise.all(
appStatus.projects.map(project => {
return this.loadMeasuresAndMeta(
project.key,
// Only load metrics that apply to failing QG conditions; we don't
// need the others anyway.
project.conditions.filter(c => c.status !== 'OK').map(c => c.metric)
).then(({ measures }) => ({
measures,
project
}));
})
).then(
results => {
if (this.mounted) {
const qgStatuses = results.map(({ measures = [], project }) => {
const { key, name, status } = project;
const conditions = extractStatusConditionsFromApplicationStatusChildProject(project);
const failedConditions = this.getFailedConditions(conditions, measures);
return {
failedConditions,
key,
name,
status
};
});
this.setState({
loadingStatus: false,
measures: appMeasures,
metrics,
period,
qgStatuses
});
}
},
() => {
if (this.mounted) {
this.setState({ loadingStatus: false, qgStatuses: undefined });
}
}
);
};
loadProjectStatus = async () => {
const {
branchLike,
component: { key, name }
} = this.props;
this.setState({ loadingStatus: true });
const projectStatus = await getQualityGateProjectStatus({
projectKey: key,
...getBranchLikeQuery(branchLike)
});
// Get failing condition metric keys. We need measures for them as well to
// render them.
const metricKeys =
projectStatus.conditions !== undefined
? uniq([...METRICS, ...projectStatus.conditions.map(c => c.metricKey)])
: METRICS;
this.loadMeasuresAndMeta(key, metricKeys).then(
({ measures, metrics, period }) => {
if (this.mounted && measures) {
const { ignoredConditions, status } = projectStatus;
const conditions = extractStatusConditionsFromProjectStatus(projectStatus);
const failedConditions = this.getFailedConditions(conditions, measures);
const qgStatus = {
ignoredConditions,
failedConditions,
key,
name,
status
};
this.setState({
loadingStatus: false,
measures,
metrics,
period,
qgStatuses: [qgStatus]
});
} else if (this.mounted) {
this.setState({ loadingStatus: false, qgStatuses: undefined });
}
},
() => {
if (this.mounted) {
this.setState({ loadingStatus: false, qgStatuses: undefined });
}
}
);
};
loadMeasuresAndMeta = (componentKey: string, metricKeys: string[] = []) => {
const { branchLike } = this.props;
return getMeasuresWithPeriodAndMetrics(
componentKey,
metricKeys.length > 0 ? metricKeys : METRICS,
getBranchLikeQuery(branchLike)
).then(({ component: { measures }, metrics, period }) => {
return {
measures: enhanceMeasuresWithMetrics(measures || [], metrics || []),
metrics,
period
};
});
};
loadHistory = () => {
this.setState({ loadingHistory: true });
return Promise.all([this.loadHistoryMeasures(), this.loadAnalyses()]).then(
this.doneLoadingHistory,
this.doneLoadingHistory
);
};
loadHistoryMeasures = () => {
const { branchLike, component } = this.props;
const { graph } = this.state;
const graphMetrics = getHistoryMetrics(graph, []);
const metrics = uniq([...HISTORY_METRICS_LIST, ...graphMetrics]);
return getTimeMachineData({
...getBranchLikeQuery(branchLike),
from: FROM_DATE,
component: component.key,
metrics: metrics.join()
}).then(
({ measures }) => {
if (this.mounted) {
this.setState({
measuresHistory: measures.map(measure => ({
metric: measure.metric,
history: measure.history.map(analysis => ({
date: parseDate(analysis.date),
value: analysis.value
}))
}))
});
}
},
() => {}
);
};
loadAnalyses = () => {
const { branchLike } = this.props;
return getProjectActivity({
...getBranchLikeQuery(branchLike),
project: this.getTopLevelComponent(),
from: FROM_DATE
}).then(
({ analyses }) => {
if (this.mounted) {
this.setState({
analyses
});
}
},
() => {}
);
};
getFailedConditions = (
conditions: QualityGateStatusCondition[],
measures: T.MeasureEnhanced[]
) => {
return (
conditions
.filter(c => c.level !== 'OK')
// Enhance them with Metric information, which will be needed
// to render the conditions properly.
.map(c => enhanceConditionWithMeasure(c, measures))
// The enhancement will return undefined if it cannot find the
// appropriate measure. Make sure we filter them out.
.filter(isDefined)
);
};
getTopLevelComponent = () => {
const { component } = this.props;
let current = component.breadcrumbs.length - 1;
while (
current > 0 &&
!([
ComponentQualifier.Project,
ComponentQualifier.Portfolio,
ComponentQualifier.Application
] as string[]).includes(component.breadcrumbs[current].qualifier)
) {
current--;
}
return component.breadcrumbs[current].key;
};
doneLoadingHistory = () => {
if (this.mounted) {
this.setState({
loadingHistory: false
});
}
};
handleGraphChange = (graph: GraphType) => {
const { component } = this.props;
saveActivityGraph(BRANCH_OVERVIEW_ACTIVITY_GRAPH, component.key, graph);
this.setState({ graph, loadingHistory: true }, () => {
this.loadHistoryMeasures().then(this.doneLoadingHistory, this.doneLoadingHistory);
});
};
render() {
const { branchLike, component } = this.props;
const {
analyses,
appLeak,
graph,
loadingStatus,
loadingHistory,
measures,
measuresHistory,
metrics,
period,
qgStatuses
} = this.state;
const leakPeriod = component.qualifier === ComponentQualifier.Application ? appLeak : period;
const projectIsEmpty =
loadingStatus === false &&
(measures === undefined ||
measures.find(measure =>
([MetricKey.lines, MetricKey.new_lines] as string[]).includes(measure.metric.key)
) === undefined);
return (
<BranchOverviewRenderer
analyses={analyses}
branchLike={branchLike}
component={component}
graph={graph}
leakPeriod={leakPeriod}
loadingHistory={loadingHistory}
loadingStatus={loadingStatus}
measures={measures}
measuresHistory={measuresHistory}
metrics={metrics}
onGraphChange={this.handleGraphChange}
projectIsEmpty={projectIsEmpty}
qgStatuses={qgStatuses}
/>
);
}
}
|