From 2eb312ea23f217187dc740414331c6960a1bb7c5 Mon Sep 17 00:00:00 2001 From: Christopher Henn Date: Wed, 20 Mar 2019 10:32:33 -0700 Subject: [PATCH] Accept metaquery options as an object --- ui/src/timeMachine/actions/queryBuilder.ts | 37 +++++++------ .../timeMachine/apis/QueryBuilderFetcher.ts | 44 +++++---------- ui/src/timeMachine/apis/queryBuilder.ts | 54 +++++++++++++------ 3 files changed, 70 insertions(+), 65 deletions(-) diff --git a/ui/src/timeMachine/actions/queryBuilder.ts b/ui/src/timeMachine/actions/queryBuilder.ts index f02612d2dd2..f108d6e4b9b 100644 --- a/ui/src/timeMachine/actions/queryBuilder.ts +++ b/ui/src/timeMachine/actions/queryBuilder.ts @@ -206,7 +206,11 @@ export const loadBuckets = () => async ( dispatch(setBuilderBucketsStatus(RemoteDataState.Loading)) try { - const buckets = await queryBuilderFetcher.findBuckets(queryURL, orgID) + const buckets = await queryBuilderFetcher.findBuckets({ + url: queryURL, + orgID, + }) + const selectedBucket = getActiveQuery(getState()).builderConfig.buckets[0] dispatch(setBuilderBuckets(buckets)) @@ -244,7 +248,7 @@ export const loadTagSelector = (index: number) => async ( return } - const tagPredicates = tags.slice(0, index) + const tagsSelections = tags.slice(0, index) const queryURL = getState().links.query.self const orgID = getActiveOrg(getState()).id @@ -254,14 +258,13 @@ export const loadTagSelector = (index: number) => async ( const searchTerm = getActiveTimeMachine(getState()).queryBuilder.tags[index] .keysSearchTerm - const keys = await queryBuilderFetcher.findKeys( - index, - queryURL, + const keys = await queryBuilderFetcher.findKeys(index, { + url: queryURL, orgID, - buckets[0], - tagPredicates, - searchTerm - ) + bucket: buckets[0], + tagsSelections, + searchTerm, + }) const {key} = tags[index] @@ -299,7 +302,7 @@ const loadTagSelectorValues = (index: number) => async ( ) => { const state = getState() const {buckets, tags} = getActiveQuery(state).builderConfig - const tagPredicates = tags.slice(0, index) + const tagsSelections = tags.slice(0, index) const queryURL = state.links.query.self const orgID = getActiveOrg(state).id @@ -309,15 +312,15 @@ const loadTagSelectorValues = (index: number) => async ( const key = getActiveQuery(getState()).builderConfig.tags[index].key const searchTerm = getActiveTimeMachine(getState()).queryBuilder.tags[index] .valuesSearchTerm - const values = await queryBuilderFetcher.findValues( - index, - queryURL, + + const values = await queryBuilderFetcher.findValues(index, { + url: queryURL, orgID, - buckets[0], - tagPredicates, + bucket: buckets[0], + tagsSelections, key, - searchTerm - ) + searchTerm, + }) const {values: selectedValues} = tags[index] diff --git a/ui/src/timeMachine/apis/QueryBuilderFetcher.ts b/ui/src/timeMachine/apis/QueryBuilderFetcher.ts index 3f5c2495555..f35d803089d 100644 --- a/ui/src/timeMachine/apis/QueryBuilderFetcher.ts +++ b/ui/src/timeMachine/apis/QueryBuilderFetcher.ts @@ -3,10 +3,12 @@ import { findBuckets, findKeys, findValues, + FindBucketsOptions, + FindKeysOptions, + FindValuesOptions, } from 'src/timeMachine/apis/queryBuilder' // Types -import {BuilderConfig} from 'src/types/v2' import {WrappedCancelablePromise} from 'src/types/promises' type CancelableQuery = WrappedCancelablePromise @@ -19,17 +21,17 @@ class QueryBuilderFetcher { private findValuesCache: {[key: string]: string[]} = {} private findBucketsCache: {[key: string]: string[]} = {} - public async findBuckets(url: string, orgID: string): Promise { + public async findBuckets(options: FindBucketsOptions): Promise { this.cancelFindBuckets() - const cacheKey = JSON.stringify([...arguments]) + const cacheKey = JSON.stringify(options) const cachedResult = this.findBucketsCache[cacheKey] if (cachedResult) { return Promise.resolve(cachedResult) } - const pendingResult = findBuckets(url, orgID) + const pendingResult = findBuckets(options) pendingResult.promise.then(result => { this.findBucketsCache[cacheKey] = result @@ -46,28 +48,18 @@ class QueryBuilderFetcher { public async findKeys( index: number, - url: string, - orgID: string, - bucket: string, - tagsSelections: BuilderConfig['tags'], - searchTerm: string = '' + options: FindKeysOptions ): Promise { this.cancelFindKeys(index) - const cacheKey = JSON.stringify([...arguments].slice(1)) + const cacheKey = JSON.stringify(options) const cachedResult = this.findKeysCache[cacheKey] if (cachedResult) { return Promise.resolve(cachedResult) } - const pendingResult = findKeys( - url, - orgID, - bucket, - tagsSelections, - searchTerm - ) + const pendingResult = findKeys(options) this.findKeysQueries[index] = pendingResult @@ -86,30 +78,18 @@ class QueryBuilderFetcher { public async findValues( index: number, - url: string, - orgID: string, - bucket: string, - tagsSelections: BuilderConfig['tags'], - key: string, - searchTerm: string = '' + options: FindValuesOptions ): Promise { this.cancelFindValues(index) - const cacheKey = JSON.stringify([...arguments].slice(1)) + const cacheKey = JSON.stringify(options) const cachedResult = this.findValuesCache[cacheKey] if (cachedResult) { return Promise.resolve(cachedResult) } - const pendingResult = findValues( - url, - orgID, - bucket, - tagsSelections, - key, - searchTerm - ) + const pendingResult = findValues(options) this.findValuesQueries[index] = pendingResult diff --git a/ui/src/timeMachine/apis/queryBuilder.ts b/ui/src/timeMachine/apis/queryBuilder.ts index f97c3f82ef6..5c6c2794d75 100644 --- a/ui/src/timeMachine/apis/queryBuilder.ts +++ b/ui/src/timeMachine/apis/queryBuilder.ts @@ -14,7 +14,12 @@ export const LIMIT = 200 type CancelableQuery = WrappedCancelablePromise -export function findBuckets(url: string, orgID: string): CancelableQuery { +export interface FindBucketsOptions { + url: string + orgID: string +} + +export function findBuckets({url, orgID}: FindBucketsOptions): CancelableQuery { const query = `buckets() |> sort(columns: ["name"]) |> limit(n: ${LIMIT})` @@ -27,13 +32,21 @@ export function findBuckets(url: string, orgID: string): CancelableQuery { } } -export function findKeys( - url: string, - orgID: string, - bucket: string, - tagsSelections: BuilderConfig['tags'], - searchTerm: string = '' -): CancelableQuery { +export interface FindKeysOptions { + url: string + orgID: string + bucket: string + tagsSelections: BuilderConfig['tags'] + searchTerm?: string +} + +export function findKeys({ + url, + orgID, + bucket, + tagsSelections, + searchTerm = '', +}: FindKeysOptions): CancelableQuery { const tagFilters = formatTagFilterPredicate(tagsSelections) const searchFilter = formatSearchFilterCall(searchTerm) const previousKeyFilter = formatTagKeyFilterCall(tagsSelections) @@ -58,14 +71,23 @@ v1.tagKeys(bucket: "${bucket}", predicate: ${tagFilters}, start: -${SEARCH_DURAT } } -export function findValues( - url: string, - orgID: string, - bucket: string, - tagsSelections: BuilderConfig['tags'], - key: string, - searchTerm: string = '' -): CancelableQuery { +export interface FindValuesOptions { + url: string + orgID: string + bucket: string + tagsSelections: BuilderConfig['tags'] + key: string + searchTerm: string +} + +export function findValues({ + url, + orgID, + bucket, + tagsSelections, + key, + searchTerm = '', +}: FindValuesOptions): CancelableQuery { const tagFilters = formatTagFilterPredicate(tagsSelections) const searchFilter = formatSearchFilterCall(searchTerm)