aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/overview/components/OverviewApp.js
blob: 0271e1ecdc14d0cac635644011d2643924424635 (plain)
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
/*
 * SonarQube
 * Copyright (C) 2009-2018 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 { uniq } from 'lodash';
import QualityGate from '../qualityGate/QualityGate';
import ApplicationQualityGate from '../qualityGate/ApplicationQualityGate';
import BugsAndVulnerabilities from '../main/BugsAndVulnerabilities';
import CodeSmells from '../main/CodeSmells';
import Coverage from '../main/Coverage';
import Duplications from '../main/Duplications';
import Meta from '../meta/Meta';
import throwGlobalError from '../../../app/utils/throwGlobalError';
import { getMeasuresAndMeta } from '../../../api/measures';
import { getAllTimeMachineData } from '../../../api/time-machine';
import { parseDate } from '../../../helpers/dates';
import { enhanceMeasuresWithMetrics } from '../../../helpers/measures';
import { getLeakPeriod } from '../../../helpers/periods';
import { getCustomGraph, getGraph } from '../../../helpers/storage';
import { METRICS, HISTORY_METRICS_LIST } from '../utils';
import { DEFAULT_GRAPH, getDisplayedHistoryMetrics } from '../../projectActivity/utils';
import { getBranchName } from '../../../helpers/branches';
/*:: import type { Component, History, MeasuresList, Period } from '../types'; */
import '../styles.css';

/*::
type Props = {
  branch?: { name: string },
  component: Component,
  onComponentChange: {} => void
};
*/

/*::
type State = {
  history?: History,
  historyStartDate?: Date,
  loading: boolean,
  measures: MeasuresList,
  periods?: Array<Period>
};
*/

export default class OverviewApp extends React.PureComponent {
  /*:: mounted: boolean; */
  /*:: props: Props; */
  state /*: State */ = {
    loading: true,
    measures: []
  };

  componentDidMount() {
    this.mounted = true;
    this.loadMeasures().then(this.loadHistory);
  }

  componentDidUpdate(prevProps /*: Props */) {
    if (
      this.props.component.key !== prevProps.component.key ||
      this.props.branch !== prevProps.branch
    ) {
      this.loadMeasures().then(this.loadHistory);
    }
  }

  componentWillUnmount() {
    this.mounted = false;
  }

  loadMeasures() {
    const { branch, component } = this.props;
    this.setState({ loading: true });

    return getMeasuresAndMeta(component.key, METRICS, {
      additionalFields: 'metrics,periods',
      branch: branch && getBranchName(branch)
    }).then(
      r => {
        if (this.mounted) {
          this.setState({
            loading: false,
            measures: enhanceMeasuresWithMetrics(r.component.measures, r.metrics),
            periods: r.periods
          });
        }
      },
      error => {
        throwGlobalError(error);
        if (this.mounted) {
          this.setState({ loading: false });
        }
      }
    );
  }

  loadHistory = () => {
    const { branch, component } = this.props;

    let graphMetrics = getDisplayedHistoryMetrics(getGraph(), getCustomGraph());
    if (!graphMetrics || graphMetrics.length <= 0) {
      graphMetrics = getDisplayedHistoryMetrics(DEFAULT_GRAPH, []);
    }

    const metrics = uniq(HISTORY_METRICS_LIST.concat(graphMetrics));
    return getAllTimeMachineData(component.key, metrics, {
      branch: branch && getBranchName(branch)
    }).then(r => {
      if (this.mounted) {
        const history /*: History */ = {};
        r.measures.forEach(measure => {
          const measureHistory = measure.history.map(analysis => ({
            date: parseDate(analysis.date),
            value: analysis.value
          }));
          history[measure.metric] = measureHistory;
        });
        const historyStartDate = history[HISTORY_METRICS_LIST[0]][0].date;
        this.setState({ history, historyStartDate });
      }
    }, throwGlobalError);
  };

  getApplicationLeakPeriod = () =>
    this.state.measures.find(measure => measure.metric.key === 'new_bugs') ? { index: 1 } : null;

  renderLoading() {
    return (
      <div className="text-center">
        <i className="spinner spinner-margin" />
      </div>
    );
  }

  render() {
    const { branch, component } = this.props;
    const { loading, measures, periods, history, historyStartDate } = this.state;

    if (loading) {
      return this.renderLoading();
    }

    const leakPeriod =
      component.qualifier === 'APP' ? this.getApplicationLeakPeriod() : getLeakPeriod(periods);
    const branchName = branch && getBranchName(branch);
    const domainProps = {
      branch: branchName,
      component,
      measures,
      leakPeriod,
      history,
      historyStartDate
    };

    return (
      <div className="page page-limited">
        <div className="overview page-with-sidebar">
          <div className="overview-main page-main">
            {component.qualifier === 'APP' ? (
              <ApplicationQualityGate component={component} />
            ) : (
              <QualityGate branch={branchName} component={component} measures={measures} />
            )}

            <div className="overview-domains-list">
              <BugsAndVulnerabilities {...domainProps} />
              <CodeSmells {...domainProps} />
              <Coverage {...domainProps} />
              <Duplications {...domainProps} />
            </div>
          </div>

          <div className="page-sidebar-fixed">
            <Meta
              branch={branchName}
              component={component}
              history={history}
              measures={measures}
              onComponentChange={this.props.onComponentChange}
            />
          </div>
        </div>
      </div>
    );
  }
}