{% extends 'graphs.html' %}
{% set active_graph_page='times' %}
{% block head %}
{{ super() }}
<style>

#graph {
  font: 10px sans-serif;
  width: 100%;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  display: none;
}

.axis-title {
  font-weight: bold;
  text-anchor: end;
}

.line {
  fill: none;
  stroke: red;
  stroke-width: 1.5px;
}

.overlay {
  fill: none;
  pointer-events: all;
}

.focus circle {
  fill: none;
  stroke: steelblue;
}

.area {
  fill: #1d7fb3;
}

</style>
{% endblock %}
{% block graph_body %}
<p>
{% trans %}Average time to complete backup.{% endtrans %}
</p>
<svg id="graph" preserveAspectRatio="xMidYMid"></svg>
<script>

function formatTime(secs) {
   if(secs == 0) return '0 sec';
   var k = 60;
   var dm = 2;
   var sizes = ['secs', 'min', 'h'];
   var i = Math.floor(Math.log(secs) / Math.log(k));
   return (secs / Math.pow(k, i)).toPrecision(dm) + ' ' + sizes[i];
};

var margin = {top: 20, right: 50, bottom: 35, left: 50},
    width = parseInt(d3.select("#graph").style("width")) - margin.left - margin.right,
    height = width/2 - margin.top - margin.bottom;

var bisectDate = d3.bisector(function(d) { return d.date; }).left;

var svg = d3.select("#graph")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .attr("viewBox", "0 0 " + (width + margin.left + margin.right) + " " + (height + margin.top + margin.bottom))
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.csv("{{ url_for_graphs(repo_path, 'data') }}", type, function(error, csv_data) {
  if (error) throw error;

  // Define a temporary X axis to figure out grouping.
  var x0 = d3.time.scale().range([0, width]);
  x0.domain(d3.extent(csv_data, function(d) { return d.date; })).nice();
  var groupby;
  if(x0.ticks(d3.time.year, 1).length > 4) {
    groupby = d3.time.month;
  } else if(x0.ticks(d3.time.month, 1).length > 11) {
    groupby = d3.time.week;
  } else if(x0.ticks(d3.time.week, 1).length > 4) {
    groupby = d3.time.day;
  } else {
    groupby = d3.time.day;
  }
  
  // Aggregate data.
  var data = d3.nest()
    .key(function(d) { return groupby(d.date);})
    .rollup(function(leaves) {
      return d3.mean(leaves, function(g) {return g.elapsedtime; });
    }).entries(csv_data);

  // Convert month back to Date object
  data.forEach(function(d) {
    d.date = d3.time.format.iso.parse(d.key);
    d.elapsedtime = d.values;
  });
  data.sort(function(a,b) { return a.date - b.date; });

  // Define x axis
  var x = d3.time.scale()
    .range([0, width]);
  x.domain(d3.extent(data, function(d) { return d.date; })).nice();

  var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom");

  // Define y axis
  var y = d3.scale.linear().range([height, 0]);
  y.domain(d3.extent(data, function(d) { return d.elapsedtime; })).nice();

  var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .tickFormat(formatTime);

  // Define Area
  var area = d3.svg.area()
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(function(d) { return y(d.elapsedtime); });

  // Draw x Axis
  svg.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);

  // Draw y Axis
  svg.append("g")
      .attr("class", "y axis")
      .call(yAxis);

  svg.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area);

  var focus = svg.append("g")
      .attr("class", "focus")
      .style("display", "none");

  focus.append("circle")
      .attr("r", 4.5);

  focus.append("text")
      .attr("x", 9)
      .attr("dy", ".35em");

  focus.append("text")
      .attr("x", 9)
      .attr("dy", ".35em");

  svg.append("rect")
      .attr("class", "overlay")
      .attr("width", width)
      .attr("height", height)
      .on("mouseover", function() { focus.style("display", null); })
      .on("mouseout", function() { focus.style("display", "none"); })
      .on("mousemove", mousemove);

  function mousemove() {
    var x0 = x.invert(d3.mouse(this)[0]),
        i = bisectDate(data, x0, 1),
        d0 = data[i - 1],
        d1 = data[i],
        d = x0 - d0.date > d1.date - x0 ? d1 : d0;
    focus.attr("transform", "translate(" + x(d.date) + "," + y(d.elapsedtime) + ")");
    focus.select("text").text(formatTime(d.elapsedtime));
  }

});

function type(d) {
  var d1 = {};
  d1.date = new Date(d.date * 1000);
  d1.elapsedtime = Math.abs(d.elapsedtime);
  return d1;
}

</script>
{% endblock %}