123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- /*
- * SonarQube
- * Copyright (C) 2009-2019 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 * as React from 'react';
- import { connect } from 'react-redux';
- import { Link } from 'react-router';
- import MeasuresButtonLink from './MeasuresButtonLink';
- import MetricBox from './MetricBox';
- import Report from './Report';
- import WorstProjects from './WorstProjects';
- import { SubComponent } from '../types';
- import { PORTFOLIO_METRICS, SUB_COMPONENTS_METRICS, convertMeasures } from '../utils';
- import Measure from '../../../components/measure/Measure';
- import { getChildren } from '../../../api/components';
- import { getMeasures } from '../../../api/measures';
- import { translate } from '../../../helpers/l10n';
- import { getComponentDrilldownUrl } from '../../../helpers/urls';
- import { fetchMetrics } from '../../../store/rootActions';
- import { getMetrics, Store } from '../../../store/rootReducer';
- import '../styles.css';
-
- interface OwnProps {
- component: T.Component;
- }
-
- interface StateToProps {
- metrics: T.Dict<T.Metric>;
- }
-
- interface DispatchToProps {
- fetchMetrics: () => void;
- }
-
- type Props = StateToProps & DispatchToProps & OwnProps;
-
- interface State {
- loading: boolean;
- measures?: T.Dict<string | undefined>;
- subComponents?: SubComponent[];
- totalSubComponents?: number;
- }
-
- export class App extends React.PureComponent<Props, State> {
- mounted = false;
- state: State = { loading: true };
-
- componentDidMount() {
- this.mounted = true;
- this.props.fetchMetrics();
- this.fetchData();
- }
-
- componentDidUpdate(prevProps: Props) {
- if (prevProps.component !== this.props.component) {
- this.fetchData();
- }
- }
-
- componentWillUnmount() {
- this.mounted = false;
- }
-
- fetchData() {
- this.setState({ loading: true });
- Promise.all([
- getMeasures({ component: this.props.component.key, metricKeys: PORTFOLIO_METRICS.join() }),
- getChildren(this.props.component.key, SUB_COMPONENTS_METRICS, { ps: 20, s: 'qualifier' })
- ]).then(
- ([measures, subComponents]) => {
- if (this.mounted) {
- this.setState({
- loading: false,
- measures: convertMeasures(measures),
- subComponents: subComponents.components.map((component: any) => ({
- ...component,
- measures: convertMeasures(component.measures)
- })),
- totalSubComponents: subComponents.paging.total
- });
- }
- },
- () => {
- if (this.mounted) {
- this.setState({ loading: false });
- }
- }
- );
- }
-
- isEmpty = () => this.state.measures === undefined || this.state.measures['ncloc'] === undefined;
-
- isNotComputed = () =>
- this.state.measures && this.state.measures['reliability_rating'] === undefined;
-
- renderSpinner() {
- return (
- <div className="page page-limited">
- <div className="text-center">
- <i className="spinner spinner-margin" />
- </div>
- </div>
- );
- }
-
- renderEmpty() {
- return (
- <div className="empty-search">
- <h3>
- {!this.state.measures || !this.state.measures['projects']
- ? translate('portfolio.empty')
- : translate('portfolio.no_lines_of_code')}
- </h3>
- </div>
- );
- }
-
- renderWhenNotComputed() {
- return (
- <div className="empty-search">
- <h3>{translate('portfolio.not_computed')}</h3>
- </div>
- );
- }
-
- render() {
- const { component } = this.props;
- const { loading, measures, subComponents, totalSubComponents } = this.state;
-
- if (loading) {
- return this.renderSpinner();
- }
-
- if (this.isEmpty()) {
- return this.renderEmpty();
- }
-
- if (this.isNotComputed()) {
- return this.renderWhenNotComputed();
- }
-
- return (
- <div className="page page-limited portfolio-overview">
- <div className="page-actions">
- <Report component={component} />
- </div>
- <h1>{translate('portfolio.health_factors')}</h1>
- <div className="portfolio-boxes">
- <MetricBox component={component.key} measures={measures!} metricKey="releasability" />
- <MetricBox component={component.key} measures={measures!} metricKey="reliability" />
- <MetricBox component={component.key} measures={measures!} metricKey="vulnerabilities" />
- <MetricBox component={component.key} measures={measures!} metricKey="security_hotspots" />
- <MetricBox component={component.key} measures={measures!} metricKey="maintainability" />
- </div>
-
- <h1>{translate('portfolio.breakdown')}</h1>
- <div className="portfolio-breakdown">
- <div className="portfolio-breakdown-box">
- <h2>{translate('portfolio.number_of_projects')}</h2>
- <div className="portfolio-breakdown-metric">
- <Measure
- metricKey="projects"
- metricType="SHORT_INT"
- value={(measures && measures.projects) || '0'}
- />
- </div>
- <div className="portfolio-breakdown-box-link">
- <div>
- <MeasuresButtonLink component={component.key} metric="projects" />
- </div>
- </div>
- </div>
- <div className="portfolio-breakdown-box">
- <h2>{translate('portfolio.number_of_lines')}</h2>
- <div className="portfolio-breakdown-metric">
- <Measure
- metricKey="ncloc"
- metricType="SHORT_INT"
- value={(measures && measures.ncloc) || '0'}
- />
- </div>
- <div className="portfolio-breakdown-box-link">
- <div>
- <Link
- to={getComponentDrilldownUrl({ componentKey: component.key, metric: 'ncloc' })}>
- <span>{translate('portfolio.language_breakdown_link')}</span>
- </Link>
- </div>
- </div>
- </div>
- </div>
-
- {subComponents !== undefined && totalSubComponents !== undefined && (
- <WorstProjects
- component={component.key}
- subComponents={subComponents}
- total={totalSubComponents}
- />
- )}
- </div>
- );
- }
- }
-
- const mapDispatchToProps: DispatchToProps = { fetchMetrics };
-
- const mapStateToProps = (state: Store): StateToProps => ({
- metrics: getMetrics(state)
- });
-
- export default connect(
- mapStateToProps,
- mapDispatchToProps
- )(App);
|