-
Notifications
You must be signed in to change notification settings - Fork 16
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
feat: create fact_enrollments as MV (FC-0033) #478
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...pects/templates/aspects/apps/aspects/migrations/alembic/versions/0024_fact_enrollments.py
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,80 @@ | ||
""" | ||
create a materialized view to populate a denormalized fact table of enrollment events | ||
""" | ||
|
||
from alembic import op | ||
|
||
revision = "0024" | ||
down_revision = "0023" | ||
branch_labels = None | ||
depends_on = None | ||
on_cluster = ( | ||
" ON CLUSTER '{{CLICKHOUSE_CLUSTER_NAME}}' " | ||
if "{{CLICKHOUSE_CLUSTER_NAME}}" | ||
else "" | ||
) | ||
engine = "ReplicatedMergeTree" if "{{CLICKHOUSE_CLUSTER_NAME}}" else "MergeTree" | ||
|
||
|
||
def upgrade(): | ||
op.execute( | ||
f""" | ||
create table if not exists {{ ASPECTS_XAPI_DATABASE }}.fact_enrollments ( | ||
emission_time DateTime, | ||
org String, | ||
course_key String, | ||
course_name String, | ||
course_run String, | ||
actor_id String, | ||
enrollment_mode LowCardinality(String), | ||
enrollment_status String | ||
) ENGINE = {engine} | ||
PRIMARY KEY (org, course_key) | ||
ORDER BY (org, course_key, actor_id, enrollment_mode, enrollment_status, emission_time) | ||
""" | ||
) | ||
|
||
op.execute( | ||
f""" | ||
CREATE MATERIALIZED VIEW IF NOT EXISTS {{ ASPECTS_XAPI_DATABASE }}.fact_enrollments_mv | ||
{on_cluster} | ||
TO {{ ASPECTS_XAPI_DATABASE }}.fact_enrollments AS | ||
select | ||
enrollments.emission_time as emission_time, | ||
enrollments.org as org, | ||
enrollments.course_key as course_key, | ||
courses.course_name as course_name, | ||
courses.course_run as course_run, | ||
enrollments.actor_id as actor_id, | ||
enrollments.enrollment_mode as enrollment_mode, | ||
splitByString('/', enrollments.verb_id)[-1] as enrollment_status | ||
from | ||
{{ ASPECTS_XAPI_DATABASE }}.{{ ASPECTS_ENROLLMENT_EVENTS_TABLE }} enrollments | ||
join {{ ASPECTS_EVENT_SINK_DATABASE }}.course_names courses | ||
on enrollments.course_key = courses.course_key | ||
""" | ||
) | ||
|
||
op.execute( | ||
""" | ||
insert into {{ ASPECTS_XAPI_DATABASE }}.fact_enrollments | ||
select | ||
enrollments.emission_time as emission_time, | ||
enrollments.org as org, | ||
enrollments.course_key as course_key, | ||
courses.course_name as course_name, | ||
courses.course_run as course_run, | ||
enrollments.actor_id as actor_id, | ||
enrollments.enrollment_mode as enrollment_mode, | ||
splitByString('/', enrollments.verb_id)[-1] as enrollment_status | ||
from | ||
{{ ASPECTS_XAPI_DATABASE }}.{{ ASPECTS_ENROLLMENT_EVENTS_TABLE }} enrollments | ||
join {{ ASPECTS_EVENT_SINK_DATABASE }}.course_names courses | ||
on enrollments.course_key = courses.course_key | ||
""" | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.execute("DROP TABLE IF EXISTS {{ ASPECTS_XAPI_DATABASE }}.fact_enrollments") | ||
op.execute("DROP VIEW IF EXISTS {{ ASPECTS_XAPI_DATABASE }}.fact_enrollments_mv") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this could be LowCardinality too since it's just the two strings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that makes sense. I don't know how many states an enrollment can be in but it seems safe to say that
LowCardinality
would give us more flexibility than anis_enrolled
boolean while still saving space.