Created
January 23, 2014 19:08
-
-
Save oshikryu/8584819 to your computer and use it in GitHub Desktop.
Takes an array of time/value pairs and intelligently samples based on a given base frequency
This file contains hidden or 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
_transformFeedData: (feed) -> | |
processedFeed = {} | |
intervalWindow = [] | |
timeStep = {} | |
metaCounter = 1 | |
timeStep = @_getTimeStep feed[0].time, feed[1].time | |
initialDate = new Date(feed[0].time).getTime() | |
metaCounter = @_getMetaCount initialDate, timeStep | |
for point, ii in feed | |
intervalWindow.push parseFloat point.value | |
if metaCounter == timeStep.metaLimit | |
processedVal = @_intervalOperation intervalWindow | |
addedTime = timeStep.interval*(ii/timeStep.metaLimit) | |
processedFeed[initialDate+addedTime] = | |
time: point.time | |
value: processedVal | |
metaCounter = 0 | |
processedVal = 0 | |
intervalWindow = [] | |
metaCounter++ | |
processedFeed | |
_getMetaCount: (unixDate, timeStep) -> | |
initialDate = new Date unixDate | |
metaCounter = 1 | |
minutesInHour = 60 | |
secondsInMinute = 60 | |
hoursInDay = 24 | |
ms = 1000 | |
curMinutes = initialDate.getMinutes() | |
if curMinutes == 0 | |
metaCounter = timeStep.metaLimit | |
else | |
# determine metaCounter based on how far away it is from top of hour | |
if timeStep.interval <= secondsInMinute*minutesInHour*ms | |
factor = timeStep.feedInterval/(secondsInMinute*ms) | |
else if timeStep.interval > secondsInMinute*minutesInHour*ms && timeStep.interval <= secondsInMinute*minutesInHour*hoursInDay*ms | |
factor = timeStep.feedInterval/(secondsInMinute*minutesInHour*ms) | |
metaCounter = Math.floor curMinutes/factor | |
return metaCounter | |
_getTimeStep: (one, two) -> | |
ms = 1000 | |
cur = new Date one | |
next = new Date two | |
diff = next.getTime() - cur.getTime() | |
feedInterval = diff | |
interval = @get('interval') * ms | |
metaLimit = parseInt interval/feedInterval | |
feedInterval: feedInterval | |
interval: interval | |
metaLimit: metaLimit | |
_intervalOperation: (intervalWindow) -> | |
#type = @get 'operation' | |
# TODO: eventually set this as a model attribute | |
type = 'sum' | |
switch type | |
when 'sum' | |
result = @_sumInterval(intervalWindow) | |
when 'mean' | |
result = @_meanInterval(intervalWindow) | |
when 'min' | |
result = @_minInterval(intervalWindow) | |
when 'max' | |
result = @_maxInterval(intervalWindow) | |
else | |
console.log 'unspecified type' | |
result | |
_sumInterval: (array) -> | |
sum = 0 | |
for num in array | |
sum = sum + num | |
sum | |
_meanInterval: (array) -> | |
mean = 0 | |
sum = 0 | |
if array.length > 0 | |
for num in array | |
sum = sum + num | |
mean = parseFloat sum/array.length | |
mean | |
_minInterval: (array) -> | |
if array.length > 0 | |
min = Math.min.apply(Math, array) | |
_maxInterval: (array) -> | |
if array.length > 0 | |
max = Math.max.apply(Math, array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment