-
Notifications
You must be signed in to change notification settings - Fork 23
/
analysis.py
160 lines (133 loc) · 5.26 KB
/
analysis.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
# Global Forest Watch API
# Copyright (C) 2013 World Resource Institute
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import webapp2
import monitor
import common
import json
import traceback
from appengine_config import runtime_config
from gfw import forma, imazon, modis, umd, gcs
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext import ndb
# Support datasets for analysis.
_DATASETS = ['imazon', 'forma', 'modis', 'umd']
# Analysis route.
_ROUTE = r'/datasets/<dataset:(%s)>' % '|'.join(_DATASETS)
def _analyze(dataset, params):
if dataset == 'imazon':
return imazon.analyze(params)
elif dataset == 'forma':
return forma.analyze(params)
elif dataset == 'modis':
return modis.analyze(params)
elif dataset == 'umd':
return umd.analyze(params)
raise ValueError('Unsupported dataset for analysis: %s' % dataset)
def _parse_analysis(dataset, content):
if dataset == 'forma':
return forma.parse_analysis(content)
elif dataset == 'imazon':
return imazon.parse_analysis(content)
elif dataset == 'modis':
return modis.parse_analysis(content)
elif dataset == 'umd':
return umd.parse_analysis(content)
raise ValueError('Unsupported dataset for parse analysis %s' % dataset)
class GCSServingHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self):
blob_key = self.request.get('blob_key')
self.send_blob(blob_key)
class Cache():
@classmethod
def forma(cls, key, params):
params['dataset'] = 'forma'
filename = '{dataset}_{begin}_{end}_{iso}.json' \
.format(**params)
gcs_path = gcs.exists(filename)
if gcs_path:
blobstore_filename = '/gs%s' % gcs_path
blob_key = blobstore.create_gs_key(blobstore_filename)
blob_reader = blobstore.BlobReader(blob_key)
value = blob_reader.read()
entry = AnalysisEntry(id=blob_key, value=value)
entry.put()
return entry
@classmethod
def get(cls, key, dataset, params, bust):
if not bust:
entry = AnalysisEntry.get_by_id(key)
if entry:
return entry
if dataset == 'forma':
if 'iso' in params:
return cls.forma(key, params)
class AnalysisEntry(ndb.Model):
"""Analysis cache entry for datastore."""
value = ndb.TextProperty()
class Analysis(common.BaseApi):
def _send_error(self):
self.response.set_status(400)
msg = "Something's not right. Sorry about that! We notified the team."
self.response.out.write(msg)
def options(self, dataset):
"""Options to support CORS requests."""
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.headers['Access-Control-Allow-Headers'] = \
'Origin, X-Requested-With, Content-Type, Accept'
self.response.headers['Access-Control-Allow-Methods'] = 'POST, GET'
def get(self, dataset):
self.post(dataset)
def post(self, dataset):
params = self._get_params()
rid = self._get_id(params)
bust = params.get('bust')
if bust:
params.pop('bust')
entry = Cache.get(rid, dataset, params, bust)
if entry:
self._send_response(entry.value)
else:
response = None
error = None
try:
response = _analyze(dataset, params)
except Exception, e:
error = e
name = error.__class__.__name__
trace = traceback.format_exc()
if dataset == 'umd':
msg = 'Earth Engine analysis failure: %s: %s' % \
(name, error)
else:
msg = 'CartoDB %s analysis failure: %s: %s' % \
(dataset, name, error)
monitor.log(self.request.url, msg, error=trace,
headers=self.request.headers)
self._send_error()
return
if dataset == 'umd':
value = json.dumps(response)
AnalysisEntry(id=rid, value=value).put()
self._send_response(value)
else:
result = _parse_analysis(dataset, response.content)
value = json.dumps(result)
AnalysisEntry(id=rid, value=value).put()
self._send_response(value)
routes = [webapp2.Route(_ROUTE, handler=Analysis)]
handlers = webapp2.WSGIApplication(routes, debug=runtime_config.get('IS_DEV'))