2022-12-20 01:52:01 -08:00
|
|
|
import { computed, reactive } from 'vue';
|
|
|
|
import { defineStore } from 'pinia';
|
2023-04-24 03:18:24 -07:00
|
|
|
import type { UsageState } from '@/Interface';
|
2023-12-19 04:58:30 -08:00
|
|
|
import { activateLicenseKey, getLicense, renewLicense, requestLicenseTrial } from '@/api/usage';
|
2023-05-05 01:41:54 -07:00
|
|
|
import { useRootStore } from '@/stores/n8nRoot.store';
|
|
|
|
import { useSettingsStore } from '@/stores/settings.store';
|
|
|
|
import { useUsersStore } from '@/stores/users.store';
|
2022-12-20 01:52:01 -08:00
|
|
|
|
|
|
|
export type UsageTelemetry = {
|
|
|
|
instance_id: string;
|
2022-12-28 08:07:34 -08:00
|
|
|
action: 'view_plans' | 'manage_plan' | 'add_activation_key' | 'desktop_view_plans';
|
2022-12-20 01:52:01 -08:00
|
|
|
plan_name_current: string;
|
|
|
|
usage: number;
|
|
|
|
quota: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
const DEFAULT_PLAN_NAME = 'Community';
|
|
|
|
const DEFAULT_STATE: UsageState = {
|
|
|
|
loading: true,
|
|
|
|
data: {
|
|
|
|
usage: {
|
|
|
|
executions: {
|
|
|
|
limit: -1,
|
|
|
|
value: 0,
|
|
|
|
warningThreshold: 0.8,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
license: {
|
|
|
|
planId: '',
|
|
|
|
planName: DEFAULT_PLAN_NAME,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useUsageStore = defineStore('usage', () => {
|
|
|
|
const rootStore = useRootStore();
|
|
|
|
const settingsStore = useSettingsStore();
|
|
|
|
const usersStore = useUsersStore();
|
|
|
|
|
|
|
|
const state = reactive<UsageState>(DEFAULT_STATE);
|
|
|
|
|
|
|
|
const planName = computed(() => state.data.license.planName || DEFAULT_PLAN_NAME);
|
|
|
|
const planId = computed(() => state.data.license.planId);
|
|
|
|
const executionLimit = computed(() => state.data.usage.executions.limit);
|
|
|
|
const executionCount = computed(() => state.data.usage.executions.value);
|
|
|
|
const executionPercentage = computed(() => (executionCount.value / executionLimit.value) * 100);
|
|
|
|
const instanceId = computed(() => settingsStore.settings.instanceId);
|
|
|
|
const managementToken = computed(() => state.data.managementToken);
|
|
|
|
const appVersion = computed(() => settingsStore.settings.versionCli);
|
|
|
|
const commonSubscriptionAppUrlQueryParams = computed(
|
|
|
|
() => `instanceid=${instanceId.value}&version=${appVersion.value}`,
|
|
|
|
);
|
|
|
|
const subscriptionAppUrl = computed(() =>
|
|
|
|
settingsStore.settings.license.environment === 'production'
|
|
|
|
? 'https://subscription.n8n.io'
|
|
|
|
: 'https://staging-subscription.n8n.io',
|
|
|
|
);
|
|
|
|
|
|
|
|
const setLoading = (loading: boolean) => {
|
|
|
|
state.loading = loading;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setData = (data: UsageState['data']) => {
|
|
|
|
state.data = data;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getLicenseInfo = async () => {
|
|
|
|
const data = await getLicense(rootStore.getRestApiContext);
|
|
|
|
setData(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const activateLicense = async (activationKey: string) => {
|
|
|
|
const data = await activateLicenseKey(rootStore.getRestApiContext, { activationKey });
|
|
|
|
setData(data);
|
|
|
|
await settingsStore.getSettings();
|
|
|
|
};
|
|
|
|
|
|
|
|
const refreshLicenseManagementToken = async () => {
|
|
|
|
try {
|
|
|
|
const data = await renewLicense(rootStore.getRestApiContext);
|
|
|
|
setData(data);
|
|
|
|
} catch (error) {
|
2023-05-10 08:10:03 -07:00
|
|
|
await getLicenseInfo();
|
2022-12-20 01:52:01 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-19 04:58:30 -08:00
|
|
|
const requestEnterpriseLicenseTrial = async () => {
|
|
|
|
if (!usersStore.currentUser) {
|
|
|
|
throw new Error('User is not logged in');
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await requestLicenseTrial({
|
|
|
|
licenseType: 'enterprise',
|
|
|
|
firstName: usersStore.currentUser.firstName ?? '',
|
|
|
|
lastName: usersStore.currentUser.lastName ?? '',
|
|
|
|
email: usersStore.currentUser.email ?? '',
|
|
|
|
instanceUrl: window.location.origin,
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2022-12-20 01:52:01 -08:00
|
|
|
return {
|
|
|
|
setLoading,
|
|
|
|
getLicenseInfo,
|
|
|
|
setData,
|
|
|
|
activateLicense,
|
|
|
|
refreshLicenseManagementToken,
|
2023-12-19 04:58:30 -08:00
|
|
|
requestEnterpriseLicenseTrial,
|
2022-12-20 01:52:01 -08:00
|
|
|
planName,
|
|
|
|
planId,
|
|
|
|
executionLimit,
|
|
|
|
executionCount,
|
|
|
|
executionPercentage,
|
|
|
|
instanceId,
|
|
|
|
managementToken,
|
|
|
|
appVersion,
|
|
|
|
isCloseToLimit: computed(() =>
|
|
|
|
state.data.usage.executions.limit < 0
|
|
|
|
? false
|
|
|
|
: executionCount.value / executionLimit.value >=
|
|
|
|
state.data.usage.executions.warningThreshold,
|
|
|
|
),
|
|
|
|
viewPlansUrl: computed(
|
|
|
|
() => `${subscriptionAppUrl.value}?${commonSubscriptionAppUrlQueryParams.value}`,
|
|
|
|
),
|
|
|
|
managePlanUrl: computed(
|
|
|
|
() =>
|
|
|
|
`${subscriptionAppUrl.value}/manage?token=${managementToken.value}&${commonSubscriptionAppUrlQueryParams.value}`,
|
|
|
|
),
|
|
|
|
isLoading: computed(() => state.loading),
|
|
|
|
telemetryPayload: computed<UsageTelemetry>(() => ({
|
|
|
|
instance_id: instanceId.value,
|
|
|
|
action: 'view_plans',
|
|
|
|
plan_name_current: planName.value,
|
|
|
|
usage: executionCount.value,
|
|
|
|
quota: executionLimit.value,
|
|
|
|
})),
|
2022-12-28 08:07:34 -08:00
|
|
|
isDesktop: computed(() => settingsStore.isDesktopDeployment),
|
2022-12-20 01:52:01 -08:00
|
|
|
};
|
|
|
|
});
|