-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update queries for workflow counts (#71)
* Adding Hourly Workflow Counts Realtime CAgg and change DailyWorkflowCount to Materialized Only View * initial go on using hourly classifications for workflows * remove print statement * Update count_classifications.rb * Update count_classifications.rb * remove unused var * taking care of blank case/ no entry found case * remove logs * add frames for test * adding testing for cases when end_date is before and after current day * add tests for testing period and change eriod format to match that of data pull * adding tests for the case when there are classifications from previous day * update comment on migration * update hound comments * update db.rake with new caggs * Update hourly_workflow_classification_count.rb * Update db.rake * remove redundant returns * add frozen string literal true * rubocop fix hound * adding comment on spec * rename spec to note adding counts * update migrations to be reversible
- Loading branch information
1 parent
26ecb53
commit cef6509
Showing
9 changed files
with
295 additions
and
9 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
app/models/classification_counts/hourly_workflow_classification_count.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module ClassificationCounts | ||
class HourlyWorkflowClassificationCount < ApplicationRecord | ||
self.table_name = 'hourly_classification_count_per_workflow' | ||
attribute :classification_count, :integer | ||
|
||
def readonly? | ||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
db/migrate/20240926225916_create_hourly_workflow_classification_count.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true. | ||
|
||
class CreateHourlyWorkflowClassificationCount < ActiveRecord::Migration[7.0] | ||
# we have to disable the migration transaction because creating materialized views within it is not allowed. | ||
|
||
# Due to how the front end pulls project stats (and workflow stats) all in one go, we hit performance issues; especially if a project has multiple workflows. | ||
# We have discovered that having a non-realtime/materialized only continous aggregate for our daily workflow count cagg is more performant than real time. | ||
# We plan to do the following: | ||
# - Update the daily_classification_count_per_workflow to be materialized only (i.e. non-realtime) | ||
# - Create a subsequent realtime cagg that buckets hourly that we will create data retention policies for. The plan is for up to 72 hours worth of hourly workflow classification counts of data. | ||
# - Update workflow query to first query the daily counts first and the query the hourly counts for just the specific date of now. | ||
disable_ddl_transaction! | ||
def up | ||
execute <<~SQL | ||
create materialized view hourly_classification_count_per_workflow | ||
with ( | ||
timescaledb.continuous | ||
) as | ||
select | ||
time_bucket('1 hour', event_time) as hour, | ||
workflow_id, | ||
count(*) as classification_count | ||
from classification_events where event_time > now() - INTERVAL '5 days' | ||
group by hour, workflow_id; | ||
SQL | ||
end | ||
|
||
def down | ||
execute <<~SQL | ||
DROP materialized view hourly_classification_count_per_workflow; | ||
SQL | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
db/migrate/20240926231010_add_refresh_policy_for_hourly_workflow_count.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddRefreshPolicyForHourlyWorkflowCount < ActiveRecord::Migration[7.0] | ||
disable_ddl_transaction! | ||
def up | ||
execute <<~SQL | ||
SELECT add_continuous_aggregate_policy('hourly_classification_count_per_workflow',start_offset => INTERVAL '5 days', end_offset => INTERVAL '30 minutes', schedule_interval => INTERVAL '1 h'); | ||
SQL | ||
end | ||
|
||
def down | ||
execute <<~SQL | ||
SELECT remove_continuous_aggregate_policy('hourly_classification_count_per_workflow'); | ||
SQL | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
db/migrate/20240926231325_create_data_retention_policy_for_hourly_workflow_count.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateDataRetentionPolicyForHourlyWorkflowCount < ActiveRecord::Migration[7.0] | ||
disable_ddl_transaction! | ||
def up | ||
execute <<~SQL | ||
SELECT add_retention_policy('hourly_classification_count_per_workflow', drop_after => INTERVAL '3 days'); | ||
SQL | ||
end | ||
|
||
def down | ||
execute <<~SQL | ||
SELECT remove_retention_policy('hourly_classification_count_per_workflow'); | ||
SQL | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
db/migrate/20240926233924_alter_daily_workflow_classification_count_to_materialized_only.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class AlterDailyWorkflowClassificationCountToMaterializedOnly < ActiveRecord::Migration[7.0] | ||
disable_ddl_transaction! | ||
def up | ||
execute <<~SQL | ||
ALTER MATERIALIZED VIEW daily_classification_count_per_workflow set (timescaledb.materialized_only = true); | ||
SQL | ||
end | ||
|
||
def down | ||
execute <<~SQL | ||
ALTER MATERIALIZED VIEW daily_classification_count_per_workflow set (timescaledb.materialized_only = false); | ||
SQL | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.