Skip to content
Advertisement

Ruby on Rails converting Timescale SQL rate code to Ruby

I’m trying to use the Rate function from timescale to generate graph data. Right now I have a database view that does this using the concepts from the SQL code below from TimescaleDocs:

Is there a way to convert this directly to ruby code in a timeseries model to improve runtime?

Advertisement

Answer

It’s possible, but I bet building the same model in Ruby (MRI) will be much slower than over SQL runtime.

Would you mind sharing more details about what performance issues do you have?

If you want to build it with Ruby, I’d say that you should think about having some “window concept” that will be responsible for understanding the lag over some specific array.

Example:

In this case, I’m using raw numbers but you can build a more complex rule with objects that contains the time attribute.

You can round time as time_bucket using some simple module around the time. Check some ideas here.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement