Skip to content

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

chore: update list requesting cache for project/instance (#13264)

boojackf6f0c8363859
diff --git a/frontend/src/components/Instance/InstanceArchiveRestoreButton.vue b/frontend/src/components/Instance/InstanceArchiveRestoreButton.vue
index 168695b95b..9de03fe12b 100644
--- a/frontend/src/components/Instance/InstanceArchiveRestoreButton.vue
+++ b/frontend/src/components/Instance/InstanceArchiveRestoreButton.vue
@@ -102,8 +102,9 @@ const archiveOrRestoreInstance = async (archive: boolean) => {
]),
});
} else {
- const instanceList = instanceStore.activeInstanceList;
- if (subscriptionStore.instanceCountLimit <= instanceList.length) {
+ if (
+ subscriptionStore.instanceCountLimit <= instanceStore.instanceList.length
+ ) {
state.showFeatureModal = true;
return;
}
diff --git a/frontend/src/components/ProvideDashboardContext.vue b/frontend/src/components/ProvideDashboardContext.vue
index adb53a4569..2b1e1aeff3 100644
--- a/frontend/src/components/ProvideDashboardContext.vue
+++ b/frontend/src/components/ProvideDashboardContext.vue
@@ -25,7 +25,6 @@ import { PROJECT_V1_ROUTE_DASHBOARD } from "@/router/dashboard/workspaceRoutes";
import {
useEnvironmentV1Store,
usePolicyV1Store,
- useProjectV1Store,
useRoleStore,
useSettingV1Store,
useUserStore,
@@ -49,10 +48,8 @@ const databaseStore = useDatabaseV1Store();
const prepareProjects = async () => {
const routeName = route.name?.toString() || "";
- if (!routeName.startsWith(PROJECT_V1_ROUTE_DASHBOARD)) {
- await useProjectV1Store().listProjects();
- } else {
- // We will handle GetProject in `ProjectV1Layout.vue`.
+ if (!routeName.startsWith(`${PROJECT_V1_ROUTE_DASHBOARD}.`)) {
+ await wrapRefAsPromise(useProjectV1List().ready, true);
}
};
@@ -126,8 +123,7 @@ onMounted(async () => {
if (toProject === undefined) {
// Prepare projects if the project is not specified.
// This is useful when the user navigates to the workspace dashboard from project detail.
- const { ready } = useProjectV1List();
- await wrapRefAsPromise(ready, true);
+ await wrapRefAsPromise(useProjectV1List().ready, true);
}
await prepareDatabases(toProject);
isSwitchingProject.value = false;
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index 5ea331f488..b5a52847ec 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -45,6 +45,7 @@ import {
pushNotification,
useFilterStore,
useAppFeature,
+ useProjectV1List,
} from "@/store";
import type { SQLEditorConnection } from "@/types";
import {
@@ -68,6 +69,7 @@ import {
worksheetNameFromSlug,
extractWorksheetUID,
extractInstanceResourceName,
+ wrapRefAsPromise,
} from "@/utils";
import {
extractWorksheetConnection,
@@ -131,20 +133,21 @@ const initializeProjects = async () => {
editorStore.strictProject = false;
await initProject(DEFAULT_PROJECT_NAME);
} else {
- const projectList = await projectStore.listProjects();
+ const { projectList, ready } = useProjectV1List();
+ await wrapRefAsPromise(ready, true);
const lastView = editorStore.storedLastViewedProject;
if (
lastView &&
- projectList.findIndex((proj) => proj.name === lastView) >= 0
+ projectList.value.findIndex((proj) => proj.name === lastView) >= 0
) {
editorStore.project = lastView;
} else {
- const projectListWithoutDefaultProject = projectList.filter(
+ const projectListWithoutDefaultProject = projectList.value.filter(
(proj) => proj.name !== DEFAULT_PROJECT_NAME
);
editorStore.project =
head(projectListWithoutDefaultProject)?.name ??
- head(projectList)?.name ??
+ head(projectList.value)?.name ??
"";
}
editorStore.strictProject = false;
@@ -159,7 +162,7 @@ const handleProjectSwitched = async () => {
if (project) {
await projectStore.getOrFetchProjectByName(project, true /* silent */);
} else {
- await projectStore.listProjects();
+ await wrapRefAsPromise(useProjectV1List().ready, true);
}
tabStore.maybeInitProject(project);
};
diff --git a/frontend/src/components/SlowQuery/Settings/SlowQuerySettings.vue b/frontend/src/components/SlowQuery/Settings/SlowQuerySettings.vue
index 9461954ed2..76125b4e51 100644
--- a/frontend/src/components/SlowQuery/Settings/SlowQuerySettings.vue
+++ b/frontend/src/components/SlowQuery/Settings/SlowQuerySettings.vue
@@ -38,6 +38,7 @@ import {
pushNotification,
useEnvironmentV1List,
useEnvironmentV1Store,
+ useInstanceV1List,
useInstanceV1Store,
useSlowQueryPolicyStore,
useSlowQueryStore,
@@ -47,7 +48,7 @@ import type { ComposedSlowQueryPolicy } from "@/types";
import { isValidEnvironmentName } from "@/types";
import type { Environment } from "@/types/proto/v1/environment_service";
import type { InstanceResource } from "@/types/proto/v1/instance_service";
-import { instanceV1SupportSlowQuery } from "@/utils";
+import { instanceV1SupportSlowQuery, wrapRefAsPromise } from "@/utils";
import { SlowQueryPolicyTable } from "./components";
type LocalState = {
@@ -78,7 +79,7 @@ const policyList = computed(() => {
});
const instanceList = computed(() => {
- return instanceV1Store.activeInstanceList.filter(instanceV1SupportSlowQuery);
+ return instanceV1Store.instanceList.filter(instanceV1SupportSlowQuery);
});
const composedSlowQueryPolicyList = computed(() => {
@@ -123,7 +124,7 @@ const filteredComposedSlowQueryPolicyList = computed(() => {
const prepare = async () => {
try {
const prepareInstanceList = async () => {
- await instanceV1Store.listInstances(false /* !showDeleted */);
+ await wrapRefAsPromise(useInstanceV1List().ready, true);
};
const preparePolicyList = async () => {
await slowQueryPolicyStore.fetchPolicyList();
diff --git a/frontend/src/store/modules/v1/cache.ts b/frontend/src/store/modules/v1/cache.ts
index 1621e45363..72cd41eeaf 100644
--- a/frontend/src/store/modules/v1/cache.ts
+++ b/frontend/src/store/modules/v1/cache.ts
@@ -1,15 +1,58 @@
-export type StoreCache = {
+import { reactive } from "vue";
+
+const ATTR_SEPARATOR = "-";
+
+const LIST_CACHE = new Map<
+ // The cache name.
+ string,
+ Map<string, StoreListCache>
+>();
+
+type StoreListCache = {
// The timestamp when the cache was last updated.
timestamp: number;
// The flag indicating whether the cache is currently being fetched.
isFetching: boolean;
};
-export const getResourceStoreCacheKey = (
- // The resource type.
- resource: "project" | "instance" | "database",
- // Other attributes to include in the cache key, e.g. "view", "deleted".
- ...attrs: string[]
-) => {
- return `${resource}-${attrs.join("-")}`;
+export const useListCache = (namespace: string) => {
+ const listCache = (() => {
+ const cache = LIST_CACHE.get(namespace);
+ if (cache) {
+ return cache;
+ }
+ const newCache = reactive(new Map<string, StoreListCache>());
+ LIST_CACHE.set(namespace, newCache);
+ return newCache;
+ })();
+
+ const getCacheKey = (
+ // Other attributes to include in the cache key, e.g. "active".
+ ...attrs: string[]
+ ) => {
+ let key = namespace;
+ for (const attr of attrs.filter(Boolean)) {
+ key += `${ATTR_SEPARATOR}${attr}`;
+ }
+ return key;
+ };
+
+ const getCache = (key: string) => {
+ const keyParts = key.split(ATTR_SEPARATOR);
+ let cacheKey = "";
+ for (const part of keyParts) {
+ cacheKey += `${part}`;
+ if (listCache.has(cacheKey)) {
+ return listCache.get(cacheKey);
+ }
+ cacheKey += ATTR_SEPARATOR;
+ }
+ return undefined;
+ };
+
+ return {
+ cacheMap: listCache,
+ getCacheKey,
+ getCache,
+ };
};
diff --git a/frontend/src/store/modules/v1/instance.ts b/frontend/src/store/modules/v1/instance.ts
index ec3ce1d07d..c0569ab42a 100644
--- a/frontend/src/store/modules/v1/instance.ts
+++ b/frontend/src/store/modules/v1/instance.ts
@@ -1,16 +1,15 @@
import { defineStore } from "pinia";
-import { computed, reactive, unref, watchEffect } from "vue";
+import { computed, reactive, watchEffect } from "vue";
import { instanceServiceClient } from "@/grpcweb";
-import type { ComposedInstance, MaybeRef } from "@/types";
+import type { ComposedInstance } from "@/types";
import { unknownEnvironment, unknownInstance } from "@/types";
import { State } from "@/types/proto/v1/common";
import type { DataSource, Instance } from "@/types/proto/v1/instance_service";
import { extractInstanceResourceName } from "@/utils";
-import { getResourceStoreCacheKey, type StoreCache } from "./cache";
+import { useListCache } from "./cache";
import { useEnvironmentV1Store } from "./environment";
export const useInstanceV1Store = defineStore("instance_v1", () => {
- const listCache = reactive(new Map<string, StoreCache>());
const instanceMapByName = reactive(new Map<string, ComposedInstance>());
const reset = () => {
@@ -18,18 +17,17 @@ export const useInstanceV1Store = defineStore("instance_v1", () => {
};
// Getters
- const instanceList = computed(() => {
- const list = Array.from(instanceMapByName.values());
- return list;
+ const instanceListIncludingDeleted = computed(() => {
+ return Array.from(instanceMapByName.values());
});
- const activeInstanceList = computed(() => {
- return instanceList.value.filter((instance) => {
+ const instanceList = computed(() => {
+ return instanceListIncludingDeleted.value.filter((instance) => {
return instance.state === State.ACTIVE;
});
});
const activateInstanceCount = computed(() => {
let count = 0;
- for (const instance of activeInstanceList.value) {
+ for (const instance of instanceList.value) {
if (instance.activation) {
count++;
}
@@ -47,27 +45,6 @@ export const useInstanceV1Store = defineStore("instance_v1", () => {
});
return composedInstances;
};
- const listInstances = async (showDeleted = false) => {
- const cacheKey = getResourceStoreCacheKey(
- "instance",
- showDeleted ? "" : "active"
- );
- if (!listCache.has(cacheKey)) {
- listCache.set(cacheKey, {
- timestamp: Date.now(),
- isFetching: true,
- });
- }
- const { instances } = await instanceServiceClient.listInstances({
- showDeleted,
- });
- const composed = await upsertInstances(instances);
- listCache.set(cacheKey, {
- timestamp: Date.now(),
- isFetching: false,
- });
- return composed;
- };
const createInstance = async (instance: Instance) => {
const createdInstance = await instanceServiceClient.createInstance({
instance,
@@ -173,17 +150,16 @@ export const useInstanceV1Store = defineStore("instance_v1", () => {
return {
reset,
- listCache,
+ instanceListIncludingDeleted,
instanceList,
- activeInstanceList,
activateInstanceCount,
+ upsertInstances,
createInstance,
updateInstance,
archiveInstance,
restoreInstance,
syncInstance,
batchSyncInstances,
- listInstances,
getInstanceByName,
getOrFetchInstanceByName,
createDataSource,
@@ -192,32 +168,39 @@ export const useInstanceV1Store = defineStore("instance_v1", () => {
};
});
-export const useInstanceV1List = (showDeleted: MaybeRef<boolean> = false) => {
+export const useInstanceV1List = (showDeleted: boolean = false) => {
+ const listCache = useListCache("instance");
const store = useInstanceV1Store();
- const cacheKey = getResourceStoreCacheKey(
- "instance",
- showDeleted ? "" : "active"
- );
- const cache = computed(() => store.listCache.get(cacheKey));
+ const cacheKey = listCache.getCacheKey(showDeleted ? "" : "active");
- watchEffect(() => {
- // If request is already in progress, do not send another request.
- if (cache.value?.isFetching) {
- return;
- }
- // If cache is available and forceUpdate is false, do not send another request.
- if (cache.value) {
+ const cache = computed(() => listCache.getCache(cacheKey));
+
+ watchEffect(async () => {
+ // Skip if request is already in progress or cache is available.
+ if (cache.value?.isFetching || cache.value) {
return;
}
- store.listInstances(unref(showDeleted));
+
+ listCache.cacheMap.set(cacheKey, {
+ timestamp: Date.now(),
+ isFetching: true,
+ });
+ const { instances } = await instanceServiceClient.listInstances({
+ showDeleted,
+ });
+ await store.upsertInstances(instances);
+ listCache.cacheMap.set(cacheKey, {
+ timestamp: Date.now(),
+ isFetching: false,
+ });
});
const instanceList = computed(() => {
- if (unref(showDeleted)) {
- return store.instanceList;
- }
- return store.activeInstanceList;
+ return showDeleted
+ ? store.instanceListIncludingDeleted
+ : store.instanceList;
});
+
return {
instanceList,
ready: computed(() => cache.value && !cache.value.isFetching),
diff --git a/frontend/src/store/modules/v1/project.ts b/frontend/src/store/modules/v1/project.ts
index ae396cdc76..27cd93b3c0 100644
--- a/frontend/src/store/modules/v1/project.ts
+++ b/frontend/src/store/modules/v1/project.ts
@@ -17,12 +17,10 @@ import { State } from "@/types/proto/v1/common";
import type { Project } from "@/types/proto/v1/project_service";
import { hasWorkspacePermissionV2 } from "@/utils";
import { useCurrentUserV1 } from "../auth";
-import { getResourceStoreCacheKey, type StoreCache } from "./cache";
+import { useListCache } from "./cache";
import { useProjectIamPolicyStore } from "./projectIamPolicy";
export const useProjectV1Store = defineStore("project_v1", () => {
- const listCache = reactive(new Map<string, StoreCache>());
- const currentUser = useCurrentUserV1();
const projectMapByName = reactive(new Map<ResourceId, ComposedProject>());
const reset = () => {
@@ -49,33 +47,8 @@ export const useProjectV1Store = defineStore("project_v1", () => {
});
return composedProjectList;
};
- const listProjects = async (showDeleted = false) => {
- const cacheKey = getResourceStoreCacheKey(
- "project",
- showDeleted ? "" : "active"
- );
- if (!listCache.has(cacheKey)) {
- listCache.set(cacheKey, {
- timestamp: Date.now(),
- isFetching: true,
- });
- }
- const request = hasWorkspacePermissionV2(
- currentUser.value,
- "bb.projects.list"
- )
- ? projectServiceClient.listProjects
- : projectServiceClient.searchProjects;
- const { projects } = await request({ showDeleted });
- const composedProjects = await upsertProjectMap(projects);
- listCache.set(cacheKey, {
- timestamp: Date.now(),
- isFetching: false,
- });
- return composedProjects;
- };
const getProjectList = (showDeleted = false) => {
- if (unref(showDeleted)) {
+ if (showDeleted) {
return projectList.value;
}
return projectList.value.filter(
@@ -146,12 +119,11 @@ export const useProjectV1Store = defineStore("project_v1", () => {
return {
reset,
- listCache,
projectList,
+ upsertProjectMap,
getProjectList,
findProjectByUID,
getProjectByName,
- listProjects,
getOrFetchProjectByName,
createProject,
updateProject,
@@ -161,34 +133,45 @@ export const useProjectV1Store = defineStore("project_v1", () => {
};
});
-export const useProjectV1List = (showDeleted: MaybeRef<boolean> = false) => {
+export const useProjectV1List = (showDeleted: boolean = false) => {
+ const currentUser = useCurrentUserV1();
+ const listCache = useListCache("project");
const store = useProjectV1Store();
- const cacheKey = getResourceStoreCacheKey(
- "project",
- showDeleted ? "" : "active"
- );
- const cache = computed(() => store.listCache.get(cacheKey));
- const requestPromise = ref<Promise<ComposedProject[]> | null>(null);
+ const cacheKey = listCache.getCacheKey(showDeleted ? "" : "active");
- watchEffect(() => {
- // If request is already in progress, do not send another request.
- if (cache.value?.isFetching) {
- return;
- }
- // If cache is available, do not send another request.
- if (cache.value) {
+ const cache = computed(() => listCache.getCache(cacheKey));
+
+ watchEffect(async () => {
+ // Skip if request is already in progress or cache is available.
+ if (cache.value?.isFetching || cache.value) {
return;
}
- requestPromise.value = store.listProjects(unref(showDeleted));
+
+ listCache.cacheMap.set(cacheKey, {
+ timestamp: Date.now(),
+ isFetching: true,
+ });
+ const request = hasWorkspacePermissionV2(
+ currentUser.value,
+ "bb.projects.list"
+ )
+ ? projectServiceClient.listProjects
+ : projectServiceClient.searchProjects;
+ const { projects } = await request({ showDeleted });
+ await store.upsertProjectMap(projects);
+ listCache.cacheMap.set(cacheKey, {
+ timestamp: Date.now(),
+ isFetching: false,
+ });
});
const projectList = computed(() => {
- return store.getProjectList(unref(showDeleted));
+ return store.getProjectList(showDeleted);
});
+
return {
projectList,
ready: computed(() => cache.value && !cache.value.isFetching),
- requestPromise,
};
};
diff --git a/frontend/src/views/Archive.vue b/frontend/src/views/Archive.vue
index d455d54436..f573d23e0a 100644
--- a/frontend/src/views/Archive.vue
+++ b/frontend/src/views/Archive.vue
@@ -89,7 +89,7 @@ const environmentList = computed(() => {
});
const instanceList = computed(() => {
- return useInstanceV1List().instanceList.value.filter(
+ return useInstanceV1List(true /** showDeleted */).instanceList.value.filter(
(instance) => instance.state === State.DELETED
);
});
diff --git a/frontend/src/views/InstanceDashboard.vue b/frontend/src/views/InstanceDashboard.vue
index d7f248790e..caa73d63f8 100644
--- a/frontend/src/views/InstanceDashboard.vue
+++ b/frontend/src/views/InstanceDashboard.vue
@@ -104,8 +104,7 @@ const filteredInstanceV1List = computed(() => {
const remainingInstanceCount = computed((): number => {
return Math.max(
0,
- subscriptionStore.instanceCountLimit -
- instanceV1Store.activeInstanceList.length
+ subscriptionStore.instanceCountLimit - instanceV1Store.instanceList.length
);
});
diff --git a/frontend/src/views/sql-editor/Setting/Instance/Instance.vue b/frontend/src/views/sql-editor/Setting/Instance/Instance.vue
index 762d8c2456..c0ea13ee57 100644
--- a/frontend/src/views/sql-editor/Setting/Instance/Instance.vue
+++ b/frontend/src/views/sql-editor/Setting/Instance/Instance.vue
@@ -186,8 +186,7 @@ const filteredInstanceV1List = computed(() => {
const remainingInstanceCount = computed((): number => {
return Math.max(
0,
- subscriptionStore.instanceCountLimit -
- instanceV1Store.activeInstanceList.length
+ subscriptionStore.instanceCountLimit - instanceV1Store.instanceList.length
);
});