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.

ratings.ts 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. const RATING_GRID_SIZE = 4;
  21. export const PERCENT_MULTIPLIER = 100;
  22. export const DIFF_METRIC_PREFIX_LENGTH = 4;
  23. export const GRID_INDEX_OFFSET = 2; // Rating of 2 should get index 0 (threshold between 1 and 2)
  24. function checkNumberRating(coverageRating: number): void {
  25. if (!(typeof coverageRating === 'number' && coverageRating > 0 && coverageRating < 6)) {
  26. throw new Error(`Unknown number rating: "${coverageRating}"`);
  27. }
  28. }
  29. export function getCoverageRatingLabel(rating: number): string {
  30. checkNumberRating(rating);
  31. const mapping = ['≥ 80%', '70% - 80%', '50% - 70%', '30% - 50%', '< 30%'];
  32. return mapping[rating - 1];
  33. }
  34. export function getCoverageRatingAverageValue(rating: number): number {
  35. checkNumberRating(rating);
  36. const mapping = [90, 75, 60, 40, 15];
  37. return mapping[rating - 1];
  38. }
  39. export function getDuplicationsRatingLabel(rating: number): string {
  40. checkNumberRating(rating);
  41. const mapping = ['< 3%', '3% - 5%', '5% - 10%', '10% - 20%', '> 20%'];
  42. return mapping[rating - 1];
  43. }
  44. export function getSizeRatingLabel(rating: number): string {
  45. checkNumberRating(rating);
  46. const mapping = ['< 1k', '1k - 10k', '10k - 100k', '100k - 500k', '> 500k'];
  47. return mapping[rating - 1];
  48. }
  49. export function getSizeRatingAverageValue(rating: number): number {
  50. checkNumberRating(rating);
  51. const mapping = [500, 5000, 50000, 250000, 750000];
  52. return mapping[rating - 1];
  53. }
  54. export const getMaintainabilityGrid = (ratingGridSetting: string) => {
  55. const numbers = ratingGridSetting
  56. .split(',')
  57. .map((s) => parseFloat(s))
  58. .filter((n) => !isNaN(n));
  59. return numbers.length === RATING_GRID_SIZE ? numbers : [0, 0, 0, 0];
  60. };
  61. const DUPLICATION_RATINGS: ['A', 'B', 'C', 'D', 'E', 'F'] = ['A', 'B', 'C', 'D', 'E', 'F'];
  62. export function duplicationValueToRating(val: number) {
  63. return DUPLICATION_RATINGS[val - 1];
  64. }