|
1 | 1 | import { TypeOf, z } from "zod"; |
| 2 | +import type { MachinePresetName } from "./common.js"; |
| 3 | +import type { RuntimeEnvironmentType } from "./common.js"; |
| 4 | +import type { IdempotencyKeyScope } from "../idempotency-key-catalog/catalog.js"; |
2 | 5 |
|
3 | 6 | /** |
4 | 7 | * Request body schema for executing a query |
@@ -39,3 +42,168 @@ export const QueryExecuteResponseBody = z.discriminatedUnion("format", [ |
39 | 42 | QueryExecuteCSVResponseBody, |
40 | 43 | ]); |
41 | 44 | export type QueryExecuteResponseBody = z.infer<typeof QueryExecuteResponseBody>; |
| 45 | + |
| 46 | +// --------------------------------------------------------------------------- |
| 47 | +// Query table row types |
| 48 | +// --------------------------------------------------------------------------- |
| 49 | + |
| 50 | +/** |
| 51 | + * User-facing friendly run status values returned by the query system. |
| 52 | + */ |
| 53 | +export const runFriendlyStatus = [ |
| 54 | + "Delayed", |
| 55 | + "Queued", |
| 56 | + "Pending version", |
| 57 | + "Dequeued", |
| 58 | + "Executing", |
| 59 | + "Waiting", |
| 60 | + "Reattempting", |
| 61 | + "Paused", |
| 62 | + "Canceled", |
| 63 | + "Interrupted", |
| 64 | + "Completed", |
| 65 | + "Failed", |
| 66 | + "System failure", |
| 67 | + "Crashed", |
| 68 | + "Expired", |
| 69 | + "Timed out", |
| 70 | +] as const; |
| 71 | + |
| 72 | +export type RunFriendlyStatus = (typeof runFriendlyStatus)[number]; |
| 73 | + |
| 74 | +/** |
| 75 | + * Full row type for the `runs` query table. |
| 76 | + * |
| 77 | + * Each property corresponds to a column available in TSQL queries against the |
| 78 | + * `runs` table. Types are mapped from the underlying ClickHouse column types: |
| 79 | + * |
| 80 | + * - `String` → `string` |
| 81 | + * - `UInt8` / `UInt32` / `Int64` / `Float64` → `number` |
| 82 | + * - `DateTime64` → `string` |
| 83 | + * - `Nullable(X)` → `X | null` |
| 84 | + * - `Array(String)` → `string[]` |
| 85 | + * - `JSON` → `Record<string, unknown>` |
| 86 | + * - `LowCardinality(String)` with constrained values → narrow union type |
| 87 | + */ |
| 88 | +export interface RunsTableRow { |
| 89 | + /** Unique run ID (e.g. `run_cm1a2b3c4d5e6f7g8h9i`) */ |
| 90 | + run_id: string; |
| 91 | + /** Environment slug */ |
| 92 | + environment: string; |
| 93 | + /** Project reference (e.g. `proj_howcnaxbfxdmwmxazktx`) */ |
| 94 | + project: string; |
| 95 | + /** Environment type */ |
| 96 | + environment_type: RuntimeEnvironmentType; |
| 97 | + /** Number of attempts (starts at 1) */ |
| 98 | + attempt_count: number; |
| 99 | + /** Run status (friendly name) */ |
| 100 | + status: RunFriendlyStatus; |
| 101 | + /** Whether the run is finished (0 or 1) */ |
| 102 | + is_finished: number; |
| 103 | + /** Task identifier/slug */ |
| 104 | + task_identifier: string; |
| 105 | + /** Queue name */ |
| 106 | + queue: string; |
| 107 | + /** Batch ID (if part of a batch), or `null` */ |
| 108 | + batch_id: string | null; |
| 109 | + /** Root run ID (for child runs), or `null` */ |
| 110 | + root_run_id: string | null; |
| 111 | + /** Parent run ID (for child runs), or `null` */ |
| 112 | + parent_run_id: string | null; |
| 113 | + /** Nesting depth (0 for root runs) */ |
| 114 | + depth: number; |
| 115 | + /** Whether this is a root run (0 or 1) */ |
| 116 | + is_root_run: number; |
| 117 | + /** Whether this is a child run (0 or 1) */ |
| 118 | + is_child_run: number; |
| 119 | + /** Idempotency key */ |
| 120 | + idempotency_key: string; |
| 121 | + /** Idempotency key scope (empty string means no idempotency key is set) */ |
| 122 | + idempotency_key_scope: IdempotencyKeyScope | ""; |
| 123 | + /** Region, or `null` */ |
| 124 | + region: string | null; |
| 125 | + /** When the run was triggered (ISO 8601) */ |
| 126 | + triggered_at: string; |
| 127 | + /** When the run was queued, or `null` */ |
| 128 | + queued_at: string | null; |
| 129 | + /** When the run was dequeued, or `null` */ |
| 130 | + dequeued_at: string | null; |
| 131 | + /** When execution began, or `null` */ |
| 132 | + executed_at: string | null; |
| 133 | + /** When the run completed, or `null` */ |
| 134 | + completed_at: string | null; |
| 135 | + /** Delayed execution until this time, or `null` */ |
| 136 | + delay_until: string | null; |
| 137 | + /** Whether the run had a delay (0 or 1) */ |
| 138 | + has_delay: number; |
| 139 | + /** When the run expired, or `null` */ |
| 140 | + expired_at: string | null; |
| 141 | + /** TTL string for expiration (e.g. `"10m"`) */ |
| 142 | + ttl: string; |
| 143 | + /** Time from execution start to completion in ms, or `null` */ |
| 144 | + execution_duration: number | null; |
| 145 | + /** Time from trigger to completion in ms, or `null` */ |
| 146 | + total_duration: number | null; |
| 147 | + /** Time from queued to dequeued in ms, or `null` */ |
| 148 | + queued_duration: number | null; |
| 149 | + /** Compute usage duration in ms */ |
| 150 | + usage_duration: number; |
| 151 | + /** Compute cost in dollars */ |
| 152 | + compute_cost: number; |
| 153 | + /** Invocation cost in dollars */ |
| 154 | + invocation_cost: number; |
| 155 | + /** Total cost in dollars (compute + invocation) */ |
| 156 | + total_cost: number; |
| 157 | + /** The data returned from the task */ |
| 158 | + output: Record<string, unknown>; |
| 159 | + /** Error data if the run failed */ |
| 160 | + error: Record<string, unknown>; |
| 161 | + /** Tags added to the run */ |
| 162 | + tags: string[]; |
| 163 | + /** Code version in reverse date format (e.g. `"20240115.1"`) */ |
| 164 | + task_version: string; |
| 165 | + /** SDK package version */ |
| 166 | + sdk_version: string; |
| 167 | + /** CLI package version */ |
| 168 | + cli_version: string; |
| 169 | + /** Machine preset the run executed on */ |
| 170 | + machine: MachinePresetName; |
| 171 | + /** Whether this is a test run (0 or 1) */ |
| 172 | + is_test: number; |
| 173 | + /** Concurrency key passed when triggering */ |
| 174 | + concurrency_key: string; |
| 175 | + /** Max allowed compute duration in seconds, or `null` */ |
| 176 | + max_duration: number | null; |
| 177 | + /** Bulk action group IDs that operated on this run */ |
| 178 | + bulk_action_group_ids: string[]; |
| 179 | +} |
| 180 | + |
| 181 | +/** @internal Map of query table names to their full row types */ |
| 182 | +type QueryTableMap = { |
| 183 | + runs: RunsTableRow; |
| 184 | +}; |
| 185 | + |
| 186 | +/** |
| 187 | + * Type helper for Query results. |
| 188 | + * |
| 189 | + * @example |
| 190 | + * ```typescript |
| 191 | + * // All columns from the runs table |
| 192 | + * type AllRuns = QueryTable<"runs">; |
| 193 | + * |
| 194 | + * // Only specific columns |
| 195 | + * type MyResult = QueryTable<"runs", "status" | "run_id">; |
| 196 | + * |
| 197 | + * // Access a single field type |
| 198 | + * type Status = QueryTable<"runs">["status"]; // RunFriendlyStatus |
| 199 | + * |
| 200 | + * // Use with query.execute |
| 201 | + * const result = await query.execute<QueryTable<"runs", "status" | "run_id">>( |
| 202 | + * "SELECT status, run_id FROM runs" |
| 203 | + * ); |
| 204 | + * ``` |
| 205 | + */ |
| 206 | +export type QueryTable< |
| 207 | + TTable extends keyof QueryTableMap, |
| 208 | + TColumns extends keyof QueryTableMap[TTable] = keyof QueryTableMap[TTable], |
| 209 | +> = Pick<QueryTableMap[TTable], TColumns>; |
0 commit comments