|
| 1 | +// Shared utilities for GitHub runners functionality |
| 2 | + |
| 3 | +// Types |
| 4 | +export interface RunnerData { |
| 5 | + id: number; |
| 6 | + name: string; |
| 7 | + os: string; |
| 8 | + status: "online" | "offline"; |
| 9 | + busy: boolean; |
| 10 | + labels: Array<{ |
| 11 | + id?: number; |
| 12 | + name: string; |
| 13 | + type: "read-only" | "custom"; |
| 14 | + }>; |
| 15 | +} |
| 16 | + |
| 17 | +export interface RunnerGroup { |
| 18 | + label: string; |
| 19 | + totalCount: number; |
| 20 | + idleCount: number; |
| 21 | + busyCount: number; |
| 22 | + offlineCount: number; |
| 23 | + runners: RunnerData[]; |
| 24 | +} |
| 25 | + |
| 26 | +export interface RunnersApiResponse { |
| 27 | + groups: RunnerGroup[]; |
| 28 | + totalRunners: number; |
| 29 | +} |
| 30 | + |
| 31 | +// Utility functions |
| 32 | +export function getRunnerGroupLabel(runner: RunnerData): string { |
| 33 | + const labelNames = runner.labels.map((label) => label.name); |
| 34 | + |
| 35 | + // Find labels with "." (excluding any that end with ".runners") or starting with "macos-" |
| 36 | + // Why have such funky logic? We have many labels on our runners today, but this |
| 37 | + // is what's common in all the ones that jobs actually use. |
| 38 | + const validLabels = labelNames.filter( |
| 39 | + (name) => |
| 40 | + (name.includes(".") && !name.endsWith(".runners")) || // "*.runners" is added to autoscaled runners |
| 41 | + name.startsWith("macos-") |
| 42 | + ); |
| 43 | + |
| 44 | + if (validLabels.length > 0) { |
| 45 | + // Handle synonyms. Today these are used by macOS runners which have two |
| 46 | + // labels that runners could potentially use instead of just one. |
| 47 | + // The synonymous labels tend to look like "macos-m1-14" and "macos-m1-stable" |
| 48 | + // If we end up in this situation, assume all valid labels are valid synonyms |
| 49 | + // and treat them as such. |
| 50 | + return validLabels.join(" / "); |
| 51 | + } |
| 52 | + |
| 53 | + // Fallback: Parse runner name for grouping info |
| 54 | + // Special case for ROCm runners provided by that don't have proper GitHub labels |
| 55 | + // but use naming conventions like: linux.rocm.gpu.gfx942.1-xxxx-runner-xxxxx |
| 56 | + const runnerName = runner.name; |
| 57 | + |
| 58 | + // Look for dotted prefixes before "-" followed by random suffix |
| 59 | + const namePatterns = [ |
| 60 | + /^([a-z]+\.[a-z0-9.]+)-[a-z0-9]+/i, // linux.rocm.gpu.gfx942.1-xxxx |
| 61 | + /^([a-z]+\.[a-z0-9.]+\.[a-z0-9]+)/i, // linux.rocm.gpu prefix |
| 62 | + ]; |
| 63 | + |
| 64 | + for (const pattern of namePatterns) { |
| 65 | + const match = runnerName.match(pattern); |
| 66 | + if (match) { |
| 67 | + return match[1]; // Return the prefix part |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // If name starts with a dotted pattern, extract it |
| 72 | + if (runnerName.includes(".")) { |
| 73 | + const parts = runnerName.split("-"); |
| 74 | + if (parts[0].includes(".")) { |
| 75 | + return parts[0]; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return "unknown"; |
| 80 | +} |
| 81 | + |
| 82 | +// Helper function for sorting - pushes "unknown" labels to the end |
| 83 | +export function unknownGoesLast( |
| 84 | + a: { label: string }, |
| 85 | + b: { label: string } |
| 86 | +): number { |
| 87 | + if (a.label === "unknown" && b.label !== "unknown") return 1; |
| 88 | + if (a.label !== "unknown" && b.label === "unknown") return -1; |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +export function groupRunners(runners: RunnerData[]): RunnerGroup[] { |
| 93 | + const groups = new Map<string, RunnerData[]>(); |
| 94 | + |
| 95 | + // Group runners by label |
| 96 | + for (const runner of runners) { |
| 97 | + const label = getRunnerGroupLabel(runner); |
| 98 | + if (!groups.has(label)) { |
| 99 | + groups.set(label, []); |
| 100 | + } |
| 101 | + groups.get(label)!.push(runner); |
| 102 | + } |
| 103 | + |
| 104 | + // Convert to RunnerGroup format with counts |
| 105 | + const result: RunnerGroup[] = []; |
| 106 | + for (const [label, groupRunners] of groups.entries()) { |
| 107 | + const idleCount = groupRunners.filter( |
| 108 | + (r) => r.status === "online" && !r.busy |
| 109 | + ).length; |
| 110 | + const busyCount = groupRunners.filter( |
| 111 | + (r) => r.status === "online" && r.busy |
| 112 | + ).length; |
| 113 | + const offlineCount = groupRunners.filter( |
| 114 | + (r) => r.status === "offline" |
| 115 | + ).length; |
| 116 | + |
| 117 | + // Helper function to get status priority: idle (0), busy (1), offline (2) |
| 118 | + const getStatusPriority = (runner: RunnerData): number => { |
| 119 | + if (runner.status === "offline") return 2; |
| 120 | + if (runner.status === "online" && runner.busy) return 1; |
| 121 | + return 0; // idle |
| 122 | + }; |
| 123 | + |
| 124 | + // Sort runners by status (idle, busy, offline) then by name |
| 125 | + const sortedRunners = groupRunners.sort((a, b) => { |
| 126 | + // First compare by status priority |
| 127 | + const statusComparison = getStatusPriority(a) - getStatusPriority(b); |
| 128 | + |
| 129 | + // If status is the same, sort by name |
| 130 | + return statusComparison !== 0 |
| 131 | + ? statusComparison |
| 132 | + : a.name.localeCompare(b.name); |
| 133 | + }); |
| 134 | + |
| 135 | + result.push({ |
| 136 | + label, |
| 137 | + totalCount: groupRunners.length, |
| 138 | + idleCount, |
| 139 | + busyCount, |
| 140 | + offlineCount, |
| 141 | + runners: sortedRunners, |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + // Sort groups by unknown status first, then by total count (descending) |
| 146 | + result.sort((a, b) => { |
| 147 | + const unknownComparison = unknownGoesLast(a, b); |
| 148 | + return unknownComparison !== 0 |
| 149 | + ? unknownComparison |
| 150 | + : b.totalCount - a.totalCount; |
| 151 | + }); |
| 152 | + |
| 153 | + return result; |
| 154 | +} |
0 commit comments