@xeplr/ui-jobs 1.1.0 → 1.3.0
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/README.md +2 -0
- package/package.json +1 -1
- package/src/api/jobs.js +10 -0
- package/src/components/ParametersField.css +20 -0
- package/src/components/ParametersField.jsx +85 -0
- package/src/lib/parametersModel.js +72 -0
- package/src/pages/Jobs.jsx +19 -4
- package/src/useRunContract.js +23 -0
package/README.md
CHANGED
|
@@ -134,6 +134,8 @@ Three steps (four with `chainsApiBase`), all reachable at any time, with **Save
|
|
|
134
134
|
|
|
135
135
|
The period this job ran for passes to the next job by itself. Text that is not JSON, a missing job, or an "otherwise" that is not last is refused before anything is saved; the server checks the meaning and names the rule. An existing job's rules are saved before the job, and a new job's right after it (it needs its id).
|
|
136
136
|
|
|
137
|
+
An input the action marks `control: 'parameters'` (a procedure's parameters) is shown as rows, not JSON (`ParametersField`, `lib/parametersModel.js`): a parameter's name, and where its value comes from: **Start of the window**, **End of the window** (saved as `{window.from}` / `{window.to}`, which `@xeplr/jobs` fills in with the run's dates) or a **Fixed value**. The choices are the jobs package's run contract (`GET /jobs/run-contract`, `useRunContract`), grouped: **This run** (start / end of the window) and **The job before** (the range it was given and the range it actually used, start / end). A job with no parameters yet starts with the two window rows, and one whose name is left empty is not saved. An input marked `hidden` (the procedure's old **Window**) is shown only when a job already holds one.
|
|
138
|
+
|
|
137
139
|
With `chainsApiBase`, **Inputs** also has **From the job before**: tickboxes for the inputs another job may fill when it runs this one (the job's `acceptsInputs`). No field is ticked by default, and a field naming a connection is never offered.
|
|
138
140
|
|
|
139
141
|
A new job with a schedule defaults to **Paused**, so it can be written now and started later. A job with no schedule is saved as `ready` and runs only when triggered.
|
package/package.json
CHANGED
package/src/api/jobs.js
CHANGED
|
@@ -185,3 +185,13 @@ export async function fetchOccurrenceLogs(occurrenceId) {
|
|
|
185
185
|
const res = await authFetch(`/logs?referenceId=${encodeURIComponent(occurrenceId)}`)
|
|
186
186
|
return res.dataArray || []
|
|
187
187
|
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* What every run of a job receives — its window's start and end — as the
|
|
191
|
+
* names an input binds to: [{ key, label, description }]. From @xeplr/jobs'
|
|
192
|
+
* GET /jobs/run-contract, so the dropdowns offer exactly what the server fills.
|
|
193
|
+
*/
|
|
194
|
+
export async function getRunContract() {
|
|
195
|
+
const res = await authFetch(url('/jobs/run-contract'))
|
|
196
|
+
return res.dataArray || []
|
|
197
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* Parameters as rows — name ← where its value comes from. Widths set here
|
|
2
|
+
because the editor gives every input full width. */
|
|
3
|
+
.jb-params { display: grid; gap: 8px; margin: 6px 0 8px; }
|
|
4
|
+
.jb-param {
|
|
5
|
+
display: grid; align-items: center; gap: 8px;
|
|
6
|
+
grid-template-columns: minmax(140px, 220px) auto minmax(180px, 240px) minmax(0, 1fr) auto;
|
|
7
|
+
}
|
|
8
|
+
.jb-editor .jb-param input, .jb-editor .jb-param select { width: 100%; min-width: 0; }
|
|
9
|
+
.jb-param-name { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
|
|
10
|
+
.jb-param-arrow { color: var(--muted, #6b6a63); }
|
|
11
|
+
.jb-param-gap { min-width: 0; }
|
|
12
|
+
.jb-param-remove, .jb-param-add {
|
|
13
|
+
padding: 5px 10px; font: inherit; font-size: 12px; color: var(--ink, #1a1a19);
|
|
14
|
+
background: var(--surface, #ffffff); border: 1px solid var(--hair, #e2e1d9); border-radius: 7px; cursor: pointer;
|
|
15
|
+
}
|
|
16
|
+
.jb-param-add { justify-self: start; font-size: 13px; }
|
|
17
|
+
@media (max-width: 640px) {
|
|
18
|
+
.jb-param { grid-template-columns: 1fr auto; }
|
|
19
|
+
.jb-param-arrow { display: none; }
|
|
20
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { useState } from 'react'
|
|
2
|
+
import { FIXED, groupedChoices, rowsFromText, rowsToText, sourceOf, valueForSource } from '../lib/parametersModel.js'
|
|
3
|
+
import './ParametersField.css'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parameters as rows: a name, and where its value comes from — the start or
|
|
7
|
+
* end of the run's window, or a fixed value. Presentation and row editing only;
|
|
8
|
+
* the conversion is lib/parametersModel.js.
|
|
9
|
+
*
|
|
10
|
+
* `value` / `onChange` carry the saved text ([{ name, value }] as JSON), so
|
|
11
|
+
* the editor saves this field like any other list.
|
|
12
|
+
*
|
|
13
|
+
* @param {object} props.field the action's input ({ name, label, description })
|
|
14
|
+
* @param {string} props.value saved JSON text
|
|
15
|
+
* @param {Function} props.onChange
|
|
16
|
+
* @param {object[]} props.contract what every run receives (useRunContract)
|
|
17
|
+
*/
|
|
18
|
+
export default function ParametersField({ field, value, onChange, contract }) {
|
|
19
|
+
const [rows, setRows] = useState(() => rowsFromText(value, contract))
|
|
20
|
+
const id = `jb-in-${field.name}`
|
|
21
|
+
|
|
22
|
+
// Saved text that is not a list (typed by hand before this control
|
|
23
|
+
// existed) is shown as it is rather than guessed at.
|
|
24
|
+
if (rows === null) {
|
|
25
|
+
return (
|
|
26
|
+
<div className="jb-field jb-field--wide">
|
|
27
|
+
<label htmlFor={id}>{field.label || field.name}</label>
|
|
28
|
+
<textarea id={id} rows={4} spellCheck="false" value={value} onChange={(e) => onChange(e.target.value)} />
|
|
29
|
+
<span className="jb-help jb-warn">This is not a list of parameters, so it is shown as it was saved.</span>
|
|
30
|
+
</div>
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function update(next) {
|
|
35
|
+
setRows(next)
|
|
36
|
+
onChange(rowsToText(next))
|
|
37
|
+
}
|
|
38
|
+
const patchRow = (i, p) => update(rows.map((r, j) => (j === i ? { ...r, ...p } : r)))
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<div className="jb-field jb-field--wide">
|
|
42
|
+
<label>{field.label || field.name}</label>
|
|
43
|
+
{contract.length > 0 && (
|
|
44
|
+
<span className="jb-help">
|
|
45
|
+
Pick each value from what every run receives: this run's window (from its schedule or the job
|
|
46
|
+
before it), and, when another job started it, the range that job was given and the range it
|
|
47
|
+
actually used. Anything else is a fixed value.
|
|
48
|
+
</span>
|
|
49
|
+
)}
|
|
50
|
+
<div className="jb-params">
|
|
51
|
+
{rows.map((r, i) => {
|
|
52
|
+
const source = sourceOf(r.value, contract)
|
|
53
|
+
return (
|
|
54
|
+
<div key={i} className="jb-param">
|
|
55
|
+
<input aria-label={`Parameter ${i + 1} name`} className="jb-param-name" value={r.name}
|
|
56
|
+
placeholder={source === FIXED ? 'name' : 'e.g. startDate'}
|
|
57
|
+
onChange={(e) => patchRow(i, { name: e.target.value })} />
|
|
58
|
+
<span className="jb-param-arrow" aria-hidden="true">←</span>
|
|
59
|
+
<select aria-label={`Parameter ${i + 1} value`} value={source}
|
|
60
|
+
onChange={(e) => patchRow(i, { value: valueForSource(e.target.value, r.value, contract) })}>
|
|
61
|
+
{groupedChoices(contract).map((g) => (
|
|
62
|
+
<optgroup key={g.group} label={g.group}>
|
|
63
|
+
{g.options.map((c) => <option key={c.key} value={c.key} title={c.description}>{c.label}</option>)}
|
|
64
|
+
</optgroup>
|
|
65
|
+
))}
|
|
66
|
+
<option value={FIXED}>Fixed value</option>
|
|
67
|
+
</select>
|
|
68
|
+
{source === FIXED ? (
|
|
69
|
+
<input aria-label={`Parameter ${i + 1} fixed value`} className="jb-param-value" value={r.value == null ? '' : String(r.value)}
|
|
70
|
+
placeholder="value" onChange={(e) => patchRow(i, { value: e.target.value })} />
|
|
71
|
+
) : <span className="jb-param-gap" />}
|
|
72
|
+
<button type="button" className="jb-param-remove" title="Remove this parameter"
|
|
73
|
+
onClick={() => update(rows.filter((_, j) => j !== i))}>✕</button>
|
|
74
|
+
</div>
|
|
75
|
+
)
|
|
76
|
+
})}
|
|
77
|
+
</div>
|
|
78
|
+
<button type="button" className="jb-param-add" onClick={() => update([...rows, { name: '', value: '' }])}>+ Add parameter</button>
|
|
79
|
+
<span className="jb-help">
|
|
80
|
+
The name is the procedure's own parameter name. On MySQL, parameters go by position, so keep
|
|
81
|
+
them in the order the procedure declares them.
|
|
82
|
+
</span>
|
|
83
|
+
</div>
|
|
84
|
+
)
|
|
85
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// A PROCEDURE'S PARAMETERS AS ROWS — the form's half, as pure logic.
|
|
2
|
+
//
|
|
3
|
+
// What a job saves is the list the procedure step takes, [{ name, value }]
|
|
4
|
+
// (or bare values, positional). A value is either fixed text or BOUND to the
|
|
5
|
+
// run's window: "{window.from}" / "{window.to}", which @xeplr/jobs fills in
|
|
6
|
+
// with this run's dates (its run contract, GET /jobs/run-contract). This file
|
|
7
|
+
// turns that list into rows a person edits — a name, and where its value
|
|
8
|
+
// comes from — and back.
|
|
9
|
+
|
|
10
|
+
export const FIXED = 'fixed'
|
|
11
|
+
|
|
12
|
+
export function bindingFor(key) {
|
|
13
|
+
return '{' + key + '}'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The contract key a value is bound to, or FIXED. */
|
|
17
|
+
export function sourceOf(value, contract) {
|
|
18
|
+
const hit = (contract || []).find((c) => value === bindingFor(c.key))
|
|
19
|
+
return hit ? hit.key : FIXED
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The contract grouped for a dropdown, in its own order:
|
|
24
|
+
* [{ group, options: [{ key, label }] }].
|
|
25
|
+
*/
|
|
26
|
+
export function groupedChoices(contract) {
|
|
27
|
+
const groups = []
|
|
28
|
+
;(contract || []).forEach((c) => {
|
|
29
|
+
const name = c.group || ''
|
|
30
|
+
let g = groups.find((x) => x.group === name)
|
|
31
|
+
if (!g) { g = { group: name, options: [] }; groups.push(g) }
|
|
32
|
+
g.options.push(c)
|
|
33
|
+
})
|
|
34
|
+
return groups
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Saved text → rows. A job with no parameters yet starts with one row for
|
|
39
|
+
* this run's window start and one for its end, named by the person — the
|
|
40
|
+
* period is what nearly every scheduled procedure takes. The job before's
|
|
41
|
+
* ranges are in every row's dropdown, but not rows of their own.
|
|
42
|
+
*/
|
|
43
|
+
export function rowsFromText(text, contract) {
|
|
44
|
+
let list = []
|
|
45
|
+
try { list = text ? JSON.parse(text) : [] } catch (e) { list = null }
|
|
46
|
+
if (!Array.isArray(list)) return null
|
|
47
|
+
if (!list.length) {
|
|
48
|
+
return (contract || []).filter((c) => /^window\./.test(c.key))
|
|
49
|
+
.map((c) => ({ name: '', value: bindingFor(c.key), starter: true }))
|
|
50
|
+
}
|
|
51
|
+
return list.map((p) => (p && typeof p === 'object' && !Array.isArray(p)
|
|
52
|
+
? { name: p.name == null ? '' : String(p.name), value: p.value == null ? '' : p.value }
|
|
53
|
+
: { name: '', value: p == null ? '' : p, bare: true }))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Rows → the text the form saves. A starter row nobody named is left out, so
|
|
58
|
+
* a procedure with no date parameters gets none. A row with no name is kept
|
|
59
|
+
* as a bare (positional) value.
|
|
60
|
+
*/
|
|
61
|
+
export function rowsToText(rows) {
|
|
62
|
+
const list = (rows || [])
|
|
63
|
+
.filter((r) => !(r.starter && !String(r.name || '').trim()))
|
|
64
|
+
.map((r) => (String(r.name || '').trim() ? { name: String(r.name).trim(), value: r.value } : r.value))
|
|
65
|
+
return list.length ? JSON.stringify(list, null, 2) : ''
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A row's value when its source changes: a binding, or empty fixed text. */
|
|
69
|
+
export function valueForSource(source, current, contract) {
|
|
70
|
+
if (source === FIXED) return sourceOf(current, contract) === FIXED ? current : ''
|
|
71
|
+
return bindingFor(source)
|
|
72
|
+
}
|
package/src/pages/Jobs.jsx
CHANGED
|
@@ -12,6 +12,8 @@ import {
|
|
|
12
12
|
} from './jobsList.js'
|
|
13
13
|
import CronScheduleField from '../components/CronScheduleField.jsx'
|
|
14
14
|
import JobChainSection from '../components/JobChainSection.jsx'
|
|
15
|
+
import ParametersField from '../components/ParametersField.jsx'
|
|
16
|
+
import { useRunContract } from '../useRunContract.js'
|
|
15
17
|
import { useJobChainController } from '../useJobChainController.js'
|
|
16
18
|
import { acceptableFields, toggleAccepted } from '../lib/jobChainModel.js'
|
|
17
19
|
import './Jobs.css'
|
|
@@ -274,8 +276,14 @@ const AFTER_STEP = { key: 'after', label: 'After this job', hint: 'What runs whe
|
|
|
274
276
|
* would be asking somebody to type a password into a form, which is exactly
|
|
275
277
|
* what storing an id instead was for.
|
|
276
278
|
*/
|
|
277
|
-
function visibleFields(action) {
|
|
278
|
-
|
|
279
|
+
function visibleFields(action, inputs) {
|
|
280
|
+
// `hidden` (an action's hint): left out unless this job already holds one,
|
|
281
|
+
// so nothing saved is ever invisible.
|
|
282
|
+
const held = (f) => {
|
|
283
|
+
const v = inputs?.[f.name]
|
|
284
|
+
return v !== undefined && v !== '' && v !== '{}' && v !== '[]'
|
|
285
|
+
}
|
|
286
|
+
return (action?.inputSchema || []).filter((f) => !f.system && (!f.hidden || held(f)))
|
|
279
287
|
}
|
|
280
288
|
|
|
281
289
|
// ── labels ────────────────────────────────────────────────────────────────
|
|
@@ -435,8 +443,13 @@ function RemoteSelect({ field, value, onChange, dependsOnValue }) {
|
|
|
435
443
|
}
|
|
436
444
|
|
|
437
445
|
/** One input, rendered as whatever its declared type actually is. */
|
|
438
|
-
function ActionInput({ field, value, onChange, inputs }) {
|
|
446
|
+
function ActionInput({ field, value, onChange, inputs, contract }) {
|
|
439
447
|
const id = `jb-in-${field.name}`
|
|
448
|
+
|
|
449
|
+
// Rows rather than JSON, for an input the action marks as parameters.
|
|
450
|
+
if (field.control === 'parameters') {
|
|
451
|
+
return <ParametersField field={field} value={value} onChange={onChange} contract={contract || []} />
|
|
452
|
+
}
|
|
440
453
|
const common = { id, value, onChange: (e) => onChange(e.target.value) }
|
|
441
454
|
|
|
442
455
|
// A host-provided list, when the host has one. Without a source configured
|
|
@@ -529,6 +542,7 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
|
|
|
529
542
|
const [saving, setSaving] = useState(false)
|
|
530
543
|
const [step, setStep] = useState(0)
|
|
531
544
|
const chain = useJobChainController(job?.id || null)
|
|
545
|
+
const contract = useRunContract()
|
|
532
546
|
const STEPS = chain.enabled ? [...BASE_STEPS, AFTER_STEP] : BASE_STEPS
|
|
533
547
|
|
|
534
548
|
const action = actions.find((a) => a.name === draft.actionName) || null
|
|
@@ -604,7 +618,7 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
|
|
|
604
618
|
}
|
|
605
619
|
}
|
|
606
620
|
|
|
607
|
-
const fields = visibleFields(action)
|
|
621
|
+
const fields = visibleFields(action, draft.inputs)
|
|
608
622
|
|
|
609
623
|
return (
|
|
610
624
|
<form className="jb-editor" onSubmit={submit}>
|
|
@@ -678,6 +692,7 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
|
|
|
678
692
|
key={f.name}
|
|
679
693
|
field={f}
|
|
680
694
|
inputs={draft.inputs}
|
|
695
|
+
contract={contract}
|
|
681
696
|
value={draft.inputs?.[f.name] ?? ''}
|
|
682
697
|
onChange={(v) => patch({ inputs: { ...draft.inputs, [f.name]: v } })}
|
|
683
698
|
/>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react'
|
|
2
|
+
import { getRunContract } from './api/jobs.js'
|
|
3
|
+
|
|
4
|
+
// Fetched once per page load: it is a fact about the server, not about a job.
|
|
5
|
+
let cached = null
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The run contract (what every run receives), for the parameter dropdowns.
|
|
9
|
+
* Empty until it arrives, and empty when the server has none to give — the
|
|
10
|
+
* rows then offer only a fixed value.
|
|
11
|
+
*/
|
|
12
|
+
export function useRunContract() {
|
|
13
|
+
const [contract, setContract] = useState(cached || [])
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (cached) return
|
|
16
|
+
let live = true
|
|
17
|
+
getRunContract()
|
|
18
|
+
.then((c) => { cached = c; if (live) setContract(c) })
|
|
19
|
+
.catch(() => { if (live) setContract([]) })
|
|
20
|
+
return () => { live = false }
|
|
21
|
+
}, [])
|
|
22
|
+
return contract
|
|
23
|
+
}
|