aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/component-measures/utils.js
blob: d822e66e6749c6dc62c265c44fbcfe60247198c8 (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
/*
 * 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 { groupBy, memoize, sortBy, toPairs } from 'lodash';
import { domains } from './config/domains';
import { bubbles } from './config/bubbles';
import { getLocalizedMetricName } from '../../helpers/l10n';
import { cleanQuery, parseAsString, serializeString } from '../../helpers/query';
import { enhanceMeasure } from '../../components/measure/utils';
/*:: import type { Component, ComponentEnhanced, Query } from './types'; */
/*:: import type { RawQuery } from '../../helpers/query'; */
/*:: import type { Metric } from '../../store/metrics/actions'; */
/*:: import type { MeasureEnhanced } from '../../components/measure/types'; */

export const PROJECT_OVERVEW = 'project_overview';
export const DEFAULT_VIEW = 'list';
export const DEFAULT_METRIC = PROJECT_OVERVEW;
export const KNOWN_DOMAINS = [
  'Releasability',
  'Reliability',
  'Security',
  'Maintainability',
  'Coverage',
  'Duplications',
  'Size',
  'Complexity'
];
const BANNED_MEASURES = [
  'blocker_violations',
  'new_blocker_violations',
  'critical_violations',
  'new_critical_violations',
  'major_violations',
  'new_major_violations',
  'minor_violations',
  'new_minor_violations',
  'info_violations',
  'new_info_violations'
];

export function filterMeasures(
  measures /*: Array<MeasureEnhanced> */
) /*: Array<MeasureEnhanced> */ {
  return measures.filter(measure => !BANNED_MEASURES.includes(measure.metric.key));
}

export function sortMeasures(
  domainName /*: string */,
  measures /*: Array<MeasureEnhanced | string> */
) /*: Array<MeasureEnhanced | string> */ {
  const config = domains[domainName] || {};
  const configOrder = config.order || [];
  return sortBy(measures, [
    item => {
      if (typeof item === 'string') {
        return configOrder.indexOf(item);
      }
      const idx = configOrder.indexOf(item.metric.key);
      return idx >= 0 ? idx : configOrder.length;
    },
    item => (typeof item === 'string' ? item : getLocalizedMetricName(item.metric))
  ]);
}

export function addMeasureCategories(
  domainName /*: string */,
  measures /*: Array<MeasureEnhanced> */
) /*: Array<any> */ {
  const categories = domains[domainName] && domains[domainName].categories;
  if (categories && categories.length > 0) {
    return [...categories, ...measures];
  }
  return measures;
}

export function enhanceComponent(
  component /*: Component */,
  metric /*: ?Metric */,
  metrics /*: { [string]: Metric } */
) /*: ComponentEnhanced */ {
  const enhancedMeasures = component.measures.map(measure => enhanceMeasure(measure, metrics));
  // $FlowFixMe metric can't be null since there is a guard for it
  const measure = metric && enhancedMeasures.find(measure => measure.metric.key === metric.key);
  const value = measure ? measure.value : null;
  const leak = measure ? measure.leak : null;
  return { ...component, value, leak, measures: enhancedMeasures };
}

export function isFileType(component /*: Component */) /*: boolean */ {
  return ['FIL', 'UTS'].includes(component.qualifier);
}

export function isViewType(component /*: Component */) /*: boolean */ {
  return ['VW', 'SVW', 'APP'].includes(component.qualifier);
}

export const groupByDomains = memoize((measures /*: Array<MeasureEnhanced> */) => {
  const domains = toPairs(groupBy(measures, measure => measure.metric.domain)).map(r => ({
    name: r[0],
    measures: r[1]
  }));

  return sortBy(domains, [
    domain => {
      const idx = KNOWN_DOMAINS.indexOf(domain.name);
      return idx >= 0 ? idx : KNOWN_DOMAINS.length;
    },
    'name'
  ]);
});

export function getDefaultView(metric /*: string */) /*: string */ {
  if (!hasList(metric)) {
    return 'tree';
  }
  return DEFAULT_VIEW;
}

export function hasList(metric /*: string */) /*: boolean */ {
  return !['releasability_rating', 'releasability_effort'].includes(metric);
}

export function hasTree(metric /*: string */) /*: boolean */ {
  return metric !== 'alert_status';
}

export function hasTreemap(metric /*: string */, type /*: string */) /*: boolean */ {
  return ['PERCENT', 'RATING', 'LEVEL'].includes(type) && hasTree(metric);
}

export function hasBubbleChart(domainName /*: string */) /*: boolean */ {
  return bubbles[domainName] != null;
}

export function hasFacetStat(metric /*: string */) /*: boolean */ {
  return metric !== 'alert_status';
}

export function getBubbleMetrics(domain /*: string */, metrics /*: { [string]: Metric } */) {
  const conf = bubbles[domain];
  return {
    x: metrics[conf.x],
    y: metrics[conf.y],
    size: metrics[conf.size],
    colors: conf.colors ? conf.colors.map(color => metrics[color]) : null
  };
}

export function getBubbleYDomain(domain /*: string */) {
  return bubbles[domain].yDomain;
}

export function isProjectOverview(metric /*: string */) {
  return metric === PROJECT_OVERVEW;
}

const parseView = memoize((rawView /*:: ? */ /*: string */, metric /*: string */) => {
  const view = parseAsString(rawView) || DEFAULT_VIEW;
  if (!hasTree(metric)) {
    return 'list';
  } else if (view === 'list' && !hasList(metric)) {
    return 'tree';
  }
  return view;
});

export const parseQuery = memoize((urlQuery /*: RawQuery */) => {
  const metric = parseAsString(urlQuery['metric']) || DEFAULT_METRIC;
  return {
    metric,
    selected: parseAsString(urlQuery['selected']),
    view: parseView(urlQuery['view'], metric)
  };
});

export const serializeQuery = memoize((query /*: Query */) => {
  return cleanQuery({
    metric: query.metric === DEFAULT_METRIC ? null : serializeString(query.metric),
    selected: serializeString(query.selected),
    view: query.view === DEFAULT_VIEW ? null : serializeString(query.view)
  });
});