killbill-aviate 2.5.0 → 2.6.0.pre.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/controllers/aviate/usage_controller.rb +35 -6
- data/app/views/aviate/usage/show.html.erb +1 -1
- data/lib/aviate/client.rb +84 -24
- data/lib/aviate/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bb08294b224fcbe5b9f57339c19df55d9555f05177b80dfa74255a42f269c62c
|
|
4
|
+
data.tar.gz: 69d999496563e737afc5d21120eae3ec53f873791a3d4c97688c67a229787b1d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f07b4458a546623ae65dff896660ecc88e6c1325b9a03e1e98d5df70267fa5a9c1a5b5466a828828be7b6ca3df5b47ffa858c148c1850b0cbc4173be0164108a
|
|
7
|
+
data.tar.gz: 352501cf4922b8d3ef1cfae1186c4c01c97cbfc88c6ae1a099533a00430d07b316c569a5f7b2082a94c0c3dc058fa197c0f0525fb7d03535457515b61c764793
|
|
@@ -35,12 +35,21 @@ module Aviate
|
|
|
35
35
|
|
|
36
36
|
# Step 3: Retrieve unit types from catalog
|
|
37
37
|
unit_types = fetch_unit_types(@subscription, start_date, cached_options)
|
|
38
|
+
|
|
39
|
+
# Step 4: Rolled-up usage from the standard Kill Bill usage API (fast; delegates to the
|
|
40
|
+
# Aviate plugin's usage API for Aviate-metered subscriptions, so it covers both catalog
|
|
41
|
+
# kinds). Drives the Units Consumed stat and whether the expensive series fetch is needed.
|
|
42
|
+
rolled_up_units = fetch_rolled_up_units(subscription_id, start_date, end_date, cached_options)
|
|
43
|
+
unit_types = rolled_up_units.keys if unit_types.empty? && rolled_up_units.present?
|
|
38
44
|
@unit_types = unit_types
|
|
45
|
+
@units_consumed = unit_types.index_with { |unit| rolled_up_units&.[](unit) }
|
|
39
46
|
|
|
40
|
-
# Step
|
|
41
|
-
|
|
47
|
+
# Step 5: Fetch daily usage time-series in parallel — skipped when the rolled-up API
|
|
48
|
+
# authoritatively reports no usage in the period: the chart would be empty anyway and
|
|
49
|
+
# host_samples is expensive. nil (fetch failed) keeps the old behavior.
|
|
50
|
+
samples_by_unit = rolled_up_units == {} ? {} : fetch_all_samples(subscription_id, unit_types, start_date, end_date, cached_options)
|
|
42
51
|
|
|
43
|
-
# Step
|
|
52
|
+
# Step 6: Build cumulative series per unit type
|
|
44
53
|
@chart_data = build_chart_data(samples_by_unit, unit_types)
|
|
45
54
|
|
|
46
55
|
# Step 6: Fetch invoice dry run for accrued cost
|
|
@@ -57,7 +66,13 @@ module Aviate
|
|
|
57
66
|
|
|
58
67
|
def billing_period(subscription)
|
|
59
68
|
today = Time.zone.today
|
|
60
|
-
|
|
69
|
+
# A usage subscription has no charged_through_date until its first invoice: anchor the
|
|
70
|
+
# period on its (billing) start date, not on today, so usage recorded between the start
|
|
71
|
+
# date and today still falls inside the displayed period (technical-support#302).
|
|
72
|
+
ctd = parse_kb_date(subscription.charged_through_date) ||
|
|
73
|
+
parse_kb_date(subscription.billing_start_date) ||
|
|
74
|
+
parse_kb_date(subscription.start_date) ||
|
|
75
|
+
today
|
|
61
76
|
bcd = subscription.bill_cycle_day_local
|
|
62
77
|
|
|
63
78
|
if ctd > today
|
|
@@ -70,11 +85,11 @@ module Aviate
|
|
|
70
85
|
end
|
|
71
86
|
|
|
72
87
|
def parse_kb_date(date_str)
|
|
73
|
-
return
|
|
88
|
+
return nil if date_str.blank?
|
|
74
89
|
|
|
75
90
|
Date.parse(date_str.to_s)
|
|
76
91
|
rescue ArgumentError
|
|
77
|
-
|
|
92
|
+
nil
|
|
78
93
|
end
|
|
79
94
|
|
|
80
95
|
def compute_next_bcd(start_date, bill_cycle_day_local)
|
|
@@ -164,6 +179,20 @@ module Aviate
|
|
|
164
179
|
[]
|
|
165
180
|
end
|
|
166
181
|
|
|
182
|
+
# Rolled-up usage per unit type for the period, from the standard Kill Bill usage API
|
|
183
|
+
# (GET /1.0/kb/usages/{id}); for Aviate-metered subscriptions Kill Bill delegates to the
|
|
184
|
+
# Aviate plugin's usage API, so this covers both catalog kinds. Returns nil on error so
|
|
185
|
+
# callers can fall back to the (slower) sample-based behavior.
|
|
186
|
+
def fetch_rolled_up_units(subscription_id, start_date, end_date, cached_options)
|
|
187
|
+
usage = KillBillClient::Model::RolledUpUsage.find_by_subscription_id(subscription_id, start_date.to_s, end_date.to_s, cached_options)
|
|
188
|
+
return {} if usage.nil? || usage.rolled_up_units.blank?
|
|
189
|
+
|
|
190
|
+
usage.rolled_up_units.to_h { |unit| [unit.unit_type, unit.amount.to_f] }
|
|
191
|
+
rescue StandardError => e
|
|
192
|
+
Rails.logger.warn("Rolled-up usage fetch failed: #{e.message}")
|
|
193
|
+
nil
|
|
194
|
+
end
|
|
195
|
+
|
|
167
196
|
def fetch_all_samples(subscription_id, unit_types, start_date, end_date, cached_options)
|
|
168
197
|
samples_by_unit = {}
|
|
169
198
|
from = start_date.to_s
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
<% @unit_types.each do |unit_type| %>
|
|
22
22
|
<div class="stat-card card p-3">
|
|
23
23
|
<div class="stat-label text-muted small">Units Consumed (<%= unit_type %>)</div>
|
|
24
|
-
<div class="stat-value h4"><%= @chart_data[unit_type]&.last&.dig('y') || 0 %></div>
|
|
24
|
+
<div class="stat-value h4"><%= @units_consumed[unit_type] || @chart_data[unit_type]&.last&.dig('y') || 0 %></div>
|
|
25
25
|
</div>
|
|
26
26
|
<% end %>
|
|
27
27
|
|
data/lib/aviate/client.rb
CHANGED
|
@@ -7,6 +7,17 @@ module Killbill
|
|
|
7
7
|
|
|
8
8
|
CATALOG_STATE_AVIATE = 'CATALOG_STATE_AVIATE'
|
|
9
9
|
|
|
10
|
+
# Cap on points returned per host_samples series: enough for any chart while
|
|
11
|
+
# keeping multi-MB raw payloads from being expanded into millions of objects.
|
|
12
|
+
HOST_SAMPLES_MAX_POINTS = 500
|
|
13
|
+
# Short server-side cache for sample series (seconds): usage data is append-only
|
|
14
|
+
# and slightly stale charts are fine, repeat page views shouldn't re-pull MBs.
|
|
15
|
+
HOST_SAMPLES_CACHE_TTL = 300
|
|
16
|
+
# Per-request read timeout (ms) for the slow host_samples endpoint, kept just
|
|
17
|
+
# under the 60s killbill-client default so the page degrades to "no chart"
|
|
18
|
+
# (handled by callers) instead of hanging past upstream proxy timeouts.
|
|
19
|
+
HOST_SAMPLES_READ_TIMEOUT = 45_000
|
|
20
|
+
|
|
10
21
|
class << self
|
|
11
22
|
def aviate_plugin_available?(options = nil)
|
|
12
23
|
path = "#{KILLBILL_AVIATE_PREFIX}/health/data"
|
|
@@ -126,17 +137,16 @@ module Killbill
|
|
|
126
137
|
JSON.parse(e.message)
|
|
127
138
|
end
|
|
128
139
|
|
|
140
|
+
# Returns the usage time-series for the given subscription/unit type over
|
|
141
|
+
# [from_date, to_date], downsampled to at most HOST_SAMPLES_MAX_POINTS points.
|
|
142
|
+
# Raw series can hold millions of samples (multi-MB payloads, tens of seconds per
|
|
143
|
+
# call), so results are bucketed (preserving totals) and briefly cached to keep
|
|
144
|
+
# page loads fast and memory bounded until the plugin can downsample server-side.
|
|
129
145
|
def host_samples(subscription_id, sample_kind, from_date, to_date, options = nil)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
category_and_sample_kind: "#{subscription_id},#{sample_kind}"
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
request_options = build_request_options(options)
|
|
138
|
-
response = KillBillClient::API.get path, query_params, request_options
|
|
139
|
-
parse_csv_response(response.body)
|
|
146
|
+
cache_key = "aviate/host_samples/v1/#{subscription_id}/#{sample_kind}/#{from_date}/#{to_date}"
|
|
147
|
+
Rails.cache.fetch(cache_key, expires_in: HOST_SAMPLES_CACHE_TTL) do
|
|
148
|
+
fetch_host_samples(subscription_id, sample_kind, from_date, to_date, options)
|
|
149
|
+
end
|
|
140
150
|
rescue KillBillClient::API::ResponseError
|
|
141
151
|
[]
|
|
142
152
|
end
|
|
@@ -168,34 +178,84 @@ module Killbill
|
|
|
168
178
|
account_id.present? ? { accountId: account_id } : {}
|
|
169
179
|
end
|
|
170
180
|
|
|
171
|
-
def
|
|
181
|
+
def fetch_host_samples(subscription_id, sample_kind, from_date, to_date, options = nil)
|
|
182
|
+
path = "#{KILLBILL_AVIATE_PREFIX}/health/host_samples"
|
|
183
|
+
query_params = {
|
|
184
|
+
from: from_date,
|
|
185
|
+
to: to_date,
|
|
186
|
+
category_and_sample_kind: "#{subscription_id},#{sample_kind}"
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
request_options = build_request_options(options)
|
|
190
|
+
request_options[:read_timeout] = HOST_SAMPLES_READ_TIMEOUT
|
|
191
|
+
response = KillBillClient::API.get path, query_params, request_options
|
|
192
|
+
parse_csv_response(response.body, subscription_id, sample_kind)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# The host_samples endpoint returns a JSON array. Each element has a
|
|
196
|
+
# "samples" field with usage data serialized as flat comma-separated
|
|
197
|
+
# timestamp/value pairs: "ts1,val1,ts2,val2,..." (observed format).
|
|
198
|
+
# Newlines, if ever present, are normalized to commas before parsing.
|
|
199
|
+
#
|
|
200
|
+
# Raw series can contain millions of pairs for a single billing period,
|
|
201
|
+
# far more than any chart can display. To keep memory bounded, consecutive
|
|
202
|
+
# pairs are aggregated into at most +max_points+ buckets (per entry): each
|
|
203
|
+
# returned point carries the SUM of its bucket's values and the timestamp
|
|
204
|
+
# of the bucket's last sample, so downstream cumulative sums stay exact.
|
|
205
|
+
def parse_csv_response(body, expected_category = nil, expected_sample_kind = nil, max_points = HOST_SAMPLES_MAX_POINTS)
|
|
172
206
|
return [] if body.nil? || body.strip.empty?
|
|
173
207
|
|
|
174
|
-
# The host_samples endpoint returns a JSON array. Each element has a
|
|
175
|
-
# "samples" field with usage data serialized as flat comma-separated
|
|
176
|
-
# timestamp/value pairs: "ts1,val1,ts2,val2,..." (observed format).
|
|
177
|
-
# Newlines, if ever present, are normalized to commas before parsing.
|
|
178
208
|
parsed = JSON.parse(body)
|
|
179
209
|
return [] unless parsed.is_a?(Array)
|
|
180
210
|
|
|
181
211
|
result = []
|
|
182
212
|
parsed.each do |entry|
|
|
213
|
+
# Ignore entries for other categories/sample kinds: some plugin versions return
|
|
214
|
+
# unfiltered data when the requested subscription has no samples yet
|
|
215
|
+
# (technical-support#301).
|
|
216
|
+
next if expected_category.present? && entry['eventCategory'] != expected_category
|
|
217
|
+
next if expected_sample_kind.present? && entry['sampleKind'] != expected_sample_kind
|
|
218
|
+
|
|
183
219
|
samples_str = entry['samples']
|
|
184
220
|
next if samples_str.nil? || samples_str.strip.empty?
|
|
185
221
|
|
|
186
|
-
tokens
|
|
187
|
-
|
|
188
|
-
|
|
222
|
+
# Normalize separators and drop empty tokens (equivalent to the old
|
|
223
|
+
# split/reject, without materializing millions of token strings).
|
|
224
|
+
normalized = samples_str.tr("\n", ',').squeeze(',')
|
|
225
|
+
normalized = normalized[1..] if normalized.start_with?(',')
|
|
226
|
+
normalized = normalized.chomp(',')
|
|
227
|
+
next if normalized.empty?
|
|
228
|
+
|
|
229
|
+
pair_count = (normalized.count(',') + 1) / 2
|
|
230
|
+
stride = [(pair_count / max_points.to_f).ceil, 1].max
|
|
231
|
+
|
|
232
|
+
bucket_sum = 0.0
|
|
233
|
+
bucket_size = 0
|
|
234
|
+
bucket_ts = nil
|
|
235
|
+
|
|
236
|
+
normalized.scan(/([^,]*),([^,]*)(?:,|\z)/) do |ts_str, val_str|
|
|
237
|
+
ts = ts_str.strip.to_i
|
|
238
|
+
val = val_str.strip
|
|
239
|
+
next if ts.zero? || val.empty?
|
|
240
|
+
|
|
241
|
+
bucket_sum += val.to_f
|
|
242
|
+
bucket_ts = ts
|
|
243
|
+
bucket_size += 1
|
|
244
|
+
next if bucket_size < stride
|
|
245
|
+
|
|
246
|
+
result << { date: Time.at(bucket_ts).utc.strftime('%Y-%m-%dT%H:%M:%S'), value: bucket_sum }
|
|
247
|
+
bucket_sum = 0.0
|
|
248
|
+
bucket_size = 0
|
|
249
|
+
end
|
|
189
250
|
|
|
190
|
-
|
|
191
|
-
next if ts.zero?
|
|
251
|
+
next unless bucket_size.positive?
|
|
192
252
|
|
|
193
|
-
|
|
194
|
-
result << { date: date, value: val_str.to_f }
|
|
195
|
-
end
|
|
253
|
+
result << { date: Time.at(bucket_ts).utc.strftime('%Y-%m-%dT%H:%M:%S'), value: bucket_sum }
|
|
196
254
|
end
|
|
197
255
|
|
|
198
|
-
|
|
256
|
+
# Samples may span multiple entries (e.g. one per node): keep them chronological
|
|
257
|
+
# for the cumulative chart computation.
|
|
258
|
+
result.sort_by { |s| s[:date] }
|
|
199
259
|
rescue StandardError
|
|
200
260
|
[]
|
|
201
261
|
end
|
data/lib/aviate/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: killbill-aviate
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.6.0.pre.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kill Bill core team
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-09-
|
|
11
|
+
date: 2026-09-29 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: killbill-assets-ui
|
|
@@ -125,9 +125,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
125
125
|
version: '0'
|
|
126
126
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
127
|
requirements:
|
|
128
|
-
- - "
|
|
128
|
+
- - ">"
|
|
129
129
|
- !ruby/object:Gem::Version
|
|
130
|
-
version:
|
|
130
|
+
version: 1.3.1
|
|
131
131
|
requirements: []
|
|
132
132
|
rubygems_version: 3.4.10
|
|
133
133
|
signing_key:
|