resource-engine-fastapi 0.1.0__py3-none-any.whl

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.
Files changed (39) hide show
  1. resource_engine_fastapi/__init__.py +39 -0
  2. resource_engine_fastapi/access.py +110 -0
  3. resource_engine_fastapi/asyncapi.py +47 -0
  4. resource_engine_fastapi/auth.py +158 -0
  5. resource_engine_fastapi/authz.py +100 -0
  6. resource_engine_fastapi/caching.py +111 -0
  7. resource_engine_fastapi/docs.py +77 -0
  8. resource_engine_fastapi/documents.py +219 -0
  9. resource_engine_fastapi/email_events.py +85 -0
  10. resource_engine_fastapi/errors.py +178 -0
  11. resource_engine_fastapi/events.py +109 -0
  12. resource_engine_fastapi/explorer/__init__.py +441 -0
  13. resource_engine_fastapi/explorer/assets/auth.js +171 -0
  14. resource_engine_fastapi/explorer/assets/console.js +340 -0
  15. resource_engine_fastapi/explorer/assets/events.js +189 -0
  16. resource_engine_fastapi/explorer/assets/explorer.css +156 -0
  17. resource_engine_fastapi/explorer/assets/explorer.js +275 -0
  18. resource_engine_fastapi/explorer/assets/jobs.js +143 -0
  19. resource_engine_fastapi/explorer/assets/messages.js +85 -0
  20. resource_engine_fastapi/explorer/assets/publish.js +117 -0
  21. resource_engine_fastapi/explorer/data.py +84 -0
  22. resource_engine_fastapi/explorer/events.py +182 -0
  23. resource_engine_fastapi/explorer/jobs.py +167 -0
  24. resource_engine_fastapi/explorer/labels.py +198 -0
  25. resource_engine_fastapi/explorer/locale/ca/LC_MESSAGES/explorer.mo +0 -0
  26. resource_engine_fastapi/explorer/locale/ca/LC_MESSAGES/explorer.po +417 -0
  27. resource_engine_fastapi/explorer/locale/es/LC_MESSAGES/explorer.mo +0 -0
  28. resource_engine_fastapi/explorer/locale/es/LC_MESSAGES/explorer.po +417 -0
  29. resource_engine_fastapi/explorer/messages.py +73 -0
  30. resource_engine_fastapi/explorer/publish.py +85 -0
  31. resource_engine_fastapi/exports.py +186 -0
  32. resource_engine_fastapi/i18n.py +76 -0
  33. resource_engine_fastapi/py.typed +0 -0
  34. resource_engine_fastapi/router.py +1031 -0
  35. resource_engine_fastapi/versioning.py +49 -0
  36. resource_engine_fastapi-0.1.0.dist-info/METADATA +32 -0
  37. resource_engine_fastapi-0.1.0.dist-info/RECORD +39 -0
  38. resource_engine_fastapi-0.1.0.dist-info/WHEEL +4 -0
  39. resource_engine_fastapi-0.1.0.dist-info/licenses/LICENSE +202 -0
@@ -0,0 +1,156 @@
1
+ /* The explorer (mount_explorer): a Swagger-like list of operations, one
2
+ colour per verb, including the application's own PUB, SUB and CRON. */
3
+ :root {
4
+ --bg: #ffffff; --fg: #1f2328; --muted: #59636e; --line: #d1d9e0; --soft: #f6f8fa;
5
+ --accent: #0b5cad; --code: #eff2f5; --ok: #1a7f37; --fail: #cf222e;
6
+ --get: #2f6fd1; --post: #2f8f4e; --put: #b5650d; --patch: #0f8a86; --delete: #c93b3b;
7
+ --pub: #7b4fc9; --sub: #0b7fa8; --cron: #6b5b3e;
8
+ color-scheme: light dark;
9
+ }
10
+ @media (prefers-color-scheme: dark) {
11
+ :root:not([data-theme="light"]) {
12
+ --bg: #0f1115; --fg: #e6e8eb; --muted: #9aa4af; --line: #2d333b; --soft: #161a20;
13
+ --accent: #6cb6ff; --code: #1d232b; --ok: #3fb950; --fail: #f85149;
14
+ --get: #5b9cf0; --post: #4cb36f; --put: #e09a3e; --patch: #3cbab4; --delete: #ef6b6b;
15
+ --pub: #a582f0; --sub: #3ab4dc; --cron: #b29a6a;
16
+ }
17
+ }
18
+ * { box-sizing: border-box; }
19
+ body { margin: 0; background: var(--bg); color: var(--fg);
20
+ font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
21
+ header { display: flex; gap: .8rem; align-items: baseline; padding: .8rem 1rem;
22
+ border-bottom: 1px solid var(--line); flex-wrap: wrap; }
23
+ header h1 { font-size: 1.2rem; margin: 0; }
24
+ header .app { color: var(--muted); }
25
+ main { padding: 1rem; max-width: 72rem; margin: 0 auto; }
26
+ code, pre, .path { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
27
+ pre { background: var(--code); padding: .7rem .9rem; border-radius: 6px; overflow: auto;
28
+ font-size: .82rem; max-height: 28rem; margin: .4rem 0; }
29
+
30
+ /* the access token, once for every row */
31
+ .auth { border: 1px solid var(--line); border-radius: 8px; padding: .4rem 1rem .6rem;
32
+ background: var(--soft); }
33
+ .console label { display: grid; gap: .2rem; margin: .4rem 0; font-size: .88rem; }
34
+ .console label.inline { display: flex; gap: .4rem; align-items: center; }
35
+ .console label.required span::after { content: " *"; color: var(--fail); }
36
+ .console input[type=text], .console input[type=password], .console input[type=search],
37
+ .console select, .console textarea {
38
+ font: .88rem ui-monospace, SFMono-Regular, Menlo, monospace; padding: .38rem .5rem; width: 100%;
39
+ border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--fg); }
40
+ .console button { font: inherit; font-size: .9rem; padding: .3rem .9rem; margin: .3rem .4rem .3rem 0;
41
+ border-radius: 6px; border: 1px solid var(--accent); background: var(--accent); color: var(--bg);
42
+ cursor: pointer; }
43
+ .console button.secondary, .console .console-next button { background: var(--bg); color: var(--accent); }
44
+ .console button[disabled] { opacity: .5; cursor: default; }
45
+ .console .note { color: var(--muted); font-size: .85rem; margin: .3rem 0; }
46
+ details.console { border: 1px solid var(--line); border-radius: 8px; padding: .4rem 1rem; margin: .8rem 0; }
47
+ details.console summary { cursor: pointer; font-weight: 600; }
48
+
49
+ /* the verbs */
50
+ .legend { display: flex; flex-wrap: wrap; gap: .3rem 1rem; margin: .9rem 0 .4rem; font-size: .85rem;
51
+ color: var(--muted); }
52
+ .legend > span { display: inline-flex; gap: .35rem; align-items: center; }
53
+ .verb { display: inline-block; min-width: 4.4rem; padding: .18rem .4rem; border-radius: 4px;
54
+ color: #fff; font: 700 .78rem/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
55
+ text-align: center; letter-spacing: .03em; background: var(--verb, var(--muted)); }
56
+ .op-get { --verb: var(--get); }
57
+ .op-post { --verb: var(--post); }
58
+ .op-put { --verb: var(--put); }
59
+ .op-patch { --verb: var(--patch); }
60
+ .op-delete { --verb: var(--delete); }
61
+ .op-pub { --verb: var(--pub); }
62
+ .op-sub { --verb: var(--sub); }
63
+ .op-cron { --verb: var(--cron); }
64
+ .legend .verb { min-width: 0; }
65
+
66
+ .console input.filter { margin: .4rem 0 .8rem; font-family: inherit; }
67
+
68
+ /* a section per resource */
69
+ .group { margin: .4rem 0 1.2rem; }
70
+ .console button.tag { all: unset; box-sizing: border-box; display: flex; gap: .8rem; align-items: baseline;
71
+ width: 100%; padding: .55rem .2rem; border-bottom: 1px solid var(--line); cursor: pointer; flex-wrap: wrap; }
72
+ .tag-name { font-size: 1.15rem; font-weight: 700; }
73
+ .tag-summary { color: var(--muted); font-size: .88rem; }
74
+ .tag::before { content: "▾"; color: var(--muted); }
75
+ .tag[aria-expanded="false"]::before { content: "▸"; }
76
+ .ops { padding-top: .4rem; }
77
+
78
+ /* a row per operation */
79
+ details.op { border: 1px solid var(--verb); border-radius: 6px; margin: .45rem 0;
80
+ background: color-mix(in srgb, var(--verb) 7%, var(--bg)); }
81
+ details.op > summary { display: flex; gap: .7rem; align-items: center; padding: .4rem .5rem;
82
+ cursor: pointer; list-style: none; flex-wrap: wrap; }
83
+ details.op > summary::-webkit-details-marker { display: none; }
84
+ details.op > summary .path { font-weight: 600; font-size: .92rem; word-break: break-all; }
85
+ details.op > summary .summary { color: var(--muted); font-size: .88rem; flex: 1 1 14rem; }
86
+ details.op > summary .badge { font-size: .72rem; border: 1px solid var(--line); border-radius: 10px;
87
+ padding: 0 .45rem; color: var(--muted); }
88
+ details.op[open] > summary { border-bottom: 1px solid color-mix(in srgb, var(--verb) 40%, var(--line)); }
89
+ .try-it { padding: .5rem .9rem .9rem; background: var(--bg); border-radius: 0 0 6px 6px; }
90
+ .console .console-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
91
+ gap: 0 .8rem; align-items: end; }
92
+ .console .console-form .wide { grid-column: 1 / -1; }
93
+ .console button.execute { background: var(--verb, var(--accent)); border-color: var(--verb, var(--accent)); }
94
+ .console-output:not(:empty) { margin-top: .5rem; }
95
+ .console-status.ok { color: var(--ok); font-weight: 600; }
96
+ .console-status.fail { color: var(--fail); font-weight: 600; }
97
+ .console .console-download { font-weight: 600; color: var(--accent); }
98
+ dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: .2rem .9rem; margin: .2rem 0 .6rem;
99
+ font-size: .9rem; }
100
+ dl.facts dt { color: var(--muted); }
101
+ dl.facts dd { margin: 0; }
102
+
103
+ /* tables (job runs) */
104
+ .table-wrap { overflow-x: auto; margin: .6rem 0; }
105
+ .console table { border-collapse: collapse; width: 100%; font-size: .88rem; }
106
+ .console th, .console td { border-bottom: 1px solid var(--line); padding: .3rem .5rem; text-align: left;
107
+ vertical-align: top; }
108
+ .console th { color: var(--muted); font-weight: 600; }
109
+ .outcome-ok { color: var(--ok); }
110
+ .outcome-failed { color: var(--fail); }
111
+ .outcome-skipped { color: var(--muted); }
112
+
113
+ /* live events (SUB) */
114
+ .events-columns { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); gap: 1rem;
115
+ align-items: start; }
116
+ .events-list { list-style: none; margin: .5rem 0; padding: 0; max-height: 26rem; overflow: auto;
117
+ border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
118
+ .events-list li { border-bottom: 1px solid var(--line); }
119
+ .console .events-list button.event-row { all: unset; display: block; width: 100%; box-sizing: border-box;
120
+ cursor: pointer; padding: .35rem .6rem; font: .82rem ui-monospace, SFMono-Regular, Menlo, monospace;
121
+ color: var(--fg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
122
+ .console .events-list button.event-row:hover,
123
+ .console .events-list button.event-row:focus-visible { background: var(--soft); }
124
+ .events-detail h3 { font-size: .92rem; margin: .5rem 0 .2rem; }
125
+ @media (max-width: 720px) {
126
+ .events-columns { grid-template-columns: minmax(0, 1fr); }
127
+ .verb { min-width: 3.6rem; }
128
+ }
129
+
130
+ /* the accordions inside a resource */
131
+ details.subgroup { margin: .35rem 0 .35rem .2rem; }
132
+ details.subgroup > summary { display: flex; gap: .6rem; align-items: center; padding: .35rem .4rem;
133
+ cursor: pointer; border-radius: 6px; list-style: none; flex-wrap: wrap; }
134
+ details.subgroup > summary::-webkit-details-marker { display: none; }
135
+ details.subgroup > summary::before { content: "▸"; color: var(--muted); width: .8rem; }
136
+ details.subgroup[open] > summary::before { content: "▾"; }
137
+ details.subgroup > summary:hover { background: var(--soft); }
138
+ details.subgroup > summary .kind { font-weight: 600; }
139
+ details.subgroup > summary .count { color: var(--muted); font-size: .8rem; border: 1px solid var(--line);
140
+ border-radius: 10px; padding: 0 .45rem; }
141
+ details.subgroup > summary .chips { display: inline-flex; gap: .25rem; flex-wrap: wrap; }
142
+ .verb.chip { min-width: 0; font-size: .66rem; padding: .05rem .3rem; }
143
+ details.subgroup > div { padding-left: 1.2rem; }
144
+ @media (max-width: 720px) { details.subgroup > div { padding-left: .3rem; } }
145
+
146
+ /* signing in */
147
+ .signin { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin: .3rem 0 .2rem; }
148
+ .signin .who { font-weight: 600; }
149
+ .console button.authorize { background: var(--ok); border-color: var(--ok); }
150
+
151
+ /* outbox and dead letters */
152
+ :root { --outbox: #4a6382; --dlq: #9c2f4f; }
153
+ @media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --outbox: #8aa6c8; --dlq: #e07a98; } }
154
+ .op-outbox { --verb: var(--outbox); }
155
+ .op-dlq { --verb: var(--dlq); }
156
+ .console pre.curl { white-space: pre-wrap; word-break: break-all; }
@@ -0,0 +1,275 @@
1
+ /* The explorer (mount_explorer), laid out like Swagger UI: one section per
2
+ resource, one row per verb.
3
+ - GET, POST, PUT, PATCH, DELETE: the HTTP operations (console.js);
4
+ - SUB: an event type the resource publishes, live (events.js);
5
+ - CRON: a timed transition of the resource, or a worker job in the
6
+ "worker" section (jobs.js);
7
+ - PUB: publishing an event, when the application enables it.
8
+ Each row opens into its "Try it" form. Inside a resource the rows are
9
+ grouped in accordions by what they do (CRUD, transitions and actions,
10
+ relations, documents, export, events, schedule). */
11
+ (function () {
12
+ var R = window.ResourceExplorer;
13
+ if (!R) return;
14
+ var C = R.config, L = R.labels, el = R.el;
15
+ var root = document.getElementById("explorer");
16
+ if (!root) return;
17
+ // after a sign-in coming back from the provider (auth.js) has finished
18
+ (R.ready || Promise.resolve()).then(build);
19
+
20
+ var openGroups = {};
21
+ try { openGroups = JSON.parse(R.load("re-explorer-groups") || "{}") || {}; } catch (e) { openGroups = {}; }
22
+ var filtering = false;
23
+
24
+ // #GET/invoices/{entity_id}: open that row (and its group) and show it
25
+ function openFromHash() {
26
+ var id = decodeURIComponent(location.hash.slice(1));
27
+ if (!id) return;
28
+ var target = document.getElementById(id);
29
+ if (!target || !target.classList.contains("op")) return;
30
+ var group = target.closest("details.subgroup");
31
+ if (group) group.open = true;
32
+ target.open = true;
33
+ target.scrollIntoView({ block: "start" });
34
+ }
35
+ window.addEventListener("hashchange", openFromHash);
36
+
37
+ function build() {
38
+ var rows = []; // {element, text, group}
39
+
40
+ function verbOf(method) { return method.toLowerCase(); }
41
+
42
+ // A collapsible row: the verb, its path, a summary; the body is built when
43
+ // it first opens.
44
+ function row(group, verb, path, summary, build, extra) {
45
+ var badge = el("span", { className: "verb", text: verb });
46
+ var head = el("summary", {}, [
47
+ badge,
48
+ el("code", { className: "path", text: path }),
49
+ el("span", { className: "summary", text: summary || "" }),
50
+ extra || null,
51
+ ]);
52
+ // a link to the row: #GET/invoices/{entity_id} opens it
53
+ var id = verb + "/" + path.replace(/^\//, "");
54
+ var details = el("details", { className: "op op-" + verb.toLowerCase(), id: id }, [head]);
55
+ var built = false;
56
+ details.addEventListener("toggle", function () {
57
+ if (details.open && !built) {
58
+ built = true;
59
+ details.appendChild(build());
60
+ }
61
+ if (details.open && decodeURIComponent(location.hash.slice(1)) !== id) {
62
+ history.replaceState(null, "", location.pathname + location.search + "#" + encodeURIComponent(id));
63
+ }
64
+ });
65
+ rows.push({ element: details, text: (verb + " " + path + " " + (summary || "")).toLowerCase(),
66
+ group: group });
67
+ return details;
68
+ }
69
+
70
+ function section(name, summary, children) {
71
+ var body = el("div", { className: "ops" }, children);
72
+ var header = el("button", { type: "button", className: "tag", "aria-expanded": "true" }, [
73
+ el("span", { className: "tag-name", text: name }),
74
+ el("span", { className: "tag-summary", text: summary || "" }),
75
+ ]);
76
+ header.addEventListener("click", function () {
77
+ var open = header.getAttribute("aria-expanded") !== "true";
78
+ header.setAttribute("aria-expanded", open ? "true" : "false");
79
+ body.hidden = !open;
80
+ });
81
+ var wrapper = el("section", { className: "group" }, [header, body]);
82
+ wrapper.dataset.group = name;
83
+ return wrapper;
84
+ }
85
+
86
+ // What a row does, from its path after the resource's name.
87
+ var KINDS = ["crud", "lifecycle", "relations", "documents", "export", "events", "schedule",
88
+ "other"];
89
+ function kindOf(name, op) {
90
+ var rest = op.path.indexOf("/" + name) === 0 ? op.path.slice(name.length + 1) : op.path;
91
+ if (rest === "" || /^\/\{[^}/]+\}$/.test(rest)) return "crud";
92
+ if (/^\/exports?$/.test(rest) || /\/printouts\//.test(rest)) return "export";
93
+ if (/\/(transitions|actions)(\/|$)|\/history$/.test(rest)) return "lifecycle";
94
+ if (/\/documents(\/|$)/.test(rest) || /^\/\{[^}/]+\}\/(content|confirm)$/.test(rest)) {
95
+ return "documents";
96
+ }
97
+ if (/^\/\{[^}/]+\}\/[a-z_]+$/.test(rest)) return "relations";
98
+ return "other";
99
+ }
100
+
101
+ // One accordion of a resource's rows: its name, how many, which verbs.
102
+ function subgroup(key, kind, children, open) {
103
+ var verbs = [];
104
+ children.forEach(function (child) {
105
+ var verb = child.querySelector(".verb").textContent;
106
+ if (verbs.indexOf(verb) < 0) verbs.push(verb);
107
+ });
108
+ var head = el("summary", {}, [
109
+ el("span", { className: "kind", text: L["group_" + kind] }),
110
+ el("span", { className: "count", text: String(children.length) }),
111
+ el("span", { className: "chips" }, verbs.map(function (verb) {
112
+ return el("span", { className: "op-" + verb.toLowerCase() }, [
113
+ el("span", { className: "verb chip", text: verb })]);
114
+ })),
115
+ ]);
116
+ var details = el("details", { className: "subgroup" }, [head, el("div", {}, children)]);
117
+ details.dataset.key = key;
118
+ // the groups this tab opened or closed stay so; otherwise the defaults
119
+ var remembered = openGroups[key];
120
+ details.open = remembered === undefined ? open : remembered;
121
+ details.dataset.open = details.open ? "1" : "";
122
+ details.addEventListener("toggle", function () {
123
+ if (filtering) return; // the filter opens groups for a moment
124
+ if (details.open === (details.dataset.open === "1")) return; // set by the page, not you
125
+ openGroups[key] = details.open;
126
+ details.dataset.open = details.open ? "1" : "";
127
+ R.save("re-explorer-groups", JSON.stringify(openGroups));
128
+ });
129
+ return details;
130
+ }
131
+
132
+ function jump(op) {
133
+ var target = rows.filter(function (r) { return r.op === op; })[0];
134
+ if (!target) return;
135
+ target.element.open = true;
136
+ var parent = target.element.closest("details.subgroup");
137
+ if (parent) parent.open = true;
138
+ target.element.scrollIntoView({ behavior: "smooth", block: "start" });
139
+ }
140
+
141
+ // ------------------------------------------------------------ resources
142
+ var groups = el("div", { className: "groups" });
143
+ (C.groups || []).forEach(function (group) {
144
+ var name = group.name, buckets = {};
145
+ function add(kind, element) { (buckets[kind] = buckets[kind] || []).push(element); }
146
+ var ops = C.resources[name] || [];
147
+ ops.forEach(function (op) {
148
+ var element = row(name, op.method, op.path, op.summary, function () {
149
+ return R.operationForm(name, op, ops, jump);
150
+ });
151
+ rows[rows.length - 1].op = op;
152
+ add(kindOf(name, op), element);
153
+ });
154
+ var publishable = C.publish && C.publish.events ? C.publish.events[name] || [] : [];
155
+ publishable.forEach(function (event) {
156
+ var current = event.versions[event.versions.length - 1];
157
+ var version = el("span", { className: "badge", text: "v" + current.version });
158
+ add("events", row(name, "PUB", name + "/" + event.type, event.summary, function () {
159
+ return R.publish(name, event);
160
+ }, version));
161
+ });
162
+ var events = C.events && C.events.channels ? C.events.channels[name] || [] : [];
163
+ events.forEach(function (event) {
164
+ var version = el("span", { className: "badge", text: "v" + event.version });
165
+ add("events", row(name, "SUB", name + "/" + event.type, event.summary, function () {
166
+ return el("div", { className: "try-it" }, [R.subscribe({ resource: name, type: event.type })]);
167
+ }, version));
168
+ });
169
+ var timers = C.jobs && C.jobs.timers_by_resource ? C.jobs.timers_by_resource[name] || [] : [];
170
+ timers.forEach(function (timer) {
171
+ var text = timer.description || timer.event;
172
+ add("schedule", row(name, "CRON", name + "/" + timer.event, text, function () {
173
+ return R.cron.timer(name, timer);
174
+ }));
175
+ });
176
+ var kinds = KINDS.filter(function (kind) { return buckets[kind]; });
177
+ if (!kinds.length) return;
178
+ var children = kinds.map(function (kind) {
179
+ // CRUD opens at first; a resource with one kind of rows shows them
180
+ return subgroup(name + "/" + kind, kind, buckets[kind], kind === "crud" || kinds.length === 1);
181
+ });
182
+ groups.appendChild(section(name, group.summary, children));
183
+ });
184
+
185
+ // ------------------------------------------------ outbox and dead letters
186
+ if (R.messages) {
187
+ groups.appendChild(section(L.messages_section, L.messages_intro, [
188
+ row(L.messages_section, "OUTBOX", "outbox", L.outbox_summary, R.messages.outbox),
189
+ row(L.messages_section, "DLQ", "dead-letters", L.dlq_summary, R.messages.deadLetters),
190
+ ]));
191
+ }
192
+
193
+ // -------------------------------------------------------- worker jobs
194
+ var worker = null;
195
+ if (R.cron) {
196
+ var holder = el("div", { className: "ops" }, [el("p", { className: "note", text: L.sending })]);
197
+ worker = section(L.worker_jobs, L.jobs_intro, [holder]);
198
+ groups.appendChild(worker);
199
+ var loadJobs = function () {
200
+ R.cron.load(function (result) {
201
+ holder.textContent = "";
202
+ rows = rows.filter(function (r) { return r.group !== L.worker_jobs; });
203
+ if (!result.ok || !result.data) { R.show(holder, result); return; }
204
+ var jobs = result.data.jobs || [];
205
+ if (!jobs.length) { holder.appendChild(el("p", { className: "note", text: L.no_jobs })); return; }
206
+ jobs.forEach(function (job) {
207
+ holder.appendChild(row(L.worker_jobs, "CRON", job.name, R.cron.schedule(job), function () {
208
+ return R.cron.job(job);
209
+ }));
210
+ });
211
+ filter();
212
+ });
213
+ };
214
+ loadJobs();
215
+ window.addEventListener("re-token", loadJobs);
216
+ }
217
+
218
+ // ---------------------------------------------------------- the page
219
+ var search = el("input", { type: "search", className: "filter", placeholder: L.filter,
220
+ "aria-label": L.filter });
221
+ function filter() {
222
+ var words = search.value.trim().toLowerCase().split(/\s+/).filter(Boolean);
223
+ rows.forEach(function (r) {
224
+ r.element.hidden = !words.every(function (w) { return r.text.indexOf(w) >= 0; });
225
+ });
226
+ // while filtering, open the accordions with matches and hide the rest;
227
+ // back to how they were once the filter is empty
228
+ var wasFiltering = filtering;
229
+ filtering = words.length > 0;
230
+ [].forEach.call(groups.querySelectorAll("details.subgroup"), function (d) {
231
+ var visible = [].some.call(d.querySelectorAll("details.op"), function (o) { return !o.hidden; });
232
+ d.hidden = filtering && !visible;
233
+ if (filtering) d.open = visible;
234
+ else if (wasFiltering) d.open = d.dataset.open === "1";
235
+ });
236
+ [].forEach.call(groups.querySelectorAll("section.group"), function (s) {
237
+ var visible = [].some.call(s.querySelectorAll("details.op"), function (d) { return !d.hidden; });
238
+ s.hidden = words.length > 0 && !visible;
239
+ });
240
+ }
241
+ search.addEventListener("input", filter);
242
+
243
+ var settings = R.settings();
244
+ // a new token: rows that load on their own (the worker's jobs) load again
245
+ [].forEach.call(settings.querySelectorAll("input[type=password]"), function (input) {
246
+ input.addEventListener("change", function () { window.dispatchEvent(new Event("re-token")); });
247
+ });
248
+ var legend = el("p", { className: "legend" }, ["GET", "POST", "PUT", "PATCH", "DELETE", "PUB",
249
+ "SUB", "CRON", "OUTBOX", "DLQ"].filter(function (verb) {
250
+ if (verb === "SUB") return C.events;
251
+ if (verb === "CRON") return C.jobs;
252
+ if (verb === "PUB") return C.publish;
253
+ if (verb === "OUTBOX" || verb === "DLQ") return C.messages;
254
+ return true;
255
+ }).map(function (verb) {
256
+ return el("span", { className: "op-" + verb.toLowerCase() }, [
257
+ el("span", { className: "verb", text: verb }),
258
+ el("span", { text: L["verb_" + verb.toLowerCase()] })]);
259
+ }));
260
+ root.appendChild(el("div", { className: "auth" }, [
261
+ el("p", { className: "note", text: L.intro + (R.api ? " " + R.api : "") }),
262
+ R.auth ? R.auth.box() : null, settings]));
263
+ // a sign-in or a renewal: the token field shows the new token
264
+ window.addEventListener("re-token", function () {
265
+ [].forEach.call(settings.querySelectorAll("input[type=password]"), function (input) {
266
+ input.value = R.load("re-console-token");
267
+ });
268
+ });
269
+ if (C.me) root.appendChild(R.whoAmI(true));
270
+ root.appendChild(legend);
271
+ root.appendChild(search);
272
+ root.appendChild(groups);
273
+ openFromHash();
274
+ }
275
+ })();
@@ -0,0 +1,143 @@
1
+ /* CRON: what runs on a schedule. Two kinds of rows:
2
+ - a worker job (R.cron.job): its schedule, workers, recent runs and "run
3
+ now" (a request a worker claims, so single jobs keep their lease);
4
+ - a timed transition of a resource (R.cron.timer): when it fires, and when
5
+ it is due for one record.
6
+ R.cron.load(callback) reads the jobs the running workers registered. */
7
+ (function () {
8
+ var R = window.ResourceExplorer;
9
+ if (!R || !R.config.jobs) return;
10
+ var L = R.labels, el = R.el, J = R.config.jobs;
11
+
12
+ function fill(template, values) {
13
+ return template.replace(/\{(\w+)\}/g, function (_, key) {
14
+ return values[key] === undefined ? "" : values[key];
15
+ });
16
+ }
17
+
18
+ function seconds(value) {
19
+ if (value >= 86400 && value % 86400 === 0) return value / 86400 + " d";
20
+ if (value >= 3600 && value % 3600 === 0) return value / 3600 + " h";
21
+ if (value >= 60 && value % 60 === 0) return value / 60 + " min";
22
+ return Math.round(value * 100) / 100 + " s";
23
+ }
24
+
25
+ function when(iso) { return iso ? iso.replace("T", " ").slice(0, 19) : "—"; }
26
+
27
+ function cell(text, className) { return el("td", { text: text, className: className || "" }); }
28
+
29
+ function table(headers, rows) {
30
+ var head = el("tr", {}, headers.map(function (h) { return el("th", { text: h }); }));
31
+ return el("div", { className: "table-wrap" }, [
32
+ el("table", {}, [el("thead", {}, [head]), el("tbody", {}, rows)])]);
33
+ }
34
+
35
+ function schedule(job) {
36
+ return fill(L.every, { every: seconds(job.every) }) +
37
+ (job.single ? " · " + fill(L.single, { ttl: seconds(job.ttl) }) : "");
38
+ }
39
+
40
+ function load(callback) {
41
+ R.call("GET", J.list).then(callback);
42
+ }
43
+
44
+ // ------------------------------------------------------------- a worker job
45
+ function job(info) {
46
+ var summary = el("dl", { className: "facts" }, [
47
+ el("dt", { text: L.schedule }), el("dd", { text: schedule(info) }),
48
+ el("dt", { text: L.workers }), el("dd", { text: info.workers.join(", ") }),
49
+ el("dt", { text: L.last_run }),
50
+ el("dd", { text: info.last_run ? info.last_run.outcome + " · " +
51
+ seconds(info.last_run.seconds) + " · " + when(info.last_run.at) : L.never }),
52
+ ]);
53
+ var output = el("div", { className: "console-output", "aria-live": "polite" });
54
+ var runs = el("button", { type: "button", className: "secondary", text: L.runs });
55
+ var now = el("button", { type: "button", className: "execute", text: L.run_now });
56
+ runs.addEventListener("click", history);
57
+ now.addEventListener("click", runNow);
58
+
59
+ function history() {
60
+ output.textContent = L.sending;
61
+ R.call("GET", J.runs.replace("{name}", encodeURIComponent(info.name))).then(function (result) {
62
+ output.textContent = "";
63
+ if (!result.ok || !Array.isArray(result.data)) { R.show(output, result); return; }
64
+ if (!result.data.length) { output.appendChild(el("p", { className: "note", text: L.never })); return; }
65
+ output.appendChild(table([L.when, L.outcome, L.duration, L.worker, L.error],
66
+ result.data.map(function (run) {
67
+ return el("tr", {}, [cell(when(run.at)), cell(run.outcome, "outcome-" + run.outcome),
68
+ cell(seconds(run.seconds)), cell(run.owner), cell(run.error || "")]);
69
+ })));
70
+ });
71
+ }
72
+
73
+ function runNow() {
74
+ if (!R.allowed()) { output.textContent = L.changes_off; return; }
75
+ output.textContent = L.sending;
76
+ R.call("POST", J.run.replace("{name}", encodeURIComponent(info.name))).then(function (result) {
77
+ if (!result.ok || !result.data) { R.show(output, result); return; }
78
+ follow(result.data, 0);
79
+ });
80
+ }
81
+
82
+ function follow(request, tries) {
83
+ output.textContent = "";
84
+ var done = ["ok", "failed", "skipped"].indexOf(request.status) >= 0;
85
+ var text = done ? fill(L.request_done, { status: request.status }) : L.request_pending;
86
+ output.appendChild(el("p", {
87
+ className: "console-status " + (request.status === "failed" ? "fail" : "ok"),
88
+ text: text + (request.claimed_by ? " · " + request.claimed_by : "") }));
89
+ if (request.error) output.appendChild(el("pre", { text: request.error }));
90
+ if (done || tries > 120) return;
91
+ setTimeout(function () {
92
+ R.call("GET", J.request.replace("{id}", encodeURIComponent(request.id))).then(function (result) {
93
+ if (result.ok && result.data) follow(result.data, tries + 1);
94
+ else R.show(output, result);
95
+ });
96
+ }, 1000);
97
+ }
98
+
99
+ var actions = el("div", {}, [now, runs]);
100
+ if (!R.allowed()) actions.appendChild(el("span", { className: "note", text: " " + L.changes_off }));
101
+ return el("div", { className: "try-it" }, [summary, actions, output]);
102
+ }
103
+
104
+ // ----------------------------------------------------- a timed transition
105
+ function timer(resource, info) {
106
+ var summary = el("dl", { className: "facts" }, [
107
+ el("dt", { text: L.fires }),
108
+ el("dd", { text: fill(L.after_in, { after: seconds(info.after_seconds),
109
+ states: info.states.join(", "), since: info.since }) }),
110
+ ]);
111
+ var id = el("input", { type: "text", "aria-label": L.filter_id, spellcheck: "false" });
112
+ id.value = R.load("re-console-id-" + resource);
113
+ var show = el("button", { type: "button", className: "execute", text: L.show });
114
+ var output = el("div", { className: "console-output", "aria-live": "polite" });
115
+ show.addEventListener("click", function () {
116
+ var value = id.value.trim();
117
+ if (!value) { output.textContent = L.missing + ": " + L.filter_id; return; }
118
+ output.textContent = L.sending;
119
+ var path = J.timers.replace("{resource}", encodeURIComponent(resource))
120
+ .replace("{id}", encodeURIComponent(value));
121
+ R.call("GET", path).then(function (result) {
122
+ output.textContent = "";
123
+ if (!result.ok || !Array.isArray(result.data)) { R.show(output, result); return; }
124
+ var mine = result.data.filter(function (t) { return t.event === info.event; });
125
+ if (!mine.length) { output.appendChild(el("p", { className: "note", text: L.no_timers })); return; }
126
+ mine.forEach(function (t) {
127
+ output.appendChild(el("p", {
128
+ className: "console-status " + (t.overdue ? "fail" : "ok"),
129
+ text: L.due + ": " + when(t.due_at) + (t.overdue ? " · " + L.overdue : "") }));
130
+ });
131
+ });
132
+ });
133
+ return el("div", { className: "try-it" }, [
134
+ summary,
135
+ el("div", { className: "console-form" }, [
136
+ el("label", { className: "required" }, [el("span", { text: L.filter_id }), id]),
137
+ el("div", { className: "wide" }, [show])]),
138
+ output,
139
+ ]);
140
+ }
141
+
142
+ R.cron = { load: load, job: job, timer: timer, schedule: schedule, seconds: seconds };
143
+ })();
@@ -0,0 +1,85 @@
1
+ /* OUTBOX and DLQ: what operators ask when an event didn't arrive.
2
+ R.messages.outbox() lists the messages not sent yet (the relay keeps
3
+ retrying them); R.messages.deadLetters() the ones consumers gave up on.
4
+ "Send again" puts an outbox message back in the queue: consumers that
5
+ already handled it skip it, so only the ones that failed run it again. */
6
+ (function () {
7
+ var R = window.ResourceExplorer;
8
+ if (!R || !R.config.messages) return;
9
+ var L = R.labels, el = R.el, M = R.config.messages;
10
+
11
+ function when(iso) { return iso ? iso.replace("T", " ").slice(0, 19) : "—"; }
12
+ function cell(text, className) { return el("td", { text: text, className: className || "" }); }
13
+ function table(headers, rows) {
14
+ var head = el("tr", {}, headers.map(function (h) { return el("th", { text: h }); }));
15
+ return el("div", { className: "table-wrap" }, [
16
+ el("table", {}, [el("thead", {}, [head]), el("tbody", {}, rows)])]);
17
+ }
18
+
19
+ function sendAgain(id, output, reload) {
20
+ if (!R.allowed()) { output.textContent = L.changes_off; return; }
21
+ output.textContent = L.sending;
22
+ R.call("POST", M.send_again.replace("{id}", encodeURIComponent(id))).then(function (result) {
23
+ if (!result.ok) { R.show(output, result); return; }
24
+ output.textContent = "";
25
+ output.appendChild(el("p", { className: "console-status ok", text: L.sent_again }));
26
+ reload();
27
+ });
28
+ }
29
+
30
+ function panel(load, render) {
31
+ var list = el("div", {});
32
+ var output = el("div", { className: "console-output", "aria-live": "polite" });
33
+ var refresh = el("button", { type: "button", className: "execute", text: L.refresh });
34
+ function reload() {
35
+ list.textContent = L.sending;
36
+ R.call("GET", load).then(function (result) {
37
+ list.textContent = "";
38
+ if (!result.ok || !Array.isArray(result.data)) { R.show(list, result); return; }
39
+ render(list, result.data, output, reload);
40
+ });
41
+ }
42
+ refresh.addEventListener("click", reload);
43
+ reload();
44
+ return el("div", { className: "try-it" }, [refresh, list, output]);
45
+ }
46
+
47
+ function outbox() {
48
+ return panel(M.outbox, function (list, records, output, reload) {
49
+ if (!records.length) { list.appendChild(el("p", { className: "note", text: L.outbox_empty })); return; }
50
+ list.appendChild(table([L.when, L.event, L.attempts, L.error, ""], records.map(function (r) {
51
+ var again = el("button", { type: "button", className: "secondary", text: L.send_again });
52
+ again.addEventListener("click", function () { sendAgain(r.id, output, reload); });
53
+ return el("tr", {}, [
54
+ cell(when(r.created_at)),
55
+ el("td", {}, [el("code", { text: r.type + " v" + r.schema_version }),
56
+ el("div", { className: "note", text: r.id })]),
57
+ cell(String(r.attempts), r.attempts ? "outcome-failed" : ""),
58
+ cell(r.last_error || ""),
59
+ el("td", { className: "actions" }, [again]),
60
+ ]);
61
+ })));
62
+ });
63
+ }
64
+
65
+ function deadLetters() {
66
+ return panel(M.dead_letters, function (list, letters, output, reload) {
67
+ if (!letters.length) { list.appendChild(el("p", { className: "note", text: L.no_dead_letters })); return; }
68
+ list.appendChild(table([L.when, L.event, L.consumer, L.error, ""], letters.map(function (d) {
69
+ var action = d.in_outbox
70
+ ? el("button", { type: "button", className: "secondary", text: L.send_again })
71
+ : el("span", { className: "note", text: L.not_in_outbox });
72
+ if (d.in_outbox) action.addEventListener("click", function () { sendAgain(d.message_id, output, reload); });
73
+ return el("tr", {}, [
74
+ cell(when(d.at)),
75
+ el("td", {}, [el("code", { text: d.event_type }), el("div", { className: "note", text: d.message_id })]),
76
+ cell(d.consumer),
77
+ cell(d.error || "", "outcome-failed"),
78
+ el("td", { className: "actions" }, [action]),
79
+ ]);
80
+ })));
81
+ });
82
+ }
83
+
84
+ R.messages = { outbox: outbox, deadLetters: deadLetters };
85
+ })();