all files / src/visualizations/ time_table.jsx

18.27% Statements 19/104
0% Branches 0/75
0% Functions 0/15
18.81% Lines 19/101
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240                                                                                                                                                                                                                                                                                                                                                                                                                                                          
import ReactDOM from 'react-dom';
import React from 'react';
import propTypes from 'prop-types';
import { Table, Thead, Th, Tr, Td } from 'reactable';
import d3 from 'd3';
import Mustache from 'mustache';
import { Sparkline, LineSeries, PointSeries, VerticalReferenceLine, WithTooltip } from '@data-ui/sparkline';
 
import MetricOption from '../components/MetricOption';
import { d3format } from '../modules/utils';
import { formatDateThunk } from '../modules/dates';
import InfoTooltipWithTrigger from '../components/InfoTooltipWithTrigger';
import './time_table.css';
 
const SPARKLINE_MARGIN = {
  top: 8,
  right: 8,
  bottom: 8,
  left: 8,
};
const sparklineTooltipProps = {
  style: {
    opacity: 0.8,
  },
  offsetTop: 0,
};
 
const ACCESSIBLE_COLOR_BOUNDS = ['#ca0020', '#0571b0'];
 
function FormattedNumber({ num, format }) {
  if (format) {
    return (
      <span title={num}>{d3format(format, num)}</span>
    );
  }
  return <span>{num}</span>;
}
 
FormattedNumber.propTypes = {
  num: propTypes.number,
  format: propTypes.string,
};
 
function viz(slice, payload) {
  slice.container.css('height', slice.height());
  const records = payload.data.records;
  const fd = payload.form_data;
  const data = Object.keys(records).sort().map(iso => ({ ...records[iso], iso }));
  const reversedData = [...data].reverse();
  const metricMap = {};
  slice.datasource.metrics.forEach((m) => {
    metricMap[m.metric_name] = m;
  });
 
  let metrics;
  let defaultSort = false;
  if (payload.data.is_group_by) {
    metrics = payload.data.columns;
    defaultSort = { column: fd.column_collection[0].key, direction: 'desc' };
  } else {
    metrics = fd.metrics;
  }
  const tableData = metrics.map((metric) => {
    let leftCell;
    const context = { ...fd, metric };
    const url = fd.url ? Mustache.render(fd.url, context) : null;
    const metricLabel = metric.label || metric;
    const metricData = typeof metric === 'object' ? metric : metricMap[metric];
    if (!payload.data.is_group_by) {
      leftCell = (
        <MetricOption metric={metricData} url={url} showFormula={false} openInNewWindow />
      );
    } else {
      leftCell = url ? <a href={url} target="_blank">{metricLabel}</a> : metric;
    }
    const row = { metric: leftCell };
    fd.column_collection.forEach((column) => {
      if (column.colType === 'spark') {
        let sparkData;
        if (!column.timeRatio) {
          sparkData = data.map(d => d[metricLabel]);
        } else {
          // Period ratio sparkline
          sparkData = [];
          for (let i = column.timeRatio; i < data.length; i++) {
            const prevData = data[i - column.timeRatio][metricLabel];
            if (prevData && prevData !== 0) {
              sparkData.push(data[i][metricLabel] / prevData);
            } else {
              sparkData.push(null);
            }
          }
        }
        const formatDate = formatDateThunk(column.dateFormat);
        row[column.key] = {
          data: sparkData[sparkData.length - 1],
          display: (
            <WithTooltip
              tooltipProps={sparklineTooltipProps}
              hoverStyles={null}
              renderTooltip={({ index }) => (
                <div>
                  <strong>{d3format(column.d3format, sparkData[index])}</strong>
                  <div>{formatDate(data[index].iso)}</div>
                </div>
              )}
            >
              {({ onMouseLeave, onMouseMove, tooltipData }) => (
                <Sparkline
                  ariaLabel={`spark-${metricLabel}`}
                  width={parseInt(column.width, 10) || 300}
                  height={parseInt(column.height, 10) || 50}
                  margin={SPARKLINE_MARGIN}
                  data={sparkData}
                  onMouseLeave={onMouseLeave}
                  onMouseMove={onMouseMove}
                >
                  <LineSeries
                    showArea={false}
                    stroke="#767676"
                  />
                  {tooltipData &&
                    <VerticalReferenceLine
                      reference={tooltipData.index}
                      strokeDasharray="3 3"
                      strokeWidth={1}
                    />}
                  {tooltipData &&
                    <PointSeries
                      points={[tooltipData.index]}
                      fill="#767676"
                      strokeWidth={1}
                    />}
                </Sparkline>
              )}
            </WithTooltip>
          ),
        };
      } else {
        const recent = reversedData[0][metricLabel];
        let v;
        let errorMsg;
        if (column.colType === 'time') {
          // Time lag ratio
          const timeLag = parseInt(column.timeLag, 10);
          const totalLag = Object.keys(reversedData).length;
          if (timeLag > totalLag) {
            errorMsg = `The time lag set at ${timeLag} exceeds the length of data at ${reversedData.length}. No data available.`;
          } else {
            v = reversedData[timeLag][metricLabel];
          }
          if (column.comparisonType === 'diff') {
            v = recent - v;
          } else if (column.comparisonType === 'perc') {
            v = recent / v;
          } else if (column.comparisonType === 'perc_change') {
            v = (recent / v) - 1;
          }
        } else if (column.colType === 'contrib') {
          // contribution to column total
          v = recent / Object.keys(reversedData[0])
            .map(k => k !== 'iso' ? reversedData[0][k] : null)
            .reduce((a, b) => a + b);
        } else if (column.colType === 'avg') {
          // Average over the last {timeLag}
          v = reversedData
          .map((k, i) => i < column.timeLag ? k[metricLabel] : 0)
          .reduce((a, b) => a + b) / column.timeLag;
        }
        let color;
        if (column.bounds && column.bounds[0] !== null && column.bounds[1] !== null) {
          const scaler = d3.scale.linear()
            .domain([
              column.bounds[0],
              column.bounds[0] + ((column.bounds[1] - column.bounds[0]) / 2),
              column.bounds[1],
            ])
            .range([ACCESSIBLE_COLOR_BOUNDS[0], 'grey', ACCESSIBLE_COLOR_BOUNDS[1]]);
          color = scaler(v);
        } else if (column.bounds && column.bounds[0] !== null) {
          color = v >= column.bounds[0] ? ACCESSIBLE_COLOR_BOUNDS[1] : ACCESSIBLE_COLOR_BOUNDS[0];
        } else if (column.bounds && column.bounds[1] !== null) {
          color = v < column.bounds[1] ? ACCESSIBLE_COLOR_BOUNDS[1] : ACCESSIBLE_COLOR_BOUNDS[0];
        }
        row[column.key] = {
          data: v,
          display: errorMsg ?
            (<div>{errorMsg}</div>) :
            (<div style={{ color }}>
              <FormattedNumber num={v} format={column.d3format} />
            </div>),
          style: color && {
            boxShadow: `inset 0px -2.5px 0px 0px ${color}`,
            borderRight: '2px solid #fff',
          },
        };
      }
    });
    return row;
  });
  ReactDOM.render(
    <Table
      className="table table-no-hover"
      defaultSort={defaultSort}
      sortBy={defaultSort}
      sortable={fd.column_collection.map(c => c.key)}
    >
      <Thead>
        <Th column="metric">Metric</Th>
        {fd.column_collection.map((c, i) => (
          <Th column={c.key} key={c.key} width={c.colType === 'spark' ? '1%' : null}>
            {c.label} {c.tooltip && (
              <InfoTooltipWithTrigger
                tooltip={c.tooltip}
                label={`tt-col-${i}`}
                placement="top"
              />
            )}
          </Th>))}
      </Thead>
      {tableData.map(row => (
        <Tr key={row.metric}>
          <Td column="metric" data={row.metric}>{row.metric}</Td>
          {fd.column_collection.map(c => (
            <Td
              column={c.key}
              key={c.key}
              value={row[c.key].data}
              style={row[c.key].style}
            >
              {row[c.key].display}
            </Td>))}
        </Tr>))}
    </Table>,
    document.getElementById(slice.containerId),
  );
}
 
module.exports = viz;