Module: Legion::API::Routes::CostHelpers
- Defined in:
- lib/legion/api/costs.rb
Instance Method Summary collapse
- #cost_summary(period) ⇒ Object
- #costs_by_extension(limit) ⇒ Object
- #costs_by_worker(limit) ⇒ Object
- #metering_available? ⇒ Boolean
- #metering_records ⇒ Object
Instance Method Details
#cost_summary(period) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/legion/api/costs.rb', line 58 def cost_summary(period) now = Time.now.utc today_start = Time.utc(now.year, now.month, now.day) week_start = today_start - ((today_start.wday % 7) * 86_400) month_start = Time.utc(now.year, now.month, 1) ds = metering_records worker_count = ds.distinct.select(:worker_id).exclude(worker_id: nil).count { today: sum_cost_since(ds, today_start), week: sum_cost_since(ds, week_start), month: sum_cost_since(ds, month_start), workers: worker_count, period: period } rescue ::Sequel::Error => e { today: 0.0, week: 0.0, month: 0.0, workers: 0, error: e. } end |
#costs_by_extension(limit) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/legion/api/costs.rb', line 93 def costs_by_extension(limit) metering_records .exclude(extension: nil) .group(:extension) .select( :extension, ::Sequel.function(:sum, :cost_usd).as(:total_cost), ::Sequel.function(:count, ::Sequel.lit('*')).as(:call_count) ) .order(::Sequel.desc(:total_cost)) .limit(limit) .all rescue ::Sequel::Error [] end |
#costs_by_worker(limit) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/legion/api/costs.rb', line 78 def costs_by_worker(limit) metering_records .group(:worker_id) .select( :worker_id, ::Sequel.function(:sum, :cost_usd).as(:total_cost), ::Sequel.function(:count, ::Sequel.lit('*')).as(:call_count) ) .order(::Sequel.desc(:total_cost)) .limit(limit) .all rescue ::Sequel::Error [] end |
#metering_available? ⇒ Boolean
47 48 49 50 51 52 |
# File 'lib/legion/api/costs.rb', line 47 def metering_available? defined?(Legion::Data) && Legion::Data.respond_to?(:connection) && !Legion::Data.connection.nil? rescue StandardError => e Legion::Logging.debug("CostHelpers#metering_available? check failed: #{e.}") if defined?(Legion::Logging) false end |