/* * SonarQube * Copyright (C) 2009-2016 SonarSource SA * mailto:contact 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 _ from 'underscore'; import moment from 'moment'; import React, { Component } from 'react'; import SeverityHelper from '../../../components/shared/severity-helper'; import { BarChart } from '../../../components/charts/bar-chart'; import { getFacets, getFacet } from '../../../api/issues'; import { translate } from '../../../helpers/l10n'; import { formatMeasure } from '../../../helpers/measures'; const BASE_QUERY = { resolved: false, assignees: '__me__' }; function getTotalUrl () { return '/account/issues#resolved=false'; } function getToFixUrl () { return '/account/issues#resolved=false|statuses=CONFIRMED'; } function getToReviewUrl () { return '/account/issues#resolved=false|statuses=' + encodeURIComponent('OPEN,REOPENED'); } function getSeverityUrl (severity) { return '/account/issues#resolved=false|severities=' + severity; } function getProjectUrl (project) { return '/account/issues#resolved=false|projectUuids=' + project; } function getPeriodUrl (createdAfter, createdBefore) { return `/account/issues#resolved=false|createdAfter=${createdAfter}|createdBefore=${createdBefore}`; } export default class IssueWidgets extends Component { state = { loading: true }; componentDidMount () { this.fetchIssues(); } fetchIssues () { Promise.all([ this.fetchFacets(), this.fetchByDate() ]).then(responses => { const facets = responses[0]; const byDate = responses[1]; this.setState({ loading: false, total: facets.total, severities: facets.severities, projects: facets.projects, toFix: facets.toFix, toReview: facets.toReview, byDate }); }); } fetchFacets () { return getFacets(BASE_QUERY, ['statuses', 'severities', 'projectUuids']).then(r => { const severities = _.sortBy( _.findWhere(r.facets, { property: 'severities' }).values, (facet) => window.severityComparator(facet.val) ); const projects = _.findWhere(r.facets, { property: 'projectUuids' }).values.map(p => { const base = _.findWhere(r.response.components, { uuid: p.val }); return Object.assign({}, p, base); }); const statuses = _.findWhere(r.facets, { property: 'statuses' }).values; const toFix = _.findWhere(statuses, { val: 'CONFIRMED' }).count; const toReview = _.findWhere(statuses, { val: 'OPEN' }).count + _.findWhere(statuses, { val: 'REOPENED' }).count; const total = r.response.total; return { severities, projects, toFix, toReview, total }; }); } fetchByDate () { return getFacet(Object.assign({ createdInLast: '1w' }, BASE_QUERY), 'createdAt').then(r => r.facet); } handleByDateClick ({ value }) { const created = moment(value); const createdAfter = created.format('YYYY-MM-DD'); const createdBefore = created.add(1, 'days').format('YYYY-MM-DD'); window.location = getPeriodUrl(createdAfter, createdBefore); } renderByDate () { const data = this.state.byDate.map((d, x) => { return { x, y: d.count, value: d.val }; }); const xTicks = this.state.byDate.map(d => moment(d.val).format('dd')); const xValues = this.state.byDate.map(d => d.count); return (

{translate('my_account.issue_widget.leak_last_week')}

); } render () { return (

{translate('my_account.my_issues')}

{this.state.loading && ( )} {!this.state.loading && (
{translate('total')} {formatMeasure(this.state.total, 'SHORT_INT')}
{translate('my_account.to_review')} {formatMeasure(this.state.toReview, 'SHORT_INT')}
{translate('my_account.to_fix')} {formatMeasure(this.state.toFix, 'SHORT_INT')}
)} {!this.state.loading && this.renderByDate()} {!this.state.loading && (

{translate('my_account.issue_widget.by_severity')}

{this.state.severities.map(s => ( ))}
{formatMeasure(s.count, 'SHORT_INT')}
)} {!this.state.loading && (

{translate('my_account.issue_widget.by_project')}

{this.state.projects.map(p => ( ))}
{p.name} {formatMeasure(p.count, 'SHORT_INT')}
)}
); } }