Files
n8n-enterprise-unlocked/packages/frontend/editor-ui/src/features/insights/insights.store.ts
Csaba Tuncsik 90ba680631 feat(editor): Insights dashboard (#13739)
Co-authored-by: Guillaume Jacquart <jacquart.guillaume@gmail.com>
Co-authored-by: Raúl Gómez Morales <raul00gm@gmail.com>
2025-04-03 16:24:44 +02:00

64 lines
1.7 KiB
TypeScript

import { computed } from 'vue';
import { defineStore } from 'pinia';
import { useAsyncState } from '@vueuse/core';
import type { ListInsightsWorkflowQueryDto } from '@n8n/api-types';
import * as insightsApi from '@/features/insights/insights.api';
import { useRootStore } from '@/stores/root.store';
import { useUsersStore } from '@/stores/users.store';
import { useSettingsStore } from '@/stores/settings.store';
import { transformInsightsSummary } from '@/features/insights/insights.utils';
import { getResourcePermissions } from '@/permissions';
export const useInsightsStore = defineStore('insights', () => {
const rootStore = useRootStore();
const usersStore = useUsersStore();
const settingsStore = useSettingsStore();
const globalInsightsPermissions = computed(
() => getResourcePermissions(usersStore.currentUser?.globalScopes).insights,
);
const isInsightsEnabled = computed(() => settingsStore.settings.insights.enabled);
const isSummaryEnabled = computed(
() => globalInsightsPermissions.value.list && isInsightsEnabled.value,
);
const summary = useAsyncState(
async () => {
const raw = await insightsApi.fetchInsightsSummary(rootStore.restApiContext);
return transformInsightsSummary(raw);
},
[],
{ immediate: false },
);
const charts = useAsyncState(
async () => {
return await insightsApi.fetchInsightsByTime(rootStore.restApiContext);
},
[],
{ immediate: false },
);
const table = useAsyncState(
async (filter?: ListInsightsWorkflowQueryDto) => {
return await insightsApi.fetchInsightsByWorkflow(rootStore.restApiContext, filter);
},
{
count: 0,
data: [],
},
{ immediate: false },
);
return {
globalInsightsPermissions,
isInsightsEnabled,
isSummaryEnabled,
summary,
charts,
table,
};
});