Last active
August 29, 2015 14:21
-
-
Save djfarrelly/1d54db0474d02102b0a2 to your computer and use it in GitHub Desktop.
In an effort to more intelligently request data for sent updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AnalyticsStore = modules.exports = { | |
_data: new Backbone.Collection(), | |
_daysInCache: [], | |
grabDaysForCache: function(data) { | |
var days = data.updates.map(function(update){ | |
return Math.r(update.due_at / 24 / 3600); // days since 1970 | |
}); | |
var uniqueDays = _.uniq(days); | |
this.addDatesToCache(uniqueDays); | |
return data; | |
}, | |
addDatesToCache: function(days) { | |
this._daysInCache = _.uniq(this._daysInCache.concat(days)); | |
}, | |
// Splits our projected range into smaller ranges if we have some of the dates | |
// in the cache | |
getRangesForFetch: function(startTimestamp, days) { | |
// compare with existing days | |
var startDay = Math.floor(startTimestamp / 24 / 3600); | |
var endDay = startDay + days; | |
var ranges = []; | |
var currentRange = { start: null, end: null }; | |
for (var day = startDay; day <= endDay; day + 24 * 3600) { | |
// If the day isn't cached | |
if (this._daysInCache.indexOf(day) === -1) { | |
if (!currentRange.start) { | |
currentRange.start = day * 24 * 3600; | |
} else { | |
// Continuously update the end range | |
currentRange.end = day * 24 * 3600; | |
} | |
} else { | |
// If the date is in the queue, close off the current range | |
if (currentRange.end) { | |
ranges.push(currentRange); | |
currentRange = {}; | |
} else if (currentRange.start) { | |
currentRange.end = currentRange.start + 24 * 3600; | |
ranges.push(currentRange); | |
currentRange = {}; | |
} | |
} | |
} | |
// duplicated from above (could be refactored) | |
if (currentRange.end) { | |
ranges.push(currentRange); | |
} else if (currentRange.start) { | |
currentRange.end = currentRange.start + 24 * 3600; | |
ranges.push(currentRange); | |
} | |
return ranges; | |
}, | |
getData: function(startTimestamp, endTimestamp) { | |
//psudo-code | |
return api.get('/updates/sent.json', { | |
start: startTimestamp, | |
end: endTimestamp | |
}); | |
}, | |
saveData: function(data) { | |
this._data.add(data.updates); | |
return data; | |
}, | |
fetch: function(startTimestamp, days) { | |
var ranges = this.getRangesForFetch(startTimestamp, days); | |
if (!ranges.length) { | |
return new Promise(function(resolve, reject) { | |
resolve(this._data.toJSON()); | |
}.bind(this)); | |
} | |
return new Promise(function(resolve, reject) { | |
var numComplete = 0; | |
ranges.forEach(function(range) { | |
this.getData(range.start, range.end) | |
.then(this.grabDaysForCache.bind(this)) | |
.then(this.saveData.bind(this)) | |
.then(function() { | |
numComplete++; | |
if (numComplete === ranges.length) { | |
resolve(this._data.toJSON()); | |
} | |
}.bind(this)); | |
}.bind(this)); | |
}.bind(this)); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AnalyticsStore = require('./analyticsStore.js'); | |
var start = Math.round((new Date('2015-05-01T04:00:00.000Z')).valueOf() / 1000); | |
AnalyticsStore.fetch(start, 14) | |
.then(function(updates) { | |
// render!! | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment