@softure-ai/ci-usage 0.0.0-stage → 0.1.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE +21 -0
  3. package/README.md +128 -2
  4. package/dist/billing.d.ts +37 -0
  5. package/dist/billing.d.ts.map +1 -0
  6. package/dist/billing.js +83 -0
  7. package/dist/billing.js.map +1 -0
  8. package/dist/cli/command.d.ts +33 -0
  9. package/dist/cli/command.d.ts.map +1 -0
  10. package/dist/cli/command.js +203 -0
  11. package/dist/cli/command.js.map +1 -0
  12. package/dist/cli/main.d.ts +3 -0
  13. package/dist/cli/main.d.ts.map +1 -0
  14. package/dist/cli/main.js +10 -0
  15. package/dist/cli/main.js.map +1 -0
  16. package/dist/collect.d.ts +45 -0
  17. package/dist/collect.d.ts.map +1 -0
  18. package/dist/collect.js +189 -0
  19. package/dist/collect.js.map +1 -0
  20. package/dist/github.d.ts +41 -0
  21. package/dist/github.d.ts.map +1 -0
  22. package/dist/github.js +91 -0
  23. package/dist/github.js.map +1 -0
  24. package/dist/index.d.ts +10 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +11 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/messages/en.d.ts +51 -0
  29. package/dist/messages/en.d.ts.map +1 -0
  30. package/dist/messages/en.js +51 -0
  31. package/dist/messages/en.js.map +1 -0
  32. package/dist/messages/index.d.ts +11 -0
  33. package/dist/messages/index.d.ts.map +1 -0
  34. package/dist/messages/index.js +13 -0
  35. package/dist/messages/index.js.map +1 -0
  36. package/dist/prices.d.ts +32 -0
  37. package/dist/prices.d.ts.map +1 -0
  38. package/dist/prices.js +52 -0
  39. package/dist/prices.js.map +1 -0
  40. package/dist/publish.d.ts +29 -0
  41. package/dist/publish.d.ts.map +1 -0
  42. package/dist/publish.js +91 -0
  43. package/dist/publish.js.map +1 -0
  44. package/dist/report.d.ts +57 -0
  45. package/dist/report.d.ts.map +1 -0
  46. package/dist/report.js +168 -0
  47. package/dist/report.js.map +1 -0
  48. package/dist/runner.d.ts +28 -0
  49. package/dist/runner.d.ts.map +1 -0
  50. package/dist/runner.js +55 -0
  51. package/dist/runner.js.map +1 -0
  52. package/dist/usage.d.ts +67 -0
  53. package/dist/usage.d.ts.map +1 -0
  54. package/dist/usage.js +112 -0
  55. package/dist/usage.js.map +1 -0
  56. package/examples/ci-usage.yml +34 -0
  57. package/package.json +42 -4
  58. package/src/billing.ts +102 -0
  59. package/src/cli/command.ts +212 -0
  60. package/src/cli/main.ts +10 -0
  61. package/src/collect.ts +230 -0
  62. package/src/github.ts +113 -0
  63. package/src/index.ts +10 -0
  64. package/src/messages/en.ts +52 -0
  65. package/src/messages/index.ts +15 -0
  66. package/src/prices.ts +74 -0
  67. package/src/publish.ts +95 -0
  68. package/src/report.ts +215 -0
  69. package/src/runner.ts +64 -0
  70. package/src/usage.ts +192 -0
package/src/report.ts ADDED
@@ -0,0 +1,215 @@
1
+ // The report: one JSON document (schemaVersion 1) for agents and the same numbers as Markdown for people.
2
+ import type { BilledSku } from "./billing.js";
3
+ import type { CollectResult } from "./collect.js";
4
+ import { ciUsageMessages, formatMessage, type CiUsageMessages } from "./messages/index.js";
5
+ import type { PriceOptions } from "./prices.js";
6
+ import type { Provider } from "./runner.js";
7
+ import { summarizeUsage, type Cost, type RepositoryUsage } from "./usage.js";
8
+
9
+ export const SCHEMA_VERSION = 1;
10
+ export const REPORT_MARKER = "<!-- softure-ci-usage -->";
11
+ export const JSON_FILE = "ci-usage.json";
12
+ export const MARKDOWN_FILE = "ci-usage.md";
13
+ const TOP_JOBS = 15;
14
+
15
+ export interface RepositoryReport extends RepositoryUsage {
16
+ partial: string[];
17
+ error: string | null;
18
+ runsRunning: number;
19
+ runsFromCache: number;
20
+ /** What GitHub billed, when the billing lookup ran and succeeded; null otherwise. */
21
+ billing: BilledSku[] | null;
22
+ }
23
+
24
+ export interface CiUsageReport {
25
+ schemaVersion: typeof SCHEMA_VERSION;
26
+ generatedAt: string;
27
+ period: { from: string; to: string; days: number };
28
+ partial: boolean;
29
+ totals: Cost & { runs: number; jobs: number; byProvider: Record<Provider, Cost> };
30
+ prices: { source: string; checkedOn: string; ubicloudPremium: boolean; labels: Record<string, number> };
31
+ /** Report-wide notes, such as a refused billing lookup. */
32
+ notes: string[];
33
+ repositories: RepositoryReport[];
34
+ }
35
+
36
+ export interface BuildReportOptions {
37
+ collected: readonly CollectResult[];
38
+ from: Date;
39
+ to: Date;
40
+ generatedAt: Date;
41
+ priceOptions: PriceOptions;
42
+ billing?: { byRepository: Record<string, BilledSku[]> } | { error: string } | null;
43
+ }
44
+
45
+ function emptyCost(): Cost {
46
+ return { minutes: 0, usd: 0, unpricedMinutes: 0 };
47
+ }
48
+
49
+ function addTo(total: Cost, cost: Cost): void {
50
+ total.minutes += cost.minutes;
51
+ total.unpricedMinutes += cost.unpricedMinutes;
52
+ if (cost.usd === null) {
53
+ if (cost.minutes > 0) total.usd = null;
54
+ } else if (total.usd !== null) {
55
+ total.usd = Math.round((total.usd + cost.usd) * 10_000) / 10_000;
56
+ }
57
+ }
58
+
59
+ export function buildReport(options: BuildReportOptions): CiUsageReport {
60
+ const billing = options.billing ?? null;
61
+ const byRepository = billing !== null && "byRepository" in billing ? billing.byRepository : null;
62
+ const repositories: RepositoryReport[] = options.collected.map((result) => ({
63
+ ...summarizeUsage(result.repository, result.jobs, options.priceOptions),
64
+ partial: result.partial,
65
+ error: result.error,
66
+ runsRunning: result.runs.running,
67
+ runsFromCache: result.runs.fromCache,
68
+ billing: byRepository?.[result.repository] ?? null,
69
+ }));
70
+ const byProvider: Record<Provider, Cost> = { "github-hosted": emptyCost(), ubicloud: emptyCost(), "self-hosted": emptyCost() };
71
+ const totals = { ...emptyCost(), runs: 0, jobs: 0, byProvider };
72
+ for (const repository of repositories) {
73
+ addTo(totals, repository);
74
+ totals.runs += repository.runs;
75
+ totals.jobs += repository.jobs;
76
+ for (const provider of Object.keys(byProvider) as Provider[]) addTo(byProvider[provider], repository.byProvider[provider]);
77
+ }
78
+ const notes = billing !== null && "error" in billing ? [billing.error] : [];
79
+ const days = Math.round(((options.to.getTime() - options.from.getTime()) / 86_400_000) * 100) / 100;
80
+ return {
81
+ schemaVersion: SCHEMA_VERSION,
82
+ generatedAt: options.generatedAt.toISOString(),
83
+ period: { from: options.from.toISOString(), to: options.to.toISOString(), days },
84
+ partial: notes.length > 0 || repositories.some((repository) => repository.partial.length > 0 || repository.error !== null),
85
+ totals,
86
+ prices: {
87
+ source: options.priceOptions.prices.source,
88
+ checkedOn: options.priceOptions.prices.checkedOn,
89
+ ubicloudPremium: options.priceOptions.ubicloudPremium,
90
+ labels: options.priceOptions.prices.labels,
91
+ },
92
+ notes,
93
+ repositories,
94
+ };
95
+ }
96
+
97
+ function formatTime(iso: string): string {
98
+ return iso.replace(/:\d{2}\.\d{3}Z$/, "").replace("T", " ");
99
+ }
100
+
101
+ function formatUsd(usd: number | null, messages: CiUsageMessages): string {
102
+ return usd === null ? messages.unknown : usd.toFixed(2);
103
+ }
104
+
105
+ function formatShare(share: number): string {
106
+ return `${(share * 100).toFixed(1)}%`;
107
+ }
108
+
109
+ /** A Markdown table cell: backslashes, pipes and line breaks would break the row. */
110
+ function cell(text: string): string {
111
+ return text.replace(/\\/g, "\\\\").replace(/\|/g, "\\|").replace(/[\r\n]+/g, " ");
112
+ }
113
+
114
+ function table(header: string[], rows: string[][]): string[] {
115
+ return [`| ${header.join(" | ")} |`, `| ${header.map(() => "---").join(" | ")} |`, ...rows.map((row) => `| ${row.map(cell).join(" | ")} |`), ""];
116
+ }
117
+
118
+ function renderRepository(repository: RepositoryReport, messages: CiUsageMessages): string[] {
119
+ const lines = [`## ${repository.repository}`, ""];
120
+ if (repository.error !== null) return [...lines, formatMessage(messages.error, { error: repository.error }), ""];
121
+ if (repository.runs === 0) lines.push(messages.noRuns, "");
122
+ if (repository.byWorkflow.length > 0) {
123
+ lines.push(`### ${messages.workflowsHeading}`, "");
124
+ lines.push(
125
+ ...table(
126
+ [messages.workflow, messages.runs, messages.jobs, messages.minutes, messages.share, messages.usd],
127
+ repository.byWorkflow.map((workflow) => [
128
+ `${workflow.name} (\`${workflow.path}\`)`,
129
+ String(workflow.runs),
130
+ String(workflow.jobs),
131
+ String(workflow.minutes),
132
+ formatShare(workflow.share),
133
+ formatUsd(workflow.usd, messages),
134
+ ]),
135
+ ),
136
+ );
137
+ const jobs = repository.byWorkflow
138
+ .flatMap((workflow) => workflow.byJob.map((job) => ({ workflow: workflow.name, ...job })))
139
+ .sort((a, b) => b.minutes - a.minutes)
140
+ .slice(0, TOP_JOBS)
141
+ .filter((job) => job.minutes > 0);
142
+ if (jobs.length > 0) {
143
+ lines.push(`### ${messages.topJobsHeading}`, "");
144
+ lines.push(
145
+ ...table(
146
+ [messages.workflow, messages.job, messages.runner, messages.count, messages.minutes, messages.p50, messages.p95, messages.usd],
147
+ jobs.map((job) => [job.workflow, job.name, `\`${job.runner}\``, String(job.count), String(job.minutes), String(job.p50Minutes), String(job.p95Minutes), formatUsd(job.usd, messages)]),
148
+ ),
149
+ );
150
+ }
151
+ }
152
+ if (repository.longestJobs.length > 0) {
153
+ lines.push(`### ${messages.longestHeading}`, "");
154
+ for (const job of repository.longestJobs) {
155
+ lines.push(`- ${formatMessage(messages.longestItem, { minutes: job.minutes, url: job.url, workflow: job.workflow, job: job.job, runner: job.runner })}`);
156
+ }
157
+ lines.push("");
158
+ }
159
+ if (repository.billing !== null && repository.billing.length > 0) {
160
+ lines.push(`### ${messages.billingHeading}`, "");
161
+ lines.push(...table([messages.sku, messages.minutes, messages.gross, messages.net], repository.billing.map((sku) => [sku.sku, String(sku.minutes), sku.grossUsd.toFixed(2), sku.netUsd.toFixed(2)])));
162
+ }
163
+ const notes = [...repository.partial];
164
+ if (repository.runsRunning > 0) notes.push(formatMessage(messages.running, { count: repository.runsRunning }));
165
+ if (repository.runsFromCache > 0) notes.push(formatMessage(messages.fromCache, { count: repository.runsFromCache, total: repository.runs }));
166
+ if (repository.unpricedMinutes > 0) notes.push(formatMessage(messages.unknownPrice, { minutes: repository.unpricedMinutes }));
167
+ if (notes.length > 0) lines.push(`### ${messages.notesHeading}`, "", ...notes.map((note) => `- ${note}`), "");
168
+ return lines;
169
+ }
170
+
171
+ export function renderMarkdown(report: CiUsageReport, messages: CiUsageMessages = ciUsageMessages.en): string {
172
+ const lines = [
173
+ REPORT_MARKER,
174
+ `# ${messages.title}`,
175
+ "",
176
+ formatMessage(messages.period, { from: formatTime(report.period.from), to: formatTime(report.period.to), days: report.period.days, generated: formatTime(report.generatedAt) }),
177
+ report.partial ? messages.partial : messages.complete,
178
+ "",
179
+ messages.howToRead,
180
+ "",
181
+ `## ${messages.totalsHeading}`,
182
+ "",
183
+ ...table(
184
+ [messages.repository, messages.minutes, messages.usd, messages.githubHosted, messages.ubicloud, messages.selfHosted, messages.runs],
185
+ [
186
+ ...report.repositories.map((repository) => [
187
+ repository.repository,
188
+ repository.error === null ? String(repository.minutes) : "—",
189
+ repository.error === null ? formatUsd(repository.usd, messages) : "—",
190
+ String(repository.byProvider["github-hosted"].minutes),
191
+ String(repository.byProvider.ubicloud.minutes),
192
+ String(repository.byProvider["self-hosted"].minutes),
193
+ String(repository.runs),
194
+ ]),
195
+ ...(report.repositories.length > 1
196
+ ? [
197
+ [
198
+ `**${messages.totalsHeading}**`,
199
+ String(report.totals.minutes),
200
+ formatUsd(report.totals.usd, messages),
201
+ String(report.totals.byProvider["github-hosted"].minutes),
202
+ String(report.totals.byProvider.ubicloud.minutes),
203
+ String(report.totals.byProvider["self-hosted"].minutes),
204
+ String(report.totals.runs),
205
+ ],
206
+ ]
207
+ : []),
208
+ ],
209
+ ),
210
+ ];
211
+ if (report.notes.length > 0) lines.push(...report.notes.map((note) => `- ${note}`), "");
212
+ for (const repository of report.repositories) lines.push(...renderRepository(repository, messages));
213
+ lines.push(formatMessage(messages.prices, { source: report.prices.source, checkedOn: report.prices.checkedOn, premium: report.prices.ubicloudPremium ? messages.premium : "" }), "");
214
+ return lines.join("\n");
215
+ }
package/src/runner.ts ADDED
@@ -0,0 +1,64 @@
1
+ // Which runner a job ran on, read from the labels it asked for and the runner group that took it.
2
+
3
+ /** GitHub's billing SKUs for its standard hosted runners ("Actions runner pricing"). */
4
+ export const GITHUB_SKUS = ["actions_linux_slim", "actions_linux", "actions_linux_arm", "actions_windows", "actions_windows_arm", "actions_macos"] as const;
5
+
6
+ export type GithubSku = (typeof GITHUB_SKUS)[number];
7
+
8
+ export type Architecture = "x64" | "arm64";
9
+
10
+ export type RunnerClass =
11
+ | { provider: "github-hosted"; label: string; sku: GithubSku }
12
+ | { provider: "ubicloud"; label: string; vcpu: number; architecture: Architecture }
13
+ | { provider: "self-hosted"; label: string };
14
+
15
+ export type Provider = RunnerClass["provider"];
16
+
17
+ export interface RunnerInput {
18
+ labels: readonly string[];
19
+ runnerGroupName: string | null;
20
+ }
21
+
22
+ /** The runner group GitHub assigns to its own hosted runners. */
23
+ const GITHUB_RUNNER_GROUP = "GitHub Actions";
24
+ const UBICLOUD_LABEL = /^ubicloud(?:-(?:standard|premium)-(\d+))?(-arm)?(?:-ubuntu-\d{4})?$/;
25
+ const UBICLOUD_VCPUS = new Set([2, 4, 8, 16, 30]);
26
+ const DEFAULT_UBICLOUD_VCPU = 2;
27
+
28
+ function getGithubSku(label: string): GithubSku | null {
29
+ if (label === "ubuntu-slim") return "actions_linux_slim";
30
+ if (/^ubuntu-.*-arm$/.test(label)) return "actions_linux_arm";
31
+ if (label.startsWith("ubuntu-")) return "actions_linux";
32
+ if (/^windows-.*-arm$/.test(label)) return "actions_windows_arm";
33
+ if (label.startsWith("windows-")) return "actions_windows";
34
+ if (label.startsWith("macos-")) return "actions_macos";
35
+ return null;
36
+ }
37
+
38
+ function classifyUbicloud(label: string): RunnerClass | null {
39
+ const match = UBICLOUD_LABEL.exec(label);
40
+ if (!match) return null;
41
+ const vcpu = match[1] === undefined ? DEFAULT_UBICLOUD_VCPU : Number(match[1]);
42
+ if (!UBICLOUD_VCPUS.has(vcpu)) return null;
43
+ return { provider: "ubicloud", label, vcpu, architecture: match[2] === undefined ? "x64" : "arm64" };
44
+ }
45
+
46
+ /**
47
+ * Classifies one job's runner. A Ubicloud label wins; then a GitHub image label (`ubuntu-*`, `windows-*`, `macos-*`)
48
+ * or GitHub's own runner group; anything else is a self-hosted runner priced only from the caller's table.
49
+ */
50
+ export function classifyRunner(input: RunnerInput): RunnerClass {
51
+ const label = input.labels.join(",") || "unknown";
52
+ for (const candidate of input.labels) {
53
+ const ubicloud = classifyUbicloud(candidate);
54
+ if (ubicloud) return ubicloud;
55
+ }
56
+ for (const candidate of input.labels) {
57
+ const sku = getGithubSku(candidate);
58
+ if (sku) return { provider: "github-hosted", label: candidate, sku };
59
+ }
60
+ // A larger GitHub runner carries a custom label but runs in GitHub's group; it is billed like Linux unless the
61
+ // caller's price table names its label.
62
+ if (input.runnerGroupName === GITHUB_RUNNER_GROUP) return { provider: "github-hosted", label, sku: "actions_linux" };
63
+ return { provider: "self-hosted", label };
64
+ }
package/src/usage.ts ADDED
@@ -0,0 +1,192 @@
1
+ // Billable minutes per job and their sums per repository, workflow and job name.
2
+ import { getMinutePrice, type PriceOptions } from "./prices.js";
3
+ import { classifyRunner, type Provider, type RunnerClass } from "./runner.js";
4
+
5
+ /** One job attempt as the collector hands it over, already narrowed from the API. */
6
+ export interface JobRecord {
7
+ id: number;
8
+ runId: number;
9
+ runAttempt: number;
10
+ name: string;
11
+ workflowName: string;
12
+ workflowPath: string;
13
+ startedAt: string | null;
14
+ completedAt: string | null;
15
+ /** The run's last update; the end of a job that started but never recorded its completion. */
16
+ runUpdatedAt: string;
17
+ labels: string[];
18
+ runnerGroupName: string | null;
19
+ htmlUrl: string;
20
+ }
21
+
22
+ const MINUTE_MS = 60_000;
23
+
24
+ /**
25
+ * Billable minutes of one job: its run time rounded up to a whole minute, as GitHub and Ubicloud bill. A job that
26
+ * never started costs nothing; one that started without a recorded end is billed up to the run's last update.
27
+ */
28
+ export function getJobMinutes(job: JobRecord): number {
29
+ if (job.startedAt === null) return 0;
30
+ const start = Date.parse(job.startedAt);
31
+ const end = Date.parse(job.completedAt ?? job.runUpdatedAt);
32
+ if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start) return 0;
33
+ return Math.ceil((end - start) / MINUTE_MS);
34
+ }
35
+
36
+ export interface Cost {
37
+ minutes: number;
38
+ /** Estimated USD; null when some minutes ran on a runner without a known price. */
39
+ usd: number | null;
40
+ /** Minutes on runners without a known price, included in `minutes`. */
41
+ unpricedMinutes: number;
42
+ }
43
+
44
+ export interface JobUsage extends Cost {
45
+ name: string;
46
+ runner: string;
47
+ provider: Provider;
48
+ count: number;
49
+ p50Minutes: number;
50
+ p95Minutes: number;
51
+ }
52
+
53
+ export interface WorkflowUsage extends Cost {
54
+ name: string;
55
+ path: string;
56
+ runs: number;
57
+ jobs: number;
58
+ /** Share of the repository's minutes, 0..1. */
59
+ share: number;
60
+ byJob: JobUsage[];
61
+ }
62
+
63
+ export interface LongJob {
64
+ workflow: string;
65
+ job: string;
66
+ runner: string;
67
+ minutes: number;
68
+ url: string;
69
+ }
70
+
71
+ export interface RepositoryUsage extends Cost {
72
+ repository: string;
73
+ runs: number;
74
+ jobs: number;
75
+ byProvider: Record<Provider, Cost>;
76
+ byWorkflow: WorkflowUsage[];
77
+ longestJobs: LongJob[];
78
+ }
79
+
80
+ const LONGEST_JOBS = 10;
81
+
82
+ function emptyCost(): Cost {
83
+ return { minutes: 0, usd: 0, unpricedMinutes: 0 };
84
+ }
85
+
86
+ function addCost(total: Cost, minutes: number, price: number | null): void {
87
+ total.minutes += minutes;
88
+ if (price === null) {
89
+ total.unpricedMinutes += minutes;
90
+ if (minutes > 0) total.usd = null;
91
+ } else if (total.usd !== null) {
92
+ total.usd += minutes * price;
93
+ }
94
+ }
95
+
96
+ function roundUsd(cost: Cost): void {
97
+ if (cost.usd !== null) cost.usd = Math.round(cost.usd * 10_000) / 10_000;
98
+ }
99
+
100
+ /** Nearest-rank percentile of sorted minutes; 0 for an empty list. */
101
+ export function getPercentile(sorted: readonly number[], percentile: number): number {
102
+ if (sorted.length === 0) return 0;
103
+ const rank = Math.max(1, Math.ceil((percentile / 100) * sorted.length));
104
+ return sorted[rank - 1] ?? 0;
105
+ }
106
+
107
+ interface JobBucket {
108
+ cost: Cost;
109
+ runner: RunnerClass;
110
+ durations: number[];
111
+ }
112
+
113
+ interface WorkflowBucket {
114
+ cost: Cost;
115
+ name: string;
116
+ runIds: Set<number>;
117
+ jobs: number;
118
+ byJob: Map<string, JobBucket>;
119
+ }
120
+
121
+ /** Sums the jobs of one repository; workflows and their jobs are sorted by minutes, most first. */
122
+ export function summarizeUsage(repository: string, jobs: readonly JobRecord[], options: PriceOptions): RepositoryUsage {
123
+ const total = emptyCost();
124
+ const byProvider: Record<Provider, Cost> = { "github-hosted": emptyCost(), ubicloud: emptyCost(), "self-hosted": emptyCost() };
125
+ const workflows = new Map<string, WorkflowBucket>();
126
+ const runIds = new Set<number>();
127
+ const longest: LongJob[] = [];
128
+
129
+ for (const job of jobs) {
130
+ const minutes = getJobMinutes(job);
131
+ const runner = classifyRunner({ labels: job.labels, runnerGroupName: job.runnerGroupName });
132
+ const price = getMinutePrice(runner, options);
133
+ runIds.add(job.runId);
134
+ addCost(total, minutes, price);
135
+ addCost(byProvider[runner.provider], minutes, price);
136
+
137
+ const workflow: WorkflowBucket = workflows.get(job.workflowPath) ?? { cost: emptyCost(), name: job.workflowName, runIds: new Set<number>(), jobs: 0, byJob: new Map<string, JobBucket>() };
138
+ workflows.set(job.workflowPath, workflow);
139
+ workflow.runIds.add(job.runId);
140
+ workflow.jobs += 1;
141
+ addCost(workflow.cost, minutes, price);
142
+
143
+ const key = `${job.name}\u0000${runner.label}`;
144
+ const bucket: JobBucket = workflow.byJob.get(key) ?? { cost: emptyCost(), runner, durations: [] };
145
+ workflow.byJob.set(key, bucket);
146
+ bucket.durations.push(minutes);
147
+ addCost(bucket.cost, minutes, price);
148
+
149
+ if (minutes > 0) longest.push({ workflow: job.workflowName, job: job.name, runner: runner.label, minutes, url: job.htmlUrl });
150
+ }
151
+
152
+ const byWorkflow: WorkflowUsage[] = [...workflows.entries()].map(([path, bucket]) => {
153
+ roundUsd(bucket.cost);
154
+ const byJob: JobUsage[] = [...bucket.byJob.entries()].map(([key, jobBucket]) => {
155
+ roundUsd(jobBucket.cost);
156
+ const sorted = [...jobBucket.durations].sort((a, b) => a - b);
157
+ return {
158
+ name: key.split("\u0000")[0] ?? key,
159
+ runner: jobBucket.runner.label,
160
+ provider: jobBucket.runner.provider,
161
+ count: sorted.length,
162
+ p50Minutes: getPercentile(sorted, 50),
163
+ p95Minutes: getPercentile(sorted, 95),
164
+ ...jobBucket.cost,
165
+ };
166
+ });
167
+ byJob.sort((a, b) => b.minutes - a.minutes || a.name.localeCompare(b.name));
168
+ return {
169
+ name: bucket.name,
170
+ path,
171
+ runs: bucket.runIds.size,
172
+ jobs: bucket.jobs,
173
+ share: total.minutes === 0 ? 0 : Math.round((bucket.cost.minutes / total.minutes) * 1000) / 1000,
174
+ byJob,
175
+ ...bucket.cost,
176
+ };
177
+ });
178
+ byWorkflow.sort((a, b) => b.minutes - a.minutes || a.path.localeCompare(b.path));
179
+ roundUsd(total);
180
+ for (const cost of Object.values(byProvider)) roundUsd(cost);
181
+ longest.sort((a, b) => b.minutes - a.minutes || a.url.localeCompare(b.url));
182
+
183
+ return {
184
+ repository,
185
+ runs: runIds.size,
186
+ jobs: jobs.length,
187
+ ...total,
188
+ byProvider,
189
+ byWorkflow,
190
+ longestJobs: longest.slice(0, LONGEST_JOBS),
191
+ };
192
+ }