Class: ThreeScale::Backend::Transactor::Status::UsageReport
- Inherits:
-
Object
- Object
- ThreeScale::Backend::Transactor::Status::UsageReport
- Defined in:
- lib/3scale/backend/transactor/usage_report.rb
Instance Attribute Summary collapse
-
#period ⇒ Object
readonly
Returns the value of attribute period.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#usage_limit ⇒ Object
readonly
Returns the value of attribute usage_limit.
Instance Method Summary collapse
- #authorized? ⇒ Boolean
- #current_value ⇒ Object
- #exceeded? ⇒ Boolean
-
#initialize(status, usage_limit) ⇒ UsageReport
constructor
A new instance of UsageReport.
- #inspect ⇒ Object
- #max_value ⇒ Object
- #metric_id ⇒ Object
- #metric_name ⇒ Object
-
#remaining_same_calls ⇒ Object
Returns the number of identical calls that can be made before violating the limits defined in the usage report.
-
#remaining_time(from = Time.now) ⇒ Object
Returns -1 if the period is eternity.
- #to_h ⇒ Object
- #to_xml ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(status, usage_limit) ⇒ UsageReport
Returns a new instance of UsageReport.
8 9 10 11 12 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 8 def initialize(status, usage_limit) @status = status @usage_limit = usage_limit @period = usage_limit.period.new(status.) end |
Instance Attribute Details
#period ⇒ Object (readonly)
Returns the value of attribute period.
6 7 8 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 6 def period @period end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 6 def type @type end |
#usage_limit ⇒ Object (readonly)
Returns the value of attribute usage_limit.
6 7 8 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 6 def usage_limit @usage_limit end |
Instance Method Details
#authorized? ⇒ Boolean
70 71 72 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 70 def @status. end |
#current_value ⇒ Object
26 27 28 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 26 def current_value @current_value ||= @status.value_for_usage_limit(@usage_limit) end |
#exceeded? ⇒ Boolean
66 67 68 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 66 def exceeded? current_value > max_value end |
#inspect ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 74 def inspect "#<#{self.class.name} " \ "type=#{type} " \ "period=#{period} " \ "metric_name=#{metric_name} " \ "max_value=#{max_value} " \ "current_value=#{current_value}>" end |
#max_value ⇒ Object
22 23 24 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 22 def max_value @usage_limit.value end |
#metric_id ⇒ Object
18 19 20 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 18 def metric_id @usage_limit.metric_id end |
#metric_name ⇒ Object
14 15 16 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 14 def metric_name @metric_name ||= @status.application.metric_name(metric_id) end |
#remaining_same_calls ⇒ Object
Returns the number of identical calls that can be made before violating the limits defined in the usage report.
Authrep (with actual usage): suppose that we have a metric with a daily limit of 10, a current usage of 0, and a given usage of 2. After taking into account the given usage, the number of identical calls that could be performed is (10-2)/2 = 4.
Authorize (with predicted usage): suppose that we have a metric with a daily limit of 10, a current usage of 0, and a given usage of 2. This time, the given usage is not taken into account, as it is predicted, not to be reported. The number of identical calls that could be performed is 10/2 = 5.
Returns -1 when there is not a limit in the number of calls.
55 56 57 58 59 60 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 55 def remaining_same_calls return 0 if remaining <= 0 usage = compute_usage usage > 0 ? remaining/usage : -1 end |
#remaining_time(from = Time.now) ⇒ Object
Returns -1 if the period is eternity. Otherwise, returns the time remaining until the end of the period in seconds.
32 33 34 35 36 37 38 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 32 def remaining_time(from = Time.now) if period.granularity == Period::Granularity::Eternity -1 else (period.finish - from).ceil end end |
#to_h ⇒ Object
83 84 85 86 87 88 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 83 def to_h { period: period, metric_name: metric_name, max_value: max_value, current_value: current_value } end |
#to_xml ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 90 def to_xml xml = String.new # Node header add_head(xml) # Node content add_period(xml) if period != Period[:eternity] add_values(xml) # Node closing add_tail(xml) xml end |
#usage ⇒ Object
62 63 64 |
# File 'lib/3scale/backend/transactor/usage_report.rb', line 62 def usage @status.usage end |