// This is the main program that fetches Data Canvas - Sense Your City API. // Curran Kelleher March 2015 require(["d3", "getData", "async"], function (d3, getData, async) { var now = Date.now(), pastDay = now - 1000 * 60 * 60 * 24, pastWeek = now - 1000 * 60 * 60 * 24 * 7, pastMonth = now - 1000 * 60 * 60 * 24 * 7 * 4; async.series([ function(callback){ d3.select("#day-loading").style("visibility", "visible"); getData({ resolution: "5m", from: new Date(pastDay).toISOString(), before: new Date(now).toISOString(), },function(err, data){ d3.select("#day").text(toCSV(data)); d3.select("#day-loading").style("visibility", "hidden"); d3.select("#week-loading").style("visibility", "visible"); callback(); }); }, function(callback){ getData({ resolution: "1h", from: new Date(pastWeek).toISOString(), before: new Date(now).toISOString(), },function(err, data){ d3.select("#week").text(toCSV(data)); d3.select("#week-loading").style("visibility", "hidden"); d3.select("#month-loading").style("visibility", "visible"); callback(); }); }, function(callback){ getData({ resolution: "1h", from: new Date(pastMonth).toISOString(), before: new Date(now).toISOString(), },function(err, data){ d3.select("#month").text(toCSV(data)); d3.select("#month-loading").style("visibility", "hidden"); callback(); }); }, function(callback){ // 1 day in milliseconds. var msInterval = 1000 * 60 * 60 * 24, // Start fetching data from right now. msTime = Date.now(), // Get data from back in time 100 days. intervalCountMax = 100, // The running count of days we've fetched data for. intervalCount = 0, // The cumulative data as an array of objects. data = []; d3.select("#all-loading").style("visibility", "visible"); async.whilst( function () { return intervalCount < intervalCountMax; }, function (cb) { getData({ resolution: "5m", from: new Date(msTime - msInterval * (intervalCount + 1)).toISOString(), before: new Date(msTime - msInterval * intervalCount).toISOString() },function(err, newData){ data = _.sortBy(_.union(data, newData), "timestamp"); d3.select("#all-counter").text([ "Fetched " + data.length + " records", "over " + (intervalCount + 1) + " of " + intervalCountMax + " days" ].join("\n")); intervalCount++; cb(); }); }, function(err){ d3.select("#all").text(toCSV(data)); d3.select("#all-loading").style("visibility", "hidden"); console.log("Done fetching data"); } ); callback(); } ]); function toCSV(data){ var columns = Object.keys(data[0]); return [columns.join(",")].concat(data.map(function(d){ return columns.map(function(column){ return d[column]; }).join(","); })).join("\n"); } });