import type { BusterMetric, BusterMetricData, BusterMetricListItem } from '@/api/asset_interfaces/metric'; import type { ChartConfigProps } from '@buster/server-shared/metrics'; import type { VerificationStatus } from '@/api/asset_interfaces/share'; import type { ShareDeleteRequest, SharePostRequest, ShareUpdateRequest } from '@/api/asset_interfaces/shared_interfaces'; import { serverFetch } from '@/api/createServerInstance'; import { mainApi } from '../instances'; export const getMetric = async ({ id, password, version_number }: { id: string; password?: string; version_number?: number; //api will default to latest if not provided }) => { return mainApi .get(`/metrics/${id}`, { params: { password, version_number } }) .then((res) => res.data); }; export const getMetric_server = async ({ id, password }: Parameters[0]) => { return await serverFetch(`/metrics/${id}`, { params: { ...(password && { password }) } }); }; export const getMetricData = async ({ id, version_number, password }: { id: string; version_number?: number; password?: string; }) => { return mainApi .get(`/metrics/${id}/data`, { params: { password, version_number } }) .then((res) => res.data); }; export const listMetrics = async (params: { /** The token representing the current page number for pagination */ page_token: number; /** The number of items to return per page */ page_size: number; /** Filtering options for metrics based on verification status */ status?: VerificationStatus[] | null; }) => { return mainApi.get('/metrics', { params }).then((res) => res.data); }; export const listMetrics_server = async (params: Parameters[0]) => { return await serverFetch('/metrics', { params }); }; export const updateMetric = async (params: { /** The unique identifier of the metric to update */ id: string; /** New title for the metric */ name?: string; /** SQL query associated with the metric */ sql?: string; /** chart_config to update */ chart_config?: ChartConfigProps; /** file in yaml format to update */ file?: string; /** update the version number of the metric - default is true */ update_version?: boolean; /** restore the metric to a specific version */ restore_to_version?: number; }) => { return mainApi.put(`/metrics/${params.id}`, params).then((res) => res.data); }; export const deleteMetrics = async (params: { ids: string[] }) => { return mainApi .delete('/metrics', { data: { ids: params.ids } }) .then((res) => res.data); }; export const duplicateMetric = async (params: { id: string; message_id: string; share_with_same_people: boolean; }) => { return mainApi.post('/metrics/duplicate', params).then((res) => res.data); }; export const bulkUpdateMetricVerificationStatus = async ( params: { id: string; status: VerificationStatus; }[] ) => { return mainApi .put<{ failed_updates: []; failure_count: 0; success_count: 0; total_processed: 0; updated_metrics: BusterMetric[]; }>('/metrics', params) .then((res) => res.data); }; // share metrics export const shareMetric = async ({ id, params }: { id: string; params: SharePostRequest }) => { return mainApi.post(`/metrics/${id}/sharing`, params).then((res) => res.data); }; export const unshareMetric = async ({ id, data }: { id: string; data: ShareDeleteRequest }) => { return mainApi.delete(`/metrics/${id}/sharing`, { data }).then((res) => res.data); }; export const updateMetricShare = async ({ params, id }: { id: string; params: ShareUpdateRequest; }) => { return mainApi.put(`/metrics/${id}/sharing`, params).then((res) => res.data); };