forked from facebookarchive/puewue-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline.rb
76 lines (62 loc) · 1.81 KB
/
timeline.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
#
# Copyright (c) 2014, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
#
require "multi_json"
require "power/collector"
module Power
class Timeline
attr_accessor :search_provider, :datacenter, :period
attr_accessor :collector_provider, :cache_provider
def initialize(search_provider, datacenter, period = nil, collector_provider = nil)
@search_provider = search_provider
@datacenter = datacenter
@period = period || "24-hours"
@collector_provider = collector_provider || Power::Collector
end
def entries
collector_entries
end
def cached_json
return entries unless cache_provider
data = cache_provider.get(collector.cache_key)
unless data
data = MultiJson.dump(entries)
cache_provider.setex(collector.cache_key, 3600, data)
end
data
end
private
def collector_entries
@collector_entries ||= collector.entries
end
def collector
interval, range = determine_period
collector_provider.new(search_provider, datacenter.id, interval, range)
end
def determine_period
case period
when "24-hours"
["5m", days_back(24, 2.5)]
when "7-days"
["1.25h", days_back(168)]
when "30-days"
["5h", days_back(720)]
when "90-days"
["15h", days_back(2160)]
when "1-year"
["5d", days_back(8760)]
end
end
def days_back(hours, hour_offset = 0)
to = (Time.now.utc) - (hour_offset * 3600)
to = to - to.sec
from = to - (hours * 3600)
from..to
end
end
end