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 update_report_stats(report_id): r = Report.get(Key(report_id)) stats = {'id': report_id, 'stats': {}} for desc, table, name in tables: stats['stats'].update(stats_for(str(r.key().id()), r.assetid, table)) # sleep for some time to avoid problems with FT time.sleep(4) data = json.dumps(stats) s = StatsStore.get_for_report(report_id) if s: s.json = data s.put() else: StatsStore(report_id=report_id, json=data).put() # wait a little bit to allow app store saves the data time.sleep(1.0) update_total_stats_for_report(report_id)
def update_total_stats_for_report(report_id): r = Report.get(Key(report_id)) stats = StatsStore.get_for_report(report_id) if stats: s = stats.table_accum(tables_map['Legal Amazon']) logging.info("stats for %s" % s) if s: r.degradation = s['deg'] r.deforestation = s['def'] r.put() else: logging.error("can't find stats for %s" % report_id)
def list(self, report_id): cache_key = 'stats_' + report_id data = memcache.get(cache_key) if not data: try: data = StatsStore.all().filter('report_id =', report_id).fetch(1)[0].json except IndexError: # launch caching! logging.info("launching stats calc") deferred.defer(update_report_stats, report_id) abort(404) memcache.set(cache_key, data) return Response(data, mimetype='application/json')
def update_report_stats(report_id): r = Report.get(Key(report_id)) stats = { 'id': report_id, 'stats': {} } for desc, table, name in tables: stats['stats'].update(stats_for(str(r.key().id()), r.assetid, table)) # sleep for some time to avoid problems with FT time.sleep(4) data = json.dumps(stats) s = StatsStore.get_for_report(report_id) if s: s.json = data s.put() else: StatsStore(report_id=report_id, json=data).put() # wait a little bit to allow app store saves the data time.sleep(1.0) update_total_stats_for_report(report_id)
def flush_all(): memcache.flush_all() if request.args.get('stats', ''): for x in StatsStore.all(): x.delete() return "all killed, colonel Trautman"
def flush_all(): memcache.flush_all() if request.args.get('stats',''): for x in StatsStore.all(): x.delete() return "all killed, colonel Trautman"