@xeplr/ui-jobs 1.0.1

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.
@@ -0,0 +1,196 @@
1
+ // Jobs — the pure bits. Reading a cron expression, describing a run, and
2
+ // deciding what a job's state actually IS.
3
+ //
4
+ // Separate from the component because every one of these is a claim that can
5
+ // be wrong in a way a screenshot will not show you: "next run in 3 hours" when
6
+ // it is 3 days, "succeeded" over a job that failed and retried.
7
+
8
+ /** A job is not one flag but three, and the UI needs the combination. */
9
+ export function jobState(job) {
10
+ if (!job) return 'unknown'
11
+ // RUNNING WINS. A paused job that is mid-run is running — the pause takes
12
+ // effect at the next pick, and saying "paused" over a live action invites
13
+ // somebody to start a second one.
14
+ if (job.running) return 'running'
15
+ if (job.status === 'pause') return 'paused'
16
+ if (!job.schedule) return 'manual'
17
+ return 'scheduled'
18
+ }
19
+
20
+ export const STATE_LABELS = {
21
+ running: 'Running',
22
+ paused: 'Paused',
23
+ manual: 'Manual only',
24
+ scheduled: 'Scheduled',
25
+ unknown: 'Unknown'
26
+ }
27
+
28
+ /**
29
+ * A cron expression, in English — for the five-field expressions people
30
+ * actually write. Anything else comes back null and the caller shows the
31
+ * expression itself, which is honest: a wrong plain-English reading of a cron
32
+ * is worse than the cron, because you cannot tell it is wrong.
33
+ */
34
+ export function describeCron(expr) {
35
+ if (!expr || typeof expr !== 'string') return null
36
+ const f = expr.trim().split(/\s+/)
37
+ if (f.length !== 5) return null
38
+ const [min, hour, dom, mon, dow] = f
39
+
40
+ const every = (v) => v === '*'
41
+ const num = (v) => (/^\d+$/.test(v) ? Number(v) : null)
42
+ const at = (h, m) => `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}`
43
+
44
+ const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
45
+
46
+ // */n in the minute field — the commonest schedule anybody writes.
47
+ const stepMin = /^\*\/(\d+)$/.exec(min)
48
+ if (stepMin && every(hour) && every(dom) && every(mon) && every(dow)) {
49
+ const n = Number(stepMin[1])
50
+ return n === 1 ? 'Every minute' : `Every ${n} minutes`
51
+ }
52
+
53
+ const stepHour = /^\*\/(\d+)$/.exec(hour)
54
+ if (num(min) !== null && stepHour && every(dom) && every(mon) && every(dow)) {
55
+ const n = Number(stepHour[1])
56
+ return n === 1 ? `Every hour, at ${min} past` : `Every ${n} hours, at ${min} past`
57
+ }
58
+
59
+ if (every(min) && every(hour) && every(dom) && every(mon) && every(dow)) return 'Every minute'
60
+ if (num(min) !== null && every(hour) && every(dom) && every(mon) && every(dow)) {
61
+ return `Every hour, at ${min} past`
62
+ }
63
+ if (num(min) !== null && num(hour) !== null && every(dom) && every(mon) && every(dow)) {
64
+ return `Every day at ${at(hour, min)}`
65
+ }
66
+ if (num(min) !== null && num(hour) !== null && every(dom) && every(mon) && num(dow) !== null) {
67
+ return `Every ${DAYS[num(dow) % 7]} at ${at(hour, min)}`
68
+ }
69
+ if (num(min) !== null && num(hour) !== null && num(dom) !== null && every(mon) && every(dow)) {
70
+ return `Monthly on day ${dom} at ${at(hour, min)}`
71
+ }
72
+ return null
73
+ }
74
+
75
+ /**
76
+ * When the next run is due, as something a person reads.
77
+ *
78
+ * Deliberately says OVERDUE rather than "in -2 minutes". A due time in the
79
+ * past means the scheduler has not picked it — paused, not running, or stuck
80
+ * with `running` left true by a process that died — and that is worth seeing
81
+ * rather than rendering as a negative number.
82
+ */
83
+ export function nextRunLabel(value, now) {
84
+ if (!value) return null
85
+ const at = new Date(value).getTime()
86
+ if (Number.isNaN(at)) return null
87
+ const delta = at - (now || Date.now())
88
+ if (delta <= 0) return 'due now'
89
+ const mins = Math.round(delta / 60000)
90
+ if (mins < 1) return 'in under a minute'
91
+ if (mins < 60) return `in ${mins} minute${mins === 1 ? '' : 's'}`
92
+ const hours = Math.round(mins / 60)
93
+ if (hours < 24) return `in ${hours} hour${hours === 1 ? '' : 's'}`
94
+ const days = Math.round(hours / 24)
95
+ return `in ${days} day${days === 1 ? '' : 's'}`
96
+ }
97
+
98
+ /** How long a run took. Sub-second matters here — most actions are quick. */
99
+ export function durationLabel(ms) {
100
+ if (ms === null || ms === undefined) return null
101
+ if (ms < 1000) return `${ms}ms`
102
+ if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`
103
+ const mins = Math.floor(ms / 60000)
104
+ const secs = Math.round((ms % 60000) / 1000)
105
+ return `${mins}m ${secs}s`
106
+ }
107
+
108
+ /**
109
+ * The last run of each job, keyed by job id.
110
+ *
111
+ * The LATEST by start time, not the last in the array — occurrences arrive in
112
+ * whatever order the list endpoint returns them, and "the last one we were
113
+ * sent" is not a fact about when anything ran.
114
+ */
115
+ export function latestByJob(occurrences) {
116
+ const out = {}
117
+ ;(occurrences || []).forEach((o) => {
118
+ if (!o || !o.jobId) return
119
+ const prev = out[o.jobId]
120
+ if (!prev || new Date(o.startedAt).getTime() > new Date(prev.startedAt).getTime()) out[o.jobId] = o
121
+ })
122
+ return out
123
+ }
124
+
125
+ /** Search and sort. No paging — a workspace's job list is short by nature. */
126
+ export function prepareJobs(jobs, { query = '', sort = 'name' } = {}) {
127
+ const q = query.trim().toLowerCase()
128
+ let rows = (jobs || []).filter((j) => {
129
+ if (!q) return true
130
+ // The ACTION is searched too: "which jobs push to the database" is a real
131
+ // question, and the action is the answer to it.
132
+ return [j.name, j.description, j.actionName].filter(Boolean).join(' ').toLowerCase().includes(q)
133
+ })
134
+
135
+ rows = rows.slice().sort((a, b) => {
136
+ if (sort === 'next') {
137
+ // Jobs with no next run sort LAST rather than first — a manual job is
138
+ // not "due before everything else", it is simply not due.
139
+ const at = a.nextRunAt ? new Date(a.nextRunAt).getTime() : Infinity
140
+ const bt = b.nextRunAt ? new Date(b.nextRunAt).getTime() : Infinity
141
+ return at - bt
142
+ }
143
+ if (sort === 'action') return String(a.actionName || '').localeCompare(String(b.actionName || ''))
144
+ return String(a.name || '').localeCompare(String(b.name || ''))
145
+ })
146
+
147
+ return rows
148
+ }
149
+
150
+ /**
151
+ * A running occurrence's progress, in words — or null when there is nothing
152
+ * honest to say yet.
153
+ *
154
+ * ROWS READ, NOT A PERCENTAGE, and that is not a shortcut. The uploader counts
155
+ * what it has read, because the write queue drains behind the read and waiting
156
+ * for confirmation would report zero for minutes; and there is no denominator
157
+ * to divide by, because a query source has no row count without running it
158
+ * twice. A progress bar here would have to invent the total, and an invented
159
+ * total is worse than a number that is simply true.
160
+ *
161
+ * Null while progress is absent — the first throttled write has not landed
162
+ * yet, or the action reports nothing at all. Callers show "Running" on its own
163
+ * rather than waiting for a number that may never come.
164
+ */
165
+ export function progressLabel(occurrence) {
166
+ const p = occurrence && occurrence.progress
167
+ if (!p || p.rowsRead == null) return null
168
+ return `${formatCount(p.rowsRead)} rows`
169
+ }
170
+
171
+ /** 1200 → "1.2K", 3400000 → "3.4M". Thousands separators past nine digits stop
172
+ * being readable at a glance, and a glance is all this gets. */
173
+ export function formatCount(n) {
174
+ if (n == null || isNaN(n)) return '—'
175
+ if (n < 1000) return String(n)
176
+ if (n < 1000000) return `${(n / 1000).toFixed(n < 10000 ? 1 : 0)}K`
177
+ return `${(n / 1000000).toFixed(n < 10000000 ? 1 : 0)}M`
178
+ }
179
+
180
+ /**
181
+ * Whether a live run has gone QUIET — reporting, then stopping.
182
+ *
183
+ * `progress.at` is the moment of the last update. A job that reported 400K
184
+ * rows nine minutes ago and nothing since is not the same as one that reported
185
+ * it two seconds ago, and the numbers alone cannot tell them apart. This is
186
+ * what lets a row say "no update for 9m" instead of showing a stale count as
187
+ * though it were current.
188
+ *
189
+ * Advisory only — a movement legitimately goes quiet during a long final
190
+ * drain. It is a prompt to look, not a verdict.
191
+ */
192
+ export function progressStale(occurrence, now = Date.now(), toleranceMs = 120000) {
193
+ const at = occurrence && occurrence.progress && occurrence.progress.at
194
+ if (!at) return false
195
+ return now - new Date(at).getTime() > toleranceMs
196
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * "3 minutes ago", for the run history.
3
+ *
4
+ * Carried here rather than imported from a host: this package cannot depend on
5
+ * xeplr-bi, and a shared date helper is not worth a package of its own. Lifted
6
+ * verbatim from xeplr-bi's reportsList.js so the two read identically — if one
7
+ * ever needs to change, both should.
8
+ */
9
+ export function relativeTime(value, now) {
10
+ if (!value) return null;
11
+ const then = new Date(value).getTime();
12
+ if (Number.isNaN(then)) return null;
13
+ const seconds = Math.round(((now || Date.now()) - then) / 1000);
14
+ if (seconds < 60) return 'just now';
15
+ const minutes = Math.round(seconds / 60);
16
+ if (minutes < 60) return `${minutes} minute${minutes === 1 ? '' : 's'} ago`;
17
+ const hours = Math.round(minutes / 60);
18
+ if (hours < 24) return `${hours} hour${hours === 1 ? '' : 's'} ago`;
19
+ const days = Math.round(hours / 24);
20
+ if (days === 1) return 'yesterday';
21
+ if (days < 30) return `${days} days ago`;
22
+ const months = Math.round(days / 30);
23
+ if (months < 12) return `${months} month${months === 1 ? '' : 's'} ago`;
24
+ const years = Math.round(months / 12);
25
+ return `${years} year${years === 1 ? '' : 's'} ago`;
26
+ }
package/src/routes.js ADDED
@@ -0,0 +1,14 @@
1
+ // The paths this package's pages live at, in ONE place.
2
+ //
3
+ // A host mounts these inside its own <Routes>, so the path here is relative to
4
+ // wherever the host put them — see registerJobsUI's `basePath`. Kept as
5
+ // functions rather than string constants for the same reason the rest of the
6
+ // suite does: a path that takes an id later does not become a different kind
7
+ // of thing to call.
8
+ export const ROUTES = {
9
+ jobs: (base) => (base || '') + '/jobs'
10
+ };
11
+
12
+ // Where the standalone app puts them. A host overrides this — xeplr-bi mounts
13
+ // at /workspace/jobs, because in that product a job belongs to a workspace.
14
+ export const DEFAULT_BASE = '';