resource.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { DataFrame } from '@grafana/data';
  2. import { DimensionSupplier, ResourceDimensionConfig, ResourceDimensionMode } from './types';
  3. import { findField, getLastNotNullFieldValue } from './utils';
  4. //---------------------------------------------------------
  5. // Resource dimension
  6. //---------------------------------------------------------
  7. export function getPublicOrAbsoluteUrl(v: string): string {
  8. if (!v) {
  9. return '';
  10. }
  11. return v.indexOf(':/') > 0 ? v : (window as any).__grafana_public_path__ + v;
  12. }
  13. export function getResourceDimension(
  14. frame: DataFrame | undefined,
  15. config: ResourceDimensionConfig
  16. ): DimensionSupplier<string> {
  17. const mode = config.mode ?? ResourceDimensionMode.Fixed;
  18. if (mode === ResourceDimensionMode.Fixed) {
  19. const v = getPublicOrAbsoluteUrl(config.fixed!);
  20. return {
  21. isAssumed: !Boolean(v),
  22. fixed: v,
  23. value: () => v,
  24. get: (i) => v,
  25. };
  26. }
  27. const field = findField(frame, config.field);
  28. if (!field) {
  29. const v = '';
  30. return {
  31. isAssumed: true,
  32. fixed: v,
  33. value: () => v,
  34. get: (i) => v,
  35. };
  36. }
  37. if (mode === ResourceDimensionMode.Mapping) {
  38. const mapper = (v: any) => getPublicOrAbsoluteUrl(`${v}`);
  39. return {
  40. field,
  41. get: (i) => mapper(field.values.get(i)),
  42. value: () => mapper(getLastNotNullFieldValue(field)),
  43. };
  44. }
  45. const getIcon = (value: any): string => {
  46. const disp = field.display!;
  47. return getPublicOrAbsoluteUrl(disp(value).icon ?? '');
  48. };
  49. return {
  50. field,
  51. get: (index: number): string => getIcon(field.values.get(index)),
  52. value: () => getIcon(getLastNotNullFieldValue(field)),
  53. };
  54. }