/
report.rb
55 lines (48 loc) · 1.62 KB
/
report.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
module Stripemetrics
class Client
module Report
def get_metrics
response = get('/v1/metrics', :require_auth => true)
raise TargetError if response.status == 404
raise AuthError if response.status == 401
(response.status == 200) ? 'success' : 'failure'
response.body
end
def print(metrics, options={},args={})
table(:border => true) do
header
metrics.each do |metric|
display_row(metric) if (args.include?(metric['_type']) || options[:all])
end
end
end
def header
row do
column 'Metric', :width => 15, :align => 'left'
column 'This month', :width => 11, :align => 'right'
column 'Last month', :width => 11, :align => 'right'
column '% change', :width => 11, :align => 'right'
column 'TSM Average', :width => 11, :align => 'right'
column 'Goal', :width => 11, :align => 'right'
end
end
def display_row(metric)
row do
column metric['name']
column with_unit(metric['_type'],metric['this_month'])
column with_unit(metric['_type'],metric['last_month'])
column rounded_percent(metric['change'])
column rounded_percent(metric['tsm_avrg'])
column metric['goal']
end
end
def rounded_percent(val)
'%.2f%' % (val*100)
end
def with_unit(type,val)
unit = '$' if type.downcase =~ /revenue/
"#{unit}#{val}"
end
end
end
end