aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/overview/components/domain-treemap.js
blob: d2db3d954d4356c41cd795096406de1180e5ea95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import _ from 'underscore';
import React from 'react';

import { Treemap } from '../../../components/charts/treemap';
import { getChildren } from '../../../api/components';
import { formatMeasure } from '../../../helpers/measures';
import { getComponentUrl } from '../../../helpers/urls';


const HEIGHT = 302;


export class DomainTreemap extends React.Component {
  constructor (props) {
    super(props);
    this.state = {
      loading: true,
      files: [],
      sizeMetric: this.getMetricObject(props.metrics, props.sizeMetric),
      colorMetric: props.colorMetric ? this.getMetricObject(props.metrics, props.colorMetric) : null,
      breadcrumbs: []
    };
  }

  componentDidMount () {
    this.requestComponents(this.props.component.key);
  }

  requestComponents (componentKey) {
    let metrics = [this.props.sizeMetric, this.props.colorMetric];
    return getChildren(componentKey, metrics).then(r => {
      let components = r.map(component => {
        let measures = {};
        (component.msr || []).forEach(measure => {
          measures[measure.key] = measure.val;
        });
        return _.extend(component, {
          measures,
          key: component.copy ? `${component.copy}` : component.key
        });
      });
      this.setState({ loading: false, components });
    });
  }

  getMetricObject (metrics, metricKey) {
    return _.findWhere(metrics, { key: metricKey });
  }

  getTooltip (component) {
    let inner = [
      component.name,
      `${this.state.sizeMetric.name}:
      ${formatMeasure(component.measures[this.props.sizeMetric], this.state.sizeMetric.type)}`
    ];
    if (this.state.colorMetric) {
      let measure = component.measures[this.props.colorMetric],
          formatted = measure != null ? formatMeasure(measure, this.state.colorMetric.type) : '—';
      inner.push(`${this.state.colorMetric.name}: ${formatted}`);
    }
    inner = inner.join('<br>');
    return `<div class="text-left">${inner}</div>`;
  }

  handleRectangleClick (node) {
    this.requestComponents(node.key).then(() => {
      let nextBreadcrumbs = [...this.state.breadcrumbs];
      let index = _.findIndex(this.state.breadcrumbs, b => b.key === node.key);
      if (index !== -1) {
        nextBreadcrumbs = nextBreadcrumbs.slice(0, index);
      }
      nextBreadcrumbs = [...nextBreadcrumbs, {
        key: node.key,
        name: node.name,
        qualifier: node.qualifier
      }];
      this.setState({ breadcrumbs: nextBreadcrumbs });
    });
  }

  handleReset() {
    this.requestComponents(this.props.component.key).then(() => {
      this.setState({ breadcrumbs: [] });
    });
  }

  renderLoading () {
    return <div className="overview-chart-placeholder" style={{ height: HEIGHT }}>
      <i className="spinner"/>
    </div>;
  }

  renderTreemap () {
    if (this.state.loading) {
      return this.renderLoading();
    }

    // TODO filter out zero sized components
    let items = this.state.components.map(component => {
      let colorMeasure = this.props.colorMetric ? component.measures[this.props.colorMetric] : null;
      return {
        key: component.key,
        name: component.name,
        qualifier: component.qualifier,
        size: component.measures[this.props.sizeMetric],
        color: colorMeasure != null ? this.props.scale(colorMeasure) : '#777',
        tooltip: this.getTooltip(component),
        label: component.name,
        link: getComponentUrl(component.key)
      };
    });

    const canBeClicked = node => node.qualifier !== 'FIL' && node.qualifier !== 'UTS';

    return <Treemap
        items={items}
        breadcrumbs={this.state.breadcrumbs}
        height={HEIGHT}
        canBeClicked={canBeClicked}
        onRectangleClick={this.handleRectangleClick.bind(this)}
        onReset={this.handleReset.bind(this)}/>;
  }

  render () {
    let color = this.props.colorMetric ?
        <li>{window.tp('overview.chart.legend.color_x', this.state.colorMetric.name)}</li> : null;
    return <div className="overview-domain-chart">
      <div className="overview-card-header">
        <h2 className="overview-title">{window.t('overview.chart.components')}</h2>
        <ul className="list-inline small">
          <li>
            {window.tp('overview.chart.legend.size_x', this.state.sizeMetric.name)}
          </li>
          {color}
        </ul>
      </div>
      <div className="overview-treemap">
        {this.renderTreemap()}
      </div>
    </div>;
  }
}

DomainTreemap.propTypes = {
  sizeMetric: React.PropTypes.string.isRequired,
  colorMetric: React.PropTypes.string,
  scale: React.PropTypes.func
};