Passing array values in filter option

I am using an array to store the multiple cities.
I want to iterate over the cites and pass them one by one or as a whole string and pass a regex in the filter option

I am pasting my code below.

import "array"
city_name = ["cityA","cityB","cityC"]

from(bucket: "bucket_name")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "measurement_name")
  |> filter(fn: (r) => r["City"] == city_name |> array.map(fn: (x) => ({_value: x}))  )
  |> filter(fn: (r) => r["_field"] == "field_name")
  |> aggregateWindow(every: 1h, fn: first, createEmpty: false)
  |> increase()
  |> yield(name: "last")

I want to pass each city one by one and calculate the query for the respective city.

I was trying another approach passing all three cities at once like regex.


b = frnd_name |> array.map(fn: (x) => ({
city_name: "/" + x + "/",
city_name2: x + city_name2,
}))

I want to generate the string city_name = /CityA|CityB|CityC/ and pass into the filter option

 |> filter(fn: (r) => r["City"] =~  city_name)

@Anaisdg @scott is it possible to do this with an array in flux?

One more help, is it possible in Flux language to get the difference between two arrays and it will return the third array with the difference?

array_a = [1,2,3,4,5]
array_b = [2,4]
result_array = [1,3,5]

Thanks

@Ravikant_Gautam There are a few ways to accomplish what you’re trying to do. The simplest is to use contains() in your filter to see if a value exists in an array, but this has known performance issues. If your dataset isn’t too large, it may not be an issue:

import "array"
city_name = ["cityA","cityB","cityC"]

from(bucket: "bucket_name")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "measurement_name")
  |> filter(fn: (r) => contains(set: city_name , value: r["City"])
  |> filter(fn: (r) => r["_field"] == "field_name")
  |> aggregateWindow(every: 1h, fn: first, createEmpty: false)
  |> increase()
  |> yield(name: "last")

Another option would be to query each of the city data sets separately and union them together. You should be able to use array.map to do this:

import "array"

city_name = ["cityA", "cityB", "cityC"]
getCityData = (tables=<-, city) => tables |> filter(fn: (r) => r["City"] == city)

data =
    from(bucket: "bucket_name")
        |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
        |> filter(fn: (r) => r["_measurement"] == "measurement_name")
        |> filter(fn: (r) => r["_field"] == "field_name")

union(tables: array.map(arr: city_name, fn: (x) => data |> getCityData(city: x)))
    |> aggregateWindow(every: 1h, fn: first, createEmpty: false)
    |> increase()
    |> yield(name: "last")

I don’t think I’d recommend going the regular expression route due to performance and there isn’t really a way to convert an array into a regexp (that I can think of).

No, this currently isn’t possible in Flux.