Fix up api quota expiry display.
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled

This commit is contained in:
Dan Milne
2025-10-12 22:01:41 +11:00
parent 50b7db5ebe
commit d8889a29c0
3 changed files with 32 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
class Components::User::ProfileView < Components::Base class Components::User::ProfileView < Components::Base
include Phlex::Rails::Helpers::TimeAgoInWords include Phlex::Rails::Helpers::TimeAgoInWords
include Phlex::Rails::Helpers::DistanceOfTimeInWords
# include Rails.application.routes.url_helpers # include Rails.application.routes.url_helpers
def initialize(user:, connection:, quota_status: nil) def initialize(user:, connection:, quota_status: nil)
@@ -133,7 +134,7 @@ class Components::User::ProfileView < Components::Base
end end
elsif @connection.connected? elsif @connection.connected?
div(class: "flex items-center justify-between p-3 bg-green-50 border border-green-200 rounded") do div(class: "flex items-center justify-between p-3 bg-green-50 border border-green-200 rounded") do
div do div(class: "flex-1 mr-4") do
div(class: "text-sm font-medium text-green-800") { "Connected to TBDB" } div(class: "text-sm font-medium text-green-800") { "Connected to TBDB" }
div(class: "text-xs text-green-600 mt-1") do div(class: "text-xs text-green-600 mt-1") do
if @connection.api_base_url.present? if @connection.api_base_url.present?
@@ -157,7 +158,7 @@ class Components::User::ProfileView < Components::Base
turbo_method: "delete", turbo_method: "delete",
turbo_confirm: "Are you sure you want to disconnect from TBDB? This will affect all users." turbo_confirm: "Are you sure you want to disconnect from TBDB? This will affect all users."
}, },
class: "text-sm text-red-600 hover:text-red-700 font-medium" class: "text-sm text-red-600 hover:text-red-700 font-medium whitespace-nowrap"
) { "Disconnect" } ) { "Disconnect" }
end end
else else
@@ -202,7 +203,7 @@ class Components::User::ProfileView < Components::Base
# Reset time # Reset time
if @quota_status[:reset_at] if @quota_status[:reset_at]
div(class: "flex items-center justify-between text-xs text-gray-500") do div(class: "flex items-center justify-between text-xs text-gray-500") do
span { "Resets at #{@quota_status[:reset_at].strftime('%I:%M %p %Z')}" } span { "Resets in #{quota_reset_time_text}" }
span { "Updated #{time_ago_in_words(@quota_status[:updated_at])} ago" } span { "Updated #{time_ago_in_words(@quota_status[:updated_at])} ago" }
end end
end end
@@ -235,6 +236,25 @@ class Components::User::ProfileView < Components::Base
private private
def quota_reset_time_text
return "" unless @quota_status && @quota_status[:reset_at]
seconds = (@quota_status[:reset_at] - Time.current).to_i
return "soon" if seconds <= 0
hours = seconds / 3600
minutes = (seconds % 3600) / 60
if hours >= 24
days = hours / 24
"#{days} #{'day'.pluralize(days)}"
elsif hours > 0
"#{hours} #{'hour'.pluralize(hours)}"
else
"#{minutes} #{'minute'.pluralize(minutes)}"
end
end
def quota_percentage_color def quota_percentage_color
return "text-gray-600" unless @quota_status return "text-gray-600" unless @quota_status

View File

@@ -296,12 +296,17 @@ module Tbdb
quota_max = limits["quota_max"] quota_max = limits["quota_max"]
current_usage = usage["current_quota"] || 0 current_usage = usage["current_quota"] || 0
quota_expires_at = usage["quota_expires_at"]
if quota_max if quota_max
remaining = quota_max - current_usage remaining = quota_max - current_usage
# quota_window is in seconds, end of window is now + window duration # Use quota_expires_at from API if present, otherwise fallback to quota_window calculation
reset_time = Time.now.to_i + (limits["quota_window"] || 86400) reset_time = if quota_expires_at.present?
Rails.logger.debug "Extracted quota from response body: #{remaining}/#{quota_max}" Time.parse(quota_expires_at).to_i
else
Time.now.to_i + (limits["quota_window"] || 86400)
end
Rails.logger.debug "Extracted quota from response body: #{remaining}/#{quota_max}, resets at #{Time.at(reset_time)}"
store_quota_in_cache(remaining, quota_max, reset_time) store_quota_in_cache(remaining, quota_max, reset_time)
end end
end end

View File

@@ -80,4 +80,5 @@ Rails.application.configure do
# Apply autocorrection by RuboCop to files generated by `bin/rails generate`. # Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
# config.generators.apply_rubocop_autocorrect_after_generate! # config.generators.apply_rubocop_autocorrect_after_generate!
end end