@bridgebrew/scheduler 0.0.0-stage → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tsconfig.json ADDED
@@ -0,0 +1 @@
1
+ { "extends": "./.bridge/tsconfig.json" }
package/view.ts ADDED
@@ -0,0 +1,98 @@
1
+ // The words and the numbers the scheduled tasks' screens use (research-040): a schedule as a person reads it, when it
2
+ // runs next, and an interval as a form's count and unit. The scheduler's task answer and the types here are the same
3
+ // shape, kept apart so the browser never imports the server's file.
4
+
5
+ /** A schedule as `POST /scheduler/tasks` takes it. */
6
+ export interface IntervalSchedule {
7
+ type: 'interval';
8
+ everyMs: number;
9
+ }
10
+ export interface FixedTimeSchedule {
11
+ type: 'fixed_time';
12
+ timeOfDay: string;
13
+ /** 0 is Sunday, as JavaScript's `Date#getDay`; every day without it. */
14
+ weekdays?: number[] | undefined;
15
+ }
16
+ export type Schedule = IntervalSchedule | FixedTimeSchedule;
17
+
18
+ export type IntervalUnit = 'minutes' | 'hours' | 'days';
19
+
20
+ export interface IntervalParts {
21
+ count: number;
22
+ unit: IntervalUnit;
23
+ }
24
+
25
+ const MINUTE = 60_000;
26
+ const HOUR = 60 * MINUTE;
27
+ const DAY = 24 * HOUR;
28
+
29
+ const UNIT_MS: Record<IntervalUnit, number> = {
30
+ minutes: MINUTE,
31
+ hours: HOUR,
32
+ days: DAY,
33
+ };
34
+
35
+ const DAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] as const;
36
+ /** Monday first, as a week reads. */
37
+ const WEEK_ORDER = [1, 2, 3, 4, 5, 6, 0] as const;
38
+
39
+ /** An interval in its largest whole unit: 90 minutes stays minutes, 24 hours is one day. */
40
+ export function intervalParts(everyMs: number): IntervalParts {
41
+ if (everyMs >= DAY && everyMs % DAY === 0) return { count: everyMs / DAY, unit: 'days' };
42
+ if (everyMs >= HOUR && everyMs % HOUR === 0) return { count: everyMs / HOUR, unit: 'hours' };
43
+ return { count: Math.max(1, Math.round(everyMs / MINUTE)), unit: 'minutes' };
44
+ }
45
+
46
+ /** The milliseconds an interval of `count` of `unit` lasts. */
47
+ export function everyMsOf({ count, unit }: IntervalParts): number {
48
+ return count * UNIT_MS[unit];
49
+ }
50
+
51
+ /** The singular of an interval's unit ("minute", "hour", "day"). */
52
+ const singular = (unit: IntervalUnit) => unit.slice(0, -1);
53
+
54
+ /** A schedule as a person reads it: "Every 15 minutes", "Weekdays at 08:30", "Mon, Wed at 18:05". */
55
+ export function scheduleLabel(schedule: Schedule): string {
56
+ if (schedule.type === 'interval') {
57
+ const { count, unit } = intervalParts(schedule.everyMs);
58
+ return `Every ${count === 1 ? singular(unit) : `${count} ${unit}`}`;
59
+ }
60
+ const days = [...new Set(schedule.weekdays ?? [])].sort((a, b) => a - b);
61
+ if (days.length === 0 || days.length === 7) return `Every day at ${schedule.timeOfDay}`;
62
+ if (days.length === 5 && days.every((day, at) => day === at + 1))
63
+ return `Weekdays at ${schedule.timeOfDay}`;
64
+ if (days.length === 2 && days[0] === 0 && days[1] === 6)
65
+ return `Weekends at ${schedule.timeOfDay}`;
66
+ const names = WEEK_ORDER.filter((day) => days.includes(day)).map((day) => DAY_NAMES[day]);
67
+ return `${names.join(', ')} at ${schedule.timeOfDay}`;
68
+ }
69
+
70
+ /** A task's state, as the scheduler derives it (`GET /scheduler/tasks`). */
71
+ export type TaskState = 'waiting' | 'running' | 'off' | 'suspended';
72
+
73
+ /** What a task is doing, relative to `now`: "Running", "Suspended", "Paused", "Not planned", "Next run in 2 h". */
74
+ export function nextLabel(
75
+ {
76
+ enabled,
77
+ nextRunAt,
78
+ state,
79
+ pastDeadline,
80
+ }: { enabled: boolean; nextRunAt: string | null; state?: TaskState; pastDeadline?: boolean },
81
+ now = Date.now(),
82
+ ): string {
83
+ if (state === 'running') {
84
+ const running = pastDeadline ? 'Running past its deadline' : 'Running';
85
+ return enabled ? running : `${running} · paused after this run`;
86
+ }
87
+ if (!enabled) return 'Paused';
88
+ if (state === 'suspended') return 'Suspended';
89
+ if (nextRunAt === null) return 'Not planned';
90
+ const at = Date.parse(nextRunAt);
91
+ const ahead = Number.isFinite(at) ? at - now : 0;
92
+ if (ahead < MINUTE) return 'Next run in less than a minute';
93
+ const minutes = Math.floor(ahead / MINUTE);
94
+ if (minutes < 60) return `Next run in ${minutes} min`;
95
+ const hours = Math.floor(ahead / HOUR);
96
+ if (hours < 24) return `Next run in ${hours} h`;
97
+ return `Next run in ${Math.floor(ahead / DAY)} d`;
98
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.