You’ll have to look a bit into Flux or InfluxQL. I think Flux is more powerful.
Have a look at this topic for some inspiration: