|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref, computed } from 'vue' |
| 3 | +import { executeRawQuery, parseRows } from '@/composables/useQuery' |
| 4 | +import type { ColumnMeta } from '@/types/rpc' |
| 5 | +
|
| 6 | +interface QueryTemplate { |
| 7 | + label: string |
| 8 | + description: string |
| 9 | + sql: string |
| 10 | +} |
| 11 | +
|
| 12 | +const QUERY_TEMPLATES: QueryTemplate[] = [ |
| 13 | + { |
| 14 | + label: 'All Jobs', |
| 15 | + description: 'List all jobs ordered by submission time', |
| 16 | + sql: 'SELECT job_id, user_id, state, submitted_at_ms, started_at_ms, finished_at_ms, num_tasks, error FROM jobs ORDER BY submitted_at_ms DESC LIMIT 100', |
| 17 | + }, |
| 18 | + { |
| 19 | + label: 'Running Tasks', |
| 20 | + description: 'Tasks currently in running state', |
| 21 | + sql: 'SELECT task_id, job_id, state, started_at_ms, failure_count, preemption_count FROM tasks WHERE state = 3 ORDER BY started_at_ms DESC LIMIT 100', |
| 22 | + }, |
| 23 | + { |
| 24 | + label: 'Worker Status', |
| 25 | + description: 'All workers with health and resource info', |
| 26 | + sql: 'SELECT worker_id, address, healthy, active, consecutive_failures, last_heartbeat_ms, committed_gpu, committed_tpu FROM workers ORDER BY last_heartbeat_ms DESC', |
| 27 | + }, |
| 28 | + { |
| 29 | + label: 'Jobs per User', |
| 30 | + description: 'Job count grouped by user and state', |
| 31 | + sql: 'SELECT user_id, state, COUNT(*) as job_count FROM jobs GROUP BY user_id, state ORDER BY user_id ASC', |
| 32 | + }, |
| 33 | +] |
| 34 | +
|
| 35 | +const sqlInput = ref(QUERY_TEMPLATES[0].sql) |
| 36 | +
|
| 37 | +const columns = ref<ColumnMeta[]>([]) |
| 38 | +const rows = ref<Record<string, unknown>[]>([]) |
| 39 | +const loading = ref(false) |
| 40 | +const error = ref<string | null>(null) |
| 41 | +const hasExecuted = ref(false) |
| 42 | +
|
| 43 | +const PAGE_SIZE = 25 |
| 44 | +const currentPage = ref(0) |
| 45 | +
|
| 46 | +const totalPages = computed(() => |
| 47 | + Math.max(1, Math.ceil(rows.value.length / PAGE_SIZE)) |
| 48 | +) |
| 49 | +
|
| 50 | +const paginatedRows = computed(() => { |
| 51 | + const start = currentPage.value * PAGE_SIZE |
| 52 | + return rows.value.slice(start, start + PAGE_SIZE) |
| 53 | +}) |
| 54 | +
|
| 55 | +function applyTemplate(template: QueryTemplate) { |
| 56 | + sqlInput.value = template.sql |
| 57 | +} |
| 58 | +
|
| 59 | +async function execute() { |
| 60 | + loading.value = true |
| 61 | + error.value = null |
| 62 | + hasExecuted.value = true |
| 63 | + currentPage.value = 0 |
| 64 | + columns.value = [] |
| 65 | + rows.value = [] |
| 66 | +
|
| 67 | + try { |
| 68 | + const response = await executeRawQuery({ sql: sqlInput.value }) |
| 69 | + columns.value = response.columns |
| 70 | + rows.value = parseRows(response.columns, response.rows) |
| 71 | + } catch (e) { |
| 72 | + error.value = e instanceof Error ? e.message : String(e) |
| 73 | + } finally { |
| 74 | + loading.value = false |
| 75 | + } |
| 76 | +} |
| 77 | +
|
| 78 | +function goToPage(page: number) { |
| 79 | + if (page < 0 || page >= totalPages.value) return |
| 80 | + currentPage.value = page |
| 81 | +} |
| 82 | +
|
| 83 | +function formatCellValue(value: unknown): string { |
| 84 | + if (value === null || value === undefined) return '\u2014' |
| 85 | + if (typeof value === 'object') return JSON.stringify(value) |
| 86 | + return String(value) |
| 87 | +} |
| 88 | +
|
| 89 | +function handleKeydown(event: KeyboardEvent) { |
| 90 | + if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') { |
| 91 | + execute() |
| 92 | + } |
| 93 | +} |
| 94 | +</script> |
| 95 | + |
| 96 | +<template> |
| 97 | + <div class="space-y-4"> |
| 98 | + <!-- Templates --> |
| 99 | + <div class="flex items-center gap-2 flex-wrap"> |
| 100 | + <span class="text-xs text-text-secondary">Templates:</span> |
| 101 | + <button |
| 102 | + v-for="template in QUERY_TEMPLATES" |
| 103 | + :key="template.label" |
| 104 | + class="px-2.5 py-1 text-xs rounded border border-surface-border text-text-secondary |
| 105 | + hover:text-text hover:bg-surface-sunken transition-colors" |
| 106 | + :title="template.description" |
| 107 | + @click="applyTemplate(template)" |
| 108 | + > |
| 109 | + {{ template.label }} |
| 110 | + </button> |
| 111 | + </div> |
| 112 | + |
| 113 | + <!-- SQL input --> |
| 114 | + <div> |
| 115 | + <label class="block text-xs font-medium text-text-secondary mb-1"> |
| 116 | + SQL Query (SELECT only) |
| 117 | + </label> |
| 118 | + <textarea |
| 119 | + v-model="sqlInput" |
| 120 | + rows="6" |
| 121 | + class="w-full font-mono text-[13px] bg-surface-sunken text-text border border-surface-border |
| 122 | + rounded-lg px-3 py-2 resize-y focus:outline-none focus:ring-1 focus:ring-accent" |
| 123 | + placeholder="SELECT * FROM jobs LIMIT 10" |
| 124 | + spellcheck="false" |
| 125 | + @keydown="handleKeydown" |
| 126 | + /> |
| 127 | + </div> |
| 128 | + |
| 129 | + <!-- Execute button --> |
| 130 | + <div class="flex items-center gap-3"> |
| 131 | + <button |
| 132 | + class="px-4 py-2 text-sm font-medium text-white bg-accent rounded-lg |
| 133 | + hover:bg-accent/90 transition-colors disabled:opacity-50 disabled:cursor-not-allowed" |
| 134 | + :disabled="loading" |
| 135 | + @click="execute" |
| 136 | + > |
| 137 | + <span v-if="loading" class="inline-flex items-center gap-1.5"> |
| 138 | + <svg class="animate-spin h-3.5 w-3.5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"> |
| 139 | + <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" /> |
| 140 | + <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" /> |
| 141 | + </svg> |
| 142 | + Executing... |
| 143 | + </span> |
| 144 | + <span v-else>Execute</span> |
| 145 | + </button> |
| 146 | + <span class="text-xs text-text-muted"> |
| 147 | + Ctrl+Enter / Cmd+Enter to run |
| 148 | + </span> |
| 149 | + </div> |
| 150 | + |
| 151 | + <!-- Error display --> |
| 152 | + <div |
| 153 | + v-if="error" |
| 154 | + class="px-4 py-3 text-sm text-status-danger bg-status-danger-bg rounded-lg border border-status-danger-border" |
| 155 | + > |
| 156 | + {{ error }} |
| 157 | + </div> |
| 158 | + |
| 159 | + <!-- Results --> |
| 160 | + <div v-if="hasExecuted && !error && !loading"> |
| 161 | + <!-- Results header --> |
| 162 | + <div class="flex items-center justify-between mb-2"> |
| 163 | + <span class="text-xs text-text-secondary"> |
| 164 | + {{ rows.length }} row{{ rows.length !== 1 ? 's' : '' }} returned |
| 165 | + <template v-if="columns.length > 0"> |
| 166 | + · {{ columns.length }} column{{ columns.length !== 1 ? 's' : '' }} |
| 167 | + </template> |
| 168 | + </span> |
| 169 | + </div> |
| 170 | + |
| 171 | + <!-- Results table --> |
| 172 | + <div v-if="rows.length > 0" class="overflow-x-auto border border-surface-border rounded-lg"> |
| 173 | + <table class="w-full border-collapse"> |
| 174 | + <thead> |
| 175 | + <tr class="border-b border-surface-border bg-surface-sunken"> |
| 176 | + <th |
| 177 | + v-for="col in columns" |
| 178 | + :key="col.name" |
| 179 | + class="px-3 py-2 text-left text-xs font-semibold uppercase tracking-wider text-text-secondary whitespace-nowrap" |
| 180 | + > |
| 181 | + {{ col.name }} |
| 182 | + </th> |
| 183 | + </tr> |
| 184 | + </thead> |
| 185 | + <tbody> |
| 186 | + <tr |
| 187 | + v-for="(row, rowIdx) in paginatedRows" |
| 188 | + :key="rowIdx" |
| 189 | + class="border-b border-surface-border-subtle hover:bg-surface-raised transition-colors" |
| 190 | + > |
| 191 | + <td |
| 192 | + v-for="col in columns" |
| 193 | + :key="col.name" |
| 194 | + class="px-3 py-2 text-[13px] font-mono whitespace-nowrap max-w-xs truncate" |
| 195 | + :title="formatCellValue(row[col.name])" |
| 196 | + > |
| 197 | + {{ formatCellValue(row[col.name]) }} |
| 198 | + </td> |
| 199 | + </tr> |
| 200 | + </tbody> |
| 201 | + </table> |
| 202 | + |
| 203 | + <!-- Pagination --> |
| 204 | + <div |
| 205 | + v-if="totalPages > 1" |
| 206 | + class="flex items-center justify-between px-3 py-2 text-xs text-text-secondary border-t border-surface-border" |
| 207 | + > |
| 208 | + <span> |
| 209 | + {{ currentPage * PAGE_SIZE + 1 }}–{{ Math.min((currentPage + 1) * PAGE_SIZE, rows.length) }} |
| 210 | + of {{ rows.length }} |
| 211 | + </span> |
| 212 | + <div class="flex items-center gap-1"> |
| 213 | + <button |
| 214 | + :disabled="currentPage === 0" |
| 215 | + class="px-2 py-1 rounded hover:bg-surface-raised disabled:opacity-30 disabled:cursor-not-allowed" |
| 216 | + @click="goToPage(currentPage - 1)" |
| 217 | + > |
| 218 | + Prev |
| 219 | + </button> |
| 220 | + <span class="px-2 font-mono">{{ currentPage + 1 }} / {{ totalPages }}</span> |
| 221 | + <button |
| 222 | + :disabled="currentPage >= totalPages - 1" |
| 223 | + class="px-2 py-1 rounded hover:bg-surface-raised disabled:opacity-30 disabled:cursor-not-allowed" |
| 224 | + @click="goToPage(currentPage + 1)" |
| 225 | + > |
| 226 | + Next |
| 227 | + </button> |
| 228 | + </div> |
| 229 | + </div> |
| 230 | + </div> |
| 231 | + |
| 232 | + <!-- Empty results --> |
| 233 | + <div |
| 234 | + v-else |
| 235 | + class="flex items-center justify-center py-12 text-text-muted text-sm border border-surface-border rounded-lg" |
| 236 | + > |
| 237 | + Query returned no rows |
| 238 | + </div> |
| 239 | + </div> |
| 240 | + </div> |
| 241 | +</template> |
0 commit comments