-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(api-service): refactor usage queries to use materialized views #9824
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 1 commit
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
437e45b
feat(api): Add ClickHouse data seeding scripts and materialized view …
djabarovgeorge 844f5f9
refactor(analytic-logs): Remove unused WorkflowRunOptimizeCronService…
djabarovgeorge 038e027
refactor(analytic-logs): Replace TraceLogRepository with MessageSentC…
djabarovgeorge 2fc70fe
feat(analytic-logs): Implement feature flag logic for message deliver…
djabarovgeorge 6269f12
refactor(analytic-logs): Rename method for fetching message delivery …
djabarovgeorge 8bacfe5
refactor(analytic-logs): Remove MessageSentCountsRepository as it is …
djabarovgeorge f35251e
refactor(analytic-logs): Replace MessageSentCountsRepository with Wor…
djabarovgeorge 2bb1208
feat(analytic-logs): Add new method for average messages per subscrib…
djabarovgeorge 0eb0e1f
feat(activity): Implement provider volume data retrieval
djabarovgeorge 7dfb2d5
refactor(vscode): Update settings to exclude additional build artifac…
djabarovgeorge 49559ae
feat(activity): Integrate feature flag logic for interaction trend an…
djabarovgeorge 971585c
feat(activity): Enhance delivery trend chart with feature flag integr…
djabarovgeorge 7073aaf
feat(activity): Integrate feature flag logic for workflow volume data…
djabarovgeorge 2fb6a67
refactor(analytic-logs): Remove InteractionCountsRepository and relat…
djabarovgeorge 6897c70
refactor(analytic-logs): Remove WorkflowVolumeCounts and related comp…
djabarovgeorge 6325722
refactor(analytic-logs): Remove deprecated SQL migrations for workflo…
djabarovgeorge e04aa3b
refactor(activity): update feature flag keys for various charts
djabarovgeorge 2f671ca
feat(analytic-logs): Add provider_id column to traces table and updat…
djabarovgeorge 0cbd9f6
refactor(analytic-logs): Replace ProviderVolumeCounts with WorkflowAc…
djabarovgeorge c7a96d8
refactor(analytic-logs): Rename WorkflowActivityCounts to TraceRollup…
djabarovgeorge 6285055
feat: update hash
djabarovgeorge c24394d
Merge branch 'next' into wip-improve-pref-usage-2
djabarovgeorge 618db5c
refactor(activity): update feature flag keys for various charts to an…
djabarovgeorge b281e92
feat(analytics): add provider_id column to traces table and update re…
djabarovgeorge b83443b
fix(clickhouse-seeder): update trace event type selection logic and a…
djabarovgeorge 86e3791
refactor(analytics): update trace rollup table and materialized view …
djabarovgeorge f67b588
fix(analytic-logs): change provider_id type to non-nullable string in…
djabarovgeorge b51bfa2
refactor(analytics): reorder columns in trace rollup table and schema…
djabarovgeorge 80d4bce
chore: update hash to next
djabarovgeorge 9b61f6c
chore: update hash to next
djabarovgeorge 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
feat(activity): Implement provider volume data retrieval
- Loading branch information
commit 0eb0e1fd625d6da41b39e7c29bc96488434aa6c6
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
apps/api/migrations/clickhouse-migrations/4_provider_volume_counts.sql
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,30 @@ | ||
| -- Provider volume counts table | ||
| -- Pre-aggregates completed step runs by provider for efficient volume queries | ||
| -- Handles message volume per provider from step_runs table | ||
|
|
||
| CREATE TABLE IF NOT EXISTS provider_volume_counts ( | ||
| date Date, | ||
| organization_id String, | ||
| environment_id String, | ||
| workflow_id String DEFAULT '', | ||
| provider_id String, | ||
| count UInt64 | ||
| ) | ||
| ENGINE = SummingMergeTree(count) | ||
| PARTITION BY toYYYYMM(date) | ||
| ORDER BY (organization_id, environment_id, date, workflow_id, provider_id); | ||
|
|
||
| -- Materialized view populates from step_runs table (completed messaging steps) | ||
| CREATE MATERIALIZED VIEW IF NOT EXISTS provider_volume_counts_mv | ||
| TO provider_volume_counts | ||
| AS SELECT | ||
| toDate(created_at) AS date, | ||
| organization_id, | ||
| environment_id, | ||
| ifNull(workflow_id, '') AS workflow_id, | ||
| ifNull(provider_id, '') AS provider_id, | ||
| 1 AS count | ||
| FROM step_runs | ||
| WHERE | ||
| status = 'completed' | ||
| AND step_type IN ('in_app', 'email', 'sms', 'chat', 'push'); |
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
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
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
2 changes: 2 additions & 0 deletions
2
libs/application-generic/src/services/analytic-logs/provider-volume-counts/index.ts
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,2 @@ | ||
| export * from './provider-volume-counts.repository'; | ||
| export * from './provider-volume-counts.schema'; |
83 changes: 83 additions & 0 deletions
83
...ic/src/services/analytic-logs/provider-volume-counts/provider-volume-counts.repository.ts
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,83 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { PinoLogger } from 'nestjs-pino'; | ||
| import { FeatureFlagsService } from '../../feature-flags/feature-flags.service'; | ||
| import { ClickHouseService } from '../clickhouse.service'; | ||
| import { LogRepository } from '../log.repository'; | ||
| import { | ||
| PROVIDER_VOLUME_COUNTS_ORDER_BY, | ||
| PROVIDER_VOLUME_COUNTS_TABLE_NAME, | ||
| ProviderVolumeCount, | ||
| providerVolumeCountsSchema, | ||
| } from './provider-volume-counts.schema'; | ||
|
|
||
| @Injectable() | ||
| export class ProviderVolumeCountsRepository extends LogRepository< | ||
| typeof providerVolumeCountsSchema, | ||
| ProviderVolumeCount | ||
| > { | ||
| public readonly table = PROVIDER_VOLUME_COUNTS_TABLE_NAME; | ||
| public readonly identifierPrefix = 'pvc_'; | ||
|
|
||
| constructor( | ||
| protected readonly clickhouseService: ClickHouseService, | ||
| protected readonly logger: PinoLogger, | ||
| protected readonly featureFlagsService: FeatureFlagsService | ||
| ) { | ||
| super( | ||
| clickhouseService, | ||
| logger, | ||
| providerVolumeCountsSchema, | ||
| PROVIDER_VOLUME_COUNTS_ORDER_BY, | ||
| featureFlagsService | ||
| ); | ||
| this.logger.setContext(this.constructor.name); | ||
| } | ||
|
|
||
| async getProviderVolumeData( | ||
| environmentId: string, | ||
| organizationId: string, | ||
| startDate: Date, | ||
| endDate: Date, | ||
| workflowIds?: string[] | ||
| ): Promise<Array<{ provider_id: string; count: string }>> { | ||
| const workflowFilter = | ||
| workflowIds && workflowIds.length > 0 ? 'AND workflow_id IN {workflowIds:Array(String)}' : ''; | ||
|
|
||
| const query = ` | ||
| SELECT | ||
| provider_id, | ||
| sum(count) as count | ||
| FROM ${PROVIDER_VOLUME_COUNTS_TABLE_NAME} | ||
| WHERE | ||
| environment_id = {environmentId:String} | ||
| AND organization_id = {organizationId:String} | ||
| AND date >= {startDate:Date} | ||
| AND date <= {endDate:Date} | ||
| ${workflowFilter} | ||
| GROUP BY provider_id | ||
| ORDER BY count DESC | ||
| LIMIT 5 | ||
| `; | ||
|
|
||
| const params: Record<string, unknown> = { | ||
| environmentId, | ||
| organizationId, | ||
| startDate: startDate.toISOString().split('T')[0], | ||
| endDate: endDate.toISOString().split('T')[0], | ||
| }; | ||
|
|
||
| if (workflowIds && workflowIds.length > 0) { | ||
| params.workflowIds = workflowIds; | ||
| } | ||
|
|
||
| const result = await this.clickhouseService.query<{ | ||
| provider_id: string; | ||
| count: string; | ||
| }>({ | ||
| query, | ||
| params, | ||
| }); | ||
|
|
||
| return result.data; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
...eneric/src/services/analytic-logs/provider-volume-counts/provider-volume-counts.schema.ts
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,38 @@ | ||
| import { CHDate, CHString, CHUInt64, ClickhouseSchema } from 'clickhouse-schema'; | ||
|
|
||
| export const PROVIDER_VOLUME_COUNTS_TABLE_NAME = 'provider_volume_counts'; | ||
|
|
||
| const schemaDefinition = { | ||
| date: { type: CHDate() }, | ||
| organization_id: { type: CHString() }, | ||
| environment_id: { type: CHString() }, | ||
| workflow_id: { type: CHString() }, | ||
| provider_id: { type: CHString() }, | ||
| count: { type: CHUInt64() }, | ||
| }; | ||
|
|
||
| export const PROVIDER_VOLUME_COUNTS_ORDER_BY: (keyof typeof schemaDefinition)[] = [ | ||
| 'organization_id', | ||
| 'environment_id', | ||
| 'date', | ||
| 'workflow_id', | ||
| 'provider_id', | ||
| ]; | ||
|
|
||
| const clickhouseSchemaOptions = { | ||
| table_name: PROVIDER_VOLUME_COUNTS_TABLE_NAME, | ||
| engine: 'SummingMergeTree', | ||
| order_by: `(${PROVIDER_VOLUME_COUNTS_ORDER_BY.join(', ')})` as any, | ||
| additional_options: ['PARTITION BY toYYYYMM(date)'], | ||
| }; | ||
|
|
||
| export const providerVolumeCountsSchema = new ClickhouseSchema(schemaDefinition, clickhouseSchemaOptions); | ||
|
|
||
| export type ProviderVolumeCount = { | ||
| date: string; | ||
| organization_id: string; | ||
| environment_id: string; | ||
| workflow_id: string; | ||
| provider_id: string; | ||
| count: number; | ||
| }; |
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
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 wonder if to keep this in the code
process.env.NODE_ENV !== 'local'it is pretty annoying to develop with this limit locally