You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As an ORM user, several times we need to organize migrations and heavy sql repetitions.
Today, I was working on an example and I'd like to bring this to expose the use-case. Check the full example.
I was thinking about how to bring the migration creation for complex rolling up models to avoid such boilerplate on all examples.
Let's consider we want to track count of downloads in multiple timeframes, so we have the model:
classDownload < ActiveRecord::Baseacts_as_hypertabletime_column: 'ts'scope:by_hour,->(query)doselect("time_bucket('1h', ts) as bucket, #{query}")endscope:all_by_hour,->doby_hour("count(*) as downloads").group(1)endscope:all_gems_by_hour,->doby_hour("gem_name, count(*) as downloads").group(1,2)endscope:versions_by_hour,->doby_hour("gem_name, gem_version, count(*) as downloads").group(1,2,3)endend
Then, we need to create the continuous aggregates and also rollup to another level. So, let's do the cagg anonymous class to allow us to rollup:
cagg=->(view_name)doClass.new(ActiveRecord::Base)doself.table_name="downloads_#{view_name}"SUM="sum(downloads) as downloads"scope:rollup,->(range='1d',query=SUM)doselect("time_bucket('#{range}', ts) as ts, #{query}").group(1)endscope:per_hour,->(query=SUM)dorollup('1h',query)endscope:per_day,->(query=SUM)dorollup('1d',query)endscope:per_week,->(query=SUM)dorollup('1w',query)endscope:per_month,->(query=SUM)dorollup('1mon',query)endscope:per_year,->(query=SUM)dorollup('1y',query)enddefreadonly?trueenddefself.refresh!ActiveRecord::Base.connection.execute<<-SQL CALL refresh_continuous_aggregate('#{table_name}', null, null); SQLendendend
Ideas for DSL for cascading the hierarchical continuous aggregates
I was thinking about how to avoid the boiler place as we need a more dynamic settings for such case. To generate the migrations, the minimal information would be something like:
{[:minutely,:hourly,:daily,:monthly]=>"count() as total"]
or
{%w[1m1h1d1w]=>'count(*) as total'}
This can be done in several ways like:
Embed the metadata into create_hypertable macro.
create_hypertable:name, ...,aggregates: ...
Pros: Very useful for POCs
Cons: hard to track on large projects.
Embed in the arguments of the acts_as_hypertable macro
acts_as_hypertable ...,aggregates: ...
Make it a standalone macro info that can be used in the model
How to make it part of the model, in a way that the query experience is very smooth and intuitive with the context.
Download.first# fetches data from hypertableDownload::PerMinute.first# fetches from downloads_all_per_minute continuous aggregatesDownload::PerHour.first# fetches from downloads_all_per_hour hierarchical continuous aggregates
Also, for generating the continuous aggregates + hierarchical, the migration could be extracted from it.
As an ORM user, several times we need to organize migrations and heavy sql repetitions.
Today, I was working on an example and I'd like to bring this to expose the use-case. Check the full example.
I was thinking about how to bring the migration creation for complex rolling up models to avoid such boilerplate on all examples.
Let's consider we want to track count of downloads in multiple timeframes, so we have the model:
Then, we need to create the continuous aggregates and also rollup to another level. So, let's do the
cagg
anonymous class to allow us to rollup:Then we have the models to bind the sql properly:
And then we can build a migration that creates the base continuous aggregate and rolls up the continuous aggregates with hierarchical references:
Ideas for DSL for cascading the hierarchical continuous aggregates
I was thinking about how to avoid the boiler place as we need a more dynamic settings for such case. To generate the migrations, the minimal information would be something like:
or
This can be done in several ways like:
Pros: Very useful for POCs
Cons: hard to track on large projects.
acts_as_hypertable
macroConvention over configuration
How to make it part of the model, in a way that the query experience is very smooth and intuitive with the context.
Also, for generating the continuous aggregates + hierarchical, the migration could be extracted from it.
Any ideas or comments based on your usage and how would you like to use the library?
The text was updated successfully, but these errors were encountered: