Skip to content

Commit 2494cb0

Browse files
authored
fix: potentially fixes the loading state forever bug (#4324)
1 parent 20d4e86 commit 2494cb0

File tree

1 file changed

+165
-172
lines changed

1 file changed

+165
-172
lines changed

src/flows/pipes/QueryBuilder/context.tsx

Lines changed: 165 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React, {
44
createContext,
55
useContext,
66
useState,
7-
useCallback,
87
useMemo,
98
} from 'react'
109

@@ -189,7 +188,7 @@ export const QueryBuilderProvider: FC = ({children}) => {
189188
[data.tags, cardMeta]
190189
)
191190

192-
const add = useCallback(() => {
191+
const add = () => {
193192
cards.push(getDefaultCard())
194193

195194
setCardMeta([
@@ -203,77 +202,76 @@ export const QueryBuilderProvider: FC = ({children}) => {
203202
])
204203

205204
update({tags: cards.map(toBuilderConfig)})
206-
}, [cards])
207-
208-
const loadKeys = useCallback(
209-
(idx, search) => {
210-
if (
211-
!data.buckets[0] ||
212-
typeof data.buckets[0] === 'string' ||
213-
!cards[idx]
214-
) {
215-
return
216-
}
217-
218-
if (!cardMeta[idx]) {
219-
return
220-
}
221-
222-
if (cardMeta[idx].loadingKeys === RemoteDataState.Loading) {
223-
return
224-
}
205+
}
225206

226-
cardMeta.splice(idx, 1, {
227-
...cardMeta[idx],
228-
loadingKeys: RemoteDataState.Loading,
207+
const loadKeys = (idx, search) => {
208+
if (
209+
!data.buckets[0] ||
210+
typeof data.buckets[0] === 'string' ||
211+
!cards[idx]
212+
) {
213+
return
214+
}
215+
216+
if (!cardMeta[idx]) {
217+
return
218+
}
219+
220+
if (cardMeta[idx].loadingKeys === RemoteDataState.Loading) {
221+
return
222+
}
223+
224+
cardMeta.splice(idx, 1, {
225+
...cardMeta[idx],
226+
loadingKeys: RemoteDataState.Loading,
227+
})
228+
setCardMeta([...cardMeta])
229+
230+
const tagSelections = cards
231+
.filter(card => card.keys.selected[0] && card.values.selected.length)
232+
.map(card => {
233+
const fluxTags = card.values.selected
234+
.map(
235+
value =>
236+
`r["${card.keys.selected[0]}"] == "${value.replace(
237+
/\\/g,
238+
'\\\\'
239+
)}"`
240+
)
241+
.join(' or ')
242+
243+
return `(${fluxTags})`
229244
})
230-
setCardMeta([...cardMeta])
245+
const tagString = tagSelections.length
246+
? tagSelections.join(' and ')
247+
: 'true'
248+
const searchString = search
249+
? `\n |> filter(fn: (r) => r._value =~ regexp.compile(v: "(?i:" + regexp.quoteMeta(v: "${search}") + ")"))`
250+
: ''
251+
const previousTagSelections = cards
252+
.slice(0, idx)
253+
.map(card => `r._value != "${card.keys.selected[0]}"`)
254+
const previousTagString = previousTagSelections.length
255+
? `\n |> filter(fn: (r) => ${previousTagSelections.join(' and ')})`
256+
: ''
257+
258+
const {scope} = getPanelQueries(id)
259+
260+
let _source = 'import "regexp"\n'
261+
if (data.buckets[0].type === 'sample') {
262+
_source += `import "influxdata/influxdb/sample"\nsample.data(set: "${data.buckets[0].id}")`
263+
} else {
264+
_source += `from(bucket: "${data.buckets[0].name}")`
265+
}
231266

232-
const tagSelections = cards
233-
.filter(card => card.keys.selected[0] && card.values.selected.length)
234-
.map(card => {
235-
const fluxTags = card.values.selected
236-
.map(
237-
value =>
238-
`r["${card.keys.selected[0]}"] == "${value.replace(
239-
/\\/g,
240-
'\\\\'
241-
)}"`
242-
)
243-
.join(' or ')
244-
245-
return `(${fluxTags})`
246-
})
247-
const tagString = tagSelections.length
248-
? tagSelections.join(' and ')
249-
: 'true'
250-
const searchString = search
251-
? `\n |> filter(fn: (r) => r._value =~ regexp.compile(v: "(?i:" + regexp.quoteMeta(v: "${search}") + ")"))`
252-
: ''
253-
const previousTagSelections = cards
254-
.slice(0, idx)
255-
.map(card => `r._value != "${card.keys.selected[0]}"`)
256-
const previousTagString = previousTagSelections.length
257-
? `\n |> filter(fn: (r) => ${previousTagSelections.join(' and ')})`
258-
: ''
259-
260-
const {scope} = getPanelQueries(id)
261-
262-
let _source = 'import "regexp"\n'
263-
if (data.buckets[0].type === 'sample') {
264-
_source += `import "influxdata/influxdb/sample"\nsample.data(set: "${data.buckets[0].id}")`
265-
} else {
266-
_source += `from(bucket: "${data.buckets[0].name}")`
267-
}
268-
269-
const limit = isFlagEnabled('increasedMeasurmentTagLimit')
270-
? EXTENDED_TAG_LIMIT
271-
: DEFAULT_TAG_LIMIT
272-
273-
// TODO: Use the `v1.tagKeys` function from the Flux standard library once
274-
// this issue is resolved: https://github.com/influxdata/flux/issues/1071
275-
query(
276-
`${_source}
267+
const limit = isFlagEnabled('increasedMeasurmentTagLimit')
268+
? EXTENDED_TAG_LIMIT
269+
: DEFAULT_TAG_LIMIT
270+
271+
// TODO: Use the `v1.tagKeys` function from the Flux standard library once
272+
// this issue is resolved: https://github.com/influxdata/flux/issues/1071
273+
query(
274+
`${_source}
277275
|> range(${formatTimeRangeArguments(range)})
278276
|> filter(fn: (r) => ${tagString})
279277
|> keys()
@@ -282,96 +280,93 @@ export const QueryBuilderProvider: FC = ({children}) => {
282280
|> filter(fn: (r) => r._value != "_time" and r._value != "_start" and r._value != "_stop" and r._value != "_value")
283281
|> sort()
284282
|> limit(n: ${limit})`,
285-
scope
286-
)
287-
.then(resp => {
288-
return (Object.values(resp.parsed.table.columns).filter(
289-
c => c.name === '_value' && c.type === 'string'
290-
)[0]?.data ?? []) as string[]
291-
})
292-
.then(keys => {
293-
if (!cards[idx].keys.selected[0]) {
294-
if (idx === 0 && keys.includes('_measurement')) {
295-
cards[idx].keys.selected = ['_measurement']
296-
} else {
297-
cards[idx].keys.selected = [keys[0]]
298-
}
299-
300-
update({tags: cards.map(toBuilderConfig)})
301-
} else if (!keys.includes(cards[idx].keys.selected[0])) {
302-
keys.unshift(cards[idx].keys.selected[0])
283+
scope
284+
)
285+
.then(resp => {
286+
return (Object.values(resp.parsed.table.columns).filter(
287+
c => c.name === '_value' && c.type === 'string'
288+
)[0]?.data ?? []) as string[]
289+
})
290+
.then(keys => {
291+
if (!cards[idx].keys.selected[0]) {
292+
if (idx === 0 && keys.includes('_measurement')) {
293+
cards[idx].keys.selected = ['_measurement']
294+
} else {
295+
cards[idx].keys.selected = [keys[0]]
303296
}
304297

305-
cardMeta.splice(idx, 1, {
306-
keys,
307-
values: [],
308-
loadingKeys: RemoteDataState.Done,
309-
loadingValues: RemoteDataState.NotStarted,
310-
})
311-
setCardMeta([...cardMeta])
312-
})
313-
.catch(e => {
314-
console.error(e)
298+
update({tags: cards.map(toBuilderConfig)})
299+
} else if (!keys.includes(cards[idx].keys.selected[0])) {
300+
keys.unshift(cards[idx].keys.selected[0])
301+
}
302+
303+
cardMeta.splice(idx, 1, {
304+
keys,
305+
values: [],
306+
loadingKeys: RemoteDataState.Done,
307+
loadingValues: RemoteDataState.NotStarted,
315308
})
316-
},
317-
[data.buckets, cards]
318-
)
309+
setCardMeta([...cardMeta])
310+
})
311+
.catch(e => {
312+
console.error(e)
313+
})
314+
}
319315

320-
const loadValues = useCallback(
321-
(idx, search) => {
322-
if (
323-
cardMeta[idx].loadingValues === RemoteDataState.Loading ||
324-
!data.buckets.length
325-
) {
326-
return
327-
}
316+
const loadValues = (idx, search) => {
317+
if (
318+
cardMeta[idx].loadingValues === RemoteDataState.Loading ||
319+
!data.buckets.length
320+
) {
321+
return
322+
}
328323

329-
cardMeta.splice(idx, 1, {
330-
...cardMeta[idx],
331-
loadingValues: RemoteDataState.Loading,
324+
cardMeta.splice(idx, 1, {
325+
...cardMeta[idx],
326+
loadingValues: RemoteDataState.Loading,
327+
})
328+
329+
setCardMeta([...cardMeta])
330+
331+
const tagSelections = cards
332+
.slice(0, idx)
333+
.filter(card => card.keys.selected[0] && card.values.selected.length)
334+
.map(card => {
335+
const fluxTags = card.values.selected
336+
.map(
337+
value =>
338+
`r["${card.keys.selected[0]}"] == "${value.replace(
339+
/\\/g,
340+
'\\\\'
341+
)}"`
342+
)
343+
.join(' or ')
344+
345+
return `(${fluxTags})`
332346
})
347+
const tagString = tagSelections.length
348+
? tagSelections.join(' and ')
349+
: 'true'
350+
const searchString = search
351+
? `\n |> filter(fn: (r) => r._value =~ regexp.compile(v: "(?i:" + regexp.quoteMeta(v: "${search}") + ")"))`
352+
: ''
353+
354+
const {scope} = getPanelQueries(id)
355+
let _source = 'import "regexp"\n'
356+
if (data.buckets[0].type === 'sample') {
357+
_source += `import "influxdata/influxdb/sample"\nsample.data(set: "${data.buckets[0].id}")`
358+
} else {
359+
_source += `from(bucket: "${data.buckets[0].name}")`
360+
}
333361

334-
setCardMeta([...cardMeta])
362+
const limit = isFlagEnabled('increasedMeasurmentTagLimit')
363+
? EXTENDED_TAG_LIMIT
364+
: DEFAULT_TAG_LIMIT
335365

336-
const tagSelections = cards
337-
.slice(0, idx)
338-
.filter(card => card.keys.selected[0] && card.values.selected.length)
339-
.map(card => {
340-
const fluxTags = card.values.selected
341-
.map(
342-
value =>
343-
`r["${card.keys.selected[0]}"] == "${value.replace(
344-
/\\/g,
345-
'\\\\'
346-
)}"`
347-
)
348-
.join(' or ')
349-
350-
return `(${fluxTags})`
351-
})
352-
const tagString = tagSelections.length
353-
? tagSelections.join(' and ')
354-
: 'true'
355-
const searchString = search
356-
? `\n |> filter(fn: (r) => r._value =~ regexp.compile(v: "(?i:" + regexp.quoteMeta(v: "${search}") + ")"))`
357-
: ''
358-
359-
const {scope} = getPanelQueries(id)
360-
let _source = 'import "regexp"\n'
361-
if (data.buckets[0].type === 'sample') {
362-
_source += `import "influxdata/influxdb/sample"\nsample.data(set: "${data.buckets[0].id}")`
363-
} else {
364-
_source += `from(bucket: "${data.buckets[0].name}")`
365-
}
366-
367-
const limit = isFlagEnabled('increasedMeasurmentTagLimit')
368-
? EXTENDED_TAG_LIMIT
369-
: DEFAULT_TAG_LIMIT
370-
371-
// TODO: Use the `v1.tagValues` function from the Flux standard library once
372-
// this issue is resolved: https://github.com/influxdata/flux/issues/1071
373-
query(
374-
`${_source}
366+
// TODO: Use the `v1.tagValues` function from the Flux standard library once
367+
// this issue is resolved: https://github.com/influxdata/flux/issues/1071
368+
query(
369+
`${_source}
375370
|> range(${formatTimeRangeArguments(range)})
376371
|> filter(fn: (r) => ${tagString})
377372
|> keep(columns: ["${cards[idx].keys.selected[0]}"])
@@ -381,27 +376,25 @@ export const QueryBuilderProvider: FC = ({children}) => {
381376
}")${searchString}
382377
|> limit(n: ${limit})
383378
|> sort()`,
384-
scope
385-
)
386-
.then(resp => {
387-
return (Object.values(resp.parsed.table.columns).filter(
388-
c => c.name === '_value' && c.type === 'string'
389-
)[0]?.data ?? []) as string[]
390-
})
391-
.then(values => {
392-
cardMeta.splice(idx, 1, {
393-
...cardMeta[idx],
394-
values,
395-
loadingValues: RemoteDataState.Done,
396-
})
397-
setCardMeta([...cardMeta])
398-
})
399-
.catch(e => {
400-
console.error(e)
379+
scope
380+
)
381+
.then(resp => {
382+
return (Object.values(resp.parsed.table.columns).filter(
383+
c => c.name === '_value' && c.type === 'string'
384+
)[0]?.data ?? []) as string[]
385+
})
386+
.then(values => {
387+
cardMeta.splice(idx, 1, {
388+
...cardMeta[idx],
389+
values,
390+
loadingValues: RemoteDataState.Done,
401391
})
402-
},
403-
[data.buckets, cards]
404-
)
392+
setCardMeta([...cardMeta])
393+
})
394+
.catch(e => {
395+
console.error(e)
396+
})
397+
}
405398

406399
const remove = (idx: number): void => {
407400
if (idx === 0 || idx > cards.length) {

0 commit comments

Comments
 (0)