Class: ThreeScale::Backend::Transactor::Status::UsageReport

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale/backend/transactor/usage_report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.timestamp)
end

Instance Attribute Details

#periodObject (readonly)

Returns the value of attribute period.



6
7
8
# File 'lib/3scale/backend/transactor/usage_report.rb', line 6

def period
  @period
end

#typeObject (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_limitObject (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

Returns:

  • (Boolean)


70
71
72
# File 'lib/3scale/backend/transactor/usage_report.rb', line 70

def authorized?
  @status.authorized?
end

#current_valueObject



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

Returns:

  • (Boolean)


66
67
68
# File 'lib/3scale/backend/transactor/usage_report.rb', line 66

def exceeded?
  current_value > max_value
end

#inspectObject



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_valueObject



22
23
24
# File 'lib/3scale/backend/transactor/usage_report.rb', line 22

def max_value
  @usage_limit.value
end

#metric_idObject



18
19
20
# File 'lib/3scale/backend/transactor/usage_report.rb', line 18

def metric_id
  @usage_limit.metric_id
end

#metric_nameObject



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_callsObject

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_hObject



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_xmlObject



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

#usageObject



62
63
64
# File 'lib/3scale/backend/transactor/usage_report.rb', line 62

def usage
  @status.usage
end