-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlurryApi.rb
135 lines (105 loc) · 3.77 KB
/
FlurryApi.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require 'yaml'
require 'curb'
require 'json'
class FlurryApi
# Initialize the following:
# * Yaml configuration parameters
def initialize
@config = YAML::load(File.open('parameters.yml'))['flurry']
end
# Get all defined metrics for all apps defined in the config yaml file
# for a given date range.
# Returns an array of hashes in the form
# {"metric_name"=>"SOME_NAME", "date"=>"YYYY-MM-DD", "value"=>number}
def get_metrics_date_range(start_date, end_date)
metrics = Array.new
# Iterate through each app in the config file
@config['apps'].each do |app|
# Get metrics and add them to combined metrics
if app['metrics']
metrics += get_app_metrics(app['metrics'], app['api_key'], start_date, end_date)
end
end
metrics
end
# Get all defined metrics for all apps defined in the config yaml file
# for a given date.
def get_metrics_date(date)
get_metrics_date_range(date, date)
end
# Get all defined metrics for all apps defined in the config yaml file
# for days back from today.
def get_metrics_days_back(days_back)
now = Time.now - (1*24*60*60)
prev = Time.now - ((days_back+1)*24*60*60)
now = format_date(now)
prev = format_date(prev)
get_metrics_date_range(prev, now)
end
# Get all defined metrics for all apps defined in the config yaml file
# for the last 45 days. Flurry sessions continue to update after the
# date passes, because often it isn't reported until the next time the
# app is opened.
def get_metrics_latest
get_metrics_days_back(45)
end
private
def get_app_metrics(metrics_config, api_key, start_date, end_date)
metrics = Array.new
# Iterate through each metirc
metrics_config.each do |metric|
days = fetch_metric_list(metric['type'], api_key, start_date, end_date)
if days.kind_of?(Array)
days.each do |day|
metrics << create_metric(metric['name'], day['@date'], day['@value'])
end
else
# For single day, an array is not returned
metrics << create_metric(metric['name'], days['@date'], days['@value'])
end
end
metrics
end
def create_metric(metric_name, date, value)
metric = Hash.new
metric['metric_name'] = metric_name
metric['date'] = date
metric['value'] = value
metric
end
def format_date(date)
date.strftime("%Y-%m-%d")
end
def fetch_metric_list(metric_type, api_key, start_date, end_date)
path = "/appMetrics/#{metric_type}?apiAccessCode=#{@config['api_access_code']}"
params = "&apiKey=#{api_key}&startDate=#{start_date}&endDate=#{end_date}&groupBy=DAYS"
response = JSON.parse(api_request(path+params))
response['day']
end
def api_request(path)
api_uri = @config['api_uri']
url = api_uri + path
# Attempt the api call
result = api_call(url)
status = result.response_code
# Sometimes the calls fail unexpectedly, give it a couple tries.
i = 0
while (i < 3) && (status != 200)
result = api_call(url)
i += 1
status = result.response_code
end
if status != 200
raise "Api call #{url} failed with status #{status}"
end
result.body_str
end
def api_call(url)
# The Flurry API is rate limited at 1 request per second... lame. Delaying to be safe.
sleep(1)
result = Curl.get(url) do |api_http|
api_http.headers['Accept'] = 'application/json'
end
result
end
end