Support time discretization in scripts

This can be used to ensure parameter refresh times are aligned (as
opposed to using Date.now() - X). It also allows for possible future
optimizations by exploiting the fact that the value of Date.now(X)
remains static for a given amount of time.

The variance value can be used to shift the discretization threshold
points based on the hash of the device ID for load distribution.

The djb2 hashing algorithm has been replaced with MD5 for better
distribution.
This commit is contained in:
Zaid Abdulla committed 2017-11-10 14:34:24 -08:00
1 parent ed873cdd4a
commit 6a9c62b639
6 files changed
+121 -47

No files matched your search

+9 -24
View File
@@ -31,6 +31,8 @@ cache = require './cache'
localCache = require './local-cache'
db = require './db'
logger = require './logger'
scheduling = require './scheduling'
MAX_CYCLES = 4
@@ -198,28 +200,6 @@ transferComplete = (sessionContext, rpc) ->
)
testSchedule = (schedule, timestamp) ->
range = schedule.schedule.nextRange(10, new Date(timestamp))
prev = schedule.schedule.prevRange(1, new Date(timestamp), new Date(timestamp - schedule.duration))
if prev
first = +(prev[0] ? 0)
last = +(prev[1] ? 0) + schedule.duration
else
first = +(range[0][0] ? 0)
last = +(range[0][1] ? 0) + schedule.duration
for r in range
if last < r[0]
break
last = +(r[1] ? 0) + schedule.duration
if timestamp >= first
return [true, Math.max(0, last - timestamp)]
else
return [false, first - timestamp]
# Append providions and remove duplicates
appendProvisions = (original, toAppend) ->
modified = false
@@ -293,8 +273,13 @@ applyPresets = (sessionContext) ->
eventsMatch = false
break
continue if (not eventsMatch) or (preset.schedule and not
(preset.schedule.schedule and testSchedule(preset.schedule, sessionContext.timestamp)[0]))
continue if not eventsMatch
if preset.schedule
continue if not preset.schedule.schedule
r = scheduling.cron(sessionContext.timestamp, preset.schedule.schedule)
if not (r[0] + preset.schedule.duration > sessionContext.timestamp)
continue
filteredPresets.push(preset)
for k of preset.precondition