Skip to content

Conversation

julienbourdeau
Copy link
Contributor

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

@julienbourdeau julienbourdeau self-assigned this Apr 25, 2025
@julienbourdeau julienbourdeau force-pushed the feat/subscription-activity branch 2 times, most recently from 1d6cf68 to 59afd2d Compare April 25, 2025 11:57
@julienbourdeau julienbourdeau marked this pull request as ready for review April 25, 2025 11:57
Copy link

@Copilot Copilot AI left a 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

@julienbourdeau julienbourdeau marked this pull request as draft April 28, 2025 07:57
@julienbourdeau julienbourdeau force-pushed the feat/subscription-activity branch from 7c64e14 to a21a68f Compare April 28, 2025 10:15
@julienbourdeau julienbourdeau force-pushed the feat/subscription-activity branch 4 times, most recently from 30e21a8 to 816371b Compare April 28, 2025 12:34
@julienbourdeau julienbourdeau requested a review from Copilot April 28, 2025 12:42
@julienbourdeau julienbourdeau marked this pull request as ready for review April 28, 2025 12:42
Copy link

@Copilot Copilot AI left a 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

@julienbourdeau julienbourdeau requested a review from nudded April 28, 2025 12:44
Copy link
Collaborator

@vincent-pochet vincent-pochet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

@julienbourdeau julienbourdeau force-pushed the feat/subscription-activity branch from 018badd to 1e96836 Compare April 28, 2025 20:04
@julienbourdeau julienbourdeau merged commit e9669ec into main Apr 29, 2025
14 checks passed
@julienbourdeau julienbourdeau deleted the feat/subscription-activity branch April 29, 2025 07:07
annvelents pushed a commit that referenced this pull request May 12, 2025
## 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>
julienbourdeau added a commit that referenced this pull request May 22, 2025
…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`. 🙌
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants