Don't understanding how to return variable

Hi ! I’m new to Flux and I don’t understand how to return the value of a variable !
I read the doc https://docs.influxdata.com/flux/v0.50/guides/conditional-logic for the 0.5 Flux version and I want to return the hour value.
Here is my code, but I think it doesn’t work as well…

My code

transformHour = (number=<-) => {
     number 
        |> hour:
            if number._value >= 60 then number._value / 60 
            else if number._value >= 3600 then number._value / 3600 
            else number._value
        }

from(bucket: "test/autogen")
  |> range(start: dashboardTime)
  |> filter(fn: (r) => r._measurement == "my_measurement" and (r._field == "my_field"))
  |> count()
  |> transformHour()

This is what I obtain:


That’s not what I want.
The visualization mode is Single Stat.
I just would like to understand how to return hour value.

Thank you in advance
Nico

EDIT
I tried this code with a yield() funtion, but it seems not working.

transformHour = (number=<-) => 
    { 
	number 
        |> hour:
            if number._value >= 60 then number._value / 60 
            else if number._value >= 3600 then number._value / 3600 
            else number._value
   		||> yield(name: hour)
    } 

Where is the problem?

The UI can only visualise table data ATM. Instead, you will have better luck with the InfluxDB CLI.

influx repl should drop you into a shell :+1:

Hello @NicolasDrapier,
I believe you’re missing a map(). The following works for me:

from(bucket: "default")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "cpu")
  |> filter(fn: (r) => r._field == "usage_system")
  |> filter(fn: (r) => r.cpu == "cpu-total")
  |> map(fn: (r) => ({
        output:        
        if r._value >= 60.0 then r._value / 60.0 
            else if r._value >= 3600.0 then r._value / 3600.0 
            else r._value
            }))
1 Like

This work from me !
Thank you so much

1 Like