Fix up api quota expiry display.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
class Components::User::ProfileView < Components::Base
|
||||
include Phlex::Rails::Helpers::TimeAgoInWords
|
||||
include Phlex::Rails::Helpers::DistanceOfTimeInWords
|
||||
# include Rails.application.routes.url_helpers
|
||||
|
||||
def initialize(user:, connection:, quota_status: nil)
|
||||
@@ -133,7 +134,7 @@ class Components::User::ProfileView < Components::Base
|
||||
end
|
||||
elsif @connection.connected?
|
||||
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-xs text-green-600 mt-1") do
|
||||
if @connection.api_base_url.present?
|
||||
@@ -157,7 +158,7 @@ class Components::User::ProfileView < Components::Base
|
||||
turbo_method: "delete",
|
||||
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" }
|
||||
end
|
||||
else
|
||||
@@ -202,7 +203,7 @@ class Components::User::ProfileView < Components::Base
|
||||
# Reset time
|
||||
if @quota_status[:reset_at]
|
||||
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" }
|
||||
end
|
||||
end
|
||||
@@ -235,6 +236,25 @@ class Components::User::ProfileView < Components::Base
|
||||
|
||||
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
|
||||
return "text-gray-600" unless @quota_status
|
||||
|
||||
|
||||
@@ -296,12 +296,17 @@ module Tbdb
|
||||
|
||||
quota_max = limits["quota_max"]
|
||||
current_usage = usage["current_quota"] || 0
|
||||
quota_expires_at = usage["quota_expires_at"]
|
||||
|
||||
if quota_max
|
||||
remaining = quota_max - current_usage
|
||||
# quota_window is in seconds, end of window is now + window duration
|
||||
reset_time = Time.now.to_i + (limits["quota_window"] || 86400)
|
||||
Rails.logger.debug "Extracted quota from response body: #{remaining}/#{quota_max}"
|
||||
# Use quota_expires_at from API if present, otherwise fallback to quota_window calculation
|
||||
reset_time = if quota_expires_at.present?
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,4 +80,5 @@ Rails.application.configure do
|
||||
|
||||
# Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
|
||||
# config.generators.apply_rubocop_autocorrect_after_generate!
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user