-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhr.py
245 lines (177 loc) · 6.26 KB
/
hr.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#
#
#
#
#
from xml.dom.minidom import parse, parseString
import urllib
import urllib2
import base64
import math
import sys
from datetime import datetime, timedelta
hr_username = None
hr_password = 'x'
hr_url = None
output_dir = None
def getText(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
# create a list of deals from highrise
def load_deals():
deals = []
req = urllib2.Request(hr_url)
base64string = base64.encodestring('%s:%s' % (hr_username, hr_password))[:-1]
req.add_header("Authorization", "Basic %s" % base64string)
try:
f = urllib2.urlopen(req)
except urllib2.HTTPError:
print('Eeep, did you specify a valid Highrise API key (401) or a valid Highrise url (404)?', 'Url should be like https://mycompany.highrisehq.com/deals.xml')
raise
deal_dom = parseString(f.read())
for node in deal_dom.getElementsByTagName('deal'):
deal = {}
deal['name'] = getText(node.getElementsByTagName('name')[0].childNodes)
deal['created_on'] = getText(node.getElementsByTagName('created-at')[0].childNodes)
# like 2010-08-30T05:34:02Z
deal['created_on'] = datetime.strptime( deal['created_on'] , '%Y-%m-%dT%H:%M:%SZ')
deal['status'] = getText(node.getElementsByTagName('status')[0].childNodes)
deal['status_changed_on'] = getText(node.getElementsByTagName('status-changed-on')[0].childNodes)
# like 2010-08-31
if len(deal['status_changed_on']) > 0:
deal['status_changed_on'] = datetime.strptime(deal['status_changed_on'], '%Y-%m-%d')
deal['updated-at'] = datetime.strptime( getText(node.getElementsByTagName('updated-at')[0].childNodes) , '%Y-%m-%dT%H:%M:%SZ')
deals.append(deal)
return deals
# generate a dictionary of information that can be used to request a chart from google
def generate_chart_data(deals, chart_title):
if len(deals) == 0:
return None
# colors
# buffer (white, transparent), progress (gray), completed (blue), lost (red)
chco = ['ffffff00', 'cccccc', '4d89f9', 'ff0000']
# labels
chdl = [' ', 'In progress', 'Won', 'Lost']
# find date ranges
min_date = datetime.utcnow()
max_date = datetime.utcnow()
for deal in deals:
if deal['created_on'] < min_date:
min_date = deal['created_on']
try:
if deal['status_changed_on'] > max_date:
max_date = deal['status_changed_on']
except TypeError:
pass
date_diff = max_date - min_date
total_days = float(date_diff.days)
#print('Difference in days = %d' % date_diff.days)
chd_buffer = []
chd_starts = []
chd_lost = []
chd_winner = []
chxl = []
chyl = []
for deal in deals:
#first point is diff between min_date and created_on (when the deal started)
days = (deal['created_on'] - min_date).days
# google charts are a percentage so we need to calculate this
days = math.floor(days / total_days * 100)
chd_buffer.append(str(days))
# second point is diff between created_on and status_updated_on,
# if status_updated_on doesn't exist it means the deal is still live
end_date = deal['status_changed_on']
if end_date == '':
end_date = datetime.utcnow()
days = (end_date - deal['created_on']).days
# if a deal is created today or created and closed on the same day it will not display unless we give it a value here
if days < 1:
days = 1
days = math.ceil(days / total_days * 100)
if deal['status'] == 'won':
chd_starts.append('0')
chd_winner.append(str(days))
chd_lost.append('0')
elif deal['status'] == 'lost':
chd_starts.append('0')
chd_winner.append('0')
chd_lost.append(str(days))
else:
chd_starts.append(str(days))
chd_winner.append('0')
chd_lost.append('0')
chyl.append(deal['name'])
# add a date label every x days
for day in range(int(total_days)):
if day == 0 or day % 14 == 0 or day == int(total_days) - 1:
chxl.append((min_date + timedelta(days=day)).strftime('%d-%m'))
# calculate chart area, max 300,000 px
chart_width = 600
max_height = 500
line_height = 25 # each line needs about 25px
lines = len(chyl)
height = lines * line_height + (25 * 3) # extra two lines for the title and bottom axis labels
if height > max_height:
height = max_height
print ('max height reached, data may be truncated')
chart_data = {}
# type
chart_data['cht'] = 'bhs'
# size
chart_data['chs'] = str(chart_width) + 'x' + str(height)
# data
chart_data['chd'] = 't:' + ','.join(chd_buffer) + '|' + ','.join(chd_starts) + '|' + ','.join(chd_winner) + '|' + ','.join(chd_lost)
# scale
chart_data['chds'] = '0,100'
# axis
chart_data['chxt'] = 'x,y'
# axis labels
chart_data['chxl'] = '0:|' + '|'.join(chxl) + '|1:|' + '|'.join(chyl)
# color
chart_data['chco'] = ','.join(chco)
# legend
chart_data['chdl'] = '|'.join(chdl)
# grid lines
chart_data['chg'] = '7.1,0'
# title
chart_data['chtt'] = chart_title
return chart_data
# request a chart from google
def generate_graphic(chart_data, chart_name):
if chart_data is None:
print ('no deals, no graph')
return
req = urllib2.Request('http://chart.apis.google.com/chart', urllib.urlencode(chart_data))
f = urllib2.urlopen(req)
try:
with open(output_dir + chart_name + '.png', 'wb') as img:
img.write(f.read())
except IOError:
print('Did you specify a valid output direction with a *trailing* slash?', 'C:\\temp\\', '/var/tmp/')
raise
def main():
deals = load_deals()
lost_deals = filter(lambda deal: deal['status'] == 'lost', deals)
lost_deals = generate_chart_data(lost_deals, 'Lost Deals')
generate_graphic(lost_deals, 'lost_deals')
pending_deals = filter(lambda deal: deal['status'] == 'pending', deals)
pending_deals = generate_chart_data(pending_deals, 'Pending Deals')
generate_graphic(pending_deals, 'pending_deals')
# alt syntax - lambda vs list comprehension
won_deals = filter(lambda deal: deal['status'] == 'won', deals)
#won_deals = [deal for deal in deals if deal['status'] == 'won']
won_deals = generate_chart_data(won_deals, 'Won Deals')
generate_graphic(won_deals, 'won_deals')
deals = generate_chart_data(deals, 'All Deals')
generate_graphic(deals, 'deals')
if __name__ == "__main__":
if len(sys.argv) < 4:
print "Usage: hr.py <highrise_api_key> <highrise_url> <output_dir>"
sys.exit(0)
hr_username = sys.argv[1]
hr_url = sys.argv[2]
output_dir = sys.argv[3]
main()