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
|
# File 'lib/prosperity/extractors/interval.rb', line 7
def to_a
if metric.sql?
fragment = <<-SQL
WITH prosperity_metric_count AS (
#{metric.sql}
)
SELECT to_char(#{metric.group_by}, '#{period.db_strf_str}') AS bucket, #{aggregate.to_sql} AS result
FROM prosperity_metric_count
WHERE (#{metric.group_by} BETWEEN '#{@start_time.iso8601}' AND '#{@end_time.iso8601}')
GROUP BY bucket
SQL
result = ActiveRecord::Base.connection.execute(fragment)
s = result.to_a.inject({}) {|accum, el|
accum.update(el["bucket"] => el["result"].to_f)
}
elsif metric.ruby?
data = []
period.each_period(start_time, end_time) do |start_time|
new = metric.value_at.call(start_time, period)
last = metric.value_at.call(start_time - period.duration, period)
data << new - last
end
return data
else
s = scope.where("#{metric.group_by} BETWEEN ? AND ?", @start_time, @end_time)
group_by_sql = "to_char(#{metric.group_by}, '#{period.db_strf_str}')"
s = s.group(group_by_sql)
s = aggregate.apply(s, group_by_sql: group_by_sql)
end
data = []
period.each_period(start_time, end_time) do |start_time|
str = start_time.strftime(period.ruby_strf_str)
value = s.has_key?(str) ? s[str].to_f : 0.0
data << value
end
data
end
|