|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class LlmQuotaUsage < ActiveRecord::Base |
| 4 | + self.table_name = "llm_quota_usages" |
| 5 | + |
| 6 | + QuotaExceededError = Class.new(StandardError) |
| 7 | + |
| 8 | + belongs_to :user |
| 9 | + belongs_to :llm_quota |
| 10 | + |
| 11 | + validates :user_id, presence: true |
| 12 | + validates :llm_quota_id, presence: true |
| 13 | + validates :input_tokens_used, presence: true, numericality: { greater_than_or_equal_to: 0 } |
| 14 | + validates :output_tokens_used, presence: true, numericality: { greater_than_or_equal_to: 0 } |
| 15 | + validates :usages, presence: true, numericality: { greater_than_or_equal_to: 0 } |
| 16 | + validates :started_at, presence: true |
| 17 | + validates :reset_at, presence: true |
| 18 | + |
| 19 | + def self.find_or_create_for(user:, llm_quota:) |
| 20 | + usage = find_or_initialize_by(user: user, llm_quota: llm_quota) |
| 21 | + |
| 22 | + if usage.new_record? |
| 23 | + now = Time.current |
| 24 | + usage.started_at = now |
| 25 | + usage.reset_at = now + llm_quota.duration_seconds.seconds |
| 26 | + usage.input_tokens_used = 0 |
| 27 | + usage.output_tokens_used = 0 |
| 28 | + usage.usages = 0 |
| 29 | + usage.save! |
| 30 | + end |
| 31 | + |
| 32 | + usage |
| 33 | + end |
| 34 | + |
| 35 | + def reset_if_needed! |
| 36 | + return if Time.current < reset_at |
| 37 | + |
| 38 | + now = Time.current |
| 39 | + update!( |
| 40 | + input_tokens_used: 0, |
| 41 | + output_tokens_used: 0, |
| 42 | + usages: 0, |
| 43 | + started_at: now, |
| 44 | + reset_at: now + llm_quota.duration_seconds.seconds, |
| 45 | + ) |
| 46 | + end |
| 47 | + |
| 48 | + def increment_usage!(input_tokens:, output_tokens:) |
| 49 | + reset_if_needed! |
| 50 | + |
| 51 | + increment!(:usages) |
| 52 | + increment!(:input_tokens_used, input_tokens) |
| 53 | + increment!(:output_tokens_used, output_tokens) |
| 54 | + end |
| 55 | + |
| 56 | + def check_quota! |
| 57 | + reset_if_needed! |
| 58 | + |
| 59 | + if quota_exceeded? |
| 60 | + raise QuotaExceededError.new( |
| 61 | + I18n.t( |
| 62 | + "discourse_ai.errors.quota_exceeded", |
| 63 | + relative_time: AgeWords.distance_of_time_in_words(reset_at, Time.now), |
| 64 | + ), |
| 65 | + ) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + def quota_exceeded? |
| 70 | + return false if !llm_quota |
| 71 | + |
| 72 | + (llm_quota.max_tokens.present? && total_tokens_used > llm_quota.max_tokens) || |
| 73 | + (llm_quota.max_usages.present? && usages > llm_quota.max_usages) |
| 74 | + end |
| 75 | + |
| 76 | + def total_tokens_used |
| 77 | + input_tokens_used + output_tokens_used |
| 78 | + end |
| 79 | + |
| 80 | + def remaining_tokens |
| 81 | + return nil if llm_quota.max_tokens.nil? |
| 82 | + [0, llm_quota.max_tokens - total_tokens_used].max |
| 83 | + end |
| 84 | + |
| 85 | + def remaining_usages |
| 86 | + return nil if llm_quota.max_usages.nil? |
| 87 | + [0, llm_quota.max_usages - usages].max |
| 88 | + end |
| 89 | + |
| 90 | + def percentage_tokens_used |
| 91 | + return 0 if llm_quota.max_tokens.nil? || llm_quota.max_tokens.zero? |
| 92 | + [(total_tokens_used.to_f / llm_quota.max_tokens * 100).round, 100].min |
| 93 | + end |
| 94 | + |
| 95 | + def percentage_usages_used |
| 96 | + return 0 if llm_quota.max_usages.nil? || llm_quota.max_usages.zero? |
| 97 | + [(usages.to_f / llm_quota.max_usages * 100).round, 100].min |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +# == Schema Information |
| 102 | +# |
| 103 | +# Table name: llm_quota_usages |
| 104 | +# |
| 105 | +# id :bigint not null, primary key |
| 106 | +# user_id :bigint not null |
| 107 | +# llm_quota_id :bigint not null |
| 108 | +# input_tokens_used :integer not null |
| 109 | +# output_tokens_used :integer not null |
| 110 | +# usages :integer not null |
| 111 | +# started_at :datetime not null |
| 112 | +# reset_at :datetime not null |
| 113 | +# created_at :datetime not null |
| 114 | +# updated_at :datetime not null |
| 115 | +# |
| 116 | +# Indexes |
| 117 | +# |
| 118 | +# index_llm_quota_usages_on_llm_quota_id (llm_quota_id) |
| 119 | +# index_llm_quota_usages_on_user_id_and_llm_quota_id (user_id,llm_quota_id) UNIQUE |
| 120 | +# |
0 commit comments