api.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { dateTime } from '@grafana/data';
  2. import { getBackendSrv } from '@grafana/runtime';
  3. export const DAILY_SUMMARY_DATE_FORMAT = 'YYYY-MM-DD';
  4. export interface RecentUser {
  5. id: number;
  6. name?: string;
  7. avatarUrl: string;
  8. login: string;
  9. email?: string;
  10. hasCustomAvatar?: boolean;
  11. }
  12. export interface UserViewDTO {
  13. user: RecentUser;
  14. viewed: string;
  15. }
  16. export interface DashboardUsersInfoDTO {
  17. creator?: UserViewDTO;
  18. lastEditor?: UserViewDTO;
  19. }
  20. export interface DashboardDailySummaryDTO {
  21. day: string;
  22. dashboardId: number;
  23. views: number;
  24. queries: number;
  25. errors: number;
  26. loadDuration: number;
  27. }
  28. export interface DataSourceDailySummaryDTO {
  29. day: string;
  30. dataSourceId: number;
  31. views: number;
  32. queries: number;
  33. errors: number;
  34. loadDuration: number;
  35. }
  36. export const getRecentUsers = async (dashboardId: number, limit: number): Promise<UserViewDTO[]> => {
  37. return await getBackendSrv().get(`/api/usage/dashboard/${dashboardId}/views/recent?limit=${limit}`);
  38. };
  39. export const getUserViews = async (dashboardId: number, limit: number): Promise<UserViewDTO[]> => {
  40. return await getBackendSrv().get(`/api/usage/dashboard/${dashboardId}/views?limit=${limit}`);
  41. };
  42. const formatSummaries = <T extends DashboardDailySummaryDTO | DataSourceDailySummaryDTO>(apiSummaries: any): T[] => {
  43. const summariesArray: T[] = [];
  44. for (let day in apiSummaries) {
  45. summariesArray.push(apiSummaries[day]);
  46. }
  47. summariesArray.sort(
  48. (a, b) =>
  49. dateTime(a.day, DAILY_SUMMARY_DATE_FORMAT).valueOf() - dateTime(b.day, DAILY_SUMMARY_DATE_FORMAT).valueOf()
  50. );
  51. return summariesArray;
  52. };
  53. export const getDashboardDailySummaries = async (
  54. dashboardId: number,
  55. days: string[]
  56. ): Promise<DashboardDailySummaryDTO[]> => {
  57. const dailySummaries = await getBackendSrv().get(`/api/usage/dashboard/${dashboardId}/daily`, {
  58. days,
  59. });
  60. return formatSummaries<DashboardDailySummaryDTO>(dailySummaries);
  61. };
  62. export const getDataSourceDailySummaries = async (
  63. dataSourceUid: string,
  64. from: string,
  65. to: string
  66. ): Promise<DataSourceDailySummaryDTO[]> => {
  67. const dailySummaries = await getBackendSrv().get(`/api/usage/datasource/${dataSourceUid}/daily`, {
  68. from,
  69. to,
  70. });
  71. return formatSummaries<DataSourceDailySummaryDTO>(dailySummaries);
  72. };
  73. export const getDashboardUsersInfo = async (dashboardId: number): Promise<DashboardUsersInfoDTO> => {
  74. return await getBackendSrv().get(`/api/usage/dashboard/${dashboardId}/info`);
  75. };