influxQLMetadataQuery.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import InfluxDatasource from './datasource';
  2. import { InfluxQueryBuilder } from './query_builder';
  3. import { InfluxQueryTag } from './types';
  4. const runExploreQuery = (
  5. type: string,
  6. withKey: string | undefined,
  7. withMeasurementFilter: string | undefined,
  8. target: { measurement: string | undefined; tags: InfluxQueryTag[]; policy: string | undefined },
  9. datasource: InfluxDatasource
  10. ): Promise<Array<{ text: string }>> => {
  11. const builder = new InfluxQueryBuilder(target, datasource.database);
  12. const q = builder.buildExploreQuery(type, withKey, withMeasurementFilter);
  13. return datasource.metricFindQuery(q);
  14. };
  15. export async function getAllPolicies(datasource: InfluxDatasource): Promise<string[]> {
  16. const target = { tags: [], measurement: undefined, policy: undefined };
  17. const data = await runExploreQuery('RETENTION POLICIES', undefined, undefined, target, datasource);
  18. return data.map((item) => item.text);
  19. }
  20. export async function getAllMeasurementsForTags(
  21. measurementFilter: string | undefined,
  22. tags: InfluxQueryTag[],
  23. datasource: InfluxDatasource
  24. ): Promise<string[]> {
  25. const target = { tags, measurement: undefined, policy: undefined };
  26. const data = await runExploreQuery('MEASUREMENTS', undefined, measurementFilter, target, datasource);
  27. return data.map((item) => item.text);
  28. }
  29. export async function getTagKeysForMeasurementAndTags(
  30. measurement: string | undefined,
  31. policy: string | undefined,
  32. tags: InfluxQueryTag[],
  33. datasource: InfluxDatasource
  34. ): Promise<string[]> {
  35. const target = { tags, measurement, policy };
  36. const data = await runExploreQuery('TAG_KEYS', undefined, undefined, target, datasource);
  37. return data.map((item) => item.text);
  38. }
  39. export async function getTagValues(
  40. tagKey: string,
  41. measurement: string | undefined,
  42. policy: string | undefined,
  43. tags: InfluxQueryTag[],
  44. datasource: InfluxDatasource
  45. ): Promise<string[]> {
  46. const target = { tags, measurement, policy };
  47. const data = await runExploreQuery('TAG_VALUES', tagKey, undefined, target, datasource);
  48. return data.map((item) => item.text);
  49. }
  50. export async function getFieldKeysForMeasurement(
  51. measurement: string,
  52. policy: string | undefined,
  53. datasource: InfluxDatasource
  54. ): Promise<string[]> {
  55. const target = { tags: [], measurement, policy };
  56. const data = await runExploreQuery('FIELDS', undefined, undefined, target, datasource);
  57. return data.map((item) => item.text);
  58. }