import { dateTime } from '@grafana/data'; import { getBackendSrv } from '@grafana/runtime'; export const DAILY_SUMMARY_DATE_FORMAT = 'YYYY-MM-DD'; export interface RecentUser { id: number; name?: string; avatarUrl: string; login: string; email?: string; hasCustomAvatar?: boolean; } export interface UserViewDTO { user: RecentUser; viewed: string; } export interface DashboardUsersInfoDTO { creator?: UserViewDTO; lastEditor?: UserViewDTO; } export interface DashboardDailySummaryDTO { day: string; dashboardId: number; views: number; queries: number; errors: number; loadDuration: number; } export interface DataSourceDailySummaryDTO { day: string; dataSourceId: number; views: number; queries: number; errors: number; loadDuration: number; } export const getRecentUsers = async (dashboardId: number, limit: number): Promise => { return await getBackendSrv().get(`/api/usage/dashboard/${dashboardId}/views/recent?limit=${limit}`); }; export const getUserViews = async (dashboardId: number, limit: number): Promise => { return await getBackendSrv().get(`/api/usage/dashboard/${dashboardId}/views?limit=${limit}`); }; const formatSummaries = (apiSummaries: any): T[] => { const summariesArray: T[] = []; for (let day in apiSummaries) { summariesArray.push(apiSummaries[day]); } summariesArray.sort( (a, b) => dateTime(a.day, DAILY_SUMMARY_DATE_FORMAT).valueOf() - dateTime(b.day, DAILY_SUMMARY_DATE_FORMAT).valueOf() ); return summariesArray; }; export const getDashboardDailySummaries = async ( dashboardId: number, days: string[] ): Promise => { const dailySummaries = await getBackendSrv().get(`/api/usage/dashboard/${dashboardId}/daily`, { days, }); return formatSummaries(dailySummaries); }; export const getDataSourceDailySummaries = async ( dataSourceUid: string, from: string, to: string ): Promise => { const dailySummaries = await getBackendSrv().get(`/api/usage/datasource/${dataSourceUid}/daily`, { from, to, }); return formatSummaries(dailySummaries); }; export const getDashboardUsersInfo = async (dashboardId: number): Promise => { return await getBackendSrv().get(`/api/usage/dashboard/${dashboardId}/info`); };