Skip to content

Commit

Permalink
Merge pull request #6116 from influxdata/fix/version-detection
Browse files Browse the repository at this point in the history
fix: version detection
  • Loading branch information
vlastahajek authored Dec 11, 2024
2 parents f6f73ae + 677f486 commit 78dbc05
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
1. [#6103](https://github.com/influxdata/chronograf/pull/6103): Set active database for InfluxQL meta queries.
2. [#6105](https://github.com/influxdata/chronograf/pull/6105): Prevent dangerous InfluxQL statements from auto-execution.
3. [#6111](https://github.com/influxdata/chronograf/pull/6111): Loading Hosts page for large number of hosts.
4. [#6116](https://github.com/influxdata/chronograf/pull/6116): Showing tag values in Flux query builder connected to InfluxDB Enterprise.

### Other

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,16 @@ export interface FindValuesOptions {
limit?: number
}

export function findValues({
source,
bucket,
tagsSelections,
key,
searchTerm = '',
timeRange = DEFAULT_TIME_RANGE,
limit = FQB_RESULTS_LIMIT,
}: FindValuesOptions): CancelBox<TruncatedResult<string[]>> {
const tagFilter = formatTagFilter(tagsSelections)
const timeRangeArguments = rangeArguments(timeRange)

export function createValuesQuery(
searchTerm: string,
dbVersion: string,
tagsSelections: BuilderTagsType[],
bucket: string,
timeRangeArguments: string,
tagFilter: string,
key: string,
limit: number
) {
// requires Flux package to work which we will put in the query
const searchFilter = !searchTerm
? ''
Expand All @@ -103,8 +101,9 @@ export function findValues({

// 1.x InfluxDB produce wrong results when _field tag is filtered,
// experiments showed that keeping an extra column is a workaround
const version = dbVersion || ''
const v1ExtraKeep =
(source.version || '').startsWith('1.') &&
(version.startsWith('1.') || version === 'ENT') &&
tagsSelections.some(x => x.tagKey === '_field' && x.tagValues?.length)
? ', "_field"'
: ''
Expand All @@ -118,6 +117,30 @@ from(bucket: ${fluxString(bucket)})
|> distinct(column: ${fluxString(key)})${searchFilter}
|> sort()
|> limit(n: ${limit})`
return query
}

export function findValues({
source,
bucket,
tagsSelections,
key,
searchTerm = '',
timeRange = DEFAULT_TIME_RANGE,
limit = FQB_RESULTS_LIMIT,
}: FindValuesOptions): CancelBox<TruncatedResult<string[]>> {
const tagFilter = formatTagFilter(tagsSelections)
const timeRangeArguments = rangeArguments(timeRange)
const query = createValuesQuery(
searchTerm,
source.version,
tagsSelections,
bucket,
timeRangeArguments,
tagFilter,
key,
limit
)

return extractBoxedCol(runQuery(source, query), '_value', limit)
}
Expand Down
140 changes: 140 additions & 0 deletions ui/test/flux/components/fluxQueries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import {createValuesQuery} from 'src/shared/components/TimeMachine/fluxQueryBuilder/apis/fluxQueries'
import {BuilderTagsType} from 'src/shared/components/TimeMachine/fluxQueryBuilder/types'

describe('createQuery', () => {
;[
{
name: 'query for v1 with no _field',
searchTerm: '',
dbVersion: '1.11.0',
tagsSelections: [
{
tagKey: '_measurement',
tagValues: ['win_cpu'],
},
] as BuilderTagsType[],
bucket: 'telegraf',
timeRangeArguments: 'start: -6h',
tagFilter: '\n |> filter(fn: (r) => (r["_measurement"] == "win_cpu"))',
key: '_measurement',
limit: 200,
query: `import "regexp"
from(bucket: "telegraf")
|> range(start: -6h)
|> filter(fn: (r) => (r["_measurement"] == "win_cpu"))
|> keep(columns: ["_measurement"])
|> group()
|> distinct(column: "_measurement")
|> sort()
|> limit(n: 200)`,
},
{
name: 'query for v1 with _field',
searchTerm: '',
dbVersion: '1.11.0',
tagsSelections: [
{
tagKey: '_measurement',
tagValues: ['win_cpu'],
},
{
tagKey: '_field',
tagValues: ['Percent_Idle_Time'],
},
] as BuilderTagsType[],
bucket: 'telegraf',
timeRangeArguments: 'start: -1h',
tagFilter:
'\n |> filter(fn: (r) => (r["_measurement"] == "win_cpu") and (r["_field"] == "Percent_Idle_Time"))',
key: 'host',
limit: 200,
query: `import "regexp"
from(bucket: "telegraf")
|> range(start: -1h)
|> filter(fn: (r) => (r["_measurement"] == "win_cpu") and (r["_field"] == "Percent_Idle_Time"))
|> keep(columns: ["host", "_field"])
|> group()
|> distinct(column: "host")
|> sort()
|> limit(n: 200)`,
},
{
name: 'query for ENT with _field',
searchTerm: '',
dbVersion: 'ENT',
tagsSelections: [
{
tagKey: '_measurement',
tagValues: ['win_cpu'],
},
{
tagKey: '_field',
tagValues: ['Percent_Idle_Time'],
},
] as BuilderTagsType[],
bucket: 'telegraf',
timeRangeArguments: 'start: -1h',
tagFilter:
'\n |> filter(fn: (r) => (r["_measurement"] == "win_cpu") and (r["_field"] == "Percent_Idle_Time"))',
key: 'host',
limit: 200,
query: `import "regexp"
from(bucket: "telegraf")
|> range(start: -1h)
|> filter(fn: (r) => (r["_measurement"] == "win_cpu") and (r["_field"] == "Percent_Idle_Time"))
|> keep(columns: ["host", "_field"])
|> group()
|> distinct(column: "host")
|> sort()
|> limit(n: 200)`,
},
{
name: 'query for v2 w/o _field',
searchTerm: '',
dbVersion: '2.7.0',
tagsSelections: [
{
tagKey: '_measurement',
tagValues: ['win_cpu'],
},
{
tagKey: '_field',
tagValues: ['Percent_Idle_Time'],
},
] as BuilderTagsType[],
bucket: 'telegraf',
timeRangeArguments: 'start: -1h',
tagFilter:
'\n |> filter(fn: (r) => (r["_measurement"] == "win_cpu") and (r["_field"] == "Percent_Idle_Time"))',
key: 'host',
limit: 100,
query: `import "regexp"
from(bucket: "telegraf")
|> range(start: -1h)
|> filter(fn: (r) => (r["_measurement"] == "win_cpu") and (r["_field"] == "Percent_Idle_Time"))
|> keep(columns: ["host"])
|> group()
|> distinct(column: "host")
|> sort()
|> limit(n: 100)`,
},
].forEach(x => {
it('creates a query ' + x.name, () => {
const query = createValuesQuery(
x.searchTerm,
x.dbVersion,
x.tagsSelections,
x.bucket,
x.timeRangeArguments,
x.tagFilter,
x.key,
x.limit
)
expect(query).toEqual(x.query)
})
})
})

0 comments on commit 78dbc05

Please sign in to comment.