Skip to content

Commit

Permalink
Accept metaquery options as an object
Browse files Browse the repository at this point in the history
  • Loading branch information
chnn committed Mar 20, 2019
1 parent 4904992 commit 2eb312e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 65 deletions.
37 changes: 20 additions & 17 deletions ui/src/timeMachine/actions/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand All @@ -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]

Expand Down Expand Up @@ -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

Expand All @@ -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]

Expand Down
44 changes: 12 additions & 32 deletions ui/src/timeMachine/apis/QueryBuilderFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>
Expand All @@ -19,17 +21,17 @@ class QueryBuilderFetcher {
private findValuesCache: {[key: string]: string[]} = {}
private findBucketsCache: {[key: string]: string[]} = {}

public async findBuckets(url: string, orgID: string): Promise<string[]> {
public async findBuckets(options: FindBucketsOptions): Promise<string[]> {
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
Expand All @@ -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<string[]> {
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

Expand All @@ -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<string[]> {
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

Expand Down
54 changes: 38 additions & 16 deletions ui/src/timeMachine/apis/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export const LIMIT = 200

type CancelableQuery = WrappedCancelablePromise<string[]>

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})`
Expand All @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 2eb312e

Please sign in to comment.