Skip to content
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(daily_usage): Add clock to refresh all daily usage #2735

Merged
merged 6 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/jobs/clock/compute_all_daily_usages_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Clock
class ComputeAllDailyUsagesJob < ApplicationJob
include SentryCronConcern

queue_as 'clock'

def perform
DailyUsages::ComputeAllService.call
end
end
end
11 changes: 11 additions & 0 deletions app/jobs/daily_usages/compute_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module DailyUsages
class ComputeJob < ApplicationJob
queue_as 'low_priority'

def perform(subscription, timestamp:)
DailyUsages::ComputeService.call(subscription:, timestamp:).raise_if_error!
end
end
end
9 changes: 9 additions & 0 deletions app/models/daily_usage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ class DailyUsage < ApplicationRecord
belongs_to :organization
belongs_to :customer
belongs_to :subscription

scope :refreshed_at_in_timezone, ->(timestamp) do
at_time_zone = Utils::Timezone.at_time_zone_sql(customer: "cus", organization: "org")

joins("INNER JOIN customers AS cus ON daily_usages.customer_id = cus.id")
.joins("INNER JOIN organizations AS org ON daily_usages.organization_id = org.id")
.where("DATE((daily_usages.refreshed_at)#{at_time_zone}) = DATE(:timestamp#{at_time_zone})", timestamp:)
end
end

# == Schema Information
Expand All @@ -12,6 +20,7 @@ class DailyUsage < ApplicationRecord
#
# id :uuid not null, primary key
# from_datetime :datetime not null
# refreshed_at :datetime not null
# to_datetime :datetime not null
# usage :jsonb not null
# created_at :datetime not null
Expand Down
30 changes: 15 additions & 15 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Organization < ApplicationRecord
include Currencies

EMAIL_SETTINGS = [
'invoice.finalized',
'credit_note.created'
"invoice.finalized",
"credit_note.created"
].freeze

has_many :memberships
Expand All @@ -25,8 +25,8 @@ class Organization < ApplicationRecord
has_many :add_ons
has_many :daily_usages
has_many :invites
has_many :integrations, class_name: 'Integrations::BaseIntegration'
has_many :payment_providers, class_name: 'PaymentProviders::BaseProvider'
has_many :integrations, class_name: "Integrations::BaseIntegration"
has_many :payment_providers, class_name: "PaymentProviders::BaseProvider"
has_many :payment_requests
has_many :taxes
has_many :wallets, through: :customers
Expand All @@ -38,13 +38,13 @@ class Organization < ApplicationRecord
has_many :error_details
has_many :dunning_campaigns

has_many :stripe_payment_providers, class_name: 'PaymentProviders::StripeProvider'
has_many :gocardless_payment_providers, class_name: 'PaymentProviders::GocardlessProvider'
has_many :adyen_payment_providers, class_name: 'PaymentProviders::AdyenProvider'
has_many :stripe_payment_providers, class_name: "PaymentProviders::StripeProvider"
has_many :gocardless_payment_providers, class_name: "PaymentProviders::GocardlessProvider"
has_many :adyen_payment_providers, class_name: "PaymentProviders::AdyenProvider"

has_many :hubspot_integrations, class_name: 'Integrations::HubspotIntegration'
has_many :netsuite_integrations, class_name: 'Integrations::NetsuiteIntegration'
has_many :xero_integrations, class_name: 'Integrations::XeroIntegration'
has_many :hubspot_integrations, class_name: "Integrations::HubspotIntegration"
has_many :netsuite_integrations, class_name: "Integrations::NetsuiteIntegration"
has_many :xero_integrations, class_name: "Integrations::XeroIntegration"

has_one_attached :logo

Expand All @@ -53,7 +53,7 @@ class Organization < ApplicationRecord
:per_organization
].freeze

INTEGRATIONS = %w[netsuite okta anrok xero progressive_billing hubspot auto_dunning].freeze
INTEGRATIONS = %w[netsuite okta anrok xero progressive_billing hubspot auto_dunning revenue_analytics].freeze
PREMIUM_INTEGRATIONS = INTEGRATIONS - %w[anrok]

enum document_numbering: DOCUMENT_NUMBERINGS
Expand Down Expand Up @@ -87,7 +87,7 @@ class Organization < ApplicationRecord
def logo_url
return if logo.blank?

Rails.application.routes.url_helpers.rails_blob_url(logo, host: ENV['LAGO_API_URL'])
Rails.application.routes.url_helpers.rails_blob_url(logo, host: ENV["LAGO_API_URL"])
end

def base64_logo
Expand All @@ -105,11 +105,11 @@ def eu_vat_eligible?

def payment_provider(provider)
case provider
when 'stripe'
when "stripe"
stripe_payment_provider
when 'gocardless'
when "gocardless"
gocardless_payment_provider
when 'adyen'
when "adyen"
adyen_payment_provider
end
end
Expand Down
40 changes: 40 additions & 0 deletions app/services/daily_usages/compute_all_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module DailyUsages
class ComputeAllService < BaseService
def initialize(timestamp: Time.current)
@timestamp = timestamp

super
end

def call
subscriptions.find_each do |subscription|
DailyUsages::ComputeJob.perform_later(subscription, timestamp:)
end

result
end

private

attr_reader :timestamp

def subscriptions
# NOTE(DailyUsage): For now the query filters organizations having revenue_analytics premium integrations
# This might change in the future
Subscription
.with(already_refreshed_today: already_refreshed_today)
.joins(customer: :organization)
.merge(Organization.with_revenue_analytics_support)
.joins("LEFT JOIN already_refreshed_today ON subscriptions.id = already_refreshed_today.subscription_id")
.active
.where("already_refreshed_today.subscription_id IS NULL") # Exclude subscriptions that already have a daily usage record for today in customer's timezone
.where("DATE_PART('hour', (:timestamp#{at_time_zone})) IN (0, 1, 2)", timestamp:) # Refresh the usage as soom as a subscription starts a new day in customer's timezone
end

def already_refreshed_today
vincent-pochet marked this conversation as resolved.
Show resolved Hide resolved
DailyUsage.refreshed_at_in_timezone(timestamp).select(:subscription_id)
end
end
end
51 changes: 51 additions & 0 deletions app/services/daily_usages/compute_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

module DailyUsages
class ComputeService < BaseService
def initialize(subscription:, timestamp:)
@subscription = subscription
@timestamp = timestamp
super
end

def call
if existing_daily_usage.present?
result.daily_usage = existing_daily_usage
return result
end

daily_usage = DailyUsage.create!(
organization: subscription.organization,
customer: subscription.customer,
subscription:,
external_subscription_id: subscription.external_id,
usage: ::V1::Customers::UsageSerializer.new(current_usage).serialize,
from_datetime: current_usage.from_datetime,
to_datetime: current_usage.to_datetime,
refreshed_at: timestamp
)

result.daily_usage = daily_usage
result
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
end

private

attr_reader :subscription, :timestamp

def current_usage
@current_usage ||= Invoices::CustomerUsageService.call(
customer: subscription.customer,
subscription: subscription,
apply_taxes: false
).raise_if_error!.usage
end

def existing_daily_usage
@existing_daily_usage ||= DailyUsage.refreshed_at_in_timezone(timestamp)
.find_by(subscription_id: subscription.id)
end
end
end
6 changes: 6 additions & 0 deletions clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,10 @@ module Clockwork
Sentry.capture_exception(e)
end
end

every(1.hour, 'schedule:compute_daily_usage', at: '*:15') do
Clock::ComputeAllDailyUsagesJob
.set(sentry: {"slug" => 'lago_compute_daily_usage', "cron" => '15 */1 * * *'})
.perform_later
end
end
9 changes: 9 additions & 0 deletions db/migrate/20241025081408_add_refreshed_at_to_daily_usage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class AddRefreshedAtToDailyUsage < ActiveRecord::Migration[7.1]
def change
safety_assured do
add_column :daily_usages, :refreshed_at, :datetime, null: false # rubocop:disable Rails/NotNullColumn
end
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions spec/clockwork_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,25 @@
end
end
end

describe 'schedule:compute_daily_usage' do
let(:job) { 'schedule:compute_daily_usage' }
let(:start_time) { Time.zone.parse('1 Apr 2022 00:01:00') }
let(:end_time) { Time.zone.parse('1 Apr 2022 01:01:00') }

it 'enqueue a activate subscriptions job' do
Clockwork::Test.run(
file: clock_file,
start_time:,
end_time:,
tick_speed: 1.second
)

expect(Clockwork::Test).to be_ran_job(job)
expect(Clockwork::Test.times_run(job)).to eq(1)

Clockwork::Test.block_for(job).call
expect(Clock::ComputeAllDailyUsagesJob).to have_been_enqueued
end
end
end
3 changes: 2 additions & 1 deletion spec/factories/daily_usages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
organization { customer.organization }
subscription { create(:subscription, customer:) }

external_subscriptin_id { subscription.external_id }
external_subscription_id { subscription.external_id }
from_datetime { Time.current.beginning_of_month }
to_datetime { Time.current.end_of_month }
refreshed_at { Time.current }
usage { {} }
end
end
4 changes: 4 additions & 0 deletions spec/factories/subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
started_at { 1.month.ago }
terminated_at { Time.zone.now }
end

trait :calendar do
billing_time { :calendar }
end
end
end
17 changes: 17 additions & 0 deletions spec/jobs/clock/compute_all_daily_usages_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Clock::ComputeAllDailyUsagesJob, type: :job do
subject(:compute_job) { described_class }

describe '.perform' do
before { allow(DailyUsages::ComputeAllService).to receive(:call) }

it 'removes all old webhooks' do
compute_job.perform_now

expect(DailyUsages::ComputeAllService).to have_received(:call)
end
end
end
25 changes: 25 additions & 0 deletions spec/jobs/daily_usages/compute_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe DailyUsages::ComputeJob, type: :job do
subject(:compute_job) { described_class }

let(:subscription) { create(:subscription) }
let(:timestamp) { Time.current }

let(:result) { BaseService::Result.new }

describe '.perform' do
it 'removes all old webhooks' do
allow(DailyUsages::ComputeService).to receive(:call)
.with(subscription:, timestamp:)
.and_return(result)

compute_job.perform_now(subscription, timestamp:)

expect(DailyUsages::ComputeService).to have_received(:call)
.with(subscription:, timestamp:).once
end
end
end
Loading