Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classification user groups migrations and models #23

Merged
merged 12 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module UserGroupClassificationCounts
class DailyGroupClassificationCount < ApplicationRecord
self.table_name = 'daily_group_classification_count_and_time'
attribute :classification_count, :integer
attribute :total_session_time, :integer
attribute :user_group_id, :integer

def readonly?
true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module UserGroupClassificationCounts
class DailyGroupProjectClassificationCount < ApplicationRecord
self.table_name = 'daily_group_classification_count_and_time_per_project'
attribute :classification_count, :integer
attribute :total_session_time, :integer
attribute :project_id, :integer
attribute :user_group_id, :integer

def readonly?
true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module UserGroupClassificationCounts
class DailyGroupUserClassificationCount < ApplicationRecord
self.table_name = 'daily_group_classification_count_and_time_per_user'
attribute :classification_count, :integer
attribute :total_session_time, :integer
attribute :user_id, :integer
attribute :user_group_id, :integer

def readonly?
true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module UserGroupClassificationCounts
class DailyGroupUserProjectClassificationCount < ApplicationRecord
self.table_name = 'daily_group_classification_count_and_time_per_user_per_project'
attribute :classification_count, :integer
attribute :total_session_time, :integer
attribute :user_id, :integer
attribute :project_id, :integer
attribute :user_group_id, :integer

def readonly?
true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module UserGroupClassificationCounts
class DailyGroupUserWorkflowClassificationCount < ApplicationRecord
self.table_name = 'daily_group_classification_count_and_time_per_user_per_workflow'
attribute :classification_count, :integer
attribute :total_session_time, :integer
attribute :user_id, :integer
attribute :workflow_id, :integer
attribute :user_group_id, :integer

def readonly?
true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module UserGroupClassificationCounts
class DailyGroupWorkflowClassificationCount < ApplicationRecord
self.table_name = 'daily_group_classification_count_and_time_per_workflow'
attribute :classification_count, :integer
attribute :total_session_time, :integer
attribute :workflow_id, :integer
attribute :user_group_id, :integer

def readonly?
true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

class CreateDailyGroupClassificationsCountAndTime < ActiveRecord::Migration[7.0]
# we have to disable the migration transaction because creating materialized views within it is not allowed.
disable_ddl_transaction!
def change
execute <<~SQL
create materialized view daily_group_classification_count_and_time
with (
timescaledb.continuous
) as
select
time_bucket('1d', event_time) as day,
user_group_id,
count(*) as classification_count,
sum(session_time) as total_session_time
from classification_user_groups
group by day, user_group_id;
SQL
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class CreateDailyGroupProjectClassificationCountAndTime < ActiveRecord::Migration[7.0]
# we have to disable the migration transaction because creating materialized views within it is not allowed.
disable_ddl_transaction!
def change
execute <<~SQL
create materialized view daily_group_classification_count_and_time_per_project
with (
timescaledb.continuous
) as
select
time_bucket('1d', event_time) as day,
user_group_id,
project_id,
count(*) as classification_count,
sum(session_time) as total_session_time
from classification_user_groups
group by day, user_group_id, project_id;
SQL
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class CreateDailyGroupWorkflowClassificationCountAndTime < ActiveRecord::Migration[7.0]
# we have to disable the migration transaction because creating materialized views within it is not allowed.
disable_ddl_transaction!
def change
execute <<~SQL
create materialized view daily_group_classification_count_and_time_per_workflow
with (
timescaledb.continuous
) as
select
time_bucket('1d', event_time) as day,
user_group_id,
workflow_id,
count(*) as classification_count,
sum(session_time) as total_session_time
from classification_user_groups
group by day, user_group_id, workflow_id;
SQL
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

class CreateDailyGroupUserClassificationCountAndTime < ActiveRecord::Migration[7.0]
# we have to disable the migration transaction because creating materialized views within it is not allowed.
disable_ddl_transaction!
## We should note that this view looks very similar to the daily_user_classification_count_and_time (where the columns of that table are day, user_id, classification_count, total_session_time).
## The only difference between this view and daily_user_classification_count_and_time is that we are grouping by user_group_id (i.e. user_group_id is a column in this view).
## Even though the view are very similar we cannot query from just daily_user_classification_count_and_time to get stats info of that user for that group.
## Reason being:
## A) daily_user_classification_count_and_time does not consider when a user has joined a group.
## (So if we queried for all time for the user group, the user's old classifications [when he/she/they were not part of the group] would be counted towards the user group)
## [Vice versa if a user LEAVES a user group]
## B) On the flip side, we cannot query from just daily_group_classification_count_and_time_per_user, because not every user belongs to a group.
def change
execute <<~SQL
create materialized view daily_group_classification_count_and_time_per_user
with (
timescaledb.continuous
) as
select
time_bucket('1d', event_time) as day,
user_group_id,
user_id,
count(*) as classification_count,
sum(session_time) as total_session_time
from classification_user_groups
group by day, user_group_id, user_id;
SQL
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class CreateDailyGroupUserProjectCountAndTime < ActiveRecord::Migration[7.0]
# we have to disable the migration transaction because creating materialized views within it is not allowed.
disable_ddl_transaction!
## We should note that this view looks very similar to the daily_user_classification_count_and_time_per_project
## (where the columns of that table are day, user_id, classification_count, total_session_time, project_id).
## The only difference between this view and daily_user_classification_count_and_time_per_project is that we are grouping by user_group_id (i.e. user_group_id is a column in this view).
## Even though the views are very similar we cannot query from just daily_user_classification_count_and_time_per_project to get stats info of that user for that group.
## Reason being:
## A) daily_user_classification_count_and_time_per_project does not consider WHEN a user has joined a group.
## (So if we queried for all time for the user group, the user's old classifications [when he/she/they were not part of the group] would be counted towards the user group)
## [Vice versa if a user LEAVES a user group]
## B) On the flip side, we cannot query from just daily_group_classification_count_and_time_per_user_per_prject when querying for just user stats, because not every user belongs to a group.
def change
execute <<~SQL
create materialized view daily_group_classification_count_and_time_per_user_per_project
with (
timescaledb.continuous
) as
select
time_bucket('1d', event_time) as day,
user_group_id,
user_id,
project_id,
count(*) as classification_count,
sum(session_time) as total_session_time
from classification_user_groups
group by day, user_group_id, user_id, project_id;
SQL
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

class CreateDailyGroupUserWorkflowCountAndTime < ActiveRecord::Migration[7.0]
# we have to disable the migration transaction because creating materialized views within it is not allowed.
disable_ddl_transaction!
## We should note that this view looks very similar to the daily_user_classification_count_and_time_per_workflow
## (where the columns of that table are day, user_id, classification_count, total_session_time, workflow_id).
## The only difference between this view and daily_user_classification_count_and_time_per_workflow is that we are grouping by user_group_id (i.e. user_group_id is a column in this view).
## Even though the views are very similar we cannot query from just daily_user_classification_count_and_time_per_workflow to get stats info of that user for that group.
## Reason being:
## A) daily_user_classification_count_and_time_per_workflow does not consider WHEN a user has joined a group.
## (So if we queried for all time for the user group, the user's old classifications [when he/she/they were not part of the group] would be counted towards the user group)
## [Vice versa if a user LEAVES a user group]
## B) On the flip side, we cannot query from just daily_group_classification_count_and_time_per_user_per_workflow when querying for just user stats, because not every user belongs to a group.
def change
execute <<~SQL
create materialized view daily_group_classification_count_and_time_per_user_per_workflow
with (
timescaledb.continuous
) as
select
time_bucket('1d', event_time) as day,
user_group_id,
user_id,
workflow_id,
count(*) as classification_count,
sum(session_time) as total_session_time
from classification_user_groups
group by day, user_group_id, user_id, workflow_id;
SQL
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_07_12_142126) do
ActiveRecord::Schema[7.0].define(version: 2023_08_01_163827) do
yuenmichelle1 marked this conversation as resolved.
Show resolved Hide resolved
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "timescaledb"
Expand Down