/
main.py
239 lines (184 loc) · 6.76 KB
/
main.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
#!/usr/bin/python2
import sys
sys.stdout = sys.stderr
import atexit
import threading
import os, os.path
import cherrypy
import pandas as pd
#cherrypy.config.update({'environment': 'embedded'})
cherrypy.config.update({'server.socket_port': 8090,
'engine.autoreload_on': False,
'log.access_file': '/tmp/unmillenium_access.log',
'log.error_file': '/tmp/unmillenium_error.log'})
print "Loading data"
data = pd.read_csv('data_final_02.csv.bz2',low_memory=False)
#store = pd.HDFStore('data_final_02.h5')
#data = store['table']
print "Done."
class Home(object):
@cherrypy.expose
def index(self):
return open('index.html')
class RetrieveCountries(object):
exposed = True
@cherrypy.tools.accept(media='application/json')
def POST(self, area=None):
if area is not None:
df = data[data["Region"] == area]
else:
df = data
#group = df.groupby("CountryName")
#unique_coutnries = group.first()
#final_df = unique_coutnries[['Region','CountryId']]
#final_df.reset_index(level=0,inplace=True)
un = df.drop_duplicates(cols='CountryName', take_last=True)
final_df = un[['CountryId','Region','CountryName']]
return final_df.to_json(orient='records')
class RetrieveCountriesin1990(object):
exposed = True
@cherrypy.tools.accept(media='application/json')
def POST(self, seriesId=None):
copy_data = data
filter1 = copy_data['SeriesRowId'] == int(seriesId)
filter2 = copy_data['Year'] == 1990
copy_data = copy_data[filter1 & filter2]
#group = copy_data.groupby("CountryName")
#unique_coutnries = group.first()
#final_df = unique_coutnries[['Region','CountryId']]
#final_df.reset_index(level=0,inplace=True)
un = copy_data.drop_duplicates(cols='CountryName', take_last=True)
final_df = un[['CountryId','Region','CountryName']]
return final_df.to_json(orient='records')
class RetrieveIndicators(object):
exposed = True
@cherrypy.tools.accept(media='application/json')
def POST(self, ids=None):
if ids is not None:
filters = map(int,ids)
df = data[data['SeriesRowId'].isin(filters)]
else:
df = data
#group = df.groupby('SeriesName')
#unique_indicators = group.first()
#final_df = unique_indicators[['GoalName','GoalId','TargetName','TargetId','IndicatorName','SeriesRowId','IsMdg','Disc-level']]
#final_df.reset_index(level=0,inplace=True)
un = df.drop_duplicates(cols='SeriesName', take_last=True)
final_df = un[['SeriesName','GoalName','GoalId','TargetName','TargetId','IndicatorName','SeriesRowId','IsMdg','Disc-level']]
return final_df.to_json(orient='records')
class RetrieveData(object):
exposed = True
@cherrypy.tools.accept(media='application/json')
def POST(self, colname=None, subset_key=None, subset_value=None, isunique=None):
#self.data = pd.read_csv('data_treated.csv')
datajson = data
if subset_key is not None and subset_value is not None:
datajson = datajson[datajson[subset_key] == int(subset_value)]
if colname is not None:
datajson = datajson[colname]
if isunique is not None:
datajson = pd.unique(datajson).tolist()
return datajson
return datajson.to_json(orient='records')
class RetrieveUnique(object):
exposed = True
@cherrypy.tools.accept(media='text/plain')
def POST(self, colname=None, subset_key=None, subset_value=None):
datajson = data
if subset_key is not None and subset_value is not None:
datajson = datajson[datajson[subset_key] == subset_value]
if colname is not None:
datajson = datajson[datajson["Disc-level"] == 0]
datajson = datajson[colname]
datajson = pd.unique(datajson).tolist()
#if datajson is not digit():
return ',,,'.join(datajson)
#else:
# return datajson
class RetrieveSeries(object):
exposed = True
@cherrypy.tools.accept(media='application/json')
def POST(self, column=None, value=None, date=None, region=None, ctr=None):
#import pdb
#pdb.set_trace()
filters = map(int,value)
# NEED TO CHECK LENGTH OF value HERE BECAUSE E MAY HAVE FOUR ELEMENTS
#if len(filters) > 2:
if len(filters) == 4:
xvals = filters[:2]
yvals = filters[2:]
dx = data[data[column].isin(xvals)]
dy = data[data[column].isin(yvals)]
else:
dx = data[data[column] == filters[0]]
dy = data[data[column] == filters[1]]
if region is not None:
dx = dx[dx['Region'] == region]
dy = dy[dy['Region'] == region]
if ctr is not None:
dx = dx[dx['CountryId'] == int(ctr)]
dy = dy[dy['CountryId'] == int(ctr)]
ddatex = dx[dx['Year'] == int(date)]
dx['axis'] = 'x'
ddatey = dy[dy['Year'] == int(date)]
dy['axis'] = 'y'
datajson = pd.concat([dx,dy])
datajson['gx_min'] = dx['Value'].min()
datajson['gx_max'] = dx['Value'].max()
datajson['gy_min'] = dy['Value'].min()
datajson['gy_max'] = dy['Value'].max()
datajson['dx_min'] = ddatex['Value'].min()
datajson['dx_max'] = ddatex['Value'].max()
datajson['dy_min'] = ddatey['Value'].min()
datajson['dy_max'] = ddatey['Value'].max()
return datajson.to_json(orient='records');
if __name__ == '__main__':
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/retrievecountries': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'application/json')],
},
'/retrievecountriesin1990': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'application/json')],
},
'/retrieveindicators': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'application/json')],
},
'/retrievedata': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'application/json')],
},
'/retrieveunique': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')],
},
'/retrieveseries': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'application/json')],
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './static'
}
}
webapp = Home()
webapp.retrievecountries = RetrieveCountries()
webapp.retrievecountriesin1990 = RetrieveCountriesin1990()
webapp.retrieveindicators = RetrieveIndicators()
webapp.retrievedata = RetrieveData()
webapp.retrieveunique = RetrieveUnique()
webapp.retrieveseries = RetrieveSeries()
#cherrypy.quickstart(webapp, '/unmilleniumgoals', conf)
cherrypy.quickstart(webapp, '/', conf)