mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-07-31 19:20:25 +00:00
cron solution. @colegleason
This commit is contained in:
parent
c9ddf0e15d
commit
673267fb39
2 changed files with 18 additions and 22 deletions
|
|
@ -31,10 +31,7 @@ api.startOfWeek = api.startOfWeek = (options={}) ->
|
||||||
|
|
||||||
api.startOfDay = (options={}) ->
|
api.startOfDay = (options={}) ->
|
||||||
o = sanitizeOptions(options)
|
o = sanitizeOptions(options)
|
||||||
dayStart = moment(o.now).startOf('day').add('h', o.dayStart)
|
moment(o.now).startOf('day').add('h', o.dayStart)
|
||||||
# if between midnight and Custom Day Start, jump back to previous day
|
|
||||||
dayStart.subtract('day', 1) if moment(o.now).isBefore(dayStart)
|
|
||||||
return dayStart
|
|
||||||
|
|
||||||
dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s'}
|
dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s'}
|
||||||
|
|
||||||
|
|
@ -1244,3 +1241,4 @@ api.wrap = (user, main=true) ->
|
||||||
get: ->
|
get: ->
|
||||||
tasks = user.habits.concat(user.dailys).concat(user.todos).concat(user.rewards)
|
tasks = user.habits.concat(user.dailys).concat(user.todos).concat(user.rewards)
|
||||||
_.object(_.pluck(tasks, "id"), tasks)
|
_.object(_.pluck(tasks, "id"), tasks)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ rewrapUser = (user)->
|
||||||
expectStrings = (obj, paths) ->
|
expectStrings = (obj, paths) ->
|
||||||
_.each paths, (path) -> expect(obj[path]).to.be.ok()
|
_.each paths, (path) -> expect(obj[path]).to.be.ok()
|
||||||
|
|
||||||
# options.hoursAgo: hours since the last cron was executed
|
# options.daysAgo: days ago when the last cron was executed
|
||||||
beforeAfter = (options={}) ->
|
beforeAfter = (options={}) ->
|
||||||
user = newUser()
|
user = newUser()
|
||||||
[before, after] = [user, _.cloneDeep(user)]
|
[before, after] = [user, _.cloneDeep(user)]
|
||||||
|
|
@ -57,9 +57,9 @@ beforeAfter = (options={}) ->
|
||||||
if options.limitOne
|
if options.limitOne
|
||||||
before["#{options.limitOne}s"] = [before["#{options.limitOne}s"][0]]
|
before["#{options.limitOne}s"] = [before["#{options.limitOne}s"][0]]
|
||||||
after["#{options.limitOne}s"] = [after["#{options.limitOne}s"][0]]
|
after["#{options.limitOne}s"] = [after["#{options.limitOne}s"][0]]
|
||||||
lastCron = +(moment(options.now || +new Date).subtract('hours', options.hoursAgo)) unless options.hoursAgo == undefined
|
lastCron = +(moment(options.now || +new Date).subtract('days', options.daysAgo)) if options.daysAgo
|
||||||
_.each [before,after], (obj) ->
|
_.each [before,after], (obj) ->
|
||||||
obj.lastCron = lastCron unless options.hoursAgo == undefined
|
obj.lastCron = lastCron if options.daysAgo
|
||||||
{before:before, after:after}
|
{before:before, after:after}
|
||||||
#TODO calculate actual poins
|
#TODO calculate actual poins
|
||||||
|
|
||||||
|
|
@ -293,7 +293,7 @@ describe 'Cron', ->
|
||||||
# expect(paths.lastCron).to.be true # busted cron (was set to after today's date)
|
# expect(paths.lastCron).to.be true # busted cron (was set to after today's date)
|
||||||
|
|
||||||
it 'only dailies & todos are effected', ->
|
it 'only dailies & todos are effected', ->
|
||||||
{before,after} = beforeAfter({hoursAgo:24})
|
{before,after} = beforeAfter({daysAgo:1})
|
||||||
before.dailys = before.todos = after.dailys = after.todos = []
|
before.dailys = before.todos = after.dailys = after.todos = []
|
||||||
after.fns.cron()
|
after.fns.cron()
|
||||||
expect(after.lastCron).to.not.be before.lastCron # make sure cron was run
|
expect(after.lastCron).to.not.be before.lastCron # make sure cron was run
|
||||||
|
|
@ -310,7 +310,7 @@ describe 'Cron', ->
|
||||||
@clock.restore()
|
@clock.restore()
|
||||||
|
|
||||||
it 'should preen user history', ->
|
it 'should preen user history', ->
|
||||||
{before,after} = beforeAfter({hoursAgo:24})
|
{before,after} = beforeAfter({daysAgo:1})
|
||||||
history = [
|
history = [
|
||||||
# Last year should be condensed to one entry, avg: 1
|
# Last year should be condensed to one entry, avg: 1
|
||||||
{date:'09/01/2012', value: 0}
|
{date:'09/01/2012', value: 0}
|
||||||
|
|
@ -368,7 +368,7 @@ describe 'Cron', ->
|
||||||
|
|
||||||
describe 'Todos', ->
|
describe 'Todos', ->
|
||||||
it '1 day missed', ->
|
it '1 day missed', ->
|
||||||
{before,after} = beforeAfter({hoursAgo:24})
|
{before,after} = beforeAfter({daysAgo:1})
|
||||||
before.dailys = after.dailys = []
|
before.dailys = after.dailys = []
|
||||||
after.fns.cron()
|
after.fns.cron()
|
||||||
|
|
||||||
|
|
@ -394,7 +394,7 @@ describe 'Cron', ->
|
||||||
runCron = (options) ->
|
runCron = (options) ->
|
||||||
_.each [480, 240, 0, -120], (timezoneOffset) -> # test different timezones
|
_.each [480, 240, 0, -120], (timezoneOffset) -> # test different timezones
|
||||||
now = shared.startOfWeek({timezoneOffset}).add('hours', options.currentHour||0)
|
now = shared.startOfWeek({timezoneOffset}).add('hours', options.currentHour||0)
|
||||||
{before,after} = beforeAfter({now, timezoneOffset, hoursAgo:options.hoursAgo||0, dayStart:options.dayStart||0, limitOne:'daily'})
|
{before,after} = beforeAfter({now, timezoneOffset, daysAgo:1, dayStart:options.dayStart||0, limitOne:'daily'})
|
||||||
before.dailys[0].repeat = after.dailys[0].repeat = options.repeat if options.repeat
|
before.dailys[0].repeat = after.dailys[0].repeat = options.repeat if options.repeat
|
||||||
before.dailys[0].streak = after.dailys[0].streak = 10
|
before.dailys[0].streak = after.dailys[0].streak = 10
|
||||||
before.dailys[0].completed = after.dailys[0].completed = true if options.checked
|
before.dailys[0].completed = after.dailys[0].completed = true if options.checked
|
||||||
|
|
@ -411,7 +411,7 @@ describe 'Cron', ->
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
'due yesterday':
|
'due yesterday':
|
||||||
defaults: {hoursAgo:24, limitOne: 'daily'}
|
defaults: {daysAgo:1, limitOne: 'daily'}
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
'(simple)': {expect:'losePoints'}
|
'(simple)': {expect:'losePoints'}
|
||||||
|
|
@ -421,7 +421,7 @@ describe 'Cron', ->
|
||||||
defaults: {repeat:{su:1,m:true,t:1,w:1,th:1,f:1,s:1}}
|
defaults: {repeat:{su:1,m:true,t:1,w:1,th:1,f:1,s:1}}
|
||||||
steps:
|
steps:
|
||||||
'pre-dayStart':
|
'pre-dayStart':
|
||||||
defaults: {hoursAgo:2,currentHour:3, dayStart:4, shouldDo:true}
|
defaults: {currentHour:3, dayStart:4, shouldDo:true}
|
||||||
steps:
|
steps:
|
||||||
'checked': {checked: true, expect:'noChange'}
|
'checked': {checked: true, expect:'noChange'}
|
||||||
'un-checked': {checked: false, expect:'noChange'}
|
'un-checked': {checked: false, expect:'noChange'}
|
||||||
|
|
@ -435,7 +435,7 @@ describe 'Cron', ->
|
||||||
defaults: {repeat:{su:1,m:false,t:1,w:1,th:1,f:1,s:1}}
|
defaults: {repeat:{su:1,m:false,t:1,w:1,th:1,f:1,s:1}}
|
||||||
steps:
|
steps:
|
||||||
'pre-dayStart':
|
'pre-dayStart':
|
||||||
defaults: {hoursAgo:23, currentHour:3, dayStart:4, shouldDo:true}
|
defaults: {currentHour:3, dayStart:4, shouldDo:true}
|
||||||
steps:
|
steps:
|
||||||
'checked': {checked: true, expect:'noChange'}
|
'checked': {checked: true, expect:'noChange'}
|
||||||
'un-checked': {checked: false, expect:'noChange'}
|
'un-checked': {checked: false, expect:'noChange'}
|
||||||
|
|
@ -448,18 +448,18 @@ describe 'Cron', ->
|
||||||
'not due yesterday':
|
'not due yesterday':
|
||||||
defaults: repeatWithoutLastWeekday()
|
defaults: repeatWithoutLastWeekday()
|
||||||
steps:
|
steps:
|
||||||
'(simple)': {hoursAgo:24, expect:'noDamage'}
|
'(simple)': {expect:'noDamage'}
|
||||||
'post-dayStart': {hoursAgo:23,currentHour:5,dayStart:4, expect:'noDamage'}
|
'post-dayStart': {currentHour:5,dayStart:4, expect:'noDamage'}
|
||||||
'pre-dayStart': {hoursAgo:21, currentHour:3, dayStart:4, expect:'noChange'}
|
'pre-dayStart': {currentHour:3, dayStart:4, expect:'noChange'}
|
||||||
|
|
||||||
recurseCronMatrix = (obj, options={}) ->
|
recurseCronMatrix = (obj, options={}) ->
|
||||||
if obj.steps
|
if obj.steps
|
||||||
_.each obj.steps, (step, text) ->
|
_.each obj.steps, (step, text) ->
|
||||||
o = _.cloneDeep options
|
o = _.cloneDeep options
|
||||||
o.text ?= ''; o.text += " #{text} "
|
o.text ?= ''; o.text += " #{text} "
|
||||||
recurseCronMatrix step, _.assign(o, obj.defaults)
|
recurseCronMatrix step, _.defaults(o,obj.defaults)
|
||||||
else
|
else
|
||||||
it "#{options.text}", -> runCron(_.assign(options, obj))
|
it "#{options.text}", -> runCron(_.defaults(obj,options))
|
||||||
recurseCronMatrix(cronMatrix)
|
recurseCronMatrix(cronMatrix)
|
||||||
|
|
||||||
it 'calculates day differences with dayStart properly', ->
|
it 'calculates day differences with dayStart properly', ->
|
||||||
|
|
@ -493,8 +493,6 @@ describe 'Helper', ->
|
||||||
expect(shared.startOfDay({now: new Date(2013, 0, 1, 0)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00'
|
expect(shared.startOfDay({now: new Date(2013, 0, 1, 0)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00'
|
||||||
expect(shared.startOfDay({now: new Date(2013, 0, 1, 5)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00'
|
expect(shared.startOfDay({now: new Date(2013, 0, 1, 5)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00'
|
||||||
expect(shared.startOfDay({now: new Date(2013, 0, 1, 23, 59, 59)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00'
|
expect(shared.startOfDay({now: new Date(2013, 0, 1, 23, 59, 59)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00'
|
||||||
# between midnight and custom day start
|
|
||||||
expect(shared.startOfDay({now: new Date(2013, 0, 2, 2), dayStart: 4}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 04:00'
|
|
||||||
|
|
||||||
it 'counts pets', ->
|
it 'counts pets', ->
|
||||||
pets = {}
|
pets = {}
|
||||||
|
|
@ -507,4 +505,4 @@ describe 'Helper', ->
|
||||||
|
|
||||||
pets = { "Wolf-Base": 2, "Wolf-Veteran": 1, "Wolf-Cerberus": 1, "Dragon-Hydra": 1}
|
pets = { "Wolf-Base": 2, "Wolf-Veteran": 1, "Wolf-Cerberus": 1, "Dragon-Hydra": 1}
|
||||||
expect(shared.countPets(null, pets)).to.eql 1
|
expect(shared.countPets(null, pets)).to.eql 1
|
||||||
expect(shared.countPets(_.size(pets), pets)).to.eql 1
|
expect(shared.countPets(_.size(pets), pets)).to.eql 1
|
||||||
Loading…
Reference in a new issue