linkSuppliers.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {
  2. DataLink,
  3. DisplayValue,
  4. FieldDisplay,
  5. formattedValueToString,
  6. getFieldDisplayValuesProxy,
  7. getTimeField,
  8. InterpolateFunction,
  9. Labels,
  10. LinkModelSupplier,
  11. ScopedVar,
  12. ScopedVars,
  13. } from '@grafana/data';
  14. import { PanelModel } from 'app/features/dashboard/state/PanelModel';
  15. import { getLinkSrv } from './link_srv';
  16. interface SeriesVars {
  17. name?: string;
  18. refId?: string;
  19. }
  20. interface FieldVars {
  21. name: string;
  22. labels?: Labels;
  23. }
  24. interface ValueVars {
  25. raw: any;
  26. numeric: number;
  27. text: string;
  28. time?: number;
  29. calc?: string;
  30. }
  31. interface DataViewVars {
  32. name?: string;
  33. refId?: string;
  34. fields?: Record<string, DisplayValue>;
  35. }
  36. interface DataLinkScopedVars extends ScopedVars {
  37. __series: ScopedVar<SeriesVars>;
  38. __field: ScopedVar<FieldVars>;
  39. __value: ScopedVar<ValueVars>;
  40. __data: ScopedVar<DataViewVars>;
  41. }
  42. /**
  43. * Link suppliers creates link models based on a link origin
  44. */
  45. export const getFieldLinksSupplier = (value: FieldDisplay): LinkModelSupplier<FieldDisplay> | undefined => {
  46. const links = value.field.links;
  47. if (!links || links.length === 0) {
  48. return undefined;
  49. }
  50. return {
  51. getLinks: (replaceVariables: InterpolateFunction) => {
  52. const scopedVars: Partial<DataLinkScopedVars> = {};
  53. if (value.view) {
  54. const { dataFrame } = value.view;
  55. scopedVars['__series'] = {
  56. value: {
  57. name: dataFrame.name,
  58. refId: dataFrame.refId,
  59. },
  60. text: 'Series',
  61. };
  62. const field = value.colIndex !== undefined ? dataFrame.fields[value.colIndex] : undefined;
  63. if (field) {
  64. scopedVars['__field'] = {
  65. value: {
  66. name: field.name,
  67. labels: field.labels,
  68. },
  69. text: 'Field',
  70. };
  71. if (value.rowIndex !== undefined && value.rowIndex >= 0) {
  72. const { timeField } = getTimeField(dataFrame);
  73. scopedVars['__value'] = {
  74. value: {
  75. raw: field.values.get(value.rowIndex),
  76. numeric: value.display.numeric,
  77. text: formattedValueToString(value.display),
  78. time: timeField ? timeField.values.get(value.rowIndex) : undefined,
  79. },
  80. text: 'Value',
  81. };
  82. }
  83. // Expose other values on the row
  84. if (value.view) {
  85. scopedVars['__data'] = {
  86. value: {
  87. name: dataFrame.name,
  88. refId: dataFrame.refId,
  89. fields: getFieldDisplayValuesProxy({
  90. frame: dataFrame,
  91. rowIndex: value.rowIndex!,
  92. }),
  93. },
  94. text: 'Data',
  95. };
  96. }
  97. } else {
  98. // calculation
  99. scopedVars['__value'] = {
  100. value: {
  101. raw: value.display.numeric,
  102. numeric: value.display.numeric,
  103. text: formattedValueToString(value.display),
  104. calc: value.name,
  105. },
  106. text: 'Value',
  107. };
  108. }
  109. } else {
  110. console.log('VALUE', value);
  111. }
  112. const replace: InterpolateFunction = (value: string, vars: ScopedVars | undefined, fmt?: string | Function) => {
  113. const finalVars: ScopedVars = {
  114. ...(scopedVars as ScopedVars),
  115. ...vars,
  116. };
  117. return replaceVariables(value, finalVars, fmt);
  118. };
  119. return links.map((link: DataLink) => {
  120. return getLinkSrv().getDataLinkUIModel(link, replace, value);
  121. });
  122. },
  123. };
  124. };
  125. export const getPanelLinksSupplier = (panel: PanelModel): LinkModelSupplier<PanelModel> | undefined => {
  126. const links = panel.links;
  127. if (!links || links.length === 0) {
  128. return undefined;
  129. }
  130. return {
  131. getLinks: () => {
  132. return links.map((link) => {
  133. return getLinkSrv().getDataLinkUIModel(link, panel.replaceVariables, panel);
  134. });
  135. },
  136. };
  137. };