-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wip - add support for detected_field/.../values
- Loading branch information
1 parent
0d5ed56
commit 7cc4a41
Showing
6 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { SceneObject } from '@grafana/scenes'; | ||
import { AdHocVariableFilter, MetricFindValue, ScopedVars, TimeRange } from '@grafana/data'; | ||
import { DataSourceWithBackend, getDataSourceSrv } from '@grafana/runtime'; | ||
import { getDataSource } from './scenes'; | ||
import { logger } from './logger'; | ||
import { LokiQuery } from './lokiQuery'; | ||
import { getValueFromFieldsFilter } from './variableGetters'; | ||
import { VAR_FIELDS, VAR_LEVELS, VAR_METADATA } from './variables'; | ||
|
||
//@todo export from scenes | ||
export interface AdHocFilterWithLabels extends AdHocVariableFilter { | ||
keyLabel?: string; | ||
valueLabels?: string[]; | ||
} | ||
|
||
type FetchDetectedLabelValuesOptions = { | ||
expr?: string; | ||
timeRange?: TimeRange; | ||
limit?: number; | ||
scopedVars?: ScopedVars; | ||
}; | ||
|
||
interface LokiLanguageProvider { | ||
fetchDetectedLabelValues: (labelName: string, options?: FetchDetectedLabelValuesOptions) => Promise<string[]>; | ||
} | ||
|
||
export const getDetectedFieldValuesTagValuesProvider = async ( | ||
filter: AdHocFilterWithLabels, | ||
expr: string, | ||
sceneRef: SceneObject, | ||
timeRange: TimeRange, | ||
variable: typeof VAR_FIELDS | typeof VAR_METADATA | typeof VAR_LEVELS | ||
): Promise<{ | ||
replace?: boolean; | ||
values: MetricFindValue[]; | ||
}> => { | ||
const datasource_ = await getDataSourceSrv().get(getDataSource(sceneRef)); | ||
if (!(datasource_ instanceof DataSourceWithBackend)) { | ||
logger.error(new Error('getTagValuesProvider: Invalid datasource!')); | ||
throw new Error('Invalid datasource!'); | ||
} | ||
const datasource = datasource_ as DataSourceWithBackend<LokiQuery>; | ||
|
||
const languageProvider = datasource.languageProvider as LokiLanguageProvider; | ||
|
||
if (languageProvider) { | ||
// Filter out other values for this key so users can include other values for this label | ||
const options: FetchDetectedLabelValuesOptions = { | ||
expr, | ||
limit: 1000, | ||
timeRange, | ||
}; | ||
|
||
let results = await languageProvider.fetchDetectedLabelValues(filter.key, options); | ||
// If the variable has a parser in the value, make sure we extract it and carry it over | ||
// @todo can the parser ever change? | ||
if (variable === VAR_FIELDS) { | ||
const valueDecoded = getValueFromFieldsFilter(filter, variable); | ||
return { | ||
replace: true, | ||
values: results.map((v) => ({ | ||
text: v, | ||
value: JSON.stringify({ | ||
value: v, | ||
parser: valueDecoded.parser, | ||
}), | ||
})), | ||
}; | ||
} | ||
|
||
return { replace: true, values: results.map((v) => ({ text: v })) }; | ||
} else { | ||
logger.error(new Error('getTagValuesProvider: missing or invalid languageProvider!')); | ||
return { replace: true, values: [] }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters