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.

WorstProjects.tsx 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 { max, sortBy } from 'lodash';
  21. import * as React from 'react';
  22. import { Link } from 'react-router';
  23. import { colors } from '../../../app/theme';
  24. import BranchIcon from '../../../components/icons/BranchIcon';
  25. import QualifierIcon from '../../../components/icons/QualifierIcon';
  26. import Measure from '../../../components/measure/Measure';
  27. import { translate, translateWithParameters } from '../../../helpers/l10n';
  28. import { formatMeasure } from '../../../helpers/measures';
  29. import { getComponentOverviewUrl } from '../../../helpers/urls';
  30. import { ComponentQualifier } from '../../../types/component';
  31. import { SubComponent } from '../types';
  32. interface Props {
  33. component: string;
  34. subComponents: SubComponent[];
  35. total: number;
  36. }
  37. export default function WorstProjects({ component, subComponents, total }: Props) {
  38. const count = subComponents.length;
  39. if (!count) {
  40. return null;
  41. }
  42. const maxLoc = max(
  43. subComponents.map(component => Number(component.measures['ncloc'] || 0))
  44. ) as number;
  45. const projectsPageUrl = { pathname: '/code', query: { id: component } };
  46. const subCompList = sortBy(
  47. subComponents,
  48. c => c.qualifier,
  49. c => c.name.toLowerCase(),
  50. c => c.branch?.toLowerCase()
  51. );
  52. return (
  53. <div className="panel panel-white portfolio-sub-components" id="portfolio-sub-components">
  54. <table className="data zebra">
  55. <thead>
  56. <tr>
  57. <th>&nbsp;</th>
  58. <th className="text-center portfolio-sub-components-cell">
  59. {translate('metric_domain.Releasability')}
  60. </th>
  61. <th className="text-center portfolio-sub-components-cell">
  62. {translate('metric_domain.Reliability')}
  63. </th>
  64. <th className="text-center portfolio-sub-components-cell">
  65. {translate('portfolio.metric_domain.vulnerabilities')}
  66. </th>
  67. <th className="text-center portfolio-sub-components-cell">
  68. {translate('portfolio.metric_domain.security_hotspots')}
  69. </th>
  70. <th className="text-center portfolio-sub-components-cell">
  71. {translate('metric_domain.Maintainability')}
  72. </th>
  73. <th className="text-center portfolio-sub-components-cell">
  74. {translate('metric.ncloc.name')}
  75. </th>
  76. </tr>
  77. </thead>
  78. <tbody>
  79. {subCompList.map(comp => (
  80. <tr key={[comp.key, comp.branch].filter(s => !!s).join('/')}>
  81. <td>
  82. <span className="display-flex-center">
  83. <QualifierIcon className="spacer-right" qualifier={comp.qualifier} />
  84. <Link
  85. to={getComponentOverviewUrl(comp.refKey || comp.key, comp.qualifier, {
  86. branch: comp.branch
  87. })}>
  88. {comp.name}
  89. </Link>
  90. {[ComponentQualifier.Application, ComponentQualifier.Project].includes(
  91. comp.qualifier as ComponentQualifier
  92. ) &&
  93. (comp.branch ? (
  94. <span className="spacer-left">
  95. <BranchIcon className="little-spacer-right" />
  96. <span className="note">{comp.branch}</span>
  97. </span>
  98. ) : (
  99. <span className="spacer-left badge">{translate('branches.main_branch')}</span>
  100. ))}
  101. </span>
  102. </td>
  103. {comp.qualifier === ComponentQualifier.Project
  104. ? renderCell(comp.measures, 'alert_status', 'LEVEL')
  105. : renderCell(comp.measures, 'releasability_rating', 'RATING')}
  106. {renderCell(comp.measures, 'reliability_rating', 'RATING')}
  107. {renderCell(comp.measures, 'security_rating', 'RATING')}
  108. {renderCell(comp.measures, 'security_review_rating', 'RATING')}
  109. {renderCell(comp.measures, 'sqale_rating', 'RATING')}
  110. {renderNcloc(comp.measures, maxLoc)}
  111. </tr>
  112. ))}
  113. </tbody>
  114. </table>
  115. {total > count && (
  116. <footer className="spacer-top note text-center">
  117. {translateWithParameters(
  118. 'x_of_y_shown',
  119. formatMeasure(count, 'INT'),
  120. formatMeasure(total, 'INT')
  121. )}
  122. <Link className="spacer-left" to={projectsPageUrl}>
  123. {translate('show_more')}
  124. </Link>
  125. </footer>
  126. )}
  127. </div>
  128. );
  129. }
  130. function renderCell(measures: T.Dict<string | undefined>, metric: string, type: string) {
  131. return (
  132. <td className="text-center">
  133. <Measure metricKey={metric} metricType={type} value={measures[metric]} />
  134. </td>
  135. );
  136. }
  137. function renderNcloc(measures: T.Dict<string | undefined>, maxLoc: number) {
  138. const ncloc = Number(measures['ncloc'] || 0);
  139. const barWidth = maxLoc > 0 ? Math.max(1, Math.round((ncloc / maxLoc) * 50)) : 0;
  140. return (
  141. <td className="text-right">
  142. <span className="note">
  143. <Measure metricKey="ncloc" metricType="SHORT_INT" value={measures['ncloc']} />
  144. </span>
  145. {maxLoc > 0 && (
  146. <svg className="spacer-left" height="16" width="50">
  147. <rect
  148. className="bar-chart-bar"
  149. fill={colors.blue}
  150. height="10"
  151. width={barWidth}
  152. x="0"
  153. y="3"
  154. />
  155. </svg>
  156. )}
  157. </td>
  158. );
  159. }