Tag Variable based on time

I am trying to get influx to filter tag values based on time, but the query I am using doesn’t seem to work:

  from(bucket: "my_bucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> keep(columns: ["mac_address"])
    |> group()
    |> distinct(column: "mac_address")

The plan was then to use this query in Grafana as a variable

Can anyone help as regardless of what time value I set it shows the same count of mac addresses

@oneofthemany Flux will only return tag values that exist in the queried data set, so it can’t guarantee that the count of mac addresses will always be the same because there may be a different number of mac addresses depending on the queried time range. With that said, I’d recommend using schema.tagValues() to get values of the mac_address tag.

import "influxdata/influxdb/schema"

schema.tagValues(
  bucket: "my_bucket",
  tag: "mac_address",
  start: -30d
)

@scott - thanks for the reply, but that also does not work

re your comment

Flux will only return tag values that exist in the queried data set, so it can’t guarantee that the count of mac addresses will always be the same

the issue that presents itself is that the list is not affected by the start: -Xd function within the schema.tagValues query

Question on the above:

Is this a known bug?

ideally the solution needs to be flexible based on the range within a given time period to offer better variable granularity

@oneofthemany I just tested, and schema.tagValues() does appear to be respecting the time range defined in the function. Here’s what I did to test:

  1. Created a test InfluxDB bucket.

  2. Used the following query to write to points to InfluxDB with different tag values at different times.

    import "array"
    import "experimental"
    
    array.from(rows: [
        {_time: now(), _measurement: "test", testTag: "t1", _field: "foo", _value: 1.0},
        {_time: experimental.subDuration(d: 40d, from: now()), _measurement: "test", testTag: "t2", _field: "foo", _value: 2.0},
    ])
        |> to(bucket: "test")
    
  3. I then queried two different time ranges – one to include one tag value, the other to return both. The tests behaved as expected:

    import "influxdata/influxdb/schema"
    
    schema.tagValues(bucket: "test", tag: "testTag", start: -30d)
    

    Returned:

    _value
    t1
    import "influxdata/influxdb/schema"
    
    schema.tagValues(bucket: "test", tag: "testTag", start: -60d)
    

    Returned:

    _value
    t1
    t2

One thing to note here is that schema.tagValues does not accept a stop parameter, so the right time boundary is always now(). However, you can create your own custom function to accept the stop time. Here’s the source definition for schema.tagValues: flux/schema.flux at master · influxdata/flux · GitHub

You can create your own using that definition:

customTagValues = (bucket, tag, predicate=(r) => true, start=-30d, stop=now()) => from(bucket: bucket)
    |> range(start: start, stop: stop)
    |> filter(fn: predicate)
    |> keep(columns: [tag])
    |> group()
    |> distinct(column: tag)

customTagValues(
    bucket: "my_bucket",
    start: v.timeRangeStart,
    stop: v.timeRangeStop,
)