feat(queries): 完成 M04 查询定义与版本激活

交付不可变查询版本、保存即生效与激活(D26),并带控制台列表筛选排序与试跑预览;补 RLS 下 current_version_no 回填迁移。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
unknownandCursor committed 2026-11-18 18:32:24 +08:00
1 parent 8c0335040f
commit 21cc721fcc
97 files changed
+13354 -153

No files matched your search

+400
View File
@@ -0,0 +1,400 @@
<script setup lang="ts">
/**
* Query list. Filters are drafts until 查询; sort and page hit the server.
* Row actions: 打开 / 切换版本 / 预览 (D26). Disabled rows stay visible; 预览 is disabled
* with a title. 预览 opens the detail scrolled to the trial-run panel.
*/
import { computed, onMounted, reactive, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import {
ElAlert,
ElButton,
ElDialog,
ElInput,
ElOption,
ElPagination,
ElSelect,
ElTable,
ElTableColumn,
ElTag,
vLoading,
} from "element-plus";
import { listDatasources } from "../datasources/client";
import type { Datasource } from "../datasources/types";
import { LIST_DEFAULT_PAGE_SIZE, LIST_PAGE_SIZES, actionsColumnWidth } from "../tableLayout";
import { listQueries, listVersions } from "./client";
import { describeError } from "./errors";
import { formatDateTime, versionLabel } from "./format";
import type { QuerySummary, VersionSummary } from "./types";
import VersionsTable from "./VersionsTable.vue";
const { t, te } = useI18n();
const router = useRouter();
const items = ref<QuerySummary[]>([]);
const total = ref(0);
const datasources = ref<Datasource[]>([]);
const loading = ref(true);
const loadError = ref("");
/** Draft filters edited in the toolbar; applied only on 查询 / 重置. */
const draft = reactive({ name: "", datasourceId: "", status: "" });
const applied = reactive({ name: "", datasourceId: "", status: "" });
const page = ref(1);
const pageSize = ref(LIST_DEFAULT_PAGE_SIZE);
const sortBy = ref("updated_at");
const sortOrder = ref<"asc" | "desc">("desc");
const datasourceNames = computed(() => new Map(datasources.value.map((item) => [item.id, item.name])));
const hasAppliedFilters = computed(
() =>
applied.name.trim() !== "" || applied.datasourceId !== "" || applied.status !== "",
);
const actionsWidth = actionsColumnWidth(3);
const switchOpen = ref(false);
const switchRow = ref<QuerySummary | null>(null);
const switchVersions = ref<VersionSummary[]>([]);
const switchLoading = ref(false);
const switchError = ref("");
async function loadOptions(): Promise<void> {
try {
// Option lists need a wide page; not the table page size.
const sources = await listDatasources({ page: 1, page_size: 100, sort_by: "name", sort_order: "asc" });
datasources.value = sources.items;
} catch {
datasources.value = [];
}
}
async function load(): Promise<void> {
loading.value = true;
loadError.value = "";
try {
const enabled =
applied.status === "enabled" ? true : applied.status === "disabled" ? false : undefined;
const result = await listQueries({
name: applied.name.trim() || undefined,
datasource_id: applied.datasourceId || undefined,
enabled,
sort_by: sortBy.value,
sort_order: sortOrder.value,
page: page.value,
page_size: pageSize.value,
});
items.value = result.items;
total.value = result.total;
} catch (error) {
loadError.value = describeError(error, t, te, "query.list.loadFailed");
items.value = [];
total.value = 0;
} finally {
loading.value = false;
}
}
function applyFilters(): void {
applied.name = draft.name;
applied.datasourceId = draft.datasourceId;
applied.status = draft.status;
page.value = 1;
void load();
}
function resetFilters(): void {
draft.name = "";
draft.datasourceId = "";
draft.status = "";
applied.name = "";
applied.datasourceId = "";
applied.status = "";
page.value = 1;
void load();
}
function onSortChange(payload: { prop: string | null; order: string | null }): void {
const propMap: Record<string, string> = {
name: "name",
datasource: "datasource_name",
status: "enabled",
currentVersion: "current_version_no",
updated_at: "updated_at",
};
const next = payload.prop ? propMap[payload.prop] : undefined;
if (!next || !payload.order) {
sortBy.value = "updated_at";
sortOrder.value = "desc";
} else {
sortBy.value = next;
sortOrder.value = payload.order === "ascending" ? "asc" : "desc";
}
page.value = 1;
void load();
}
function onPageChange(next: number): void {
page.value = next;
void load();
}
function onSizeChange(next: number): void {
pageSize.value = next;
page.value = 1;
void load();
}
function asRow(source: unknown): QuerySummary {
return source as QuerySummary;
}
function rowClass({ row }: { row: unknown }): string {
return asRow(row).enabled ? "" : "query-row-off";
}
function sourceName(source: unknown): string {
const row = asRow(source);
return datasourceNames.value.get(row.datasource_id) ?? row.datasource_id;
}
function currentVersion(source: unknown): string {
return versionLabel(asRow(source).current_version_no) ?? t("query.versions.none");
}
function openCreate(): void {
void router.push({ name: "query-create" });
}
function openDetail(source: unknown): void {
void router.push({ name: "query-detail", params: { id: asRow(source).id } });
}
function openPreview(source: unknown): void {
void router.push({ name: "query-detail", params: { id: asRow(source).id }, query: { preview: "1" } });
}
async function openSwitch(source: unknown): Promise<void> {
const row = asRow(source);
switchRow.value = row;
switchOpen.value = true;
switchLoading.value = true;
switchError.value = "";
switchVersions.value = [];
try {
switchVersions.value = (await listVersions(row.id)).items;
} catch (error) {
switchError.value = describeError(error, t, te, "query.versions.loadFailed");
} finally {
switchLoading.value = false;
}
}
async function onSwitchActivated(): Promise<void> {
switchOpen.value = false;
await load();
}
onMounted(async () => {
await loadOptions();
await load();
});
</script>
<template>
<div class="h-full overflow-auto p-5" data-testid="query-list">
<div class="mb-3 flex flex-wrap items-center gap-2" data-testid="query-list-toolbar">
<div class="w-[200px]" data-testid="query-filter-name">
<ElInput v-model="draft.name" clearable :placeholder="t('query.list.filterName')" />
</div>
<div class="w-[200px]" data-testid="query-filter-datasource">
<ElSelect
v-model="draft.datasourceId"
clearable
class="w-full"
:placeholder="t('query.list.filterDatasource')"
>
<ElOption
v-for="source in datasources"
:key="source.id"
:label="source.name"
:value="source.id"
/>
</ElSelect>
</div>
<div class="w-[140px]" data-testid="query-filter-status">
<ElSelect
v-model="draft.status"
clearable
class="w-full"
:placeholder="t('query.list.filterStatus')"
>
<ElOption :label="t('query.status.enabled')" value="enabled" />
<ElOption :label="t('query.status.disabled')" value="disabled" />
</ElSelect>
</div>
<ElButton type="primary" data-testid="query-filter-search" @click="applyFilters">
{{ t("query.list.filterSearch") }}
</ElButton>
<ElButton data-testid="query-filter-reset" @click="resetFilters">
{{ t("query.list.filterReset") }}
</ElButton>
<div class="ml-auto">
<ElButton type="primary" data-testid="query-create" @click="openCreate">
{{ t("query.list.create") }}
</ElButton>
</div>
</div>
<ElAlert
v-if="loadError"
type="error"
data-testid="query-load-error"
:title="loadError"
show-icon
:closable="false"
/>
<div
v-else-if="!loading && total === 0 && !hasAppliedFilters"
data-testid="query-empty"
class="rounded-lg border border-dashed border-[var(--border-default)] bg-white px-5 py-14 text-center"
>
<h3 class="mb-1.5 text-[15px] font-semibold">{{ t("query.list.empty") }}</h3>
<p class="text-[var(--text-secondary)]">{{ t("query.list.emptyHint") }}</p>
</div>
<div
v-else-if="!loading && total === 0 && hasAppliedFilters"
data-testid="query-filter-empty"
class="rounded-lg border border-dashed border-[var(--border-default)] bg-white px-5 py-14 text-center"
>
<h3 class="mb-1.5 text-[15px] font-semibold">{{ t("query.list.filterEmpty") }}</h3>
<p class="text-[var(--text-secondary)]">{{ t("query.list.filterEmptyHint") }}</p>
</div>
<template v-else>
<ElTable
v-loading="loading"
:data="items"
row-key="id"
data-testid="query-table"
:default-sort="{ prop: 'updated_at', order: 'descending' }"
:row-class-name="rowClass"
@sort-change="onSortChange"
>
<ElTableColumn
prop="name"
:label="t('query.columns.name')"
min-width="180"
sortable="custom"
/>
<ElTableColumn
prop="datasource"
:label="t('query.columns.datasource')"
min-width="160"
sortable="custom"
>
<template #default="{ row }">{{ sourceName(row) }}</template>
</ElTableColumn>
<ElTableColumn
prop="status"
:label="t('query.columns.status')"
min-width="110"
sortable="custom"
>
<template #default="{ row }">
<ElTag :type="row.enabled ? 'success' : 'info'" effect="light" data-testid="row-status">
{{ row.enabled ? t("query.status.enabled") : t("query.status.disabled") }}
</ElTag>
</template>
</ElTableColumn>
<ElTableColumn
prop="currentVersion"
:label="t('query.columns.currentVersion')"
min-width="120"
sortable="custom"
>
<template #default="{ row }">{{ currentVersion(row) }}</template>
</ElTableColumn>
<ElTableColumn
prop="updated_at"
:label="t('query.columns.updatedAt')"
min-width="150"
sortable="custom"
>
<template #default="{ row }">{{ formatDateTime(row.updated_at) }}</template>
</ElTableColumn>
<ElTableColumn
:label="t('query.columns.actions')"
:width="actionsWidth"
align="right"
fixed="right"
>
<template #default="{ row }">
<div class="whitespace-nowrap" :data-testid="`query-row-${row.id}`">
<ElButton link type="primary" data-testid="row-open" @click="openDetail(row)">
{{ t("query.actions.open") }}
</ElButton>
<ElButton link type="primary" data-testid="row-switch-version" @click="openSwitch(row)">
{{ t("query.actions.switchVersion") }}
</ElButton>
<ElButton
link
type="primary"
data-testid="row-preview"
:disabled="!row.enabled"
:title="row.enabled ? undefined : t('query.actions.previewDisabledWhy')"
@click="openPreview(row)"
>
{{ t("query.actions.preview") }}
</ElButton>
</div>
</template>
</ElTableColumn>
</ElTable>
<div class="mt-3 flex justify-end" data-testid="query-pagination">
<ElPagination
background
layout="total, sizes, prev, pager, next"
:total="total"
:page-size="pageSize"
:page-sizes="[...LIST_PAGE_SIZES]"
:current-page="page"
@current-change="onPageChange"
@size-change="onSizeChange"
/>
</div>
</template>
<ElDialog
v-model="switchOpen"
align-center
append-to-body
:close-on-click-modal="false"
:title="t('query.versions.switchTitle', { name: switchRow?.name ?? '' })"
width="680px"
data-testid="query-switch-dialog"
>
<ElAlert
v-if="switchError"
type="error"
:title="switchError"
show-icon
:closable="false"
/>
<div v-else v-loading="switchLoading">
<VersionsTable
v-if="switchRow"
:definition-id="switchRow.id"
:versions="switchVersions"
:active-no="switchRow.current_version_no ?? undefined"
@activated="onSwitchActivated"
/>
</div>
</ElDialog>
</div>
</template>
<style scoped>
:deep(.query-row-off td) {
color: var(--text-muted);
}
</style>