Class: ThreeScale::Backend::Utilization
- Inherits:
-
Object
- Object
- ThreeScale::Backend::Utilization
- Includes:
- Comparable
- Defined in:
- lib/3scale/backend/utilization.rb
Instance Attribute Summary collapse
-
#current_value ⇒ Object
readonly
Returns the value of attribute current_value.
-
#max_value ⇒ Object
readonly
Returns the value of attribute max_value.
-
#metric_id ⇒ Object
readonly
Returns the value of attribute metric_id.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
Class Method Summary collapse
-
.max_in_all_metrics(service_id, app_id) ⇒ Object
Note: this can return nil.
-
.max_in_metrics(service_id, app_id, metric_ids) ⇒ Object
Note: this can return nil.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(limit, current_value) ⇒ Utilization
constructor
A new instance of Utilization.
- #ratio ⇒ Object
-
#to_s ⇒ Object
Returns in the format needed by the Alerts class.
Constructor Details
#initialize(limit, current_value) ⇒ Utilization
Returns a new instance of Utilization.
8 9 10 11 12 13 14 |
# File 'lib/3scale/backend/utilization.rb', line 8 def initialize(limit, current_value) @metric_id = limit.metric_id @period = limit.period @max_value = limit.value @current_value = current_value @encoded = encoded(limit, current_value) end |
Instance Attribute Details
#current_value ⇒ Object (readonly)
Returns the value of attribute current_value.
6 7 8 |
# File 'lib/3scale/backend/utilization.rb', line 6 def current_value @current_value end |
#max_value ⇒ Object (readonly)
Returns the value of attribute max_value.
6 7 8 |
# File 'lib/3scale/backend/utilization.rb', line 6 def max_value @max_value end |
#metric_id ⇒ Object (readonly)
Returns the value of attribute metric_id.
6 7 8 |
# File 'lib/3scale/backend/utilization.rb', line 6 def metric_id @metric_id end |
#period ⇒ Object (readonly)
Returns the value of attribute period.
6 7 8 |
# File 'lib/3scale/backend/utilization.rb', line 6 def period @period end |
Class Method Details
.max_in_all_metrics(service_id, app_id) ⇒ Object
Note: this can return nil
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/3scale/backend/utilization.rb', line 36 def self.max_in_all_metrics(service_id, app_id) application = Backend::Application.load!(service_id, app_id) usage = Usage.application_usage(application, Time.now.getutc) status = Transactor::Status.new(service_id: service_id, application: application, values: usage) # Preloads all the metric names to avoid fetching them one by one when # generating the usage reports application.load_metric_names max = status.application_usage_reports.map do |usage_report| Utilization.new(usage_report.usage_limit, usage_report.current_value) end.max # Avoid returning a utilization for disabled metrics max && max.max_value > 0 ? max : nil end |
.max_in_metrics(service_id, app_id, metric_ids) ⇒ Object
Note: this can return nil
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/3scale/backend/utilization.rb', line 58 def self.max_in_metrics(service_id, app_id, metric_ids) application = Backend::Application.load!(service_id, app_id) limits = UsageLimit.load_for_affecting_metrics( service_id, application.plan_id, metric_ids ) usage = Usage.application_usage_for_limits(application, Time.now.getutc, limits) max = limits.map do |limit| Utilization.new(limit, usage[limit.period][limit.metric_id]) end.max # Avoid returning a utilization for disabled metrics max && max.max_value > 0 ? max : nil end |
Instance Method Details
#<=>(other) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/3scale/backend/utilization.rb', line 26 def <=>(other) # Consider "disabled" the lowest ones if ratio == 0 && other.ratio == 0 return max_value <=> other.max_value end ratio <=> other.ratio end |
#ratio ⇒ Object
16 17 18 19 |
# File 'lib/3scale/backend/utilization.rb', line 16 def ratio return 0 if max_value == 0 # Disabled metric current_value/max_value.to_f end |
#to_s ⇒ Object
Returns in the format needed by the Alerts class.
22 23 24 |
# File 'lib/3scale/backend/utilization.rb', line 22 def to_s @encoded end |