further refactor and test scheduler

This commit is contained in:
gabrielburnworth 2017-12-15 16:16:28 -08:00
parent 2275c66bc3
commit 507246b46e
2 changed files with 36 additions and 24 deletions

View file

@ -2,6 +2,7 @@ import { scheduler, scheduleForFarmEvent, TimeLine, farmEventIntervalSeconds } f
import * as moment from "moment";
import { TimeUnit } from "../../../interfaces";
import { Moment } from "moment";
import { range, padStart } from "lodash";
describe("scheduler", () => {
it("runs every 4 hours, starting Tu, until Th w/ origin of Mo", () => {
@ -118,6 +119,30 @@ describe("scheduler", () => {
current_time: "2017-08-03T17:30:30.000Z"
},
[]);
testSchedule("first 60 items",
{
"start_time": "2017-08-02T17:00:00.000Z",
"end_time": "2017-08-02T19:00:00.000Z",
"repeat": 1,
"time_unit": "minutely",
current_time: "2017-08-01T15:30:00.000Z"
},
range(0, 60)
.map((x: number) =>
moment(`2017-08-02T17:${padStart("" + x, 2, "0")}:00.000Z`)));
testSchedule("only 60 items",
{
"start_time": "2017-08-02T16:00:00.000Z",
"end_time": "2017-08-02T21:00:00.000Z",
"repeat": 1,
"time_unit": "minutely",
current_time: "2017-08-02T17:01:00.000Z"
},
range(0, 60)
.map((x: number) =>
moment(`2017-08-02T17:${padStart("" + x, 2, "0")}:00.000Z`)));
});
describe("farmEventIntervalSeconds", () => {

View file

@ -1,6 +1,6 @@
import * as moment from "moment";
import { Moment, unitOfTime } from "moment";
import { times, last } from "lodash";
import { take, filter, range } from "lodash";
import { TimeUnit } from "../../interfaces";
import { NEVER } from "../edit_fe_form";
@ -19,29 +19,16 @@ export function scheduler({
endTime,
intervalSeconds
}: SchedulerProps): Moment[] {
if (currentTime > endTime) {
return []; // Farm event is over
}
const timeSinceStart = currentTime.unix() - startTime.unix();
const itemsMissed = Math.floor(timeSinceStart / intervalSeconds);
const nextItemTime = startTime.clone()
.add((itemsMissed * intervalSeconds), "seconds");
const scheduledItems = [
timeSinceStart > 0
? nextItemTime // start time in the past
: startTime]; // start time in the future
times(60, () => { // get next 60 or so calendar items
const previousItem = last(scheduledItems);
if (previousItem) {
const nextItem = previousItem.clone().add(intervalSeconds, "seconds");
if (nextItem.isBefore(endTime)) {
scheduledItems.push(nextItem);
}
}
});
// Match FarmBot OS calendar item execution grace period
const gracePeriodCutoffTime = currentTime.subtract(1, "minutes");
return scheduledItems.filter(m => m.isAfter(gracePeriodCutoffTime));
return take(filter(
// Generate all occurrences in Farm Event.
range(startTime.unix(), endTime.unix(), intervalSeconds),
// Filter out past occurrences.
// Match FarmBot OS calendar item execution grace period (60 seconds).
(itemTime) => itemTime >= (currentTime.unix() - 60)),
// Take only the next 60 occurrences for performance reasons.
60) // At the least, this provides the next hour of calendar items.
// Convert from seconds back to Moment.
.map(x => moment.unix(x));
}
/** Translate farmbot interval names to momentjs interval names */