-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DailyUsage): Add daily_usages:fill_history task (#2751)
## Context This PR is part of the Usage Revenue and unit. Today, Lago does not offer a way to retrieve customer usage with a granularity lower than the billing period (via invoices and fees). On the other end, it is possible to get the current usage for a customer/subscription, but this usage is just a “snapshot” of the usage a the current time. ## Description This PR adds a rake task to fill the daily usage history for a specific organization
- Loading branch information
1 parent
9df5d09
commit 8da36e9
Showing
3 changed files
with
79 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'timecop' | ||
|
||
namespace :daily_usages do | ||
desc "Fill past daily usage" | ||
task :fill_history, [:organization_id, :days_ago] => :environment do |_task, args| | ||
abort "Missing organization_id\n\n" unless args[:organization_id] | ||
|
||
Rails.logger.level = Logger::INFO | ||
|
||
days_ago = (args[:days_ago] || 120).to_i.days.ago | ||
organization = Organization.find(args[:organization_id]) | ||
|
||
subscriptions = organization.subscriptions | ||
.where(status: [:active, :terminated]) | ||
.where.not(started_at: nil) | ||
.where('terminated_at IS NULL OR terminated_at >= ?', days_ago) | ||
.includes(customer: :organization) | ||
|
||
subscriptions.find_each do |subscription| | ||
from = subscription.started_at.to_date | ||
if from < days_ago | ||
from = days_ago.to_date | ||
end | ||
|
||
to = (subscription.terminated_at || Time.current).to_date | ||
|
||
(from..to).each do |date| | ||
datetime = date.in_time_zone(subscription.customer.applicable_timezone).beginning_of_day.utc | ||
|
||
next if date == Date.today && | ||
DailyUsage.refreshed_at_in_timezone(datetime).where(subscription_id: subscription.id).exists? | ||
|
||
Timecop.freeze(datetime + 5.minutes) do | ||
usage = Invoices::CustomerUsageService.call( | ||
customer: subscription.customer, | ||
subscription: subscription, | ||
apply_taxes: false, | ||
with_cache: false, | ||
max_to_datetime: datetime | ||
).raise_if_error!.usage | ||
|
||
DailyUsage.create!( | ||
organization:, | ||
customer: subscription.customer, | ||
subscription:, | ||
external_subscription_id: subscription.external_id, | ||
usage: ::V1::Customers::UsageSerializer.new(usage, includes: %i[charges_usage]).serialize, | ||
from_datetime: usage.from_datetime, | ||
to_datetime: usage.to_datetime, | ||
refreshed_at: datetime | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |