aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/projects/utils.ts
blob: d12434eceaec4f0b4feba7e72a79961ff17a45c5 (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
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
///
/// 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.
///

/*
 * 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 { sumBy, uniq } from 'lodash';
import { translate } from '../../helpers/l10n';
import { RequestData } from '../../helpers/request';
import { getOrganizations } from '../../api/organizations';
import { searchProjects, Facet } from '../../api/components';
import { getMeasuresForProjects } from '../../api/measures';
import { isDiffMetric, getPeriodValue } from '../../helpers/measures';
import { Query, convertToFilter } from './query';

interface SortingOption {
  class?: string;
  value: string;
}

export const SORTING_METRICS: SortingOption[] = [
  { value: 'name' },
  { value: 'analysis_date' },
  { value: 'reliability' },
  { value: 'security' },
  { value: 'maintainability' },
  { value: 'coverage' },
  { value: 'duplications' },
  { value: 'size' }
];

export const SORTING_LEAK_METRICS: SortingOption[] = [
  { value: 'name' },
  { value: 'analysis_date' },
  { value: 'new_reliability', class: 'projects-leak-sorting-option' },
  { value: 'new_security', class: 'projects-leak-sorting-option' },
  { value: 'new_maintainability', class: 'projects-leak-sorting-option' },
  { value: 'new_coverage', class: 'projects-leak-sorting-option' },
  { value: 'new_duplications', class: 'projects-leak-sorting-option' },
  { value: 'new_lines', class: 'projects-leak-sorting-option' }
];

export const SORTING_SWITCH: { [x: string]: string } = {
  analysis_date: 'analysis_date',
  name: 'name',
  reliability: 'new_reliability',
  security: 'new_security',
  maintainability: 'new_maintainability',
  coverage: 'new_coverage',
  duplications: 'new_duplications',
  size: 'new_lines',
  new_reliability: 'reliability',
  new_security: 'security',
  new_maintainability: 'maintainability',
  new_coverage: 'coverage',
  new_duplications: 'duplications',
  new_lines: 'size'
};

export const VIEWS = ['overall', 'leak'];

export const VISUALIZATIONS = [
  'risk',
  'reliability',
  'security',
  'maintainability',
  'coverage',
  'duplications'
];

const PAGE_SIZE = 50;
const PAGE_SIZE_VISUALIZATIONS = 99;

const METRICS = [
  'alert_status',
  'bugs',
  'reliability_rating',
  'vulnerabilities',
  'security_rating',
  'code_smells',
  'sqale_rating',
  'duplicated_lines_density',
  'coverage',
  'ncloc',
  'ncloc_language_distribution'
];

const LEAK_METRICS = [
  'alert_status',
  'new_bugs',
  'new_reliability_rating',
  'new_vulnerabilities',
  'new_security_rating',
  'new_code_smells',
  'new_maintainability_rating',
  'new_coverage',
  'new_duplicated_lines_density',
  'new_lines'
];

const METRICS_BY_VISUALIZATION: { [x: string]: string[] } = {
  risk: ['reliability_rating', 'security_rating', 'coverage', 'ncloc', 'sqale_index'],
  // x, y, size, color
  reliability: ['ncloc', 'reliability_remediation_effort', 'bugs', 'reliability_rating'],
  security: ['ncloc', 'security_remediation_effort', 'vulnerabilities', 'security_rating'],
  maintainability: ['ncloc', 'sqale_index', 'code_smells', 'sqale_rating'],
  coverage: ['complexity', 'coverage', 'uncovered_lines'],
  duplications: ['ncloc', 'duplicated_lines_density', 'duplicated_blocks']
};

const FACETS = [
  'reliability_rating',
  'security_rating',
  'sqale_rating',
  'coverage',
  'duplicated_lines_density',
  'ncloc',
  'alert_status',
  'languages',
  'tags'
];

const LEAK_FACETS = [
  'new_reliability_rating',
  'new_security_rating',
  'new_maintainability_rating',
  'new_coverage',
  'new_duplicated_lines_density',
  'new_lines',
  'alert_status',
  'languages',
  'tags'
];

const CUMULATIVE_FACETS = [
  'reliability',
  'new_reliability',
  'security',
  'new_security',
  'maintainability',
  'new_maintainability',
  'coverage',
  'new_coverage',
  'duplications',
  'new_duplications',
  'size',
  'new_lines'
];

const REVERSED_FACETS = ['coverage', 'new_coverage'];

export function localizeSorting(sort?: string): string {
  return translate('projects.sort', sort || 'name');
}

export function parseSorting(sort: string): { sortValue: string; sortDesc: boolean } {
  const desc = sort[0] === '-';
  return { sortValue: desc ? sort.substr(1) : sort, sortDesc: desc };
}

export function fetchProjects(
  query: Query,
  isFavorite: boolean,
  organization?: string,
  pageIndex = 1
) {
  const ps = query.view === 'visualizations' ? PAGE_SIZE_VISUALIZATIONS : PAGE_SIZE;
  const data = convertToQueryData(query, isFavorite, organization, {
    p: pageIndex > 1 ? pageIndex : undefined,
    ps,
    facets: defineFacets(query).join(),
    f: 'analysisDate,leakPeriodDate'
  });
  return searchProjects(data).then(({ components, facets, paging }) => {
    return Promise.all([
      fetchProjectMeasures(components, query),
      fetchProjectOrganizations(components)
    ]).then(([measures, organizations]) => {
      return {
        facets: getFacetsMap(facets),
        projects: components
          .map(component => {
            const componentMeasures: { [key: string]: string } = {};
            measures.filter(measure => measure.component === component.key).forEach(measure => {
              const value = isDiffMetric(measure.metric)
                ? getPeriodValue(measure, 1)
                : measure.value;
              if (value !== undefined) {
                componentMeasures[measure.metric] = value;
              }
            });
            return { ...component, measures: componentMeasures };
          })
          .map(component => {
            const organization = organizations.find(o => o.key === component.organization);
            return { ...component, organization };
          }),
        total: paging.total
      };
    });
  });
}

function defineMetrics(query: Query): string[] {
  switch (query.view) {
    case 'visualizations':
      return METRICS_BY_VISUALIZATION[query.visualization || 'risk'];
    case 'leak':
      return LEAK_METRICS;
    default:
      return METRICS;
  }
}

function defineFacets(query: Query): string[] {
  if (query.view === 'leak') {
    return LEAK_FACETS;
  }
  return FACETS;
}

function convertToQueryData(
  query: Query,
  isFavorite: boolean,
  organization?: string,
  defaultData = {}
) {
  const data: RequestData = { ...defaultData, organization };
  const filter = convertToFilter(query, isFavorite);
  const sort = convertToSorting(query as any);

  if (filter) {
    data.filter = filter;
  }
  if (sort.s) {
    data.s = sort.s;
  }
  if (sort.asc !== undefined) {
    data.asc = sort.asc;
  }
  return data;
}

function fetchProjectMeasures(projects: Array<{ key: string }>, query: Query) {
  if (!projects.length) {
    return Promise.resolve([]);
  }

  const projectKeys = projects.map(project => project.key);
  const metrics = defineMetrics(query);
  return getMeasuresForProjects(projectKeys, metrics);
}

function fetchProjectOrganizations(projects: Array<{ organization: string }>) {
  if (!projects.length) {
    return Promise.resolve([]);
  }

  const organizations = uniq(projects.map(project => project.organization));
  return getOrganizations({ organizations: organizations.join() }).then(r => r.organizations);
}

function mapFacetValues(values: Array<{ val: string; count: number }>) {
  const map: { [value: string]: number } = {};
  values.forEach(value => {
    map[value.val] = value.count;
  });
  return map;
}

export function cumulativeMapFacetValues(values: Array<{ val: string; count: number }>) {
  const noDataVal = values.find(value => value.val === 'NO_DATA');
  const filteredValues = noDataVal ? values.filter(value => value.val !== 'NO_DATA') : values;

  let sum = sumBy(filteredValues, value => value.count);
  const map: { [value: string]: number } = {};
  filteredValues.forEach((value, index) => {
    map[value.val] = index > 0 && index < values.length - 1 ? sum : value.count;
    sum -= value.count;
  });

  if (noDataVal) {
    map[noDataVal.val] = noDataVal.count;
  }
  return map;
}

function getFacetsMap(facets: Facet[]) {
  const map: { [property: string]: { [value: string]: number } } = {};
  facets.forEach(facet => {
    const property = mapMetricToProperty(facet.property);
    const { values } = facet;
    if (REVERSED_FACETS.includes(property)) {
      values.reverse();
    }
    map[property] = CUMULATIVE_FACETS.includes(property)
      ? cumulativeMapFacetValues(values)
      : mapFacetValues(values);
  });
  return map;
}

function mapPropertyToMetric(property?: string) {
  const map: { [property: string]: string } = {
    analysis_date: 'analysisDate',
    reliability: 'reliability_rating',
    new_reliability: 'new_reliability_rating',
    security: 'security_rating',
    new_security: 'new_security_rating',
    maintainability: 'sqale_rating',
    new_maintainability: 'new_maintainability_rating',
    coverage: 'coverage',
    new_coverage: 'new_coverage',
    duplications: 'duplicated_lines_density',
    new_duplications: 'new_duplicated_lines_density',
    size: 'ncloc',
    new_lines: 'new_lines',
    gate: 'alert_status',
    languages: 'languages',
    tags: 'tags',
    search: 'query'
  };
  return property && map[property];
}

function convertToSorting({ sort }: Query): { s?: string; asc?: boolean } {
  if (sort && sort[0] === '-') {
    return { s: mapPropertyToMetric(sort.substr(1)), asc: false };
  }
  return { s: mapPropertyToMetric(sort) };
}

function mapMetricToProperty(metricKey: string) {
  const map: { [metric: string]: string } = {
    analysisDate: 'analysis_date',
    reliability_rating: 'reliability',
    new_reliability_rating: 'new_reliability',
    security_rating: 'security',
    new_security_rating: 'new_security',
    sqale_rating: 'maintainability',
    new_maintainability_rating: 'new_maintainability',
    coverage: 'coverage',
    new_coverage: 'new_coverage',
    duplicated_lines_density: 'duplications',
    new_duplicated_lines_density: 'new_duplications',
    ncloc: 'size',
    new_lines: 'new_lines',
    alert_status: 'gate',
    languages: 'languages',
    tags: 'tags',
    query: 'search'
  };
  return map[metricKey];
}