You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.ts 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { chunk, flatMap, groupBy, sortBy } from 'lodash';
  21. import { CCT_SOFTWARE_QUALITY_METRICS } from '../../helpers/constants';
  22. import { getLocalizedMetricName, translate } from '../../helpers/l10n';
  23. import { localizeMetric } from '../../helpers/measures';
  24. import { get, save } from '../../helpers/storage';
  25. import { MetricKey, MetricType } from '../../types/metrics';
  26. import { GraphType, MeasureHistory, ParsedAnalysis, Serie } from '../../types/project-activity';
  27. import { Dict, Metric } from '../../types/types';
  28. export const DEFAULT_GRAPH = GraphType.issues;
  29. const GRAPHS_METRICS_DISPLAYED: Dict<string[]> = {
  30. [GraphType.issues]: [MetricKey.violations],
  31. [GraphType.coverage]: [MetricKey.lines_to_cover, MetricKey.uncovered_lines],
  32. [GraphType.duplications]: [MetricKey.ncloc, MetricKey.duplicated_lines],
  33. };
  34. const GRAPHS_METRICS: Dict<string[]> = {
  35. [GraphType.issues]: GRAPHS_METRICS_DISPLAYED[GraphType.issues].concat([
  36. MetricKey.reliability_rating,
  37. MetricKey.security_rating,
  38. MetricKey.sqale_rating,
  39. ]),
  40. [GraphType.coverage]: [...GRAPHS_METRICS_DISPLAYED[GraphType.coverage], MetricKey.coverage],
  41. [GraphType.duplications]: [
  42. ...GRAPHS_METRICS_DISPLAYED[GraphType.duplications],
  43. MetricKey.duplicated_lines_density,
  44. ],
  45. };
  46. export const LINE_CHART_DASHES = [0, 3, 7];
  47. export function isCustomGraph(graph: GraphType) {
  48. return graph === GraphType.custom;
  49. }
  50. export function getGraphTypes(ignoreCustom = false) {
  51. const graphs = [GraphType.issues, GraphType.coverage, GraphType.duplications];
  52. return ignoreCustom ? graphs : [...graphs, GraphType.custom];
  53. }
  54. export function hasDataValues(serie: Serie) {
  55. return serie.data.some((point) => Boolean(point.y || point.y === 0));
  56. }
  57. export function hasHistoryData(series: Serie[]) {
  58. return series.some((serie) => serie.data && serie.data.length > 1);
  59. }
  60. export function getSeriesMetricType(series: Serie[]) {
  61. return series.length > 0 ? series[0].type : MetricType.Integer;
  62. }
  63. export function getDisplayedHistoryMetrics(graph: GraphType, customMetrics: string[]) {
  64. return isCustomGraph(graph) ? customMetrics : GRAPHS_METRICS_DISPLAYED[graph];
  65. }
  66. export function getHistoryMetrics(graph: GraphType, customMetrics: string[]) {
  67. return isCustomGraph(graph) ? customMetrics : GRAPHS_METRICS[graph];
  68. }
  69. export function hasHistoryDataValue(series: Serie[]) {
  70. return series.some((serie) => serie.data && serie.data.length > 1 && hasDataValues(serie));
  71. }
  72. export function splitSeriesInGraphs(series: Serie[], maxGraph: number, maxSeries: number) {
  73. return flatMap(
  74. groupBy(series, (serie) => serie.type),
  75. (type) => chunk(type, maxSeries),
  76. ).slice(0, maxGraph);
  77. }
  78. export function generateCoveredLinesMetric(
  79. uncoveredLines: MeasureHistory,
  80. measuresHistory: MeasureHistory[],
  81. ): Serie {
  82. const linesToCover = measuresHistory.find(
  83. (measure) => measure.metric === MetricKey.lines_to_cover,
  84. );
  85. return {
  86. data: linesToCover
  87. ? uncoveredLines.history.map((analysis, idx) => ({
  88. x: analysis.date,
  89. y: Number(linesToCover.history[idx].value) - Number(analysis.value),
  90. }))
  91. : [],
  92. name: 'covered_lines',
  93. translatedName: translate('project_activity.custom_metric.covered_lines'),
  94. type: MetricType.Integer,
  95. };
  96. }
  97. export function generateSeries(
  98. measuresHistory: MeasureHistory[],
  99. graph: GraphType,
  100. metrics: Metric[],
  101. displayedMetrics: string[],
  102. ): Serie[] {
  103. if (displayedMetrics.length <= 0 || measuresHistory === undefined) {
  104. return [];
  105. }
  106. return sortBy(
  107. measuresHistory
  108. .filter((measure) => displayedMetrics.indexOf(measure.metric) >= 0)
  109. .map((measure) => {
  110. if (measure.metric === MetricKey.uncovered_lines && !isCustomGraph(graph)) {
  111. return generateCoveredLinesMetric(measure, measuresHistory);
  112. }
  113. const metric = findMetric(measure.metric, metrics);
  114. const isSoftwareQualityMetric = CCT_SOFTWARE_QUALITY_METRICS.includes(
  115. metric?.key as MetricKey,
  116. );
  117. return {
  118. data: measure.history.map((analysis) => {
  119. let { value } = analysis;
  120. if (value !== undefined && isSoftwareQualityMetric) {
  121. value = JSON.parse(value).total;
  122. }
  123. return {
  124. x: analysis.date,
  125. y: metric?.type === MetricType.Level ? value : Number(value),
  126. };
  127. }),
  128. name: measure.metric,
  129. translatedName: metric ? getLocalizedMetricName(metric) : localizeMetric(measure.metric),
  130. type: !metric || isSoftwareQualityMetric ? MetricType.Integer : metric.type,
  131. };
  132. }),
  133. (serie) =>
  134. displayedMetrics.indexOf(
  135. serie.name === 'covered_lines' ? MetricKey.uncovered_lines : serie.name,
  136. ),
  137. );
  138. }
  139. export function saveActivityGraph(
  140. namespace: string,
  141. project: string,
  142. graph: GraphType,
  143. metrics?: string[],
  144. ) {
  145. save(namespace, graph, project);
  146. if (isCustomGraph(graph) && metrics) {
  147. save(`${namespace}.custom`, metrics.join(','), project);
  148. }
  149. }
  150. export function getActivityGraph(
  151. namespace: string,
  152. project: string,
  153. ): { graph: GraphType; customGraphs: string[] } {
  154. const customGraphs = get(`${namespace}.custom`, project);
  155. return {
  156. graph: (get(namespace, project) as GraphType) || DEFAULT_GRAPH,
  157. customGraphs: customGraphs ? customGraphs.split(',') : [],
  158. };
  159. }
  160. export function getAnalysisEventsForDate(analyses: ParsedAnalysis[], date?: Date) {
  161. if (date) {
  162. const analysis = analyses.find((a) => a.date.valueOf() === date.valueOf());
  163. if (analysis) {
  164. return analysis.events;
  165. }
  166. }
  167. return [];
  168. }
  169. function findMetric(key: string, metrics: Metric[]) {
  170. return metrics.find((metric) => metric.key === key);
  171. }