utils.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { dateTime, locationUtil, PanelModel, TimeRange, urlUtil } from '@grafana/data';
  2. import { config } from '@grafana/runtime';
  3. import { createShortLink } from 'app/core/utils/shortLinks';
  4. import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
  5. export interface BuildParamsArgs {
  6. useCurrentTimeRange: boolean;
  7. selectedTheme?: string;
  8. panel?: PanelModel;
  9. search?: string;
  10. range?: TimeRange;
  11. orgId?: number;
  12. }
  13. export function buildParams({
  14. useCurrentTimeRange,
  15. selectedTheme,
  16. panel,
  17. search = window.location.search,
  18. range = getTimeSrv().timeRange(),
  19. orgId = config.bootData.user.orgId,
  20. }: BuildParamsArgs): URLSearchParams {
  21. const searchParams = new URLSearchParams(search);
  22. searchParams.set('from', String(range.from.valueOf()));
  23. searchParams.set('to', String(range.to.valueOf()));
  24. searchParams.set('orgId', String(orgId));
  25. if (!useCurrentTimeRange) {
  26. searchParams.delete('from');
  27. searchParams.delete('to');
  28. }
  29. if (selectedTheme !== 'current') {
  30. searchParams.set('theme', selectedTheme!);
  31. }
  32. if (panel && !searchParams.has('editPanel')) {
  33. searchParams.set('viewPanel', String(panel.id));
  34. }
  35. return searchParams;
  36. }
  37. export function buildBaseUrl() {
  38. let baseUrl = window.location.href;
  39. const queryStart = baseUrl.indexOf('?');
  40. if (queryStart !== -1) {
  41. baseUrl = baseUrl.substring(0, queryStart);
  42. }
  43. return baseUrl;
  44. }
  45. export async function buildShareUrl(
  46. useCurrentTimeRange: boolean,
  47. selectedTheme?: string,
  48. panel?: PanelModel,
  49. shortenUrl?: boolean
  50. ) {
  51. const baseUrl = buildBaseUrl();
  52. const params = buildParams({ useCurrentTimeRange, selectedTheme, panel });
  53. const shareUrl = urlUtil.appendQueryToUrl(baseUrl, params.toString());
  54. if (shortenUrl) {
  55. return await createShortLink(shareUrl);
  56. }
  57. return shareUrl;
  58. }
  59. export function buildSoloUrl(
  60. useCurrentTimeRange: boolean,
  61. dashboardUid: string,
  62. selectedTheme?: string,
  63. panel?: PanelModel
  64. ) {
  65. const baseUrl = buildBaseUrl();
  66. const params = buildParams({ useCurrentTimeRange, selectedTheme, panel });
  67. let soloUrl = baseUrl.replace(config.appSubUrl + '/dashboard/', config.appSubUrl + '/dashboard-solo/');
  68. soloUrl = soloUrl.replace(config.appSubUrl + '/d/', config.appSubUrl + '/d-solo/');
  69. // For handling the case when default_home_dashboard_path is set in the grafana config
  70. const strippedUrl = locationUtil.stripBaseFromUrl(baseUrl);
  71. if (strippedUrl === '/') {
  72. soloUrl = `${config.appUrl}d-solo/${dashboardUid}`;
  73. }
  74. const panelId = params.get('editPanel') ?? params.get('viewPanel') ?? '';
  75. params.set('panelId', panelId);
  76. params.delete('editPanel');
  77. params.delete('viewPanel');
  78. return urlUtil.appendQueryToUrl(soloUrl, params.toString());
  79. }
  80. export function buildImageUrl(
  81. useCurrentTimeRange: boolean,
  82. dashboardUid: string,
  83. selectedTheme?: string,
  84. panel?: PanelModel
  85. ) {
  86. let soloUrl = buildSoloUrl(useCurrentTimeRange, dashboardUid, selectedTheme, panel);
  87. let imageUrl = soloUrl.replace(config.appSubUrl + '/dashboard-solo/', config.appSubUrl + '/render/dashboard-solo/');
  88. imageUrl = imageUrl.replace(config.appSubUrl + '/d-solo/', config.appSubUrl + '/render/d-solo/');
  89. imageUrl += '&width=1000&height=500' + getLocalTimeZone();
  90. return imageUrl;
  91. }
  92. export function buildIframeHtml(
  93. useCurrentTimeRange: boolean,
  94. dashboardUid: string,
  95. selectedTheme?: string,
  96. panel?: PanelModel
  97. ) {
  98. let soloUrl = buildSoloUrl(useCurrentTimeRange, dashboardUid, selectedTheme, panel);
  99. return '<iframe src="' + soloUrl + '" width="450" height="200" frameborder="0"></iframe>';
  100. }
  101. export function getLocalTimeZone() {
  102. const utcOffset = '&tz=UTC' + encodeURIComponent(dateTime().format('Z'));
  103. // Older browser does not the internationalization API
  104. if (!(window as any).Intl) {
  105. return utcOffset;
  106. }
  107. const dateFormat = (window as any).Intl.DateTimeFormat();
  108. if (!dateFormat.resolvedOptions) {
  109. return utcOffset;
  110. }
  111. const options = dateFormat.resolvedOptions();
  112. if (!options.timeZone) {
  113. return utcOffset;
  114. }
  115. return '&tz=' + encodeURIComponent(options.timeZone);
  116. }