all files / candela/components/TrackerDash/ InfoPane.js

44.88% Statements 57/127
44.93% Branches 31/69
40% Functions 8/20
15.66% Lines 13/83
10 statements, 2 functions, 16 branches Ignored     
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                                                                                                                                                                                                                                                                                          
import _ from 'underscore';
import $ from 'jquery';
import d3 from 'd3';
import nv from 'nvd3';
 
import StatusBarWidget from './StatusBarWidget';
import ErrorBulletWidget from './ErrorBulletWidget';
import { deArray, failValue, warningValue } from './utility.js';
 
import infoPane from './templates/infoPane.jade';
import VisComponent from '../../VisComponent';
 
class InfoPane extends VisComponent {
  constructor (el, statusBarEl, settings) {
    super(el);
    this.$el = $(this.el);
 
    this.statusBarEl = statusBarEl;
 
    this.name = settings.name || 'Ground Truth';
    this.branch = settings.branch || 'master';
    this.day = settings.day || this.getToday();
    this.warning = settings.warning || 3;
    this.fail = settings.fail || 4;
    this.max = settings.max || 5;
    this.producerLink = settings.producer_link || null;
 
    this.numIncomplete = 0;
    this.numSuccess = 0;
    this.numBad = 0;
    this.numFail = 0;
    this.allValues = [];
    this.aggTrends = settings.aggTrends;
    _.each(settings.trendValuesByDataset, _.bind(function (dataset) {
      let current = deArray(dataset.current, d3.median);
      this.allValues.push(current);
 
      if (settings.trendMap[dataset.trend].incompleteThreshold) {
        this.numIncomplete++;
      } else {
        let failTrend = settings.trendMap[dataset.trend].fail;
        let warningTrend = settings.trendMap[dataset.trend].warning;
        if (failValue(current, warningTrend, failTrend)) {
          this.numFail++;
        } else if (warningValue(current, warningTrend, failTrend)) {
          this.numBad++;
        } else {
          this.numSuccess++;
        }
      }
    }, this));
  }
 
  getToday () {
    const today = new Date();
    let dd = today.getDate();
    let mm = today.getMonth() + 1;  // January is 0!
    const yyyy = today.getFullYear();
 
    if (dd < 10) {
      dd = `0${dd}`;
    }
 
    if (mm < 10) {
      mm = `0${mm}`;
    }
 
    return `${yyyy}/${mm}/${dd}`;
  }
 
  render () {
    // Test if any of the aggregate trends have spark line historical data.
    const sparklinesExist = (_.find(this.aggTrends, function (trend) {
      return trend.history && trend.history.length > 1;
    }, this)) !== undefined;
    this.$el.html(infoPane({
      name: this.name,
      branch: this.branch,
      day: this.day,
      aggTrends: this.aggTrends,
      sparklinesExist: sparklinesExist,
      producerLink: this.producerLink
    })).promise().done(_.bind(function () {
      this.aggBullets = {};
      _.each(this.aggTrends, (trend, key, list) => {
        if (trend.history && trend.history.length > 1) {
          nv.addGraph({
            generate: _.bind(function () {
              let parent = $('#' + trend.id_selector + '-aggregate-sparkline');
              let width = parent.width();
              let height = parent.height();
              let chart = nv.models.sparklinePlus()
                .margin({right: 40})
                .height(height)
                .width(width)
                .x(function (d, i) { return i; })
                .showLastValue(false);
              d3.select('#' + trend.id_selector + '-aggregate-sparkline svg')
                .datum(_.map(trend.history, (curValue, index) => {
                  return {x: index, y: curValue};
                }))
                .call(chart);
              return chart;
            }, this),
            callback: function (graph) {
              nv.utils.windowResize(function () {
                let parent = $('#' + trend.id_selector + '-aggregate-sparkline');
                let width = parent.width();
                let height = parent.height();
                graph.width(width).height(height);
 
                d3.select('#' + trend.id_selector + '-aggregate-sparkline svg')
                  .attr('width', width)
                  .attr('height', height)
                  .transition().duration(0)
                  .call(graph);
              });
            }
          });
        }
        let current = trend.history[trend.history.length - 1];
        current = deArray(current, d3.median);
        const el = $(`#${trend.id_selector}-aggregate-bullet-svg`).get(0);
        this.aggBullets[trend.id_selector] = new ErrorBulletWidget(el, {
          result: {
            current: Math.round(current * 10000) / 10000
          },
          trend: trend
        }).render();
        let dotSelector = '#' + trend.id_selector + '-aggregate-dot';
        if (!trend.incompleteThreshold) {
          if (failValue(current, trend.warning, trend.fail)) {
            $(dotSelector).attr('class', 'fail');
          } else if (warningValue(current, trend.warning, trend.fail)) {
            $(dotSelector).attr('class', 'bad');
          }
        }
      }, this);
    }, this));
 
    let statusBar = new StatusBarWidget(this.statusBarEl, {
      numSuccess: this.numSuccess,
      numBad: this.numBad,
      numFail: this.numFail,
      numIncomplete: this.numIncomplete
    });
    statusBar.render();
  }
}
 
export default InfoPane;