def cacheTopWeeklyDomains(self, weekly_stats): memcache_top_domains_key = 'latest_top_domains' top_domains_cache = memcache.get(memcache_top_domains_key) if top_domains_cache: logging.info('getting top domains from cache') return top_domains_cache if weekly_stats is None: logging.info('fetching top 10 domains from db') weekly_stats=WeeklyDomainStats.gql('ORDER BY date DESC, count DESC ').fetch(10) if weekly_stats is None: logging.info('Not enough data') return None domains = [ w.domain for w in weekly_stats if w.count > 10 ] default_domain = ' ... for more domains visit: http://www.instaright.com/stats' domains.append(default_domain) logging.info('returning and storing top domain info in cache') memcache.set(memcache_top_domains_key, domains) return domains
def weeklyFeed(self,tdate, reqId): try: if not tdate: s="<?xml version=\"1.0\" encoding=\"UTF-8\"?><daily_stats>" s+="</daily_stats>" self.response.out.write(s) return else: try: targetdate=datetime.datetime.strptime(tdate, '%Y-%m-%d').date() except: e = sys.exc_info()[1] logging.error('error formating date %s => %s' %(tdate, e)) targetdate=datetime.date.today() - datetime.timedelta(days=1) logging.info('Fetching stats for %s' % targetdate) weeklyStats=WeeklyDomainStats.gql('WHERE date = :1 ORDER BY count DESC ', targetdate) if weeklyStats is None or weeklyStats.count() == 0 : logging.info('Not enough data for graph') self.response.out.write('Not enough data for graph') return logging.info('about to prepare %s query results for visualisation.' % weeklyStats.count()) xmpp_handler = XMPPHandler() xmpp_handler.cacheTopWeeklyDomains(weeklyStats) datastore = self.prepareforvisualize(weeklyStats) # prepare for output description ={"domain": ("string", "Domain"), "count":("number", "Total Links")} columnnames = [ "domain", "count" ] data_table = gviz_api.DataTable(description) data_table.LoadData(datastore) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(data_table.ToJSonResponse(columns_order=(columnnames), req_id=reqId)) except: e = sys.exc_info()[1] logging.error('Error while fetching weekly feed %s' % e)
def latestWeeklyStats(self): memcache_key = "weekly_stats_dates"+str(datetime.date.today()) logging.info('looking up for memcache key: %s' % memcache_key) if memcache.get(memcache_key): logging.info('key found in cache') return memcache.get(memcache_key) topWeeklyStat = WeeklyDomainStats.gql(' ORDER by date desc ').get() if topWeeklyStat: dateInStats = topWeeklyStat.date if not dateInStats: logging.info('can\'t figure out last succesful weekly stats execution') return None numberOfStats = 5 dayOffset = 7 datesForWeeklyStats = [] while numberOfStats > 0: logging.info('weekly stats date : %s ' % dateInStats) datesForWeeklyStats.append(dateInStats) dateInStats = dateInStats - datetime.timedelta(days = 7) numberOfStats = numberOfStats - 1 logging.info("weekly stats dates %s" % datesForWeeklyStats) memcache.set(memcache_key, datesForWeeklyStats) return datesForWeeklyStats