aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/projects/visualizations/Risk.tsx
blob: 8f7d1aedbbc35d9945e88f94dbaf7bcd654b7554 (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
/*
 * 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.
 */
import * as React from 'react';
import { Project } from '../types';
import ColorRatingsLegend from '../../../components/charts/ColorRatingsLegend';
import BubbleChart from '../../../components/charts/BubbleChart';
import { formatMeasure } from '../../../helpers/measures';
import { translate, translateWithParameters } from '../../../helpers/l10n';
import { RATING_COLORS } from '../../../helpers/constants';
import { getProjectUrl } from '../../../helpers/urls';

const X_METRIC = 'sqale_index';
const X_METRIC_TYPE = 'SHORT_WORK_DUR';
const Y_METRIC = 'coverage';
const Y_METRIC_TYPE = 'PERCENT';
const SIZE_METRIC = 'ncloc';
const SIZE_METRIC_TYPE = 'SHORT_INT';
const COLOR_METRIC_1 = 'reliability_rating';
const COLOR_METRIC_2 = 'security_rating';
const COLOR_METRIC_TYPE = 'RATING';

interface Props {
  displayOrganizations: boolean;
  projects: Project[];
}

export default class Risk extends React.PureComponent<Props> {
  getMetricTooltip(metric: { key: string; type: string }, value?: number) {
    const name = translate('metric', metric.key, 'name');
    const formattedValue = value != null ? formatMeasure(value, metric.type) : '–';
    return `<div>${name}: ${formattedValue}</div>`;
  }

  getTooltip(
    project: Project,
    x?: number,
    y?: number,
    size?: number,
    color1?: number,
    color2?: number
  ) {
    const fullProjectName =
      this.props.displayOrganizations && project.organization
        ? `${project.organization.name} / <strong>${project.name}</strong>`
        : `<strong>${project.name}</strong>`;
    const inner = [
      `<div class="little-spacer-bottom">${fullProjectName}</div>`,
      this.getMetricTooltip({ key: COLOR_METRIC_1, type: COLOR_METRIC_TYPE }, color1),
      this.getMetricTooltip({ key: COLOR_METRIC_2, type: COLOR_METRIC_TYPE }, color2),
      this.getMetricTooltip({ key: Y_METRIC, type: Y_METRIC_TYPE }, y),
      this.getMetricTooltip({ key: X_METRIC, type: X_METRIC_TYPE }, x),
      this.getMetricTooltip({ key: SIZE_METRIC, type: SIZE_METRIC_TYPE }, size)
    ].join('');
    return `<div class="text-left">${inner}</div>`;
  }

  render() {
    const items = this.props.projects.map(project => {
      const x = project.measures[X_METRIC] != null ? Number(project.measures[X_METRIC]) : undefined;
      const y = project.measures[Y_METRIC] != null ? Number(project.measures[Y_METRIC]) : undefined;
      const size =
        project.measures[SIZE_METRIC] != null ? Number(project.measures[SIZE_METRIC]) : undefined;
      const color1 =
        project.measures[COLOR_METRIC_1] != null
          ? Number(project.measures[COLOR_METRIC_1])
          : undefined;
      const color2 =
        project.measures[COLOR_METRIC_2] != null
          ? Number(project.measures[COLOR_METRIC_2])
          : undefined;
      return {
        x: x || 0,
        y: y || 0,
        size: size || 0,
        color:
          color1 != null && color2 != null
            ? RATING_COLORS[Math.max(color1, color2) - 1]
            : undefined,
        key: project.key,
        tooltip: this.getTooltip(project, x, y, size, color1, color2),
        link: getProjectUrl(project.key)
      };
    });

    const formatXTick = (tick: number) => formatMeasure(tick, X_METRIC_TYPE);
    const formatYTick = (tick: number) => formatMeasure(tick, Y_METRIC_TYPE);

    return (
      <div>
        <BubbleChart
          formatXTick={formatXTick}
          formatYTick={formatYTick}
          height={600}
          items={items}
          padding={[80, 20, 60, 100]}
          yDomain={[100, 0]}
        />
        <div className="measure-details-bubble-chart-axis x">
          {translate('metric', X_METRIC, 'name')}
        </div>
        <div className="measure-details-bubble-chart-axis y">
          {translate('metric', Y_METRIC, 'name')}
        </div>
        <div className="measure-details-bubble-chart-axis size">
          <span className="spacer-right">
            {translateWithParameters(
              'component_measures.legend.color_x',
              translate('projects.worse_of_reliablity_and_security')
            )}
          </span>
          {translateWithParameters(
            'component_measures.legend.size_x',
            translate('metric', SIZE_METRIC, 'name')
          )}
          <ColorRatingsLegend className="big-spacer-top" />
        </div>
      </div>
    );
  }
}