123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- import { eachRight, map, remove } from 'lodash';
- import { SelectableValue } from '@grafana/data';
- import { mapSegmentsToSelectables, mapStringsToSelectables } from '../components/helpers';
- import { GraphiteSegment, GraphiteTag, GraphiteTagOperator } from '../types';
- import {
- TAG_PREFIX,
- GRAPHITE_TAG_OPERATORS,
- handleMetricsAutoCompleteError,
- handleTagsAutoCompleteError,
- } from './helpers';
- import { GraphiteQueryEditorState } from './store';
- /**
- * All auto-complete lists are updated while typing. To avoid performance issues we do not render more
- * than MAX_SUGGESTIONS limits in a single dropdown.
- *
- * MAX_SUGGESTIONS is per metrics and tags separately. On the very first dropdown where metrics and tags are
- * combined together meaning it may end up with max of 2 * MAX_SUGGESTIONS items in total.
- */
- const MAX_SUGGESTIONS = 5000;
- /**
- * Providers are hooks for views to provide temporal data for autocomplete. They don't modify the state.
- */
- /**
- * Return list of available options for a segment with given index
- *
- * It may be:
- * - mixed list of metrics and tags (only when nothing was selected)
- * - list of metric names (if a metric name was selected for this segment)
- */
- async function getAltSegments(
- state: GraphiteQueryEditorState,
- index: number,
- prefix: string
- ): Promise<GraphiteSegment[]> {
- let query = prefix.length > 0 ? '*' + prefix + '*' : '*';
- if (index > 0) {
- query = state.queryModel.getSegmentPathUpTo(index) + '.' + query;
- }
- const options = {
- range: state.range,
- requestId: 'get-alt-segments',
- };
- try {
- const segments = await state.datasource.metricFindQuery(query, options);
- const altSegments: GraphiteSegment[] = map(segments, (segment) => {
- return {
- value: segment.text,
- expandable: segment.expandable,
- };
- });
- if (index > 0 && altSegments.length === 0) {
- return altSegments;
- }
- // add query references
- if (index === 0) {
- eachRight(state.queries, (target) => {
- if (target.refId === state.queryModel.target.refId) {
- return;
- }
- altSegments.unshift({
- type: 'series-ref',
- value: '#' + target.refId,
- expandable: false,
- });
- });
- }
- // add template variables
- eachRight(state.templateSrv.getVariables(), (variable) => {
- altSegments.unshift({
- type: 'template',
- value: '$' + variable.name,
- expandable: true,
- });
- });
- // add wildcard option and limit number of suggestions (API doesn't support limiting
- // hence we are doing it here)
- altSegments.unshift({ value: '*', expandable: true });
- altSegments.splice(MAX_SUGGESTIONS);
- if (state.supportsTags && index === 0) {
- removeTaggedEntry(altSegments);
- return await addAltTagSegments(state, prefix, altSegments);
- } else {
- return altSegments;
- }
- } catch (err) {
- handleMetricsAutoCompleteError(state, err);
- }
- return [];
- }
- /**
- * Get the list of segments with tags and metrics. Suggestions are reduced in getAltSegments and addAltTagSegments so in case
- * we hit MAX_SUGGESTIONS limit there are always some tags and metrics shown.
- */
- export async function getAltSegmentsSelectables(
- state: GraphiteQueryEditorState,
- index: number,
- prefix: string
- ): Promise<Array<SelectableValue<GraphiteSegment>>> {
- return mapSegmentsToSelectables(await getAltSegments(state, index, prefix));
- }
- export function getTagOperatorsSelectables(): Array<SelectableValue<GraphiteTagOperator>> {
- return mapStringsToSelectables(GRAPHITE_TAG_OPERATORS);
- }
- /**
- * Returns tags as dropdown options
- */
- async function getTags(state: GraphiteQueryEditorState, index: number, tagPrefix: string): Promise<string[]> {
- try {
- const tagExpressions = state.queryModel.renderTagExpressions(index);
- const values = await state.datasource.getTagsAutoComplete(tagExpressions, tagPrefix, {
- range: state.range,
- limit: MAX_SUGGESTIONS,
- });
- const altTags = map(values, 'text');
- altTags.splice(0, 0, state.removeTagValue);
- return altTags;
- } catch (err) {
- handleTagsAutoCompleteError(state, err);
- }
- return [];
- }
- export async function getTagsSelectables(
- state: GraphiteQueryEditorState,
- index: number,
- tagPrefix: string
- ): Promise<Array<SelectableValue<string>>> {
- return mapStringsToSelectables(await getTags(state, index, tagPrefix));
- }
- /**
- * List of tags when a tag is added. getTags is used for editing.
- * When adding - segment is used. When editing - dropdown is used.
- */
- async function getTagsAsSegments(state: GraphiteQueryEditorState, tagPrefix: string): Promise<GraphiteSegment[]> {
- let tagsAsSegments: GraphiteSegment[];
- try {
- const tagExpressions = state.queryModel.renderTagExpressions();
- const values = await state.datasource.getTagsAutoComplete(tagExpressions, tagPrefix, {
- range: state.range,
- limit: MAX_SUGGESTIONS,
- });
- tagsAsSegments = map(values, (val) => {
- return {
- value: val.text,
- type: 'tag',
- expandable: false,
- };
- });
- } catch (err) {
- tagsAsSegments = [];
- handleTagsAutoCompleteError(state, err);
- }
- return tagsAsSegments;
- }
- /**
- * Get list of tags, used when adding additional tags (first tag is selected from a joined list of metrics and tags)
- */
- export async function getTagsAsSegmentsSelectables(
- state: GraphiteQueryEditorState,
- tagPrefix: string
- ): Promise<Array<SelectableValue<GraphiteSegment>>> {
- return mapSegmentsToSelectables(await getTagsAsSegments(state, tagPrefix));
- }
- async function getTagValues(
- state: GraphiteQueryEditorState,
- tag: GraphiteTag,
- index: number,
- valuePrefix: string
- ): Promise<string[]> {
- const tagExpressions = state.queryModel.renderTagExpressions(index);
- const tagKey = tag.key;
- const values = await state.datasource.getTagValuesAutoComplete(tagExpressions, tagKey, valuePrefix, {
- limit: MAX_SUGGESTIONS,
- });
- const altValues = map(values, 'text');
- // Add template variables as additional values
- eachRight(state.templateSrv.getVariables(), (variable) => {
- altValues.push('${' + variable.name + ':regex}');
- });
- return altValues;
- }
- export async function getTagValuesSelectables(
- state: GraphiteQueryEditorState,
- tag: GraphiteTag,
- index: number,
- valuePrefix: string
- ): Promise<Array<SelectableValue<string>>> {
- return mapStringsToSelectables(await getTagValues(state, tag, index, valuePrefix));
- }
- /**
- * Add segments with tags prefixed with "tag: " to include them in the same list as metrics
- */
- async function addAltTagSegments(
- state: GraphiteQueryEditorState,
- prefix: string,
- altSegments: GraphiteSegment[]
- ): Promise<GraphiteSegment[]> {
- let tagSegments = await getTagsAsSegments(state, prefix);
- tagSegments = map(tagSegments, (segment) => {
- segment.value = TAG_PREFIX + segment.value;
- return segment;
- });
- return altSegments.concat(...tagSegments);
- }
- function removeTaggedEntry(altSegments: GraphiteSegment[]) {
- remove(altSegments, (s) => s.value === '_tagged');
- }
|