def show_tables(): """ creates a report for specified month """ month = request.args.get('month','') year = request.args.get('year','') day= request.args.get('day','') if not month or not year: abort(400) start = date(year=int(year), month=int(month), day=int(day)) r = Report(start=start, finished=False) r.put() assetid = request.args.get('assetid', '') month = request.args.get('fmonth','') year = request.args.get('fyear','') day= request.args.get('fday','') if assetid and month and year and day: r.end = date(year=int(year), month=int(month), day=int(day)) r.assetid = assetid r.finished = True r.put() deferred.defer(update_report_stats, str(r.key())) return r.as_json()
def close(self, report_id): """ close current and create new one """ r = Report.get(Key(report_id)) if not r.finished: ndfi = NDFI(r.comparation_range(), r.range()) data = ndfi.freeze_map(r.base_map(), int(settings.FT_TABLE_ID), r.key().id()) logging.info(data) if 'data' not in data: abort(400) data = data['data']['id'] r.close(data) cache_key = NDFIMapApi._cache_key(report_id) memcache.delete(cache_key) # open new report new_report = Report(start=date.today()) new_report.put() return str(new_report.key()) return "already finished"
class StatsTest(unittest.TestCase, GoogleAuthMixin): """ test for reporting api """ def setUp(self): app.config['TESTING'] = True self.app = app.test_client() self.login('*****@*****.**', 'testuser') # generate data for reports self.r = Report(start=date(year=2011, month=2, day=1), end=date(year=2011, month=3, day=1), finished=True) self.r.put(); stats = { 'id': str(self.r.key()), 'stats': { '0000_01': { 'id': '01', 'table': '0000', 'def': 1, 'deg': 2 }, '0000_02': { 'id': '02', 'table': '0000', 'def': 1, 'deg': 2 }, '0001_02': { 'id': '02', 'table': '0001', 'def': 1, 'deg': 2 } } } StatsStore(report_id=str(self.r.key()), json=json.dumps(stats)).put(); def test_table(self): """ get reports for all regions """ rv = self.app.get('/api/v0/stats/0000?reports=' + str(self.r.key().id())) self.assertEquals(200, rv.status_code) self.assertEquals('text/csv; charset=utf-8', rv.content_type) rows = rv.data.split('\n') header = rows[0].split(',') row1= rows[1].split(',') self.assertEquals('report_id', header[0]) self.assertEquals('start_date', header[1]) self.assertEquals('end_date', header[2]) self.assertEquals('deforestated', header[3]) self.assertEquals('degradated\r', header[4]) self.assertEquals(str(self.r.key().id()), row1[0]) self.assertEquals(2, float(row1[3])) self.assertEquals(4, float(row1[4])) def test_table_zone(self): """ get reports for all regions """ rv = self.app.get('/api/v0/stats/0000/02?reports=' + str(self.r.key().id())) self.assertEquals(200, rv.status_code) self.assertEquals('text/csv; charset=utf-8', rv.content_type) rows = rv.data.split('\n') header = rows[0].split(',') row1= rows[1].split(',') self.assertEquals('report_id', header[0]) self.assertEquals('start_date', header[1]) self.assertEquals('end_date', header[2]) self.assertEquals('deforestated', header[3]) self.assertEquals('degradated\r', header[4]) self.assertEquals(str(self.r.key().id()), row1[0]) self.assertAlmostEquals(1, float(row1[3])) self.assertAlmostEquals(2, float(row1[4])) def test_non_existing(self): rv = self.app.get('/api/v0/stats/0002?reports=' + str(self.r.key().id())) self.assertEquals(404, rv.status_code) def test_non_existing_report(self): rv = self.app.get('/api/v0/stats/0000?reports=123123,' + str(self.r.key().id())) self.assertEquals(404, rv.status_code)
class StatsTest(unittest.TestCase): """ test for reporting api """ def setUp(self): # create resources self.polygon = [[[-63.154907226557498, -4.8118385341739005], [-64.143676757807498, -4.8118385341739005], [-64.132690429682498, -6.2879986723276584], [-62.814331054682498, -6.3535159310087908]]] self.inv_polygon = [[[y, x] for x, y in self.polygon[0]]] self.r = Report(start=date(year=2011, month=2, day=1), end=date(year=2011, month=3, day=1), finished=True) self.r.put() self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() self.area = Area(geo=json.dumps(self.inv_polygon), added_by=None, type=1, cell=self.cell) self.area.put() self.area.create_fusion_tables() self.ndfi = NDFI(self.r.comparation_range(), self.r.range()) self.stats = Stats() def test_stats(self): """ """ # freeze map with on area report_id = self.r.key().id() data = self.ndfi.freeze_map(self.r.base_map(), int(settings.FT_TABLE_ID), self.r.key().id()) self.assertTrue('data' in data) self.assertTrue('id' in data['data']) self.assertTrue(len(data['data']['id']) > 0) asset_id = data['data']['id'] print "report id: ", asset_id # get stats for this area st = self.stats.get_stats_for_polygon(report_id, asset_id, self.polygon) self.assertTrue(st is not None) polygon_stats = st[0] print polygon_stats self.assertTrue(float(polygon_stats['def']) > 0.0) self.assertAlmostEquals(0.0, float(polygon_stats['deg'])) def_area = float(polygon_stats['def']) print "deforested area: ", def_area # get stats for this area # move the polygon a little bit p = [[[x, y + 1.0] for x, y in self.polygon[0]]] st = self.stats.get_stats_for_polygon(report_id, asset_id, p) self.assertTrue(st is not None) print st[0] polygon_stats = st['data']['properties']['classHistogram']['values']['null']['values'] self.assertTrue(float(polygon_stats['def']) > 0.0) self.assertTrue(float(polygon_stats['def']) < def_area) print "new deforested area: ", float(polygon_stats['def']) # search in area whiout deforesation p = [[[x+4.0, y] for x, y in self.polygon[0]]] st = self.stats.get_stats_for_polygon(report_id, asset_id, p) self.assertTrue(st is not None) polygon_stats = st[0] self.assertAlmostEquals(0.0, float(polygon_stats['def'])) self.assertAlmostEquals(0.0, float(polygon_stats['deg']))