Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: use advanced search for slow query (#9741)

ecmadaoe59eaeaabd4c
diff --git a/frontend/src/components/DatabaseSlowQueryPanel.vue b/frontend/src/components/DatabaseSlowQueryPanel.vue
index 8bb0ac4d0f..a059881c6d 100644
--- a/frontend/src/components/DatabaseSlowQueryPanel.vue
+++ b/frontend/src/components/DatabaseSlowQueryPanel.vue
@@ -1,43 +1,45 @@
<template>
- <div>
- <SlowQueryPanel
- v-if="database"
- v-model:filter="filter"
- :filter-types="['time-range']"
- :show-project-column="false"
- :show-environment-column="false"
- :show-instance-column="false"
- :show-database-column="false"
- />
- </div>
+ <SlowQueryPanel
+ v-if="database"
+ :show-project-column="false"
+ :show-environment-column="false"
+ :show-instance-column="false"
+ :show-database-column="false"
+ :support-option-id-list="[]"
+ :readonly-search-scopes="readonlyScopes"
+ />
</template>
<script lang="ts" setup>
-import { shallowRef, watch } from "vue";
-import {
- type SlowQueryFilterParams,
- SlowQueryPanel,
- defaultSlowQueryFilterParams,
-} from "@/components/SlowQuery";
+import { computed } from "vue";
+import { SlowQueryPanel } from "@/components/SlowQuery";
import type { ComposedDatabase } from "@/types";
+import {
+ SearchScope,
+ extractEnvironmentResourceName,
+ extractInstanceResourceName,
+} from "@/utils";
const props = defineProps<{
database: ComposedDatabase;
}>();
-const filter = shallowRef<SlowQueryFilterParams>({
- ...defaultSlowQueryFilterParams(),
- environment: props.database.effectiveEnvironmentEntity,
- instance: props.database.instanceEntity,
- database: props.database,
+const readonlyScopes = computed((): SearchScope[] => {
+ return [
+ {
+ id: "environment",
+ value: extractEnvironmentResourceName(
+ props.database.effectiveEnvironment
+ ),
+ },
+ {
+ id: "instance",
+ value: extractInstanceResourceName(props.database.instance),
+ },
+ {
+ id: "database",
+ value: `${props.database.databaseName}-${props.database.uid}`,
+ },
+ ];
});
-
-watch(
- () => props.database.name,
- () => {
- filter.value.environment = props.database.effectiveEnvironmentEntity;
- filter.value.instance = props.database.instanceEntity;
- filter.value.database = props.database;
- }
-);
</script>
diff --git a/frontend/src/components/IssueV1/components/IssueSearch/AdvancedSearchBox.vue b/frontend/src/components/IssueV1/components/IssueSearch/AdvancedSearchBox.vue
index 4758a4c890..e0052ce450 100644
--- a/frontend/src/components/IssueV1/components/IssueSearch/AdvancedSearchBox.vue
+++ b/frontend/src/components/IssueV1/components/IssueSearch/AdvancedSearchBox.vue
@@ -3,7 +3,7 @@
<NInput
ref="inputRef"
v-model:value="inputText"
- :placeholder="placeholder || $t('issue.advanced-search.self')"
+ :placeholder="placeholder ?? $t('issue.advanced-search.self')"
class="bb-advanced-issue-search-box__input"
style="--n-padding-left: 8px; --n-padding-right: 4px"
@click="handleInputClick"
@@ -116,7 +116,7 @@ import { useSearchScopeOptions } from "./useSearchScopeOptions";
const props = withDefaults(
defineProps<{
- placeholder?: string;
+ placeholder?: string | undefined;
params: SearchParams;
readonlyScopes?: SearchScope[];
autofocus?: boolean;
@@ -126,7 +126,7 @@ const props = withDefaults(
readonlyScopes: () => [],
supportOptionIdList: () => [],
autofocus: false,
- placeholder: "",
+ placeholder: undefined,
}
);
diff --git a/frontend/src/components/ProjectSlowQueryPanel.vue b/frontend/src/components/ProjectSlowQueryPanel.vue
index d0ce8a105d..1738f65b7d 100644
--- a/frontend/src/components/ProjectSlowQueryPanel.vue
+++ b/frontend/src/components/ProjectSlowQueryPanel.vue
@@ -1,35 +1,27 @@
<template>
- <div>
- <SlowQueryPanel
- v-model:filter="filter"
- :filter-types="['environment', 'instance', 'database', 'time-range']"
- :show-project-column="false"
- />
- </div>
+ <SlowQueryPanel
+ :readonly-search-scopes="readonlyScopes"
+ :support-option-id-list="['environment', 'instance', 'database']"
+ :show-project-column="false"
+ />
</template>
<script lang="ts" setup>
-import { shallowRef, watch } from "vue";
-import {
- SlowQueryPanel,
- SlowQueryFilterParams,
- defaultSlowQueryFilterParams,
-} from "@/components/SlowQuery";
+import { computed } from "vue";
+import { SlowQueryPanel } from "@/components/SlowQuery";
import { Project } from "@/types/proto/v1/project_service";
+import { SearchScope, extractProjectResourceName } from "@/utils";
const props = defineProps<{
project: Project;
}>();
-const filter = shallowRef<SlowQueryFilterParams>({
- ...defaultSlowQueryFilterParams(),
- project: props.project,
+const readonlyScopes = computed((): SearchScope[] => {
+ return [
+ {
+ id: "project",
+ value: extractProjectResourceName(props.project.name),
+ },
+ ];
});
-
-watch(
- () => props.project.uid,
- () => {
- filter.value.project = props.project;
- }
-);
</script>
diff --git a/frontend/src/components/SlowQuery/Panel/LogFilter.vue b/frontend/src/components/SlowQuery/Panel/LogFilter.vue
index ef7e6d4079..71197b4372 100644
--- a/frontend/src/components/SlowQuery/Panel/LogFilter.vue
+++ b/frontend/src/components/SlowQuery/Panel/LogFilter.vue
@@ -1,62 +1,20 @@
<template>
<div v-if="ready" class="mb-2 space-y-2">
<div
- v-if="
- filterTypes.includes('project') || filterTypes.includes('environment')
- "
- class="flex items-center gap-x-4"
+ class="flex flex-col md:flex-row items-end md:items-center gap-y-2 gap-x-2"
>
- <div class="flex-1 flex items-center gap-x-4">
- <EnvironmentTabFilter
- v-if="filterTypes.includes('environment')"
- class="flex-1"
- :environment="params.environment?.name"
- :include-all="true"
- :disabled="loading"
- @update:environment="changeEnvironment"
- />
- </div>
-
- <div class="flex items-center justify-end gap-x-2">
- <slot name="suffix" />
- </div>
- </div>
-
- <div class="flex items-center gap-x-4">
- <NInputGroup class="flex-1">
- <ProjectSelect
- v-if="filterTypes.includes('project')"
- :project="params.project?.uid ?? String(UNKNOWN_ID)"
- :include-default-project="canVisitDefaultProject"
- :include-all="true"
- :disabled="loading"
- @update:project="changeProjectId"
- />
- <InstanceSelect
- v-if="filterTypes.includes('instance')"
- class="!w-48"
- :instance="params.instance?.uid ?? String(UNKNOWN_ID)"
- :environment="params.environment?.uid"
- :include-all="true"
- :filter="instanceFilter"
- :disabled="loading"
- @update:instance="changeInstanceId"
- />
- <DatabaseSelect
- v-if="filterTypes.includes('database')"
- :database="params.database?.uid ?? String(UNKNOWN_ID)"
- :environment="params.environment?.uid"
- :instance="params.instance?.uid"
- :project="params.project?.uid"
- :include-all="true"
- :filter="(db) => instanceFilter(db.instanceEntity)"
- :disabled="loading"
- :consistent-menu-width="false"
- @update:database="changeDatabaseId"
- />
+ <AdvancedSearchBox
+ v-if="supportOptionIdList.length > 0"
+ :params="params"
+ :autofocus="false"
+ :placeholder="''"
+ class="flex-1 hidden md:block"
+ :support-option-id-list="supportOptionIdList"
+ @update:params="$emit('update:params', $event)"
+ />
+ <NInputGroup style="width: auto">
<NDatePicker
- v-if="filterTypes.includes('time-range')"
- :value="params.fromTime"
+ :value="fromTime"
:disabled="loading"
:is-date-disabled="isDateDisabled"
:placeholder="$t('slow-query.filter.from-date')"
@@ -67,8 +25,7 @@
@update:value="changeFromTime($event)"
/>
<NDatePicker
- v-if="filterTypes.includes('time-range')"
- :value="params.toTime"
+ :value="toTime"
:disabled="loading"
:is-date-disabled="isDateDisabled"
:placeholder="$t('slow-query.filter.to-date')"
@@ -80,13 +37,7 @@
/>
</NInputGroup>
- <div
- v-if="
- !filterTypes.includes('project') &&
- !filterTypes.includes('environment')
- "
- class="flex items-center justify-end space-x-2"
- >
+ <div class="flex items-center justify-end space-x-2">
<slot name="suffix" />
</div>
</div>
@@ -95,74 +46,27 @@
<script lang="ts" setup>
import dayjs from "dayjs";
import { NDatePicker, NInputGroup } from "naive-ui";
-import { computed } from "vue";
-import {
- ProjectSelect,
- InstanceSelect,
- EnvironmentTabFilter,
- DatabaseSelect,
-} from "@/components/v2";
-import {
- useCurrentUserV1,
- useDatabaseV1Store,
- useEnvironmentV1Store,
- useInstanceV1Store,
- useProjectV1Store,
- useSlowQueryPolicyList,
-} from "@/store";
-import { UNKNOWN_ID } from "@/types";
-import { Instance } from "@/types/proto/v1/instance_service";
-import { hasWorkspacePermissionV1, instanceV1SupportSlowQuery } from "@/utils";
-import type { FilterType, SlowQueryFilterParams } from "./types";
+import { useSlowQueryPolicyList } from "@/store";
+import { SearchParams, SearchScopeId } from "@/utils";
const props = defineProps<{
- params: SlowQueryFilterParams;
- filterTypes: readonly FilterType[];
+ fromTime: number | undefined;
+ toTime: number | undefined;
+ params: SearchParams;
+ supportOptionIdList: SearchScopeId[];
loading?: boolean;
}>();
const emit = defineEmits<{
- (event: "update:params", params: SlowQueryFilterParams): void;
+ (
+ event: "update:time",
+ params: { fromTime: number | undefined; toTime: number | undefined }
+ ): void;
+ (event: "update:params", params: SearchParams): void;
}>();
-const currentUserV1 = useCurrentUserV1();
-const { list: policyList, ready } = useSlowQueryPolicyList();
+const { ready } = useSlowQueryPolicyList();
-const canVisitDefaultProject = computed(() => {
- return hasWorkspacePermissionV1(
- "bb.permission.workspace.manage-database",
- currentUserV1.value.userRole
- );
-});
-
-const changeEnvironment = (name: string) => {
- const environment = useEnvironmentV1Store().getEnvironmentByName(name);
- update({ environment });
-};
-const changeInstanceId = (uid: string | undefined) => {
- if (uid && uid !== String(UNKNOWN_ID)) {
- const instance = useInstanceV1Store().getInstanceByUID(uid);
- update({ instance });
- return;
- }
- update({ instance: undefined });
-};
-const changeDatabaseId = (uid: string | undefined) => {
- if (uid && uid !== String(UNKNOWN_ID)) {
- const database = useDatabaseV1Store().getDatabaseByUID(uid);
- update({ database });
- return;
- }
- update({ database: undefined });
-};
-const changeProjectId = (id: string | undefined) => {
- if (id && id !== String(UNKNOWN_ID)) {
- const project = useProjectV1Store().getProjectByUID(id);
- update({ project });
- return;
- }
- update({ project: undefined });
-};
const changeTime = (
fromTime: number | undefined,
toTime: number | undefined
@@ -180,37 +84,17 @@ const changeTime = (
// toTime is the end of the day
toTime = dayjs(toTime).endOf("day").valueOf();
}
- update({ fromTime, toTime });
+ emit("update:time", { fromTime, toTime });
};
const changeFromTime = (fromTime: number | undefined) => {
- const { toTime } = props.params;
+ const { toTime } = props;
changeTime(fromTime, toTime);
};
const changeToTime = (toTime: number | undefined) => {
- const { fromTime } = props.params;
+ const { fromTime } = props;
changeTime(fromTime, toTime);
};
-const update = (params: Partial<SlowQueryFilterParams>) => {
- emit("update:params", {
- ...props.params,
- ...params,
- });
-};
-
-const instanceFilter = (instance: Instance) => {
- if (!instanceV1SupportSlowQuery(instance)) {
- return false;
- }
- const policy = policyList.value.find(
- (policy) => policy.resourceUid === instance.uid
- );
- if (!policy) {
- return false;
- }
- return !!policy.slowQueryPolicy?.active;
-};
-
const isDateDisabled = (date: number) => {
return date > dayjs().endOf("day").valueOf();
};
diff --git a/frontend/src/components/SlowQuery/Panel/SlowQueryPanel.vue b/frontend/src/components/SlowQuery/Panel/SlowQueryPanel.vue
index dcc0f5f02c..ff68d6a223 100644
--- a/frontend/src/components/SlowQuery/Panel/SlowQueryPanel.vue
+++ b/frontend/src/components/SlowQuery/Panel/SlowQueryPanel.vue
@@ -1,27 +1,27 @@
<template>
<div class="space-y-4">
- <div v-if="filterTypes.length > 0">
- <LogFilter
- :params="filter"
- :filter-types="filterTypes"
- :loading="loading"
- @update:params="$emit('update:filter', $event)"
- >
- <template #suffix>
- <NButton v-if="allowAdmin" type="default" @click="goConfig">
- {{ $t("common.configure") }}
- </NButton>
- <NButton
- type="primary"
- :disabled="!allowSync"
- :loading="syncing"
- @click="syncNow"
- >
- {{ $t("common.sync-now") }}
- </NButton>
- </template>
- </LogFilter>
- </div>
+ <LogFilter
+ v-model:params="state.params"
+ :from-time="state.timeRange.fromTime"
+ :to-time="state.timeRange.toTime"
+ :loading="loading"
+ :support-option-id-list="supportOptionIdList"
+ @update:time="state.timeRange = $event"
+ >
+ <template #suffix>
+ <NButton v-if="allowAdmin" type="default" @click="goConfig">
+ {{ $t("common.configure") }}
+ </NButton>
+ <NButton
+ type="primary"
+ :disabled="!allowSync"
+ :loading="syncing"
+ @click="syncNow"
+ >
+ {{ $t("common.sync-now") }}
+ </NButton>
+ </template>
+ </LogFilter>
<div class="relative min-h-[8rem]">
<LogTable
:slow-query-log-list="slowQueryLogList"
@@ -49,8 +49,9 @@
</template>
<script lang="ts" setup>
+import dayjs from "dayjs";
import { NButton } from "naive-ui";
-import { computed, shallowRef, watch } from "vue";
+import { computed, shallowRef, watch, reactive } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import {
@@ -63,31 +64,34 @@ import {
} from "@/store";
import { ComposedSlowQueryLog } from "@/types";
import {
- extractInstanceResourceName,
- extractProjectResourceName,
+ SearchScope,
+ SearchParams,
+ SearchScopeId,
hasWorkspacePermissionV1,
+ extractInstanceResourceName,
} from "@/utils";
import DetailPanel from "./DetailPanel.vue";
import LogFilter from "./LogFilter.vue";
import LogTable from "./LogTable.vue";
-import {
- type FilterType,
- type SlowQueryFilterParams,
- FilterTypeList,
- buildListSlowQueriesRequest,
-} from "./types";
+import { buildListSlowQueriesRequest } from "./types";
const props = withDefaults(
defineProps<{
- filter: SlowQueryFilterParams;
- filterTypes?: readonly FilterType[];
+ supportOptionIdList?: SearchScopeId[];
+ readonlySearchScopes?: SearchScope[];
showProjectColumn?: boolean;
showEnvironmentColumn?: boolean;
showInstanceColumn?: boolean;
showDatabaseColumn?: boolean;
}>(),
{
- filterTypes: () => FilterTypeList,
+ supportOptionIdList: () => [
+ "environment",
+ "project",
+ "instance",
+ "database",
+ ],
+ readonlySearchScopes: () => [],
showProjectColumn: true,
showEnvironmentColumn: true,
showInstanceColumn: true,
@@ -95,10 +99,36 @@ const props = withDefaults(
}
);
-defineEmits<{
- (event: "update:filter", filter: SlowQueryFilterParams): void;
+const emit = defineEmits<{
+ (event: "update:scopes", scopes: SearchScope[]): void;
}>();
+interface LocalState {
+ timeRange: {
+ fromTime: number | undefined;
+ toTime: number | undefined;
+ };
+ params: SearchParams;
+}
+
+const defaultSlowQueryTimeRange = () => {
+ const now = dayjs();
+ const aWeekAgo = now.subtract(7, "days").startOf("day").valueOf();
+ const tonight = now.endOf("day").valueOf();
+ return {
+ fromTime: aWeekAgo,
+ toTime: tonight,
+ };
+};
+
+const state = reactive<LocalState>({
+ timeRange: defaultSlowQueryTimeRange(),
+ params: {
+ query: "",
+ scopes: [],
+ },
+});
+
const { t } = useI18n();
const router = useRouter();
const currentUser = useCurrentUserV1();
@@ -108,8 +138,29 @@ const slowQueryLogList = shallowRef<ComposedSlowQueryLog[]>([]);
const selectedSlowQueryLog = shallowRef<ComposedSlowQueryLog>();
const syncing = shallowRef(false);
+const searchScopes = computed(() => {
+ return [...props.readonlySearchScopes, ...state.params.scopes];
+});
+
+watch(
+ () => searchScopes.value,
+ (scopes) => emit("update:scopes", scopes)
+);
+
+const selectedInstanceName = computed(() => {
+ return searchScopes.value.find((s) => s.id === "instance")?.value;
+});
+
+const selectedProjectName = computed(() => {
+ return searchScopes.value.find((s) => s.id === "project")?.value;
+});
+
const params = computed(() => {
- return buildListSlowQueriesRequest(props.filter);
+ const query = buildListSlowQueriesRequest(
+ searchScopes.value,
+ state.timeRange
+ );
+ return query;
});
const allowAdmin = computed(() => {
@@ -146,13 +197,13 @@ const syncNow = async () => {
syncing.value = true;
try {
await useGracefulRequest(async () => {
- if (props.filter.instance) {
+ if (selectedInstanceName.value) {
await slowQueryStore.syncSlowQueries(
- `instances/${extractInstanceResourceName(props.filter.instance.name)}`
+ `instances/${selectedInstanceName.value}`
);
- } else if (props.filter.project) {
+ } else if (selectedProjectName.value) {
await slowQueryStore.syncSlowQueries(
- `projects/${extractProjectResourceName(props.filter.project.name)}`
+ `projects/${selectedProjectName.value}`
);
} else {
const policyList = await useSlowQueryPolicyStore().fetchPolicyList();
diff --git a/frontend/src/components/SlowQuery/Panel/types.ts b/frontend/src/components/SlowQuery/Panel/types.ts
index af6d47cf80..f324bfff6d 100644
--- a/frontend/src/components/SlowQuery/Panel/types.ts
+++ b/frontend/src/components/SlowQuery/Panel/types.ts
@@ -1,63 +1,38 @@
import dayjs from "dayjs";
+import { useDatabaseV1Store } from "@/store";
import { UNKNOWN_ID } from "@/types";
-import {
- Database,
- ListSlowQueriesRequest,
-} from "@/types/proto/v1/database_service";
-import { Environment } from "@/types/proto/v1/environment_service";
-import { Instance } from "@/types/proto/v1/instance_service";
-import { Project } from "@/types/proto/v1/project_service";
+import { ListSlowQueriesRequest } from "@/types/proto/v1/database_service";
+import { SearchScope } from "@/utils";
-export type SlowQueryFilterParams = {
- project: Project | undefined; // undefined to "All"
- environment: Environment | undefined; // undefined to "All"
- instance: Instance | undefined; // undefined to "All"
- database: Database | undefined; // undefined to "All"
- fromTime: number | undefined;
- toTime: number | undefined;
-};
-
-export const FilterTypeList = [
- "project",
- "environment",
- "instance",
- "database",
- "time-range",
-] as const;
-
-export type FilterType = typeof FilterTypeList[number];
-
-export const defaultSlowQueryFilterParams = (): SlowQueryFilterParams => {
- const now = dayjs();
- const aWeekAgo = now.subtract(7, "days").startOf("day").valueOf();
- const tonight = now.endOf("day").valueOf();
- return {
- project: undefined,
- environment: undefined,
- instance: undefined,
- database: undefined,
- fromTime: aWeekAgo,
- toTime: tonight,
- };
-};
-
-export const buildListSlowQueriesRequest = (filter: SlowQueryFilterParams) => {
+export const buildListSlowQueriesRequest = (
+ scopes: SearchScope[],
+ timeRange: { fromTime: number | undefined; toTime: number | undefined }
+) => {
const request = {} as Partial<ListSlowQueriesRequest>;
- const { project, environment, instance, database, fromTime, toTime } = filter;
+
+ const project = scopes.find((s) => s.id === "project")?.value;
+ const environment = scopes.find((s) => s.id === "environment")?.value;
+ const instance = scopes.find((s) => s.id === "instance")?.value;
+ const database = scopes.find((s) => s.id === "database")?.value;
+ const { fromTime, toTime } = timeRange;
const query: string[] = [];
request.parent = "instances/-/databases/-";
- if (database && database.uid !== String(UNKNOWN_ID)) {
- request.parent = database.name;
- } else if (instance && instance.uid !== String(UNKNOWN_ID)) {
- request.parent = `${instance.name}/databases/-`;
- } else if (environment && environment.uid !== String(UNKNOWN_ID)) {
+ if (database) {
+ const uid = database.split("-").slice(-1)[0];
+ const db = useDatabaseV1Store().getDatabaseByUID(uid);
+ if (db.uid !== `${UNKNOWN_ID}`) {
+ request.parent = db.name;
+ }
+ } else if (instance) {
+ request.parent = `instances/${instance}/databases/-`;
+ } else if (environment) {
request.parent = `instances/-/databases/-`;
- query.push(`environment = "${environment.name}"`);
+ query.push(`environment = "environments/${environment}"`);
}
if (project) {
- query.push(`project = "${project.name}"`);
+ query.push(`project = "projects/${project}"`);
}
if (fromTime) {
const start = dayjs(fromTime).toISOString();
diff --git a/frontend/src/views/SlowQuery/SlowQueryDashboard.vue b/frontend/src/views/SlowQuery/SlowQueryDashboard.vue
index 56b3a9aecc..75b32fea68 100644
--- a/frontend/src/views/SlowQuery/SlowQueryDashboard.vue
+++ b/frontend/src/views/SlowQuery/SlowQueryDashboard.vue
@@ -1,46 +1,9 @@
<template>
<div class="px-4 py-6">
- <SlowQueryPanel v-if="ready" v-model:filter="filter" />
+ <SlowQueryPanel />
</div>
</template>
<script lang="ts" setup>
-import { shallowRef, watch, watchEffect } from "vue";
-import { useRoute, useRouter } from "vue-router";
-import {
- SlowQueryPanel,
- SlowQueryFilterParams,
- defaultSlowQueryFilterParams,
-} from "@/components/SlowQuery";
-import {
- extractSlowQueryLogFilterFromQuery,
- wrapQueryFromFilterParams,
-} from "./utils";
-
-const route = useRoute();
-const router = useRouter();
-const ready = shallowRef(false);
-const filter = shallowRef<SlowQueryFilterParams>({
- ...defaultSlowQueryFilterParams(),
-});
-
-const syncFilterParamsFromQuery = async () => {
- const params = await extractSlowQueryLogFilterFromQuery(route.query);
- filter.value = params;
- ready.value = true;
-};
-
-watchEffect(syncFilterParamsFromQuery);
-
-watch(
- filter,
- () => {
- const query = wrapQueryFromFilterParams(filter.value);
- router.replace({
- ...route,
- query,
- });
- },
- { deep: true }
-);
+import { SlowQueryPanel } from "@/components/SlowQuery";
</script>
diff --git a/frontend/src/views/SlowQuery/utils.ts b/frontend/src/views/SlowQuery/utils.ts
deleted file mode 100644
index d88704356d..0000000000
--- a/frontend/src/views/SlowQuery/utils.ts
+++ /dev/null
@@ -1,90 +0,0 @@
-import type { LocationQuery } from "vue-router";
-import {
- SlowQueryFilterParams,
- defaultSlowQueryFilterParams,
-} from "@/components/SlowQuery";
-import {
- useDatabaseV1Store,
- useEnvironmentV1Store,
- useInstanceV1Store,
- useProjectV1Store,
-} from "@/store";
-import { UNKNOWN_ID } from "@/types";
-
-export const wrapQueryFromFilterParams = (params: SlowQueryFilterParams) => {
- const query: Record<string, any> = {};
- if (params.project && params.project.uid !== String(UNKNOWN_ID)) {
- query.project = params.project.uid;
- }
- if (params.environment && params.environment.uid !== String(UNKNOWN_ID)) {
- query.environment = params.environment.uid;
- }
- if (params.instance && params.instance.uid !== String(UNKNOWN_ID)) {
- query.instance = params.instance.uid;
- }
- if (params.database && params.database.uid !== String(UNKNOWN_ID)) {
- query.database = params.database.uid;
- }
- if (params.fromTime) {
- if (params.fromTime !== defaultSlowQueryFilterParams().fromTime) {
- query.fromTime = Math.floor(params.fromTime / 1000);
- }
- } else {
- query.fromTime = -1;
- }
- if (params.toTime) {
- if (params.toTime !== defaultSlowQueryFilterParams().toTime) {
- query.toTime = Math.floor(params.toTime / 1000);
- }
- } else {
- query.toTime = -1;
- }
- return query;
-};
-
-export const extractSlowQueryLogFilterFromQuery = async (
- query: LocationQuery
-) => {
- const params: SlowQueryFilterParams = defaultSlowQueryFilterParams();
- if (query.environment) {
- const id = (query.environment as string) ?? String(UNKNOWN_ID);
- const environment = useEnvironmentV1Store().getEnvironmentByUID(id);
- if (environment && environment.uid !== String(UNKNOWN_ID)) {
- params.environment = environment;
- }
- }
- if (query.project) {
- const id = (query.project as string) ?? String(UNKNOWN_ID);
- const project = await useProjectV1Store().getOrFetchProjectByUID(id);
- if (project && project.uid !== String(UNKNOWN_ID)) {
- params.project = project;
- }
- }
- if (query.instance) {
- const uid = (query.instance as string) ?? UNKNOWN_ID;
- const instance = await useInstanceV1Store().getOrFetchInstanceByUID(uid);
- if (instance && instance.uid !== String(UNKNOWN_ID)) {
- params.instance = instance;
- }
- }
- if (query.database) {
- const uid = (query.database as string) ?? UNKNOWN_ID;
- const database = await useDatabaseV1Store().getOrFetchDatabaseByUID(uid);
- if (database && database.uid !== String(UNKNOWN_ID)) {
- params.database = database;
- }
- }
- if (query.fromTime) {
- const from = parseInt(query.fromTime as string, 10) * 1000 || undefined;
- if (from) {
- params.fromTime = from > 0 ? from : undefined;
- }
- }
- if (query.toTime) {
- const to = parseInt(query.toTime as string, 10) * 1000 || undefined;
- if (to) {
- params.toTime = to > 0 ? to : undefined;
- }
- }
- return params;
-};