{I"
class:ETI"ProcessedAsset;�FI"logical_path;�TI"prosperity/graph.js;�FI"
pathname;�TI"c/Users/simon/Documents/rainforest/prosperity/app/assets/javascripts/prosperity/graph.js.coffee;�FI"content_type;�TI"application/javascript;�TI"
mtime;�Tl+?1�TI"length;�Ti�I"digest;�TI"%91d0267eafbf9e0bcdbd44c810f3d68b;�FI"source;�TI"�(function() {
  var Graph, SubGraph, updateMetricOptions,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  SubGraph = (function() {
    function SubGraph(options) {
      var chartEl, graphType, hoverCallback;
      if (options == null) {
        options = {};
      }
      this["class"] = __bind(this["class"], this);
      this.el = $('<div>', {
        "class": 'sub-graph'
      });
      graphType = this.graphType = options.graphType;
      if (options.showTitle) {
        this.el.append("<div class='title'>Loading...</div>");
      }
      this.el.find('.title').html(options.label);
      chartEl = $('<div>', {
        "class": 'sub-graph-chart'
      });
      this.el.append(chartEl);
      hoverCallback = function(index, options, content, data) {
        var key, sum, t, value;
        if (graphType === 'area') {
          data = options.data[index];
          sum = 0;
          for (key in data) {
            value = data[key];
            if (key !== 'x') {
              sum += value;
            }
          }
          t = "Total: " + sum;
          content += t;
        }
        return content;
      };
      this.data = [];
      this.chartOptions = {
        element: chartEl,
        series: [],
        xkey: "x",
        ykeys: [],
        labels: [],
        smooth: false,
        data: this.data,
        hideHover: true,
        hoverCallback: hoverCallback,
        events: [new Date().toISOString()]
      };
      if (options.key === 'change') {
        this.chartOptions.postUnits = '%';
      }
    }

    SubGraph.prototype.addSeries = function(json) {
      var data, i, point, start_time, time, _i, _len, _ref;
      data = this.data;
      start_time = Date.parse(json.start_time);
      _ref = json.data;
      for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
        point = _ref[i];
        time = start_time + i * json.period_milliseconds;
        data[i] || (data[i] = {
          x: time
        });
        data[i][json.uid] = point;
      }
      this.chartOptions.ykeys.push(json.uid);
      this.chartOptions.labels.push(json.uid);
      return this.el;
    };

    SubGraph.prototype["class"] = function() {
      if (['area', 'ratio'].indexOf(this.graphType) >= 0) {
        return Morris.Area;
      } else {
        return Morris.Line;
      }
    };

    SubGraph.prototype.draw = function() {
      if (this.graphType === 'ratio') {
        this.calculateRatios();
      }
      return this.chart = new this["class"]()(this.chartOptions);
    };

    SubGraph.prototype.calculateRatios = function() {
      var index, item, key, sum, value, _i, _len, _ref, _results;
      _ref = this.data;
      _results = [];
      for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
        item = _ref[index];
        sum = 0;
        for (key in item) {
          value = item[key];
          if (key !== 'x') {
            sum += value;
          }
        }
        _results.push((function() {
          var _results1;
          _results1 = [];
          for (key in item) {
            value = item[key];
            if (sum === 0) {
              if (key !== 'x') {
                _results1.push(item[key] = 0);
              } else {
                _results1.push(void 0);
              }
            } else {
              if (key !== 'x') {
                _results1.push(item[key] = (value / sum) * 100);
              } else {
                _results1.push(void 0);
              }
            }
          }
          return _results1;
        })());
      }
      return _results;
    };

    return SubGraph;

  })();

  Graph = (function() {
    function Graph(options) {
      this.getSubgraph = __bind(this.getSubgraph, this);
      this.render = __bind(this.render, this);
      this.url = options.url;
      this.el = options.el;
      this.$el = $(options.el);
    }

    Graph.prototype.render = function() {
      $.ajax({
        url: this.url,
        dataType: 'json',
        success: (function(_this) {
          return function(json) {
            var extractor, index, _i, _len, _ref, _results;
            _ref = json.extractors;
            _results = [];
            for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
              extractor = _ref[index];
              _results.push($.ajax({
                url: extractor.url,
                dataType: 'json',
                success: function(line_json) {
                  var subgraph;
                  subgraph = _this.getSubgraph({
                    label: line_json.label,
                    key: line_json.key,
                    graphType: json.graph_type
                  });
                  subgraph.addSeries(line_json);
                  if (_this.$el.hasClass('dashboard')) {
                    if (subgraph.chartOptions.ykeys.length === json.extractors.length) {
                      return subgraph.draw();
                    }
                  } else {
                    return subgraph.draw();
                  }
                },
                error: function(xhr) {
                  return _this.displayError(xhr);
                }
              }));
            }
            return _results;
          };
        })(this),
        error: (function(_this) {
          return function(xhr) {
            return _this.displayError(xhr);
          };
        })(this)
      });
      return this;
    };

    Graph.prototype.displayError = function(xhr) {
      var errorMessage, t;
      errorMessage = null;
      if (xhr.status >= 400 && xhr.status < 500) {
        errorMessage = JSON.parse(xhr.responseText).error;
      }
      errorMessage || (errorMessage = "An unknown server error has occured.");
      t = $('<div>', {
        "class": 'alert alert-danger',
        text: errorMessage
      });
      return this.$el.find('.errors').append(t);
    };

    Graph.prototype.getSubgraph = function(options) {
      var create;
      create = (function(_this) {
        return function(options) {
          var subgraph;
          subgraph = new SubGraph(options);
          _this.$el.find('.loading').remove();
          _this.$el.append(subgraph.el);
          return subgraph;
        };
      })(this);
      if (this.$el.hasClass('dashboard')) {
        return this.subGraph || (this.subGraph = create(options));
      } else {
        return create($.extend(options, {
          showTitle: true
        }));
      }
    };

    return Graph;

  })();

  this.Prosperity || (this.Prosperity = {});

  this.Prosperity.Graph = Graph;

  updateMetricOptions = function(el) {
    var $el, $form, $optionSelect, option, options, possibleOptions, selectedOption, _i, _len, _results;
    $el = $(el);
    if ($el.length === 0) {
      return;
    }
    $form = $el.parents('form');
    options = $form.data('metric-options');
    possibleOptions = options[$el.val()] || [];
    possibleOptions = possibleOptions.sort();
    $optionSelect = $el.parents(".graph-line").find(".metric-option-select select");
    selectedOption = $form.find("input[type=\"hidden\"][name=\"" + ($optionSelect.attr('name')) + "\"]").val();
    $optionSelect.html('');
    _results = [];
    for (_i = 0, _len = possibleOptions.length; _i < _len; _i++) {
      option = possibleOptions[_i];
      _results.push($optionSelect.append($('<option>', {
        value: option,
        text: option,
        selected: selectedOption === option
      })));
    }
    return _results;
  };

  $(document).on("change", ".edit_graph .metric-title-select select", function(e) {
    return updateMetricOptions(e.target);
  });

  $(function() {
    return $(".edit_graph .metric-title-select select").each(function(i, el) {
      return updateMetricOptions(el);
    });
  });

}).call(this);
;�TI"dependency_digest;�TI"%06bb61bc7c558384e513a55be84a482a;�FI"required_paths;�T[I"c/Users/simon/Documents/rainforest/prosperity/app/assets/javascripts/prosperity/graph.js.coffee;�FI"dependency_paths;�T[{I"	path;�TI"c/Users/simon/Documents/rainforest/prosperity/app/assets/javascripts/prosperity/graph.js.coffee;�FI"
mtime;�TI"2014-12-22T09:57:03-05:00;�TI"digest;�TI"%53716ce28ab0e1af7bc9eeaed891165b;�FI"
_version;�TI"%2b66aa67c90052d553e0328c249bc9b0;�F