Extracting Scalar Values (Type Error)

I’m trying to extract the most recent value from a data set to use in a map query. The query runs by itself in the Explorer tab, but when I try to use the extracted value in a map I am getting type error: (expected [A] but found (field: string) => B).

Here is the query I got trying to follow the documentation for extracting scalar values

option task = {name: "Scale Data", every: 1s}

getValue = (b, me, f) => {
    r = from(bucket: b)
    |> range(start: -99d)
    |> filter(fn: (r) => r["_measurement"] == me and r["_field"] == f)
    |> group(columns: ["_field"])
    |> first()
    |> aggregateWindow(every: 1s, fn: last, createEmpty: false)
    |> yield(name: "last")
    |> findColumn(
            fn: (key) => (key._field == f),
            column: "_value"
    )
    return r
}

scalar = getValue(b: "test_bucket", me: "test_measurement", f: "test_field")

from(bucket: "DataBucket")
	|> range(start: -1s)
	|> filter(fn: (r) =>
		(r["_field"] == "data"))
	|> set(key: "_measurement", value: "scaledMeasurements")
	|> map(fn: (r) =>
		({r with _value: r._value * scalar}))
	|> to(bucket: "ScaledDataBucket", fieldFn: (r) =>
		({scaledData: r._value}))

Hello @ZachElkins,
When you use findColumn you return an array of values.
Try either returning r [0]
or

|> map(fn: (r) =>
		({r with _value: r._value * scalar[0]}))

@Anaisdg
I just tried your solution, both ways give me error
failed to update task: invalid options: type error @19:9-19:10: expected {A with _value:B} but found [C]

I’ve also tried adding |> keep(columns: ["_value"]) in the function but I get a different error [A] is not Divisible

Hello @ZachElkins,
I’m sorry I got findRecord and findColumn confused. I fixed the comment above.
I recommend making sure that you can return the value you want first by

scalar = getValue(b: "test_bucket", me: "test_measurement", f: "test_field")

array.from(rows: [{_time: now(), _value: scalar[0]}])

I also notice that you group by field and then extract a column with a table with field f.
I would suggest maybe filtering for that field instead. Lmk if the above doesn’t work, I’m testing it now.

I know I’m getting the correct value from using the explorer tab to test the function on its own. I think the issue is that it is returning a data point instead of just the float value.

I’m using the group by because for some reason the way the data is being written (Using the python influxdb_client library) is putting all the points in new tables. Also I am filtering by field, I just forgot that line in the question. Updating it now.

Trying your array solution just gives me error calling function "getValue" cannot access element 0 of array of length 0 and when I try the array.from(...) line I get error: undefined identifier array

@ZachElkins,
I’m looking into it hold tight please

1 Like

@ZachElkins,
This worked for me:

import "array"

  getValue = (b, me, f) => {
    r = from(bucket: b)
    |> range(start: -99d)
    |> filter(fn: (r) => r["_measurement"] == me)
    |> group(columns: ["_field"])
    |> first()
    |> aggregateWindow(every: 1s, fn: last, createEmpty: false)
    |> yield(name: "last")
    |> findColumn(
            fn: (key) => (key._field == f),
            column: "_value"
    )
    return float(v: r[0])
    }

scalar =  getValue(b:"noaa",me:"average_temperature",f:"degrees" )

from(bucket: "noaa")
	|> range(start: -30d)
        |> filter(fn: (r) => r["_measurement"] == "average_temperature")
	|> filter(fn: (r) => (r["_field"] == "degrees"))
	|> set(key: "_measurement", value: "scaledMeasurements")
        // limit to make the response small for testing
        |> limit(n:5)
	|> map(fn: (r) =>
		({r with _value: r._value * scalar}))
    |> yield(name:"complete")

Are you doing anything different?
I’m using this dataset:
https://docs.influxdata.com/influxdb/cloud/reference/sample-data/#noaa-water-sample-data

You did help me uncover a static type issue with array.from() so thank you:

1 Like

It worked in the Explore Tab. Going to try it out in the actual task. Thanks!

1 Like

@Anaisdg

I’m getting an error trying to the function in the task, despite using the same one that worked in the explorer.

error calling function "findColumn" @15:6-16:40: no execution context for tableFind to use

The only other question I found on this error is findColumn() don't work when creating task - #2 by novotny.jan426 but it seems like the Flux I’m trying to use is closer to the solution than the issue.

@Anaisdg

Is it possible this is simply as issue with the version I’m using 2.0.4 (git:4e7a59bb9a). I found someone facing a similar issue that was resolved by updating to version 2.0.6

Ideally I’d like to find some other work around that doesn’t require updating influx.

Hello @ZachElkins,
I’m not sure. I’ll share your question with the Flux team, but it looks like you might have to update.

1 Like