aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/helpers/measures.ts
blob: 51c3047c2c73db5fca00aead8c81358228b62a21 (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
/*
 * SonarQube
 * Copyright (C) 2009-2021 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 { translate, translateWithParameters } from '../sonar-ui-common/helpers/l10n';
import { formatMeasure } from '../sonar-ui-common/helpers/measures';
import { isDefined } from '../sonar-ui-common/helpers/types';
import { MetricKey } from '../types/metrics';
import {
  QualityGateStatusCondition,
  QualityGateStatusConditionEnhanced
} from '../types/quality-gates';

/** Return a localized metric name */
export function localizeMetric(metricKey: MetricKey | string): string {
  return translate('metric', metricKey, 'name');
}

/** Return corresponding "short" for better display in UI */
export function getShortType(type: string): string {
  if (type === 'INT') {
    return 'SHORT_INT';
  } else if (type === 'WORK_DUR') {
    return 'SHORT_WORK_DUR';
  }
  return type;
}

export function enhanceMeasuresWithMetrics(
  measures: T.Measure[],
  metrics: T.Metric[]
): T.MeasureEnhanced[] {
  return measures
    .map(measure => {
      const metric = metrics.find(metric => metric.key === measure.metric);
      return metric && { ...measure, metric };
    })
    .filter(isDefined);
}

export function enhanceConditionWithMeasure(
  condition: QualityGateStatusCondition,
  measures: T.MeasureEnhanced[]
): QualityGateStatusConditionEnhanced | undefined {
  const measure = measures.find(m => m.metric.key === condition.metric);

  // Make sure we have a period index. This is necessary when dealing with
  // applications.
  let { period } = condition;
  if (measure && measure.period && !period) {
    period = measure.period.index;
  }

  return measure && { ...condition, period, measure };
}

export function isPeriodBestValue(measure: T.Measure | T.MeasureEnhanced): boolean {
  return measure.period?.bestValue || false;
}

/** Check if metric is differential */
export function isDiffMetric(metricKey: MetricKey | string): boolean {
  return metricKey.indexOf('new_') === 0;
}

function getRatingGrid(): string {
  // workaround cyclic dependencies
  const getStore = require('../app/utils/getStore').default;
  const { getGlobalSettingValue } = require('../store/rootReducer');

  const store = getStore();
  const settingValue = getGlobalSettingValue(store.getState(), 'sonar.technicalDebt.ratingGrid');
  return settingValue ? settingValue.value : '';
}

let maintainabilityRatingGrid: number[];

function getMaintainabilityRatingGrid(): number[] {
  if (maintainabilityRatingGrid) {
    return maintainabilityRatingGrid;
  }

  const str = getRatingGrid();
  const numbers = str
    .split(',')
    .map(s => parseFloat(s))
    .filter(n => !isNaN(n));

  if (numbers.length === 4) {
    maintainabilityRatingGrid = numbers;
  } else {
    maintainabilityRatingGrid = [0, 0, 0, 0];
  }

  return maintainabilityRatingGrid;
}

function getMaintainabilityRatingTooltip(rating: number): string {
  const maintainabilityGrid = getMaintainabilityRatingGrid();
  const maintainabilityRatingThreshold = maintainabilityGrid[Math.floor(rating) - 2];

  if (rating < 2) {
    return translateWithParameters(
      'metric.sqale_rating.tooltip.A',
      formatMeasure(maintainabilityGrid[0] * 100, 'PERCENT')
    );
  }

  const ratingLetter = formatMeasure(rating, 'RATING');

  return translateWithParameters(
    'metric.sqale_rating.tooltip',
    ratingLetter,
    formatMeasure(maintainabilityRatingThreshold * 100, 'PERCENT')
  );
}

export function getRatingTooltip(metricKey: MetricKey | string, value: number | string): string {
  const ratingLetter = formatMeasure(value, 'RATING');

  const finalMetricKey = isDiffMetric(metricKey) ? metricKey.substr(4) : metricKey;

  return finalMetricKey === 'sqale_rating' || finalMetricKey === 'maintainability_rating'
    ? getMaintainabilityRatingTooltip(Number(value))
    : translate('metric', finalMetricKey, 'tooltip', ratingLetter);
}

export function getDisplayMetrics(metrics: T.Metric[]) {
  return metrics.filter(metric => !metric.hidden && !['DATA', 'DISTRIB'].includes(metric.type));
}

export function findMeasure(measures: T.MeasureEnhanced[], metric: MetricKey | string) {
  return measures.find(measure => measure.metric.key === metric);
}