-
Notifications
You must be signed in to change notification settings - Fork 73
/
base.rb
119 lines (96 loc) · 3.09 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require_dependency 'stats'
module Stats
class Base
include ::Stats::KeyHelpers
include ::ThreeScale::MethodTracing
# TODO: Replace the zone default parameter by Time.zone once the
# application's default zone is set to UTC
DEFAULT_ZONE = ActiveSupport::TimeZone.new('UTC')
def initialize(*sources)
@source = sources
end
attr_reader :source
def source_key
source
end
def self.storage
@storage ||= Storage.instance
end
def storage
self.class.storage
end
private
def detail(metric)
case metric
when Metric
{
id: metric.id,
name: metric.friendly_name,
system_name: metric.system_name,
unit: metric.unit
}
when ResponseCode
{ code: metric.code }
end
end
def period_detail(options)
range, granularity = extract_range_and_granularity(options)
timezone = options[:timezone] ? extract_timezone(options) : range.begin.time_zone
{
name: options[:period],
since: range.begin,
until: range.end,
timezone: (timezone || DEFAULT_ZONE).tzinfo.name, #tzinfo.name returns the 'Continent/Country' form
granularity: granularity
}
end
def extract_timezone(options)
raise InvalidParameterError, 'missing :timezone option' unless options[:timezone]
ActiveSupport::TimeZone.new(options[:timezone])
end
def to_time(stuff, zone = nil)
zone ||= DEFAULT_ZONE
return stuff if stuff.acts_like?(:time)
begin
zone.parse(stuff.to_s)
rescue ArgumentError
raise InvalidParameterError, 'Date parameters should have valid date/time format.'
end
end
# Finds in options[:metric] options[:metric_name] or
# options[:response_code]
def extract_metric(options)
unless options[:metric] || options[:metric_name] || options[:response_code]
raise InvalidParameterError, "Missing key(s): metric_name"
end
metric = if options[:metric]
options[:metric]
elsif m = metrics.find_by_system_name(options[:metric_name])
m
elsif options[:response_code]
ResponseCode.new(options[:response_code])
end
if metric.nil?
raise InvalidParameterError, "metric #{options[:metric_name]} not found"
end
metric
end
add_three_scale_method_tracer :extract_metric
def extract_since(options)
timezone = options[:timezone]
since = options[:since]
period = sanitize_period(options[:period])
# parse :since in supplied timezone
# use current time if no :since is given
unless since.acts_like?(:time)
raise InvalidParameterError.new('You have to supply :timezone if :since is not supplied as TimeWithZone') unless timezone
timezone = ActiveSupport::TimeZone.new(timezone) if String === timezone
since = since ? timezone.parse(since.to_s) : timezone.now
end
since.beginning_of(period.to_sym)
end
def metrics
source.first.metrics
end
end
end