mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* InfluxDB: Send retention policy with InfluQL queries if it's been specified. In InfluxDB v2, due to technical limitations of the InfluxDB v1 compatibility layer, retention policies in a query (e.g. "<retention policy>.<measurement_name>") aren't honored and must be specified in the URL query parameter `rp` to be applied. Grafana doesn't send this query parameter which results in all queries resolving to the default retention policy when querying InfluxDB v2 servers using InfluxQL. This addresses the issue by sending the `rp` query parameter for queries that have specified a retention policy in the `target` given to `runExploreQuery`. The outcomes are: 1. InfluxQL queries executed against InfluxDB v2 databases will have the necessary retention policy information for queries like `SHOW FIELD KEYS FROM measurement` to function correctly. 2. InfluxQL queries executed against InfluxDB v1 databases will be unaffected, because this `rp` query parameter is unsupported there. You can read more about the rentention policy mapping behavior of InfluxDB v2 in our documentation: - https://docs.influxdata.com/influxdb/v2.6/reference/api/influxdb-1x/dbrp/#when-querying-data - https://docs.influxdata.com/influxdb/v2.6/reference/api/influxdb-1x/query/#query-a-non-default-retention-policy * Use the ? operator Co-authored-by: Ryan McKinley <ryantxu@gmail.com> Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
import InfluxDatasource from './datasource';
|
|
import { InfluxQueryBuilder } from './query_builder';
|
|
import { InfluxQueryTag } from './types';
|
|
|
|
const runExploreQuery = (
|
|
type: string,
|
|
withKey: string | undefined,
|
|
withMeasurementFilter: string | undefined,
|
|
target: { measurement: string | undefined; tags: InfluxQueryTag[]; policy: string | undefined },
|
|
datasource: InfluxDatasource
|
|
): Promise<Array<{ text: string }>> => {
|
|
const builder = new InfluxQueryBuilder(target, datasource.database);
|
|
const q = builder.buildExploreQuery(type, withKey, withMeasurementFilter);
|
|
const options = { policy: target.policy };
|
|
return datasource.metricFindQuery(q, options);
|
|
};
|
|
|
|
export async function getAllPolicies(datasource: InfluxDatasource): Promise<string[]> {
|
|
const target = { tags: [], measurement: undefined, policy: undefined };
|
|
const data = await runExploreQuery('RETENTION POLICIES', undefined, undefined, target, datasource);
|
|
return data.map((item) => item.text);
|
|
}
|
|
|
|
export async function getAllMeasurementsForTags(
|
|
measurementFilter: string | undefined,
|
|
tags: InfluxQueryTag[],
|
|
datasource: InfluxDatasource
|
|
): Promise<string[]> {
|
|
const target = { tags, measurement: undefined, policy: undefined };
|
|
const data = await runExploreQuery('MEASUREMENTS', undefined, measurementFilter, target, datasource);
|
|
return data.map((item) => item.text);
|
|
}
|
|
|
|
export async function getTagKeysForMeasurementAndTags(
|
|
measurement: string | undefined,
|
|
policy: string | undefined,
|
|
tags: InfluxQueryTag[],
|
|
datasource: InfluxDatasource
|
|
): Promise<string[]> {
|
|
const target = { tags, measurement, policy };
|
|
const data = await runExploreQuery('TAG_KEYS', undefined, undefined, target, datasource);
|
|
return data.map((item) => item.text);
|
|
}
|
|
|
|
export async function getTagValues(
|
|
tagKey: string,
|
|
measurement: string | undefined,
|
|
policy: string | undefined,
|
|
tags: InfluxQueryTag[],
|
|
datasource: InfluxDatasource
|
|
): Promise<string[]> {
|
|
const target = { tags, measurement, policy };
|
|
const data = await runExploreQuery('TAG_VALUES', tagKey, undefined, target, datasource);
|
|
return data.map((item) => item.text);
|
|
}
|
|
|
|
export async function getFieldKeysForMeasurement(
|
|
measurement: string,
|
|
policy: string | undefined,
|
|
datasource: InfluxDatasource
|
|
): Promise<string[]> {
|
|
const target = { tags: [], measurement, policy };
|
|
const data = await runExploreQuery('FIELDS', undefined, undefined, target, datasource);
|
|
return data.map((item) => item.text);
|
|
}
|