feat(grid): add suggested values to filtering criteria
This commit is contained in:
1 parent
f76cb4e46b
commit
ebae26ef34
16 files changed
+1349
-173
No files matched your search
+2
-6
@@ -3,7 +3,6 @@
|
||||
import { createApp, defineComponent, h, ref, computed } from "vue";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { useDataGridColumnFilters, type DataGridColumnFilterState, type DataGridLocalFilterDraft } from "@/composables/useDataGridColumnFilters";
|
||||
import type { DataGridLocalFilterOption } from "@/lib/dataGrid/dataGridLocalColumnFilterState";
|
||||
import type { CellValue } from "@/lib/dataGrid/cellValue";
|
||||
import type { QueryResult } from "@/types/database";
|
||||
|
||||
@@ -17,11 +16,6 @@ function mountColumnFilters(rows: CellValue[][]) {
|
||||
localFilterOpenColumn: ref<number | null>(null),
|
||||
localFilterSearch: ref(""),
|
||||
localFilterDraft: ref<DataGridLocalFilterDraft | null>(null),
|
||||
serverFilterLoading: ref(false),
|
||||
serverFilterError: ref(""),
|
||||
serverFilterOptions: ref<DataGridLocalFilterOption[]>([]),
|
||||
serverFilterLimited: ref(false),
|
||||
serverFilterValueByKey: ref(new Map()),
|
||||
serverColumnFilters: ref({}),
|
||||
};
|
||||
const result: QueryResult = { columns: ["status"], rows };
|
||||
@@ -36,6 +30,7 @@ function mountColumnFilters(rows: CellValue[][]) {
|
||||
getConnectionId: () => undefined,
|
||||
getSchema: () => undefined,
|
||||
getExecutionDatabase: () => "",
|
||||
scopeIdentity: computed(() => "test"),
|
||||
resolvedDatabaseType: computed(() => undefined),
|
||||
canUseWhereSearch: computed(() => false),
|
||||
canUseServerColumnFilter: computed(() => false),
|
||||
@@ -44,6 +39,7 @@ function mountColumnFilters(rows: CellValue[][]) {
|
||||
whereFilterInput: ref(""),
|
||||
getConnectionConfig: () => undefined,
|
||||
getIdentifierQuote: () => undefined,
|
||||
getGlobalQueryTimeoutSecs: () => undefined,
|
||||
getNewRows: () => [],
|
||||
getRowData: (row) => row,
|
||||
formatValue: (value) => String(value),
|
||||
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { computed, createApp, defineComponent, h, nextTick, ref } from "vue";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import * as api from "@/lib/backend/api";
|
||||
import { buildDataGridColumnDistinctValuesSql } from "@/lib/dataGrid/dataGridSql";
|
||||
import { DATA_GRID_DISTINCT_VALUE_DEFAULT_LIMIT, useDataGridDistinctValueLoader } from "@/composables/useDataGridDistinctValueLoader";
|
||||
import type { QueryResult } from "@/types/database";
|
||||
|
||||
vi.mock("@/lib/backend/api", () => ({ executeQuery: vi.fn() }));
|
||||
vi.mock("@/lib/dataGrid/dataGridSql", () => ({ buildDataGridColumnDistinctValuesSql: vi.fn(async () => "SELECT DISTINCT VALUES") }));
|
||||
|
||||
const disposers: Array<() => void> = [];
|
||||
|
||||
function mountLoader() {
|
||||
const identity = ref("table-1");
|
||||
const connectionId = ref<string | undefined>("connection-1");
|
||||
let loader: ReturnType<typeof useDataGridDistinctValueLoader> | undefined;
|
||||
const app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
loader = useDataGridDistinctValueLoader({
|
||||
scopeIdentity: computed(() => identity.value),
|
||||
getConnectionId: () => connectionId.value,
|
||||
getExecutionDatabase: () => "app",
|
||||
getSchema: () => "public",
|
||||
getDatabaseType: () => "postgres",
|
||||
getConnectionConfig: () => ({
|
||||
driver_profile: "native",
|
||||
query_timeout_secs: 12,
|
||||
query_timeout_inherit: false,
|
||||
}),
|
||||
getIdentifierQuote: () => undefined,
|
||||
getGlobalQueryTimeoutSecs: () => 60,
|
||||
waitForTableMeta: async () => ({
|
||||
schema: "public",
|
||||
tableName: "users",
|
||||
columns: [{ name: "status", data_type: "varchar", is_nullable: true }],
|
||||
}),
|
||||
formatValue: (value) => String(value),
|
||||
});
|
||||
return () => h("div");
|
||||
},
|
||||
}),
|
||||
);
|
||||
app.mount(document.createElement("div"));
|
||||
disposers.push(() => app.unmount());
|
||||
if (!loader) throw new Error("loader was not mounted");
|
||||
return { loader, identity, connectionId };
|
||||
}
|
||||
|
||||
function result(rows: QueryResult["rows"]): QueryResult {
|
||||
return { columns: ["dbx_value", "dbx_count"], rows };
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const dispose of disposers.splice(0)) dispose();
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe("useDataGridDistinctValueLoader", () => {
|
||||
it("keeps the default candidate limit at 1000", () => {
|
||||
expect(DATA_GRID_DISTINCT_VALUE_DEFAULT_LIMIT).toBe(1000);
|
||||
});
|
||||
|
||||
it("marks results that reach the default 1000-candidate limit", async () => {
|
||||
vi.mocked(api.executeQuery).mockResolvedValue(result(Array.from({ length: 1000 }, (_, index) => [`value-${index}`, 1])));
|
||||
const { loader } = mountLoader();
|
||||
|
||||
await loader.load({ columnIndex: 0, columnName: "status" });
|
||||
|
||||
expect(loader.options.value).toHaveLength(1000);
|
||||
expect(loader.limited.value).toBe(true);
|
||||
});
|
||||
|
||||
it("uses the requested limit, keeps NULL, caches independent results, and resets on table changes", async () => {
|
||||
const rows: QueryResult["rows"] = Array.from({ length: 101 }, (_, index) => [`value-${index}`, 101 - index]);
|
||||
rows.splice(4, 0, [null, 9]);
|
||||
vi.mocked(api.executeQuery).mockResolvedValue(result(rows));
|
||||
const { loader, identity } = mountLoader();
|
||||
|
||||
await loader.load({ columnIndex: 0, columnName: "status", limit: 100, includeCounts: true });
|
||||
|
||||
expect(buildDataGridColumnDistinctValuesSql).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
columnName: "status",
|
||||
limit: 100,
|
||||
includeCounts: true,
|
||||
excludeNulls: false,
|
||||
}),
|
||||
);
|
||||
expect(vi.mocked(buildDataGridColumnDistinctValuesSql).mock.calls[0]?.[0]).not.toHaveProperty("whereInput");
|
||||
expect(api.executeQuery).toHaveBeenCalledWith(
|
||||
"connection-1",
|
||||
"app",
|
||||
"SELECT DISTINCT VALUES",
|
||||
"public",
|
||||
undefined,
|
||||
expect.objectContaining({
|
||||
maxRows: 100,
|
||||
fetchSize: 100,
|
||||
pageSize: 100,
|
||||
timeoutSecs: 12,
|
||||
}),
|
||||
);
|
||||
expect(loader.options.value).toHaveLength(100);
|
||||
expect(loader.options.value.some((option) => option.value === null)).toBe(true);
|
||||
expect(loader.limited.value).toBe(true);
|
||||
|
||||
await loader.load({ columnIndex: 0, columnName: "status", limit: 100, includeCounts: true });
|
||||
expect(api.executeQuery).toHaveBeenCalledTimes(1);
|
||||
|
||||
identity.value = "table-2";
|
||||
await nextTick();
|
||||
expect(loader.options.value).toEqual([]);
|
||||
await loader.load({ columnIndex: 0, columnName: "status", limit: 100, includeCounts: true });
|
||||
expect(api.executeQuery).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("debounces searches and ignores a superseded response", async () => {
|
||||
let resolveFirst: ((value: QueryResult) => void) | undefined;
|
||||
vi.mocked(api.executeQuery)
|
||||
.mockImplementationOnce(() => new Promise((resolve) => (resolveFirst = resolve)))
|
||||
.mockResolvedValueOnce(result([["new", 2]]));
|
||||
const { loader } = mountLoader();
|
||||
|
||||
const first = loader.load({ columnIndex: 0, columnName: "status", searchValue: "old" });
|
||||
await vi.waitFor(() => expect(api.executeQuery).toHaveBeenCalledTimes(1));
|
||||
vi.useFakeTimers();
|
||||
loader.schedule({ columnIndex: 0, columnName: "status", searchValue: "new" });
|
||||
await vi.advanceTimersByTimeAsync(299);
|
||||
expect(api.executeQuery).toHaveBeenCalledTimes(1);
|
||||
await vi.advanceTimersByTimeAsync(1);
|
||||
await vi.advanceTimersByTimeAsync(0);
|
||||
expect(api.executeQuery).toHaveBeenCalledTimes(2);
|
||||
expect(loader.options.value.map((option) => option.value)).toEqual(["new"]);
|
||||
|
||||
resolveFirst?.(result([["old", 9]]));
|
||||
await first;
|
||||
expect(loader.options.value.map((option) => option.value)).toEqual(["new"]);
|
||||
});
|
||||
|
||||
it("clears the loading state when the connection disappears during a debounced search", async () => {
|
||||
vi.useFakeTimers();
|
||||
const { loader, connectionId } = mountLoader();
|
||||
connectionId.value = undefined;
|
||||
|
||||
loader.schedule({ columnIndex: 0, columnName: "status", searchValue: "open" });
|
||||
expect(loader.loading.value).toBe(true);
|
||||
await vi.advanceTimersByTimeAsync(300);
|
||||
|
||||
expect(loader.loading.value).toBe(false);
|
||||
expect(api.executeQuery).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("expires cached results after 30 seconds", async () => {
|
||||
let now = 1_000;
|
||||
const nowSpy = vi.spyOn(Date, "now").mockImplementation(() => now);
|
||||
vi.mocked(api.executeQuery).mockResolvedValue(result([["open", 1]]));
|
||||
const { loader } = mountLoader();
|
||||
const request = { columnIndex: 0, columnName: "status", searchValue: "open" };
|
||||
|
||||
await loader.load(request);
|
||||
now += 29_999;
|
||||
await loader.load(request);
|
||||
expect(api.executeQuery).toHaveBeenCalledTimes(1);
|
||||
|
||||
now += 2;
|
||||
await loader.load(request);
|
||||
expect(api.executeQuery).toHaveBeenCalledTimes(2);
|
||||
nowSpy.mockRestore();
|
||||
});
|
||||
|
||||
it("surfaces errors without retaining stale options and can recover", async () => {
|
||||
vi.mocked(api.executeQuery)
|
||||
.mockResolvedValueOnce(result([["ready", 1]]))
|
||||
.mockRejectedValueOnce(new Error("query failed"))
|
||||
.mockResolvedValueOnce(result([["recovered", 1]]));
|
||||
const { loader } = mountLoader();
|
||||
await loader.load({ columnIndex: 0, columnName: "status" });
|
||||
await loader.load({ columnIndex: 0, columnName: "status", searchValue: "missing" });
|
||||
expect(loader.options.value).toEqual([]);
|
||||
expect(loader.error.value).toBe("query failed");
|
||||
expect(loader.loading.value).toBe(false);
|
||||
await loader.load({ columnIndex: 0, columnName: "status", searchValue: "again" });
|
||||
expect(loader.error.value).toBe("");
|
||||
expect(loader.options.value.map((option) => option.value)).toEqual(["recovered"]);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +1,10 @@
|
||||
import { computed, onUnmounted, ref, watch, type ComputedRef, type Ref } from "vue";
|
||||
import * as api from "@/lib/backend/api";
|
||||
import { computed, ref, watch, type ComputedRef, type Ref } from "vue";
|
||||
import { buildColumnValueFilterCondition, buildColumnValuesFilterCondition, parseFilterValue, removeColumnValueFilterCondition, replaceColumnValueFilterCondition, appendColumnValueFilterCondition } from "@/lib/dataGrid/dataGridColumnFilter";
|
||||
import { buildDataGridColumnDistinctValuesSql } from "@/lib/dataGrid/dataGridSql";
|
||||
import { buildDataGridLocalFilterOptions, sortDataGridLocalFilterOptions, type DataGridLocalFilterSort, dataGridLocalFilterKey, dataGridLocalFilterLabel, rowMatchesDataGridLocalColumnFilters, type DataGridLocalFilterOption } from "@/lib/dataGrid/dataGridLocalColumnFilterState";
|
||||
import type { DataGridCachedServerColumnFilter } from "@/lib/dataGrid/dataGridFilterBuilderPersistence";
|
||||
import type { CellValue } from "@/lib/dataGrid/cellValue";
|
||||
import type { ColumnInfo, DatabaseType, QueryResult } from "@/types/database";
|
||||
import { DATA_GRID_DISTINCT_VALUE_DEFAULT_LIMIT, useDataGridDistinctValueLoader } from "@/composables/useDataGridDistinctValueLoader";
|
||||
import type { ColumnInfo, ConnectionConfig, DatabaseType, QueryResult } from "@/types/database";
|
||||
|
||||
export type DataGridLocalFilterMode = "local" | "server";
|
||||
|
||||
@@ -21,11 +20,6 @@ export interface DataGridColumnFilterState {
|
||||
localFilterOpenColumn: Ref<number | null>;
|
||||
localFilterSearch: Ref<string>;
|
||||
localFilterDraft: Ref<DataGridLocalFilterDraft | null>;
|
||||
serverFilterLoading: Ref<boolean>;
|
||||
serverFilterError: Ref<string>;
|
||||
serverFilterOptions: Ref<DataGridLocalFilterOption[]>;
|
||||
serverFilterLimited: Ref<boolean>;
|
||||
serverFilterValueByKey: Ref<Map<string, CellValue>>;
|
||||
serverColumnFilters: Ref<Record<number, DataGridCachedServerColumnFilter>>;
|
||||
}
|
||||
|
||||
@@ -37,10 +31,6 @@ interface TableMetadata {
|
||||
columns: ColumnInfo[];
|
||||
}
|
||||
|
||||
interface ConnectionConfig {
|
||||
driver_profile?: string;
|
||||
}
|
||||
|
||||
export interface UseDataGridColumnFiltersOptions {
|
||||
state: DataGridColumnFilterState;
|
||||
getResult: () => QueryResult;
|
||||
@@ -48,14 +38,16 @@ export interface UseDataGridColumnFiltersOptions {
|
||||
getConnectionId: () => string | undefined;
|
||||
getSchema: () => string | undefined;
|
||||
getExecutionDatabase: () => string;
|
||||
scopeIdentity: ComputedRef<string>;
|
||||
resolvedDatabaseType: ComputedRef<DatabaseType | undefined>;
|
||||
canUseWhereSearch: ComputedRef<boolean>;
|
||||
canUseServerColumnFilter: ComputedRef<boolean>;
|
||||
structuredFilterCount: ComputedRef<number>;
|
||||
hasStructuredFilters: ComputedRef<boolean>;
|
||||
whereFilterInput: Ref<string>;
|
||||
getConnectionConfig: () => ConnectionConfig | undefined;
|
||||
getConnectionConfig: () => Pick<ConnectionConfig, "driver_profile" | "query_timeout_secs" | "query_timeout_inherit"> | undefined;
|
||||
getIdentifierQuote: () => string | undefined;
|
||||
getGlobalQueryTimeoutSecs: () => number | undefined;
|
||||
getNewRows: () => readonly (readonly CellValue[])[];
|
||||
getRowData: (row: CellValue[], sourceIndex: number) => readonly CellValue[];
|
||||
formatValue: (value: CellValue, columnIndex: number) => string;
|
||||
@@ -67,12 +59,28 @@ export interface UseDataGridColumnFiltersOptions {
|
||||
emitLocalFiltersChange: (filters: Record<string, string[]>) => void;
|
||||
}
|
||||
|
||||
export const DATA_GRID_SERVER_COLUMN_FILTER_LIMIT = 1000;
|
||||
const SERVER_COLUMN_FILTER_DEBOUNCE_MS = 300;
|
||||
export const DATA_GRID_SERVER_COLUMN_FILTER_LIMIT = DATA_GRID_DISTINCT_VALUE_DEFAULT_LIMIT;
|
||||
|
||||
export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOptions) {
|
||||
const { state } = options;
|
||||
const localFilterSort = ref<DataGridLocalFilterSort>({ field: "value", direction: "asc" });
|
||||
const distinctValueLoader = useDataGridDistinctValueLoader({
|
||||
scopeIdentity: options.scopeIdentity,
|
||||
getConnectionId: options.getConnectionId,
|
||||
getExecutionDatabase: options.getExecutionDatabase,
|
||||
getSchema: options.getSchema,
|
||||
getDatabaseType: () => options.resolvedDatabaseType.value,
|
||||
getConnectionConfig: options.getConnectionConfig,
|
||||
getIdentifierQuote: options.getIdentifierQuote,
|
||||
getGlobalQueryTimeoutSecs: options.getGlobalQueryTimeoutSecs,
|
||||
waitForTableMeta: options.waitForTableMeta,
|
||||
formatValue: options.formatValue,
|
||||
});
|
||||
const serverFilterLoading = distinctValueLoader.loading;
|
||||
const serverFilterError = distinctValueLoader.error;
|
||||
const serverFilterOptions = distinctValueLoader.options;
|
||||
const serverFilterLimited = distinctValueLoader.limited;
|
||||
const serverFilterValueByKey = distinctValueLoader.valueByKey;
|
||||
|
||||
function toggleLocalFilterSort(field: DataGridLocalFilterSort["field"]) {
|
||||
if (state.localFilterDraft.value?.mode !== "local") return;
|
||||
@@ -83,9 +91,6 @@ export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOption
|
||||
};
|
||||
}
|
||||
|
||||
let serverFilterRequestId = 0;
|
||||
let serverFilterSearchTimer: ReturnType<typeof window.setTimeout> | undefined;
|
||||
|
||||
watch(
|
||||
state.localColumnFilters,
|
||||
(filters) => {
|
||||
@@ -142,12 +147,12 @@ export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOption
|
||||
const rowMatchesLocalColumnFilters = (data: CellValue[]) => rowMatchesDataGridLocalColumnFilters(data, state.localColumnFilters.value);
|
||||
|
||||
const localFilterAllOptions = computed(() => {
|
||||
if (state.localFilterDraft.value?.mode === "server") return state.serverFilterOptions.value;
|
||||
if (state.localFilterDraft.value?.mode === "server") return serverFilterOptions.value;
|
||||
const columnIndex = state.localFilterDraft.value?.columnIndex;
|
||||
return columnIndex === undefined ? [] : buildLocalFilterOptions(columnIndex);
|
||||
});
|
||||
const localFilterOptions = computed(() => {
|
||||
if (state.localFilterDraft.value?.mode === "server") return state.serverFilterOptions.value;
|
||||
if (state.localFilterDraft.value?.mode === "server") return serverFilterOptions.value;
|
||||
const query = state.localFilterSearch.value.trim().toLowerCase();
|
||||
const matchingOptions = localFilterAllOptions.value.filter((option) => !query || option.label.toLowerCase().includes(query));
|
||||
// Sort before limiting so frequent values outside the default first 500 remain discoverable.
|
||||
@@ -174,19 +179,6 @@ export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOption
|
||||
return !localFilterAllOptions.value.some((option) => option.label.toLowerCase() === normalized);
|
||||
});
|
||||
|
||||
function resetServerFilterState() {
|
||||
serverFilterRequestId++;
|
||||
if (serverFilterSearchTimer !== undefined) {
|
||||
window.clearTimeout(serverFilterSearchTimer);
|
||||
serverFilterSearchTimer = undefined;
|
||||
}
|
||||
state.serverFilterLoading.value = false;
|
||||
state.serverFilterError.value = "";
|
||||
state.serverFilterOptions.value = [];
|
||||
state.serverFilterLimited.value = false;
|
||||
state.serverFilterValueByKey.value = new Map();
|
||||
}
|
||||
|
||||
function openLocalFilter(columnIndex: number, requestedMode: DataGridLocalFilterMode = "local") {
|
||||
options.onOpen?.();
|
||||
localFilterSort.value = { field: "value", direction: "asc" };
|
||||
@@ -200,7 +192,7 @@ export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOption
|
||||
touched: false,
|
||||
};
|
||||
state.localFilterOpenColumn.value = columnIndex;
|
||||
resetServerFilterState();
|
||||
distinctValueLoader.reset();
|
||||
if (mode === "server") void loadServerFilterValues(columnIndex, "");
|
||||
}
|
||||
|
||||
@@ -209,24 +201,7 @@ export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOption
|
||||
state.localFilterOpenColumn.value = null;
|
||||
state.localFilterDraft.value = null;
|
||||
state.localFilterSearch.value = "";
|
||||
resetServerFilterState();
|
||||
}
|
||||
|
||||
function serverFilterOptionFromRow(row: QueryResult["rows"][number], columnIndex: number): DataGridLocalFilterOption {
|
||||
const value = (row[0] ?? null) as CellValue;
|
||||
const countValue = Number(row[1]);
|
||||
return { key: dataGridLocalFilterKey(value), label: localFilterLabel(value, columnIndex), count: Number.isFinite(countValue) ? countValue : null, value };
|
||||
}
|
||||
|
||||
function serverFilterOptionsFromResult(result: QueryResult, columnIndex: number): DataGridLocalFilterOption[] {
|
||||
const byKey = new Map<string, DataGridLocalFilterOption>();
|
||||
for (const row of result.rows) {
|
||||
const option = serverFilterOptionFromRow(row, columnIndex);
|
||||
const current = byKey.get(option.key);
|
||||
if (current) current.count = (current.count ?? 0) + (option.count ?? 0);
|
||||
else byKey.set(option.key, option);
|
||||
}
|
||||
return [...byKey.values()];
|
||||
distinctValueLoader.reset();
|
||||
}
|
||||
|
||||
function syncServerFilterDraft(columnIndex: number, filterOptions: DataGridLocalFilterOption[]) {
|
||||
@@ -236,64 +211,39 @@ export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOption
|
||||
state.localFilterDraft.value = { ...draft, values: new Set(activeFilter?.keys ?? filterOptions.map((option) => option.key)) };
|
||||
}
|
||||
|
||||
watch(serverFilterOptions, (filterOptions) => {
|
||||
const draft = state.localFilterDraft.value;
|
||||
if (draft?.mode === "server" && state.localFilterOpenColumn.value === draft.columnIndex) syncServerFilterDraft(draft.columnIndex, filterOptions);
|
||||
});
|
||||
|
||||
async function loadServerFilterValues(columnIndex: number, searchValue: string) {
|
||||
const connectionId = options.getConnectionId();
|
||||
if (!options.canUseServerColumnFilter.value || !connectionId) return;
|
||||
if (!options.canUseServerColumnFilter.value || !options.getConnectionId()) return;
|
||||
const columnName = options.getResult().columns[columnIndex];
|
||||
if (!columnName) return;
|
||||
const requestId = ++serverFilterRequestId;
|
||||
state.serverFilterLoading.value = true;
|
||||
state.serverFilterError.value = "";
|
||||
state.serverFilterLimited.value = false;
|
||||
try {
|
||||
const tableMeta = await options.waitForTableMeta();
|
||||
if (!tableMeta) return;
|
||||
const columnInfo = tableMeta.columns.find((column) => column.name === columnName);
|
||||
const sql = await buildDataGridColumnDistinctValuesSql({
|
||||
databaseType: options.resolvedDatabaseType.value,
|
||||
driverProfile: options.getConnectionConfig()?.driver_profile,
|
||||
identifierQuote: options.getIdentifierQuote(),
|
||||
catalog: tableMeta.catalog,
|
||||
database: tableMeta.database,
|
||||
schema: tableMeta.schema,
|
||||
tableName: tableMeta.tableName,
|
||||
columnName,
|
||||
columnInfo,
|
||||
searchValue: searchValue.trim() || undefined,
|
||||
limit: DATA_GRID_SERVER_COLUMN_FILTER_LIMIT,
|
||||
includeCounts: true,
|
||||
});
|
||||
const result = await api.executeQuery(connectionId, options.getExecutionDatabase(), sql, tableMeta.schema ?? options.getSchema(), undefined, {
|
||||
maxRows: DATA_GRID_SERVER_COLUMN_FILTER_LIMIT,
|
||||
fetchSize: DATA_GRID_SERVER_COLUMN_FILTER_LIMIT,
|
||||
pageSize: DATA_GRID_SERVER_COLUMN_FILTER_LIMIT,
|
||||
});
|
||||
if (requestId !== serverFilterRequestId || state.localFilterOpenColumn.value !== columnIndex) return;
|
||||
const filterOptions = serverFilterOptionsFromResult(result, columnIndex);
|
||||
const nextValueByKey = new Map(state.serverFilterValueByKey.value);
|
||||
for (const option of filterOptions) nextValueByKey.set(option.key, option.value);
|
||||
state.serverFilterValueByKey.value = nextValueByKey;
|
||||
state.serverFilterOptions.value = filterOptions;
|
||||
state.serverFilterLimited.value = result.truncated === true || result.rows.length >= DATA_GRID_SERVER_COLUMN_FILTER_LIMIT;
|
||||
syncServerFilterDraft(columnIndex, filterOptions);
|
||||
} catch (error: any) {
|
||||
if (requestId !== serverFilterRequestId) return;
|
||||
state.serverFilterOptions.value = [];
|
||||
state.serverFilterError.value = String(error?.message || error);
|
||||
} finally {
|
||||
if (requestId === serverFilterRequestId) state.serverFilterLoading.value = false;
|
||||
}
|
||||
await distinctValueLoader.load({
|
||||
columnIndex,
|
||||
columnName,
|
||||
searchValue,
|
||||
limit: DATA_GRID_SERVER_COLUMN_FILTER_LIMIT,
|
||||
includeCounts: true,
|
||||
});
|
||||
if (state.localFilterOpenColumn.value === columnIndex) syncServerFilterDraft(columnIndex, serverFilterOptions.value);
|
||||
}
|
||||
|
||||
watch(state.localFilterSearch, (value) => {
|
||||
const draft = state.localFilterDraft.value;
|
||||
if (!draft || draft.mode !== "server" || state.localFilterOpenColumn.value !== draft.columnIndex) return;
|
||||
if (serverFilterSearchTimer !== undefined) window.clearTimeout(serverFilterSearchTimer);
|
||||
serverFilterSearchTimer = window.setTimeout(() => void loadServerFilterValues(draft.columnIndex, value), SERVER_COLUMN_FILTER_DEBOUNCE_MS);
|
||||
const columnName = options.getResult().columns[draft.columnIndex];
|
||||
if (!columnName) return;
|
||||
distinctValueLoader.schedule({
|
||||
columnIndex: draft.columnIndex,
|
||||
columnName,
|
||||
searchValue: value,
|
||||
limit: DATA_GRID_SERVER_COLUMN_FILTER_LIMIT,
|
||||
includeCounts: true,
|
||||
});
|
||||
});
|
||||
|
||||
onUnmounted(resetServerFilterState);
|
||||
|
||||
function toggleLocalFilterValue(key: string) {
|
||||
const draft = state.localFilterDraft.value;
|
||||
if (!draft) return;
|
||||
@@ -339,13 +289,13 @@ export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOption
|
||||
closeLocalFilter();
|
||||
return;
|
||||
}
|
||||
if (canApplyTypedLocalFilterValue.value && state.serverFilterOptions.value.length === 0) {
|
||||
if (canApplyTypedLocalFilterValue.value && serverFilterOptions.value.length === 0) {
|
||||
await applyTypedLocalFilterValue();
|
||||
return;
|
||||
}
|
||||
const columnName = options.getResult().columns[draft.columnIndex];
|
||||
if (!columnName) return;
|
||||
const values = [...draft.values].flatMap((key) => (state.serverFilterValueByKey.value.has(key) ? [state.serverFilterValueByKey.value.get(key)!] : []));
|
||||
const values = [...draft.values].flatMap((key) => (serverFilterValueByKey.value.has(key) ? [serverFilterValueByKey.value.get(key)!] : []));
|
||||
if (values.length === 0) {
|
||||
closeLocalFilter();
|
||||
return;
|
||||
@@ -431,6 +381,10 @@ export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOption
|
||||
if (removedServerFilter && applyServerWhereFilter && options.canUseWhereSearch.value) void options.applyWhereFilter();
|
||||
}
|
||||
|
||||
function resetDistinctValueCache() {
|
||||
distinctValueLoader.reset({ clearCache: true });
|
||||
}
|
||||
|
||||
return {
|
||||
localFilterActive,
|
||||
localFilterCount,
|
||||
@@ -448,6 +402,11 @@ export function useDataGridColumnFilters(options: UseDataGridColumnFiltersOption
|
||||
toggleLocalFilterSort,
|
||||
localFilterTypedValue,
|
||||
canApplyTypedLocalFilterValue,
|
||||
serverFilterLoading,
|
||||
serverFilterError,
|
||||
serverFilterOptions,
|
||||
serverFilterLimited,
|
||||
resetDistinctValueCache,
|
||||
openLocalFilter,
|
||||
closeLocalFilter,
|
||||
toggleLocalFilterValue,
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
import { onUnmounted, ref, toValue, watch, type MaybeRefOrGetter, type Ref } from "vue";
|
||||
import * as api from "@/lib/backend/api";
|
||||
import { buildDataGridColumnDistinctValuesSql } from "@/lib/dataGrid/dataGridSql";
|
||||
import { dataGridLocalFilterKey, type DataGridLocalFilterOption } from "@/lib/dataGrid/dataGridLocalColumnFilterState";
|
||||
import { queryTimeoutSecsForConnection } from "@/lib/sql/queryTimeout";
|
||||
import type { CellValue } from "@/lib/dataGrid/cellValue";
|
||||
import type { ColumnInfo, ConnectionConfig, DatabaseType, QueryResult } from "@/types/database";
|
||||
|
||||
export const DATA_GRID_DISTINCT_VALUE_DEFAULT_LIMIT = 1000;
|
||||
export const DATA_GRID_DISTINCT_VALUE_MAX_LIMIT = 1000;
|
||||
export const DATA_GRID_DISTINCT_VALUE_DEBOUNCE_MS = 300;
|
||||
const DATA_GRID_DISTINCT_VALUE_CACHE_TTL_MS = 30_000;
|
||||
|
||||
interface DataGridDistinctValueTableMeta {
|
||||
catalog?: string;
|
||||
database?: string;
|
||||
schema?: string;
|
||||
tableName: string;
|
||||
columns: ColumnInfo[];
|
||||
}
|
||||
|
||||
export interface DataGridDistinctValueLoadRequest {
|
||||
columnIndex: number;
|
||||
columnName: string;
|
||||
searchValue?: string;
|
||||
limit?: number;
|
||||
includeCounts?: boolean;
|
||||
}
|
||||
|
||||
export interface UseDataGridDistinctValueLoaderOptions {
|
||||
scopeIdentity: MaybeRefOrGetter<string>;
|
||||
getConnectionId: () => string | undefined;
|
||||
getExecutionDatabase: () => string;
|
||||
getSchema: () => string | undefined;
|
||||
getDatabaseType: () => DatabaseType | undefined;
|
||||
getConnectionConfig: () => Pick<ConnectionConfig, "driver_profile" | "query_timeout_secs" | "query_timeout_inherit"> | undefined;
|
||||
getIdentifierQuote: () => string | undefined;
|
||||
getGlobalQueryTimeoutSecs: () => number | undefined;
|
||||
waitForTableMeta: () => Promise<DataGridDistinctValueTableMeta | null>;
|
||||
formatValue: (value: CellValue, columnIndex: number) => string;
|
||||
keyForValue?: (value: CellValue, columnInfo?: ColumnInfo) => string;
|
||||
}
|
||||
|
||||
type CachedDistinctValues = {
|
||||
expiresAt: number;
|
||||
options: DataGridLocalFilterOption[];
|
||||
limited: boolean;
|
||||
};
|
||||
|
||||
export interface DataGridDistinctValueLoader {
|
||||
loading: Ref<boolean>;
|
||||
error: Ref<string>;
|
||||
options: Ref<DataGridLocalFilterOption[]>;
|
||||
limited: Ref<boolean>;
|
||||
valueByKey: Ref<Map<string, CellValue>>;
|
||||
load: (request: DataGridDistinctValueLoadRequest) => Promise<void>;
|
||||
schedule: (request: DataGridDistinctValueLoadRequest, debounceMs?: number) => void;
|
||||
reset: (options?: { clearCache?: boolean }) => void;
|
||||
}
|
||||
|
||||
export function useDataGridDistinctValueLoader(options: UseDataGridDistinctValueLoaderOptions): DataGridDistinctValueLoader {
|
||||
const loading = ref(false);
|
||||
const error = ref("");
|
||||
const distinctOptions = ref<DataGridLocalFilterOption[]>([]);
|
||||
const limited = ref(false);
|
||||
const valueByKey = ref<Map<string, CellValue>>(new Map());
|
||||
const cache = new Map<string, CachedDistinctValues>();
|
||||
let requestId = 0;
|
||||
let timer: ReturnType<typeof window.setTimeout> | undefined;
|
||||
|
||||
function normalizedLimit(limit?: number) {
|
||||
return Math.min(DATA_GRID_DISTINCT_VALUE_MAX_LIMIT, Math.max(1, Math.trunc(limit ?? DATA_GRID_DISTINCT_VALUE_DEFAULT_LIMIT)));
|
||||
}
|
||||
|
||||
function cancelPending() {
|
||||
requestId += 1;
|
||||
if (timer !== undefined) window.clearTimeout(timer);
|
||||
timer = undefined;
|
||||
loading.value = false;
|
||||
}
|
||||
|
||||
function reset(resetOptions: { clearCache?: boolean } = {}) {
|
||||
cancelPending();
|
||||
error.value = "";
|
||||
distinctOptions.value = [];
|
||||
limited.value = false;
|
||||
valueByKey.value = new Map();
|
||||
if (resetOptions.clearCache) cache.clear();
|
||||
}
|
||||
|
||||
function optionFromRow(row: QueryResult["rows"][number], columnIndex: number, columnInfo?: ColumnInfo): DataGridLocalFilterOption {
|
||||
const value = (row[0] ?? null) as CellValue;
|
||||
const countValue = Number(row[1]);
|
||||
return {
|
||||
key: options.keyForValue?.(value, columnInfo) ?? dataGridLocalFilterKey(value),
|
||||
label: options.formatValue(value, columnIndex),
|
||||
count: Number.isFinite(countValue) ? countValue : null,
|
||||
value,
|
||||
};
|
||||
}
|
||||
|
||||
function optionsFromResult(result: QueryResult, columnIndex: number, columnInfo: ColumnInfo | undefined, displayLimit: number): DataGridLocalFilterOption[] {
|
||||
const byKey = new Map<string, DataGridLocalFilterOption>();
|
||||
for (const row of result.rows) {
|
||||
const option = optionFromRow(row, columnIndex, columnInfo);
|
||||
const current = byKey.get(option.key);
|
||||
if (current) current.count = (current.count ?? 0) + (option.count ?? 0);
|
||||
else byKey.set(option.key, option);
|
||||
if (byKey.size > displayLimit) break;
|
||||
}
|
||||
return [...byKey.values()].slice(0, displayLimit);
|
||||
}
|
||||
|
||||
function applyValues(nextOptions: DataGridLocalFilterOption[], nextLimited: boolean) {
|
||||
const nextValueByKey = new Map(valueByKey.value);
|
||||
for (const option of nextOptions) nextValueByKey.set(option.key, option.value);
|
||||
valueByKey.value = nextValueByKey;
|
||||
distinctOptions.value = nextOptions;
|
||||
limited.value = nextLimited;
|
||||
}
|
||||
|
||||
function cacheKey(request: DataGridDistinctValueLoadRequest, tableMeta: DataGridDistinctValueTableMeta, displayLimit: number) {
|
||||
return JSON.stringify([
|
||||
toValue(options.scopeIdentity),
|
||||
options.getConnectionId() ?? "",
|
||||
options.getExecutionDatabase(),
|
||||
tableMeta.catalog ?? "",
|
||||
tableMeta.database ?? "",
|
||||
tableMeta.schema ?? "",
|
||||
tableMeta.tableName,
|
||||
request.columnName,
|
||||
request.searchValue?.trim() ?? "",
|
||||
displayLimit,
|
||||
request.includeCounts !== false,
|
||||
]);
|
||||
}
|
||||
|
||||
async function performLoad(request: DataGridDistinctValueLoadRequest, activeRequestId: number) {
|
||||
loading.value = true;
|
||||
error.value = "";
|
||||
limited.value = false;
|
||||
try {
|
||||
const connectionId = options.getConnectionId();
|
||||
if (!connectionId || !request.columnName) return;
|
||||
const tableMeta = await options.waitForTableMeta();
|
||||
if (!tableMeta || activeRequestId !== requestId) return;
|
||||
const displayLimit = normalizedLimit(request.limit);
|
||||
const key = cacheKey(request, tableMeta, displayLimit);
|
||||
const now = Date.now();
|
||||
for (const [cachedKey, cachedValue] of cache) {
|
||||
if (cachedValue.expiresAt <= now) cache.delete(cachedKey);
|
||||
}
|
||||
const cached = cache.get(key);
|
||||
if (cached) {
|
||||
applyValues(cached.options, cached.limited);
|
||||
return;
|
||||
}
|
||||
|
||||
const columnInfo = tableMeta.columns.find((column) => column.name === request.columnName);
|
||||
const connection = options.getConnectionConfig();
|
||||
const sql = await buildDataGridColumnDistinctValuesSql({
|
||||
databaseType: options.getDatabaseType(),
|
||||
driverProfile: connection?.driver_profile,
|
||||
identifierQuote: options.getIdentifierQuote(),
|
||||
catalog: tableMeta.catalog,
|
||||
database: tableMeta.database,
|
||||
schema: tableMeta.schema,
|
||||
tableName: tableMeta.tableName,
|
||||
columnName: request.columnName,
|
||||
columnInfo,
|
||||
searchValue: request.searchValue?.trim() || undefined,
|
||||
limit: displayLimit,
|
||||
includeCounts: request.includeCounts !== false,
|
||||
excludeNulls: false,
|
||||
});
|
||||
const result = await api.executeQuery(connectionId, options.getExecutionDatabase(), sql, tableMeta.schema ?? options.getSchema(), undefined, {
|
||||
maxRows: displayLimit,
|
||||
fetchSize: displayLimit,
|
||||
pageSize: displayLimit,
|
||||
timeoutSecs: queryTimeoutSecsForConnection(connection, options.getGlobalQueryTimeoutSecs()),
|
||||
});
|
||||
if (activeRequestId !== requestId) return;
|
||||
const nextOptions = optionsFromResult(result, request.columnIndex, columnInfo, displayLimit);
|
||||
const nextLimited = result.truncated === true || result.rows.length >= displayLimit;
|
||||
cache.set(key, {
|
||||
expiresAt: Date.now() + DATA_GRID_DISTINCT_VALUE_CACHE_TTL_MS,
|
||||
options: nextOptions,
|
||||
limited: nextLimited,
|
||||
});
|
||||
applyValues(nextOptions, nextLimited);
|
||||
} catch (loadError: any) {
|
||||
if (activeRequestId !== requestId) return;
|
||||
distinctOptions.value = [];
|
||||
limited.value = false;
|
||||
error.value = String(loadError?.message || loadError);
|
||||
} finally {
|
||||
if (activeRequestId === requestId) loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function load(request: DataGridDistinctValueLoadRequest) {
|
||||
cancelPending();
|
||||
const activeRequestId = requestId;
|
||||
return performLoad(request, activeRequestId);
|
||||
}
|
||||
|
||||
function schedule(request: DataGridDistinctValueLoadRequest, debounceMs = DATA_GRID_DISTINCT_VALUE_DEBOUNCE_MS) {
|
||||
cancelPending();
|
||||
const activeRequestId = requestId;
|
||||
loading.value = true;
|
||||
error.value = "";
|
||||
timer = window.setTimeout(
|
||||
() => {
|
||||
timer = undefined;
|
||||
void performLoad(request, activeRequestId);
|
||||
},
|
||||
Math.max(0, debounceMs),
|
||||
);
|
||||
}
|
||||
|
||||
watch(
|
||||
() => toValue(options.scopeIdentity),
|
||||
() => reset({ clearCache: true }),
|
||||
);
|
||||
onUnmounted(() => reset({ clearCache: true }));
|
||||
|
||||
return { loading, error, options: distinctOptions, limited, valueByKey, load, schedule, reset };
|
||||
}
|
||||
Reference in new issue
Block a user