utils.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { DataFrame, PanelData, Field, getFieldDisplayName, ReducerID } from '@grafana/data';
  2. import { config } from '@grafana/runtime';
  3. import {
  4. getColorDimension,
  5. getScaledDimension,
  6. getTextDimension,
  7. getResourceDimension,
  8. ColorDimensionConfig,
  9. DimensionSupplier,
  10. ResourceDimensionConfig,
  11. ScaleDimensionConfig,
  12. TextDimensionConfig,
  13. ScalarDimensionConfig,
  14. } from 'app/features/dimensions';
  15. import { getScalarDimension } from './scalar';
  16. export function getColorDimensionFromData(
  17. data: PanelData | undefined,
  18. cfg: ColorDimensionConfig
  19. ): DimensionSupplier<string> {
  20. if (data?.series && cfg.field) {
  21. for (const frame of data.series) {
  22. const d = getColorDimension(frame, cfg, config.theme2);
  23. if (!d.isAssumed || data.series.length === 1) {
  24. return d;
  25. }
  26. }
  27. }
  28. return getColorDimension(undefined, cfg, config.theme2);
  29. }
  30. export function getScaleDimensionFromData(
  31. data: PanelData | undefined,
  32. cfg: ScaleDimensionConfig
  33. ): DimensionSupplier<number> {
  34. if (data?.series && cfg.field) {
  35. for (const frame of data.series) {
  36. const d = getScaledDimension(frame, cfg);
  37. if (!d.isAssumed || data.series.length === 1) {
  38. return d;
  39. }
  40. }
  41. }
  42. return getScaledDimension(undefined, cfg);
  43. }
  44. export function getScalarDimensionFromData(
  45. data: PanelData | undefined,
  46. cfg: ScalarDimensionConfig
  47. ): DimensionSupplier<number> {
  48. if (data?.series && cfg.field) {
  49. for (const frame of data.series) {
  50. const d = getScalarDimension(frame, cfg);
  51. if (!d.isAssumed || data.series.length === 1) {
  52. return d;
  53. }
  54. }
  55. }
  56. return getScalarDimension(undefined, cfg);
  57. }
  58. export function getResourceDimensionFromData(
  59. data: PanelData | undefined,
  60. cfg: ResourceDimensionConfig
  61. ): DimensionSupplier<string> {
  62. if (data?.series && cfg.field) {
  63. for (const frame of data.series) {
  64. const d = getResourceDimension(frame, cfg);
  65. if (!d.isAssumed || data.series.length === 1) {
  66. return d;
  67. }
  68. }
  69. }
  70. return getResourceDimension(undefined, cfg);
  71. }
  72. export function getTextDimensionFromData(
  73. data: PanelData | undefined,
  74. cfg: TextDimensionConfig
  75. ): DimensionSupplier<string> {
  76. if (data?.series && cfg.field) {
  77. for (const frame of data.series) {
  78. const d = getTextDimension(frame, cfg);
  79. if (!d.isAssumed || data.series.length === 1) {
  80. return d;
  81. }
  82. }
  83. }
  84. return getTextDimension(undefined, cfg);
  85. }
  86. export function findField(frame?: DataFrame, name?: string): Field | undefined {
  87. if (!frame || !name?.length) {
  88. return undefined;
  89. }
  90. for (const field of frame.fields) {
  91. if (name === field.name) {
  92. return field;
  93. }
  94. const disp = getFieldDisplayName(field, frame);
  95. if (name === disp) {
  96. return field;
  97. }
  98. }
  99. return undefined;
  100. }
  101. export function findFieldIndex(frame?: DataFrame, name?: string): number | undefined {
  102. if (!frame || !name?.length) {
  103. return undefined;
  104. }
  105. for (let i = 0; i < frame.fields.length; i++) {
  106. const field = frame.fields[i];
  107. if (name === field.name) {
  108. return i;
  109. }
  110. const disp = getFieldDisplayName(field, frame);
  111. if (name === disp) {
  112. return i;
  113. }
  114. }
  115. return undefined;
  116. }
  117. export function getLastNotNullFieldValue<T>(field: Field): T {
  118. const calcs = field.state?.calcs;
  119. if (calcs) {
  120. const v = calcs[ReducerID.lastNotNull];
  121. if (v != null) {
  122. return v as T;
  123. }
  124. }
  125. const data = field.values;
  126. let idx = data.length - 1;
  127. while (idx >= 0) {
  128. const v = data.get(idx--);
  129. if (v != null) {
  130. return v;
  131. }
  132. }
  133. return undefined as any;
  134. }