hammerkit 2.1.0-next.10 → 2.1.0-next.11

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.
@@ -8,6 +8,7 @@ const repo_1 = require("./repo");
8
8
  const runs_1 = require("./runs");
9
9
  const sandbox_files_api_1 = require("./sandbox-files-api");
10
10
  const sandboxes_1 = require("./sandboxes");
11
+ const paging_1 = require("./paging");
11
12
  const refSchema = zod_1.z.string().min(1).max(200);
12
13
  const createSchema = zod_1.z
13
14
  .object({
@@ -46,7 +47,10 @@ const pushSchema = zod_1.z
46
47
  *
47
48
  * POST /v1/sandboxes { repo, ref } fetch a branch into a new sandbox
48
49
  * GET /v1/sandboxes the caller's sandboxes, their limits and usage;
49
- * `?scope=all` everybody's, with `observe`
50
+ * `?scope=all` everybody's, with `observe`; `owner`,
51
+ * `repo` and `ref` narrow them, `limit` and `cursor`
52
+ * page them the last used first, `values=<field>`
53
+ * answers the values of a filter (paging.ts)
50
54
  * GET /v1/sandboxes/<id> one sandbox
51
55
  * DELETE /v1/sandboxes/<id> cancel its runs and remove it
52
56
  * POST /v1/sandboxes/<id>/fetch { ref } move a sandbox without changes to a branch's commit
@@ -81,13 +85,15 @@ async function handleSandboxes(config, sandboxes, repos, log, principal, req, re
81
85
  (0, http_1.send)(res, 403, { error: 'scope all needs an entitlement with observe' });
82
86
  return true;
83
87
  }
84
- const limits = sandboxes.limits;
85
- // the limits and the usage are the caller's, whichever sandboxes they list
86
- (0, http_1.send)(res, 200, {
87
- sandboxes: (scope === 'all' ? sandboxes.all() : sandboxes.list(principal)).map(show),
88
- limits: { count: limits.count, bytes: limits.bytes },
89
- used: sandboxes.usage(principal),
90
- });
88
+ try {
89
+ listSandboxes(config, sandboxes, principal, scope, url.searchParams, res, show);
90
+ }
91
+ catch (e) {
92
+ if (!(e instanceof paging_1.ListParamError)) {
93
+ throw e;
94
+ }
95
+ (0, http_1.send)(res, 400, { error: e.message });
96
+ }
91
97
  }
92
98
  else if (method === 'POST') {
93
99
  const body = parse(createSchema, await (0, http_1.readJson)(req));
@@ -264,4 +270,44 @@ function view(config, sandboxes, principal, sandbox) {
264
270
  run: run ?? null,
265
271
  };
266
272
  }
273
+ // what the sandboxes page narrows by, and GET /v1/sandboxes?values= answers the values of
274
+ const SANDBOX_FIELDS = ['owner', 'repo', 'ref'];
275
+ function parseSandboxFilter(params) {
276
+ return Object.fromEntries(SANDBOX_FIELDS.map((field) => [field, (0, paging_1.readList)(params, field)]).filter(([, values]) => values));
277
+ }
278
+ function filterSandboxes(sandboxes, filter) {
279
+ return sandboxes.filter((sandbox) => (!filter.owner || filter.owner.includes(sandbox.owner.sub)) &&
280
+ (!filter.repo || filter.repo.includes(sandbox.repo)) &&
281
+ (!filter.ref || filter.ref.includes(sandbox.ref)));
282
+ }
283
+ // the last used first, as the sandboxes page shows them
284
+ const sandboxKey = (sandbox) => [sandbox.lastUsedAt, sandbox.id];
285
+ function pageOfSandboxes(sandboxes, page, show) {
286
+ const answered = (0, paging_1.pageByKey)((0, paging_1.newestFirst)(sandboxes, sandboxKey), page, sandboxKey);
287
+ return { ...answered, items: answered.items.map(show) };
288
+ }
289
+ // GET /v1/sandboxes: the sandboxes of the scope that the filters let through, a page of them,
290
+ // or the values of a filter.
291
+ function listSandboxes(config, sandboxes, principal, scope, params, res, show) {
292
+ const filter = parseSandboxFilter(params);
293
+ const values = (0, paging_1.parseValues)(params, SANDBOX_FIELDS);
294
+ const page = values ? undefined : (0, paging_1.parsePage)(params);
295
+ const listed = scope === 'all' ? sandboxes.all() : sandboxes.list(principal);
296
+ const nameOf = (owner) => owner.name ?? (0, policy_1.subjectName)(config.entitlements, owner);
297
+ if (values) {
298
+ // a value of the field is counted over the sandboxes the other filters let through
299
+ const { field } = values;
300
+ const counted = (0, paging_1.countValues)(filterSandboxes(listed, { ...filter, [field]: undefined }), (sandbox) => [field === 'owner' ? { value: sandbox.owner.sub, name: nameOf(sandbox.owner) } : sandbox[field]], values);
301
+ (0, http_1.send)(res, 200, (0, paging_1.answerPage)('values', counted));
302
+ return;
303
+ }
304
+ const found = filterSandboxes(listed, filter);
305
+ const limits = sandboxes.limits;
306
+ // the limits and the usage are the caller's, whichever sandboxes they list
307
+ (0, http_1.send)(res, 200, {
308
+ ...(page ? (0, paging_1.answerPage)('sandboxes', pageOfSandboxes(found, page, show)) : { sandboxes: found.map(show) }),
309
+ limits: { count: limits.count, bytes: limits.bytes },
310
+ used: sandboxes.usage(principal),
311
+ });
312
+ }
267
313
  //# sourceMappingURL=sandboxes-api.js.map
@@ -18,6 +18,7 @@ const executor_1 = require("./executor");
18
18
  const capacity_1 = require("./capacity");
19
19
  const execute_docker_1 = require("../executer/execute-docker");
20
20
  const timings_1 = require("./timings");
21
+ const task_order_1 = require("./task-order");
21
22
  const kubernetes_backend_1 = require("./kubernetes-backend");
22
23
  const run_plan_1 = require("./run-plan");
23
24
  const run_source_environment_1 = require("./run-source-environment");
@@ -49,6 +50,7 @@ const path_1 = require("path");
49
50
  const checks_1 = require("./checks");
50
51
  const checks_api_1 = require("./checks-api");
51
52
  const overview_1 = require("./overview");
53
+ const paging_1 = require("./paging");
52
54
  var discovery_2 = require("./discovery");
53
55
  Object.defineProperty(exports, "DISCOVERY_PATH", { enumerable: true, get: function () { return discovery_2.DISCOVERY_PATH; } });
54
56
  Object.defineProperty(exports, "SERVER_PROTOCOL", { enumerable: true, get: function () { return discovery_2.SERVER_PROTOCOL; } });
@@ -122,18 +124,45 @@ async function handleRuns(config, registry, definitions, sources, sandboxes, pla
122
124
  if (method === 'GET') {
123
125
  // the results of each task are read one run at a time
124
126
  const viewer = (0, overview_1.getViewer)(config, principal);
125
- let filter;
127
+ let filter, page, values;
126
128
  try {
127
129
  filter = (0, overview_1.parseRunFilter)(url.searchParams, viewer.grant);
130
+ values = (0, paging_1.parseValues)(url.searchParams, overview_1.RUN_FIELDS);
131
+ page = values ? undefined : (0, paging_1.parsePage)(url.searchParams);
128
132
  }
129
133
  catch (e) {
130
134
  (0, http_2.send)(res, e instanceof overview_1.FilterRefused ? 403 : 400, { error: e instanceof Error ? e.message : String(e) });
131
135
  return true;
132
136
  }
133
137
  const runs = filter.scope === 'mine' ? registry.list(principal) : registry.all();
134
- (0, http_2.send)(res, 200, {
135
- runs: (0, overview_1.filterRuns)(runs, viewer, filter).map((run) => listRun(registry, definitions, viewer, run)),
136
- });
138
+ const list = (run) => listRun(registry, definitions, viewer, run);
139
+ try {
140
+ if (values) {
141
+ // a value of the field is counted over the runs the other filters let through
142
+ const { of, order } = (0, overview_1.runValues)(values.field);
143
+ const counted = (0, paging_1.countValues)((0, overview_1.filterRuns)(runs, viewer, (0, overview_1.withoutField)(filter, values.field)), of, values, order);
144
+ (0, http_2.send)(res, 200, (0, paging_1.answerPage)('values', counted));
145
+ }
146
+ else if (page) {
147
+ // newest first, as the registry keeps them
148
+ const found = (0, overview_1.filterRuns)(runs, viewer, filter);
149
+ const oldest = found[found.length - 1]?.createdAt;
150
+ const answered = (0, paging_1.pageByKey)(found, page, runKey);
151
+ (0, http_2.send)(res, 200, {
152
+ ...(0, paging_1.answerPage)('runs', { ...answered, items: answered.items.map(list) }),
153
+ ...(oldest ? { oldest } : {}),
154
+ });
155
+ }
156
+ else {
157
+ (0, http_2.send)(res, 200, { runs: (0, overview_1.filterRuns)(runs, viewer, filter).map(list) });
158
+ }
159
+ }
160
+ catch (e) {
161
+ if (!(e instanceof paging_1.ListParamError)) {
162
+ throw e;
163
+ }
164
+ (0, http_2.send)(res, 400, { error: e.message });
165
+ }
137
166
  }
138
167
  else if (method === 'POST') {
139
168
  await submitRun(config, registry, definitions, sources, sandboxes, planning, repos, endpointUrl, log, principal, req, res);
@@ -351,6 +380,8 @@ function toRunResponse(run, queue, named = false, nameOf) {
351
380
  ...(queue ? { queue } : {}),
352
381
  };
353
382
  }
383
+ // where a page of runs ends: when the last run was created, and which it is
384
+ const runKey = (run) => [run.createdAt, run.id];
354
385
  function listRun(registry, definitions, viewer, run) {
355
386
  return (0, overview_1.toListedRun)(viewer, run, (0, overview_1.queuePlace)(registry, viewer, run), entryOf(definitions));
356
387
  }
@@ -810,22 +841,53 @@ async function handleApi(api, principal, req, res, url) {
810
841
  if (!params.has('scope')) {
811
842
  params.set('scope', 'visible');
812
843
  }
813
- let filter;
844
+ // the text is looked for in the tasks, not in the runs they ran in
845
+ params.delete('q');
846
+ let filter, page, values, order;
847
+ const text = (0, paging_1.readText)(url.searchParams);
814
848
  try {
815
849
  filter = (0, overview_1.parseRunFilter)(params, viewer.grant);
850
+ values = (0, paging_1.parseValues)(url.searchParams, task_order_1.TASK_FIELDS);
851
+ page = values ? undefined : (0, paging_1.parsePage)(url.searchParams);
852
+ order = (0, task_order_1.parseTaskOrder)(url.searchParams);
816
853
  }
817
854
  catch (e) {
818
855
  (0, http_2.send)(res, e instanceof overview_1.FilterRefused ? 403 : 400, { error: e instanceof Error ? e.message : String(e) });
819
856
  return;
820
857
  }
821
- const runs = (0, overview_1.filterRuns)(registry.all(), viewer, filter).map((run) => (0, overview_1.toListedRun)(viewer, run));
822
- const oldest = (0, timings_1.getOldestCounted)(runs);
823
- // where in the repository the build file of the last run is, as its version says
824
- const tasks = (0, timings_1.getTaskStats)(runs, Date.now()).map((stats) => {
825
- const entry = stats.last.definition ? api.definitions.find(stats.last.definition)?.entry : undefined;
826
- return entry ? { ...stats, last: { ...stats.last, entry } } : stats;
827
- });
828
- (0, http_2.send)(res, 200, { tasks, ...(oldest ? { oldest } : {}) });
858
+ const statsOf = (over) => {
859
+ const runs = (0, overview_1.filterRuns)(registry.all(), viewer, over).map((run) => (0, overview_1.toListedRun)(viewer, run));
860
+ // where in the repository the build file of the last run is, as its version says
861
+ const tasks = (0, timings_1.getTaskStats)(runs, Date.now())
862
+ .map((stats) => {
863
+ const entry = stats.last.definition ? api.definitions.find(stats.last.definition)?.entry : undefined;
864
+ return entry ? { ...stats, last: { ...stats.last, entry } } : stats;
865
+ })
866
+ .filter((stats) => !text || (0, paging_1.hasText)([stats.repo, stats.task, stats.last.entry], text));
867
+ return { runs, tasks };
868
+ };
869
+ try {
870
+ if (values) {
871
+ const { sources, ...others } = filter;
872
+ void sources;
873
+ const counted = (0, paging_1.countValues)(statsOf(others).tasks, (stats) => [(0, task_order_1.taskSource)(stats)], values);
874
+ (0, http_2.send)(res, 200, (0, paging_1.answerPage)('values', counted));
875
+ return;
876
+ }
877
+ const { runs, tasks } = statsOf(filter);
878
+ const oldest = (0, timings_1.getOldestCounted)(runs);
879
+ const sorted = order ? (0, task_order_1.sortTasks)(tasks, order) : tasks;
880
+ (0, http_2.send)(res, 200, {
881
+ ...(page ? (0, paging_1.answerPage)('tasks', (0, paging_1.pageByPosition)(sorted, page)) : { tasks: sorted }),
882
+ ...(oldest ? { oldest } : {}),
883
+ });
884
+ }
885
+ catch (e) {
886
+ if (!(e instanceof paging_1.ListParamError)) {
887
+ throw e;
888
+ }
889
+ (0, http_2.send)(res, 400, { error: e.message });
890
+ }
829
891
  return;
830
892
  }
831
893
  if (await (0, approvals_api_1.handleApprovals)(config, registry, log, principal, req, res, url)) {
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sortTasks = exports.parseTaskOrder = exports.taskSource = exports.TASK_FIELDS = void 0;
4
+ const paging_1 = require("./paging");
5
+ // what GET /v1/tasks?values= answers the values of: where the tasks ran from
6
+ exports.TASK_FIELDS = ['source'];
7
+ // where the runs of a task are from, as the source filter of the runs names it
8
+ function taskSource(stats) {
9
+ return stats.repo === 'an upload' ? 'upload' : stats.repo;
10
+ }
11
+ exports.taskSource = taskSource;
12
+ // of its runs, how many came from the cache, and how many ended well (cached passed before)
13
+ const rate = (part, stats) => (stats.runs ? part / stats.runs : undefined);
14
+ // what `sort` of GET /v1/tasks orders by: the columns of the Tasks page
15
+ const SORT_BY = {
16
+ task: (stats) => stats.task,
17
+ last: (stats) => Date.parse(stats.last.at),
18
+ runs: (stats) => stats.runs,
19
+ cache: (stats) => rate(stats.cached, stats),
20
+ success: (stats) => rate(stats.runs - stats.failed, stats),
21
+ median: (stats) => stats.medianMs,
22
+ p90: (stats) => stats.p90Ms,
23
+ total: (stats) => stats.totalMs,
24
+ };
25
+ // `sort` and `order` (asc or desc, desc when left out): undefined when the caller names no sort.
26
+ function parseTaskOrder(params) {
27
+ const sort = params.get('sort');
28
+ if (sort === null) {
29
+ return undefined;
30
+ }
31
+ if (!(sort in SORT_BY)) {
32
+ throw new paging_1.ListParamError(`sort: one of ${Object.keys(SORT_BY).join(', ')}`);
33
+ }
34
+ const order = params.get('order') ?? 'desc';
35
+ if (order !== 'asc' && order !== 'desc') {
36
+ throw new paging_1.ListParamError('order: asc or desc');
37
+ }
38
+ return { sort, order };
39
+ }
40
+ exports.parseTaskOrder = parseTaskOrder;
41
+ // The tasks in that order, those without a value last, and by repository and name when equal.
42
+ function sortTasks(tasks, { sort, order }) {
43
+ const value = SORT_BY[sort];
44
+ return [...tasks].sort((a, b) => {
45
+ const x = value(a);
46
+ const y = value(b);
47
+ if (x === undefined || y === undefined) {
48
+ return x === y ? tie(a, b) : x === undefined ? 1 : -1;
49
+ }
50
+ const compared = typeof x === 'number' && typeof y === 'number' ? x - y : String(x).localeCompare(String(y));
51
+ return (order === 'asc' ? compared : -compared) || tie(a, b);
52
+ });
53
+ }
54
+ exports.sortTasks = sortTasks;
55
+ const tie = (a, b) => a.repo.localeCompare(b.repo) || a.task.localeCompare(b.task);
56
+ //# sourceMappingURL=task-order.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hammerkit",
3
3
  "description": "Containerized build tool with incremental caching that runs the same build file locally, in Docker and on Kubernetes.",
4
- "version": "2.1.0-next.10",
4
+ "version": "2.1.0-next.11",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",