Calculation of duration of the time period in hours and pass this value to Influx

Hello All!
I’m calculating duration of some processes during a time period. That works well with help of

|> map(fn: (r) => ({r with state: if r._value >= 10 then "true" else "false"}))
|> stateTracking(fn: (r) => r.state=="true", durationColumn: "duration", durationUnit: 1h)

As a result I’m getting necessary values in hours.

Now I’d like to get percentage of the calculated values in correspondence with total period. So I need to get Hours values of selected period in Grafana and use something like Map from Influx in order to calculate the percentage. The question is how to get the selected period at all and in Hours?

PS. Influx itself doesn’t have any data math functions.

It seems that the question is solved with the following:

import "math"
f1 = uint (v: v.timeRangeStart)                       // Get beginning timestamp into Unix nanosecond format
f2 = uint (v: v.timeRangeStop)                        // Get ending timestamp into Unix nanosecond format
f3 = uint (v: f2 - f1)                                // Get difference in Unix nanosecond format
f4 = float (v: float(v: f3)/float(v: 3600000000000))  // get hours
f5 = math.round (x: f4)                               // get whole hours

This code at the beginning, before the main request.

|> map(fn: (r) => ({ r with percentage: (float(v: r.duration)/f5)*100.00 }))

And this piece of code in body of Flux request. It calculates percentage.duration here is the storage of duration of a process in hours.