I have this query:
from(bucket: "solartracking")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "solartracker")
|> filter(fn: (r) => r["_field"] == "address" or r["_field"] == "moonrise" or r["_field"] == "moonset" or r["_field"] == "sunrise" or r["_field"] == "sunset" or r["_field"] == "day_length" or r["_field"] == "measurementDT")
|> filter(fn: (r) => r["location"] == v.Location)
|> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({ r with moonrise: int(v: r.moonrise) }))
|> map(fn: (r) => ({ r with MoonRise: (r.moonrise * 1000000000)}))
|> map(fn: (r) => ({ r with MoonRise: if r.MoonRise == -1000000000 then "N/A" else string(v: time(v: r.MoonRise)) }))
|> map(fn: (r) => ({ r with moonset: int(v:r.moonset) }))
|> map(fn: (r) => ({ r with MoonSet: (r.moonset * 1000000000)}))
|> map(fn: (r) => ({ r with MoonSet: if r.MoonSet == -1000000000 then "N/A" else string(v: time(v: r.MoonSet)) }))
|> map(fn: (r) => ({ r with sunrise: int(v:r.sunrise) }))
|> map(fn: (r) => ({ r with SunRise: (r.sunrise * 1000000000)}))
|> map(fn: (r) => ({ r with SunRise: if r.sunrise == -1000000000 then "N/A" else string(v: time(v: r.SunRise)) }))
|> map(fn: (r) => ({ r with sunset: int(v:r.sunset) }))
|> map(fn: (r) => ({ r with SunSet: (r.sunset * 1000000000)}))
|> map(fn: (r) => ({ r with SunSet: if r.sunset == -1000000000 then "N/A" else string(v: time(v: r.SunSet)) }))
|> map(fn: (r) => ({ r with measurementDT: int(v:r.measurementDT) }))
|> map(fn: (r) => ({ r with Checktime: (r.measurementDT * 1000000000)}))
|> map(fn: (r) => ({ r with Checktime: time(v:r.Checktime)}))
|> keep(columns: ["Checktime", "address", "MoonRise", "MoonSet", "SunRise", "SunSet", "day_length"])
Iam using an API to get some astronomy data which works great. The problem is that for moonrise, moonset, sunrise, and sunset I can get any of -:-, - , Always above the Horizon, Always below the horizon, or and actual epoch timestamp for the time of moon and sun rise and set.
before inserting data I leave the unix epoch time as is or I change everything else to -1. I really do not want to show the user 1 second before the epoch as the time so I tried using the Map function with and if else to either show the actual human readable time or “N/A”. When I do that I am limited(at least using my limited knowledge) to converting everything to a string. This displays actual times like this:
2022-04-20T15:12:00.000000000Z
which is not the end of the world but not pretty. I tried to simply convert the sting back to a time with:
|> map(fn: (r) => ({ r with MoonRise: time(v: r.MoonRise)}))
but that fails when the field is “N/A”, obviously.
I am stumped. Any help is appreciated.
TIA
John