@xeplr/ui-jobs 1.2.0 → 1.4.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 CHANGED
@@ -107,6 +107,7 @@ Every call goes through `authFetch`, which prefixes the base URL `@xeplr/ui-acco
107
107
  | pause / resume | `POST /jobs/:id/pause`, `POST /jobs/:id/resume` |
108
108
  | a run's log lines | `GET /logs?referenceId=<occurrenceId>` — **not** under `apiBase` |
109
109
  | an option list | `GET <source.url>?limit=0` — under `apiBase` |
110
+ | name suggestions | `GET <source.url>?limit=0&<param>=<value>…` for each of the field's `suggestWith` — under `apiBase` |
110
111
  | a job's "After this job" rules | `GET` / `PUT <chainsApiBase>/job-chains/:jobId` with `{ rules, jobName }` — **not** under `apiBase` |
111
112
 
112
113
  `/logs` is the platform's generic log route, not part of `@xeplr/jobs`. A host without it answers 404 and the panel shows an error in place of the log. Log lines are keyed by occurrence id because an action writes under the `referenceId` it is handed.
@@ -134,9 +135,9 @@ Three steps (four with `chainsApiBase`), all reachable at any time, with **Save
134
135
 
135
136
  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
137
 
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`). 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
+ 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 starts with none (**+ Add parameter**). When the action names a `suggestFrom` source (`db-procedure`: `procedureParams`, asked with the connection, database and procedure), clicking a name box offers the procedure's own parameter names, minus those already used. A row with neither name nor value is not saved. An input marked `hidden` (the procedure's old **Window**) is shown only when a job already holds one.
138
139
 
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.
140
+ A job's saved `acceptsInputs` (what the job before may fill) is kept as it is but no longer edited here: a job takes what it needs from the job before through its parameters. The **After this job** rules offer a condition, the job to run and values to **carry** for later jobs.
140
141
 
141
142
  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.
142
143
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xeplr/ui-jobs",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "The screens for @xeplr/jobs — schedule, run, pause and watch jobs — as routes a host app registers, or a standalone app",
5
5
  "type": "module",
6
6
  "main": "src/index.jsx",
package/src/api/jobs.js CHANGED
@@ -90,6 +90,22 @@ export function hasOptionSource(kind) {
90
90
  * be narrowed — databases on the chosen connection, rather than every database
91
91
  * on every connection.
92
92
  */
93
+ /**
94
+ * Fetch suggestions from an option source, asked with several values at once
95
+ * — `query` is { param: value }. Answers the list of values (strings), empty
96
+ * when the source is not configured or something it needs is not chosen yet.
97
+ */
98
+ export async function fetchSuggestions(kind, query) {
99
+ const src = _optionSources[kind]
100
+ if (!src) return []
101
+ const pairs = Object.entries(query || {})
102
+ if (pairs.some(([, v]) => v === undefined || v === null || v === '')) return []
103
+ const q = pairs.map(([k, v]) => `&${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('')
104
+ const res = await authFetch(url(`${src.url}?limit=0${q}`))
105
+ const list = (res && res[src.key || 'dataArray']) || []
106
+ return list.map((o) => String(o[src.value || 'value'])).filter(Boolean)
107
+ }
108
+
93
109
  export async function fetchOptions(kind, filter) {
94
110
  const src = _optionSources[kind]
95
111
  if (!src) return []
@@ -65,18 +65,8 @@ export default function JobChainSection({ chain, jobs, jobId }) {
65
65
  </select>
66
66
  </div>
67
67
 
68
- <details className="jb-advanced" open={Boolean(row.inputs || row.carry)}>
68
+ <details className="jb-advanced" open={Boolean(row.carry)}>
69
69
  <summary>Pass values on <span className="jb-dim">— optional</span></summary>
70
- <div className="jb-field">
71
- <label htmlFor={`jb-chain-inputs-${i}`}>Inputs of the next job <span className="jb-type">· JSON</span></label>
72
- <textarea id={`jb-chain-inputs-${i}`} className="jb-chain-json" rows={3}
73
- placeholder={'{ "region": "{output.region}" }'} value={row.inputs}
74
- onChange={(e) => chain.updateRow(i, { inputs: e.target.value })} />
75
- <span className="jb-help">
76
- Only inputs the next job accepts (its Inputs step lists them). Leave out
77
- <code className="jb-code">window</code> to pass this job's period on unchanged.
78
- </span>
79
- </div>
80
70
  <div className="jb-field">
81
71
  <label htmlFor={`jb-chain-carry-${i}`}>Carry for later jobs <span className="jb-type">· JSON</span></label>
82
72
  <textarea id={`jb-chain-carry-${i}`} className="jb-chain-json" rows={3}
@@ -1,5 +1,6 @@
1
- import { useState } from 'react'
2
- import { FIXED, rowsFromText, rowsToText, sourceOf, valueForSource } from '../lib/parametersModel.js'
1
+ import { useEffect, useState } from 'react'
2
+ import { fetchSuggestions } from '../api/jobs.js'
3
+ import { FIXED, groupedChoices, rowsFromText, rowsToText, sourceOf, unusedNames, valueForSource } from '../lib/parametersModel.js'
3
4
  import './ParametersField.css'
4
5
 
5
6
  /**
@@ -14,11 +15,27 @@ import './ParametersField.css'
14
15
  * @param {string} props.value saved JSON text
15
16
  * @param {Function} props.onChange
16
17
  * @param {object[]} props.contract what every run receives (useRunContract)
18
+ * @param {object} props.inputs the job's other inputs — what the name
19
+ * suggestions are asked with (field.suggestWith)
17
20
  */
18
- export default function ParametersField({ field, value, onChange, contract }) {
21
+ export default function ParametersField({ field, value, onChange, contract, inputs }) {
19
22
  const [rows, setRows] = useState(() => rowsFromText(value, contract))
20
23
  const id = `jb-in-${field.name}`
21
24
 
25
+ // THE PROCEDURE'S OWN PARAMETER NAMES, offered when a name box is clicked —
26
+ // asked again whenever the connection, database or procedure changes.
27
+ const query = Object.fromEntries(Object.entries(field.suggestWith || {}).map(([k, from]) => [k, inputs?.[from]]))
28
+ const queryKey = JSON.stringify(query)
29
+ const [names, setNames] = useState([])
30
+ useEffect(() => {
31
+ if (!field.suggestFrom) return undefined
32
+ let live = true
33
+ fetchSuggestions(field.suggestFrom, JSON.parse(queryKey))
34
+ .then((n) => { if (live) setNames(n) })
35
+ .catch(() => { if (live) setNames([]) })
36
+ return () => { live = false }
37
+ }, [field.suggestFrom, queryKey])
38
+
22
39
  // Saved text that is not a list (typed by hand before this control
23
40
  // existed) is shown as it is rather than guessed at.
24
41
  if (rows === null) {
@@ -42,8 +59,9 @@ export default function ParametersField({ field, value, onChange, contract }) {
42
59
  <label>{field.label || field.name}</label>
43
60
  {contract.length > 0 && (
44
61
  <span className="jb-help">
45
- Every run receives a window, from this job's schedule or from the job before it:{' '}
46
- {contract.map((c) => c.label.toLowerCase()).join(' and ')}. Pick it as a parameter's value.
62
+ Pick each value from what every run receives: this run's window (from its schedule or the job
63
+ before it), and, when another job started it, the range that job was given and the range it
64
+ actually used. Anything else is a fixed value.
47
65
  </span>
48
66
  )}
49
67
  <div className="jb-params">
@@ -52,12 +70,19 @@ export default function ParametersField({ field, value, onChange, contract }) {
52
70
  return (
53
71
  <div key={i} className="jb-param">
54
72
  <input aria-label={`Parameter ${i + 1} name`} className="jb-param-name" value={r.name}
55
- placeholder={source === FIXED ? 'name' : 'e.g. startDate'}
73
+ list={`${id}-names-${i}`} placeholder="name"
56
74
  onChange={(e) => patchRow(i, { name: e.target.value })} />
75
+ <datalist id={`${id}-names-${i}`}>
76
+ {unusedNames(names, rows, i).map((n) => <option key={n} value={n} />)}
77
+ </datalist>
57
78
  <span className="jb-param-arrow" aria-hidden="true">←</span>
58
79
  <select aria-label={`Parameter ${i + 1} value`} value={source}
59
80
  onChange={(e) => patchRow(i, { value: valueForSource(e.target.value, r.value, contract) })}>
60
- {contract.map((c) => <option key={c.key} value={c.key}>{c.label}</option>)}
81
+ {groupedChoices(contract).map((g) => (
82
+ <optgroup key={g.group} label={g.group}>
83
+ {g.options.map((c) => <option key={c.key} value={c.key} title={c.description}>{c.label}</option>)}
84
+ </optgroup>
85
+ ))}
61
86
  <option value={FIXED}>Fixed value</option>
62
87
  </select>
63
88
  {source === FIXED ? (
@@ -72,8 +97,10 @@ export default function ParametersField({ field, value, onChange, contract }) {
72
97
  </div>
73
98
  <button type="button" className="jb-param-add" onClick={() => update([...rows, { name: '', value: '' }])}>+ Add parameter</button>
74
99
  <span className="jb-help">
75
- The name is the procedure's own parameter name. On MySQL, parameters go by position, so keep
76
- them in the order the procedure declares them.
100
+ {names.length
101
+ ? `Click a name box to pick one of the procedure's parameters (${names.join(', ')}), or type your own.`
102
+ : 'The name is the procedure\'s own parameter name.'}
103
+ {' '}On MySQL, parameters go by position, so keep them in the order the procedure declares them.
77
104
  </span>
78
105
  </div>
79
106
  )
@@ -20,34 +20,50 @@ export function sourceOf(value, contract) {
20
20
  }
21
21
 
22
22
  /**
23
- * Saved text → rows. A job with no parameters yet starts with one row per
24
- * contract value (the window's start and end), named by the person — the
25
- * period is what nearly every scheduled procedure takes.
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 has none: the person adds
39
+ * them, picking each name from the procedure's own (see ParametersField).
26
40
  */
27
41
  export function rowsFromText(text, contract) {
28
42
  let list = []
29
43
  try { list = text ? JSON.parse(text) : [] } catch (e) { list = null }
30
44
  if (!Array.isArray(list)) return null
31
- if (!list.length) {
32
- return (contract || []).map((c) => ({ name: '', value: bindingFor(c.key), starter: true }))
33
- }
34
45
  return list.map((p) => (p && typeof p === 'object' && !Array.isArray(p)
35
46
  ? { name: p.name == null ? '' : String(p.name), value: p.value == null ? '' : p.value }
36
47
  : { name: '', value: p == null ? '' : p, bare: true }))
37
48
  }
38
49
 
39
50
  /**
40
- * Rows → the text the form saves. A starter row nobody named is left out, so
41
- * a procedure with no date parameters gets none. A row with no name is kept
42
- * as a bare (positional) value.
51
+ * Rows → the text the form saves. A row with no name is kept as a bare
52
+ * (positional) value; a row with neither name nor value is left out.
43
53
  */
44
54
  export function rowsToText(rows) {
45
55
  const list = (rows || [])
46
- .filter((r) => !(r.starter && !String(r.name || '').trim()))
56
+ .filter((r) => String(r.name || '').trim() || (r.value !== '' && r.value != null))
47
57
  .map((r) => (String(r.name || '').trim() ? { name: String(r.name).trim(), value: r.value } : r.value))
48
58
  return list.length ? JSON.stringify(list, null, 2) : ''
49
59
  }
50
60
 
61
+ /** Suggested names not already used by another row. */
62
+ export function unusedNames(names, rows, index) {
63
+ const taken = new Set((rows || []).filter((_, i) => i !== index).map((r) => String(r.name || '').trim()).filter(Boolean))
64
+ return (names || []).filter((n) => !taken.has(n))
65
+ }
66
+
51
67
  /** A row's value when its source changes: a binding, or empty fixed text. */
52
68
  export function valueForSource(source, current, contract) {
53
69
  if (source === FIXED) return sourceOf(current, contract) === FIXED ? current : ''
@@ -15,7 +15,7 @@ import JobChainSection from '../components/JobChainSection.jsx'
15
15
  import ParametersField from '../components/ParametersField.jsx'
16
16
  import { useRunContract } from '../useRunContract.js'
17
17
  import { useJobChainController } from '../useJobChainController.js'
18
- import { acceptableFields, toggleAccepted } from '../lib/jobChainModel.js'
18
+ import { acceptableFields } from '../lib/jobChainModel.js'
19
19
  import './Jobs.css'
20
20
 
21
21
  // SCHEDULED JOBS — @xeplr/jobs, in this product's chrome.
@@ -448,7 +448,7 @@ function ActionInput({ field, value, onChange, inputs, contract }) {
448
448
 
449
449
  // Rows rather than JSON, for an input the action marks as parameters.
450
450
  if (field.control === 'parameters') {
451
- return <ParametersField field={field} value={value} onChange={onChange} contract={contract || []} />
451
+ return <ParametersField field={field} value={value} onChange={onChange} contract={contract || []} inputs={inputs} />
452
452
  }
453
453
  const common = { id, value, onChange: (e) => onChange(e.target.value) }
454
454
 
@@ -536,7 +536,8 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
536
536
  Object.entries(job?.inputs || {}).map(([k, v]) => [k, toFieldValue(v)])
537
537
  ),
538
538
  // Inputs the job BEFORE this one in a chain may fill (@xeplr/jobs'
539
- // acceptsInputs). `window` always may, so it is not listed.
539
+ // acceptsInputs). No longer edited here — a job takes what it needs from
540
+ // the job before through its parameters — but kept as saved.
540
541
  acceptsInputs: Array.isArray(job?.acceptsInputs) ? job.acceptsInputs : []
541
542
  }))
542
543
  const [saving, setSaving] = useState(false)
@@ -697,35 +698,9 @@ function JobEditor({ job, jobs, actions, onCancel, onSaved }) {
697
698
  onChange={(v) => patch({ inputs: { ...draft.inputs, [f.name]: v } })}
698
699
  />
699
700
  )
700
- const acceptable = acceptableFields(fields)
701
701
  return (
702
702
  <>
703
703
  <div className="jb-grid">{primary.map(render)}</div>
704
- {chain.enabled && acceptable.length > 0 && (
705
- /* WHAT THE JOB BEFORE THIS ONE MAY FILL. Off for every
706
- field until ticked: a chain may otherwise only pass the
707
- period (window), never this job's SQL or connection. */
708
- <details className="jb-advanced" open={draft.acceptsInputs.length > 0}>
709
- <summary>
710
- From the job before
711
- <span className="jb-dim">
712
- {' — '}
713
- {draft.acceptsInputs.length ? draft.acceptsInputs.map((n) => fieldLabel(acceptable.find((f) => f.name === n) || { name: n })).join(' · ') : 'only the period'}
714
- </span>
715
- </summary>
716
- <p className="jb-help">
717
- When another job runs this one, it always passes the period it ran for. Tick the inputs
718
- it may also fill; everything else stays as saved here.
719
- </p>
720
- {acceptable.map((f) => (
721
- <label key={f.name} className="jb-chain-toggle">
722
- <input type="checkbox" checked={draft.acceptsInputs.includes(f.name)}
723
- onChange={(e) => patch({ acceptsInputs: toggleAccepted(draft.acceptsInputs, f.name, e.target.checked) })} />
724
- {fieldLabel(f)}
725
- </label>
726
- ))}
727
- </details>
728
- )}
729
704
  {advanced.length > 0 && (
730
705
  /* COLLAPSED, NOT HIDDEN. The summary names every field in
731
706
  here and its current value, so closing it never conceals