-
Notifications
You must be signed in to change notification settings - Fork 0
Features/analytic views and table #22
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aff937d
Add migration to create historical quantity tables for materialized v…
lpi-tn f75f587
Add migration to create used_feature_per_session view
lpi-tn f85e0f5
Remove unused sqlalchemy import from used_feature_per_session view
lpi-tn 0794308
Add historical quantity models and update used_feature_per_session view
lpi-tn 36111ec
Apply suggestions from code review
lpi-tn 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
This file contains hidden or 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
71 changes: 71 additions & 0 deletions
71
welearn_database/alembic/versions/5aad97149044_add_histo_table_for_materialized_views.py
This file contains hidden or 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,71 @@ | ||
| """add_histo_table_for_materialized_views | ||
|
|
||
| Revision ID: 5aad97149044 | ||
| Revises: b84462ca3800 | ||
| Create Date: 2026-06-17 11:31:55.292978 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
| from sqlalchemy import text | ||
| from sqlalchemy.dialects import postgresql | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "5aad97149044" | ||
| down_revision: Union[str, None] = "b84462ca3800" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.create_table( | ||
| "historical_qty_document_per_corpus", | ||
| sa.Column( | ||
| "id", sa.Uuid(), server_default=text("gen_random_uuid()"), nullable=False | ||
| ), | ||
| sa.Column("source_name", sa.String(), nullable=False), | ||
| sa.Column("quantity", sa.Integer(), nullable=False), | ||
| sa.Column( | ||
| "created_at", postgresql.TIMESTAMP(), server_default="NOW()", nullable=False | ||
| ), | ||
| sa.PrimaryKeyConstraint("id"), | ||
| schema="document_related", | ||
| ) | ||
|
|
||
| op.create_table( | ||
| "historical_qty_document_in_qdrant_per_corpus", | ||
| sa.Column( | ||
| "id", sa.Uuid(), server_default=text("gen_random_uuid()"), nullable=False | ||
| ), | ||
| sa.Column("source_name", sa.String(), nullable=False), | ||
| sa.Column("quantity", sa.Integer(), nullable=False), | ||
| sa.Column( | ||
| "created_at", postgresql.TIMESTAMP(), server_default="NOW()", nullable=False | ||
| ), | ||
| sa.PrimaryKeyConstraint("id"), | ||
| schema="document_related", | ||
| ) | ||
|
lpi-tn marked this conversation as resolved.
|
||
|
|
||
| op.create_table( | ||
| "historical_qty_document_in_qdrant", | ||
| sa.Column( | ||
| "id", sa.Uuid(), server_default=text("gen_random_uuid()"), nullable=False | ||
| ), | ||
| sa.Column("quantity", sa.Integer(), nullable=False), | ||
| sa.Column( | ||
| "created_at", postgresql.TIMESTAMP(), server_default="NOW()", nullable=False | ||
| ), | ||
| sa.PrimaryKeyConstraint("id"), | ||
| schema="document_related", | ||
| ) | ||
|
lpi-tn marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_table("historical_qty_document_per_corpus", schema="document_related") | ||
| op.drop_table( | ||
| "historical_qty_document_in_qdrant_per_corpus", schema="document_related" | ||
| ) | ||
| op.drop_table("historical_qty_document_in_qdrant", schema="document_related") | ||
88 changes: 88 additions & 0 deletions
88
welearn_database/alembic/versions/6d4346fad6f4_view_used_feature_per_session.py
This file contains hidden or 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,88 @@ | ||
| """view_used_feature_per_session | ||
|
|
||
| Revision ID: 6d4346fad6f4 | ||
| Revises: 5aad97149044 | ||
| Create Date: 2026-06-17 11:54:52.441998 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "6d4346fad6f4" | ||
| down_revision: Union[str, None] = "5aad97149044" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.execute(""" | ||
| CREATE OR REPLACE VIEW grafana.used_feature_per_session | ||
| AS WITH matching_features_endpoint AS ( | ||
| SELECT | ||
| endpoint_name, | ||
| feature_name | ||
| FROM | ||
| ( | ||
| VALUES | ||
| ('/api/v1/search/by_document', 'search'), | ||
| ('/api/v1/qna/chat/answer', 'chat'), | ||
| ('/api/v1/qna/chat/agent', 'chat'), | ||
| ('/api/v1/tutor/syllabus', 'syllabus'), | ||
| ('/api/v1/user/bookmarks/:document_id', 'bookmark'), | ||
| ('/api/v1/user/:user_id/bookmarks/:document_id', 'bookmark') | ||
| ) AS t(endpoint_name, feature_name) | ||
|
lpi-tn marked this conversation as resolved.
|
||
| ), | ||
| session_feature_pair AS ( | ||
| SELECT | ||
| DISTINCT | ||
| er.session_id, | ||
| mfe.feature_name | ||
| FROM | ||
| user_related.endpoint_request er | ||
| CROSS JOIN matching_features_endpoint mfe | ||
| ORDER BY | ||
| er.session_id | ||
| ), | ||
|
lpi-tn marked this conversation as resolved.
|
||
| actual_count AS ( | ||
| SELECT | ||
| er.session_id, | ||
| mfe.feature_name, | ||
| COUNT(1) AS cnt | ||
| FROM | ||
| user_related.endpoint_request er | ||
| INNER JOIN | ||
| user_related."session" s ON | ||
| s.id = er.session_id | ||
| INNER JOIN | ||
| matching_features_endpoint mfe ON | ||
| mfe.endpoint_name = er.endpoint_name | ||
| GROUP BY | ||
| er.session_id, | ||
| mfe.feature_name | ||
| ) | ||
| SELECT | ||
| s.inferred_user_id, | ||
| sfp.session_id, | ||
| sfp.feature_name, | ||
| COALESCE(ac.cnt, 0) AS cnt, | ||
| COALESCE(ac.cnt, 0) > 0 AS is_feature_used, | ||
| s.created_at AS session_created_at | ||
| FROM | ||
| session_feature_pair sfp | ||
| LEFT JOIN actual_count ac ON | ||
| ac.feature_name = sfp.feature_name | ||
| AND ac.session_id = sfp.session_id | ||
| INNER JOIN user_related."session" s ON | ||
| s.id = sfp.session_id | ||
| ORDER BY | ||
| session_created_at | ||
| """) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.execute(""" | ||
| DROP VIEW IF EXISTS grafana.used_feature_per_session; | ||
| """) | ||
|
lpi-tn marked this conversation as resolved.
|
||
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.