providers.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { eachRight, map, remove } from 'lodash';
  2. import { SelectableValue } from '@grafana/data';
  3. import { mapSegmentsToSelectables, mapStringsToSelectables } from '../components/helpers';
  4. import { GraphiteSegment, GraphiteTag, GraphiteTagOperator } from '../types';
  5. import {
  6. TAG_PREFIX,
  7. GRAPHITE_TAG_OPERATORS,
  8. handleMetricsAutoCompleteError,
  9. handleTagsAutoCompleteError,
  10. } from './helpers';
  11. import { GraphiteQueryEditorState } from './store';
  12. /**
  13. * All auto-complete lists are updated while typing. To avoid performance issues we do not render more
  14. * than MAX_SUGGESTIONS limits in a single dropdown.
  15. *
  16. * MAX_SUGGESTIONS is per metrics and tags separately. On the very first dropdown where metrics and tags are
  17. * combined together meaning it may end up with max of 2 * MAX_SUGGESTIONS items in total.
  18. */
  19. const MAX_SUGGESTIONS = 5000;
  20. /**
  21. * Providers are hooks for views to provide temporal data for autocomplete. They don't modify the state.
  22. */
  23. /**
  24. * Return list of available options for a segment with given index
  25. *
  26. * It may be:
  27. * - mixed list of metrics and tags (only when nothing was selected)
  28. * - list of metric names (if a metric name was selected for this segment)
  29. */
  30. async function getAltSegments(
  31. state: GraphiteQueryEditorState,
  32. index: number,
  33. prefix: string
  34. ): Promise<GraphiteSegment[]> {
  35. let query = prefix.length > 0 ? '*' + prefix + '*' : '*';
  36. if (index > 0) {
  37. query = state.queryModel.getSegmentPathUpTo(index) + '.' + query;
  38. }
  39. const options = {
  40. range: state.range,
  41. requestId: 'get-alt-segments',
  42. };
  43. try {
  44. const segments = await state.datasource.metricFindQuery(query, options);
  45. const altSegments: GraphiteSegment[] = map(segments, (segment) => {
  46. return {
  47. value: segment.text,
  48. expandable: segment.expandable,
  49. };
  50. });
  51. if (index > 0 && altSegments.length === 0) {
  52. return altSegments;
  53. }
  54. // add query references
  55. if (index === 0) {
  56. eachRight(state.queries, (target) => {
  57. if (target.refId === state.queryModel.target.refId) {
  58. return;
  59. }
  60. altSegments.unshift({
  61. type: 'series-ref',
  62. value: '#' + target.refId,
  63. expandable: false,
  64. });
  65. });
  66. }
  67. // add template variables
  68. eachRight(state.templateSrv.getVariables(), (variable) => {
  69. altSegments.unshift({
  70. type: 'template',
  71. value: '$' + variable.name,
  72. expandable: true,
  73. });
  74. });
  75. // add wildcard option and limit number of suggestions (API doesn't support limiting
  76. // hence we are doing it here)
  77. altSegments.unshift({ value: '*', expandable: true });
  78. altSegments.splice(MAX_SUGGESTIONS);
  79. if (state.supportsTags && index === 0) {
  80. removeTaggedEntry(altSegments);
  81. return await addAltTagSegments(state, prefix, altSegments);
  82. } else {
  83. return altSegments;
  84. }
  85. } catch (err) {
  86. handleMetricsAutoCompleteError(state, err);
  87. }
  88. return [];
  89. }
  90. /**
  91. * Get the list of segments with tags and metrics. Suggestions are reduced in getAltSegments and addAltTagSegments so in case
  92. * we hit MAX_SUGGESTIONS limit there are always some tags and metrics shown.
  93. */
  94. export async function getAltSegmentsSelectables(
  95. state: GraphiteQueryEditorState,
  96. index: number,
  97. prefix: string
  98. ): Promise<Array<SelectableValue<GraphiteSegment>>> {
  99. return mapSegmentsToSelectables(await getAltSegments(state, index, prefix));
  100. }
  101. export function getTagOperatorsSelectables(): Array<SelectableValue<GraphiteTagOperator>> {
  102. return mapStringsToSelectables(GRAPHITE_TAG_OPERATORS);
  103. }
  104. /**
  105. * Returns tags as dropdown options
  106. */
  107. async function getTags(state: GraphiteQueryEditorState, index: number, tagPrefix: string): Promise<string[]> {
  108. try {
  109. const tagExpressions = state.queryModel.renderTagExpressions(index);
  110. const values = await state.datasource.getTagsAutoComplete(tagExpressions, tagPrefix, {
  111. range: state.range,
  112. limit: MAX_SUGGESTIONS,
  113. });
  114. const altTags = map(values, 'text');
  115. altTags.splice(0, 0, state.removeTagValue);
  116. return altTags;
  117. } catch (err) {
  118. handleTagsAutoCompleteError(state, err);
  119. }
  120. return [];
  121. }
  122. export async function getTagsSelectables(
  123. state: GraphiteQueryEditorState,
  124. index: number,
  125. tagPrefix: string
  126. ): Promise<Array<SelectableValue<string>>> {
  127. return mapStringsToSelectables(await getTags(state, index, tagPrefix));
  128. }
  129. /**
  130. * List of tags when a tag is added. getTags is used for editing.
  131. * When adding - segment is used. When editing - dropdown is used.
  132. */
  133. async function getTagsAsSegments(state: GraphiteQueryEditorState, tagPrefix: string): Promise<GraphiteSegment[]> {
  134. let tagsAsSegments: GraphiteSegment[];
  135. try {
  136. const tagExpressions = state.queryModel.renderTagExpressions();
  137. const values = await state.datasource.getTagsAutoComplete(tagExpressions, tagPrefix, {
  138. range: state.range,
  139. limit: MAX_SUGGESTIONS,
  140. });
  141. tagsAsSegments = map(values, (val) => {
  142. return {
  143. value: val.text,
  144. type: 'tag',
  145. expandable: false,
  146. };
  147. });
  148. } catch (err) {
  149. tagsAsSegments = [];
  150. handleTagsAutoCompleteError(state, err);
  151. }
  152. return tagsAsSegments;
  153. }
  154. /**
  155. * Get list of tags, used when adding additional tags (first tag is selected from a joined list of metrics and tags)
  156. */
  157. export async function getTagsAsSegmentsSelectables(
  158. state: GraphiteQueryEditorState,
  159. tagPrefix: string
  160. ): Promise<Array<SelectableValue<GraphiteSegment>>> {
  161. return mapSegmentsToSelectables(await getTagsAsSegments(state, tagPrefix));
  162. }
  163. async function getTagValues(
  164. state: GraphiteQueryEditorState,
  165. tag: GraphiteTag,
  166. index: number,
  167. valuePrefix: string
  168. ): Promise<string[]> {
  169. const tagExpressions = state.queryModel.renderTagExpressions(index);
  170. const tagKey = tag.key;
  171. const values = await state.datasource.getTagValuesAutoComplete(tagExpressions, tagKey, valuePrefix, {
  172. limit: MAX_SUGGESTIONS,
  173. });
  174. const altValues = map(values, 'text');
  175. // Add template variables as additional values
  176. eachRight(state.templateSrv.getVariables(), (variable) => {
  177. altValues.push('${' + variable.name + ':regex}');
  178. });
  179. return altValues;
  180. }
  181. export async function getTagValuesSelectables(
  182. state: GraphiteQueryEditorState,
  183. tag: GraphiteTag,
  184. index: number,
  185. valuePrefix: string
  186. ): Promise<Array<SelectableValue<string>>> {
  187. return mapStringsToSelectables(await getTagValues(state, tag, index, valuePrefix));
  188. }
  189. /**
  190. * Add segments with tags prefixed with "tag: " to include them in the same list as metrics
  191. */
  192. async function addAltTagSegments(
  193. state: GraphiteQueryEditorState,
  194. prefix: string,
  195. altSegments: GraphiteSegment[]
  196. ): Promise<GraphiteSegment[]> {
  197. let tagSegments = await getTagsAsSegments(state, prefix);
  198. tagSegments = map(tagSegments, (segment) => {
  199. segment.value = TAG_PREFIX + segment.value;
  200. return segment;
  201. });
  202. return altSegments.concat(...tagSegments);
  203. }
  204. function removeTaggedEntry(altSegments: GraphiteSegment[]) {
  205. remove(altSegments, (s) => s.value === '_tagged');
  206. }