-
Notifications
You must be signed in to change notification settings - Fork 126
feat(alerting): Introduce SubscriptionActivity #3535
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
Conversation
1d6cf68
to
59afd2d
Compare
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.
Pull Request Overview
This PR introduces the SubscriptionActivity feature as a first step toward the new alerting functionality. It adds a new database table and associated models, services, jobs, and tests to support tracking and processing subscription activities.
- Adds a migration for usage_monitoring_subscription_activities.
- Introduces several services and jobs to record and process subscription activity.
- Updates clock scheduling and tests to enqueue subscription activity processing jobs.
Reviewed Changes
Copilot reviewed 33 out of 34 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
spec/clockwork_spec.rb | Added tests verifying the scheduling and interval configuration for subscription activity processing jobs. |
db/migrate/20250425110148_create_usage_monitoring_subscription_activities.rb | Migration file creating the subscription activities table with necessary indexes and columns. |
clock.rb | New scheduling logic for enqueuing subscription activity processing jobs using ENV configuration. |
app/services/usage_monitoring/*.rb | New services that handle tracking and processing of subscription activities. |
app/models/subscription.rb & app/models/organization.rb | Updated associations to support subscription activity tracking. |
app/jobs/usage_monitoring/.rb & app/jobs/clock/.rb | New jobs to perform subscription activity processing asynchronously. |
Files not reviewed (1)
- db/structure.sql: Language not supported
59afd2d
to
7c64e14
Compare
app/services/usage_monitoring/process_all_subscription_activities_service.rb
Outdated
Show resolved
Hide resolved
app/services/usage_monitoring/process_organization_subscription_activities_service.rb
Show resolved
Hide resolved
app/services/usage_monitoring/track_subscription_activity_service.rb
Outdated
Show resolved
Hide resolved
7c64e14
to
a21a68f
Compare
30e21a8
to
816371b
Compare
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.
Pull Request Overview
This PR introduces the SubscriptionActivity feature as a first step toward the new Alerting functionality by tracking subscription activity events and processing them periodically. Key changes include adding a migration and model for subscription activities, implementing services and jobs to track and process these activities, and updating tests and scheduling logic accordingly.
Reviewed Changes
Copilot reviewed 34 out of 35 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
spec/clockwork_spec.rb | Adds tests for scheduling and processing subscription activity jobs. |
db/migrate/20250428120148_create_usage_monitoring_subscription_activities.rb | Creates the table for subscription activities with unique constraints. |
clock.rb | Introduces scheduling for processing subscription activities with configurable interval. |
app/services/usage_monitoring/track_subscription_activity_service.rb | Adds a service to log subscription activities via bulk insert. |
app/services/usage_monitoring/process_subscription_activity_service.rb | Implements processing of individual subscription activities and associated lifetime usage updates. |
app/services/usage_monitoring/process_organization_subscription_activities_service.rb | Processes subscription activities in batches per organization. |
app/services/usage_monitoring/process_all_subscription_activities_service.rb | Dispatches jobs for processing all subscription activities across organizations. |
app/services/subscriptions/flag_refreshed_service.rb | Replaces lifetime usage flag refresh with subscription activity tracking. |
app/services/lifetime_usages/calculate_service.rb | Adjusts recalculation logic to separate invoiced and current usage updates. |
app/services/events/post_process_service.rb | Updates event processing to trigger subscription activity tracking. |
app/models/usage_monitoring/subscription_activity.rb | Defines the SubscriptionActivity model with associations. |
app/models/usage_monitoring.rb, app/models/subscription.rb, app/models/organization.rb | Introduces associations and scopes for subscription activities. |
app/jobs/usage_monitoring/* & app/jobs/clock/* | Adds jobs to process subscription activities and updates lifetime usage refresh job. |
Files not reviewed (1)
- db/structure.sql: Language not supported
Comments suppressed due to low confidence (1)
spec/clockwork_spec.rb:300
- [nitpick] The test description might be misleading as it enqueues a subscription activity processing job. Consider renaming it to 'enqueue a process subscription activity job' to better reflect its purpose.
it "enqueue a refresh flagged subscriptions job" do
app/services/usage_monitoring/process_organization_subscription_activities_service.rb
Show resolved
Hide resolved
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.
Looks good 👍
app/jobs/usage_monitoring/process_organization_subscription_activities_job.rb
Outdated
Show resolved
Hide resolved
app/services/usage_monitoring/process_organization_subscription_activities_service.rb
Show resolved
Hide resolved
app/services/usage_monitoring/process_subscription_activity_service.rb
Outdated
Show resolved
Hide resolved
app/services/usage_monitoring/track_subscription_activity_service.rb
Outdated
Show resolved
Hide resolved
If there is no threshold, we don't insert a line which was going to be be deleted right after. Closer to what existed before We loop over each organization to process activity because alerting is not a premium integration
Co-authored-by: Vincent Pochet <vincent@getlago.com>
018badd
to
1e96836
Compare
## Context First big step toward Alerting feature ## Description Follows: #3528 * Every time an event is received, we insert a SubscriptionActivity in the db, unique per subscription * Every 5 minutes, we recompute `current_usage` for all active subscription * If org has progressive billings, we check the thresholds * If org is using alerting, we'll check the alert at the same time (later * Stop using `lifetime_usage.recalculate_current_usage` but the job keeps looking at this column until there are not more jobs to process --------- Co-authored-by: Vincent Pochet <vincent@getlago.com>
…alerts (#3690) ## Context We're adding Alerting feature, see #3528, #3535, #3554, #3600 ## Description We introducing 2 new types of alerts: * LifetimeUsageAmount: to define alert based on lifetime total usage * BillableMetricUsageUnits: same as BillableMetricUsageAmount but with a number "used units" instead of currency amount. Creating a new alert is essentially creating a new child class of `UsageMonitoring::Alert` and implementing `find_value`. 🙌
Context
First big step toward Alerting feature
Description
Follows: #3528
current_usage
for all active subscriptionlifetime_usage.recalculate_current_usage
but the job keeps looking at this column until there are not more jobs to process