def userFeed(self, targetdate, user, reqId): if user: logging.info('getting stats for user: %s' %user) stats = UserStats.gql('WHERE instapaper_account = :1 order by date desc', user).fetch(14) if stats is None: logging.info('Not enough data for graph') self.repsonse.out.write('Not enough data for graph') return stats = [ x for x in stats if x is not None ] logging.info('retrieved %s stats' % len(stats)) description = {"date": ("string", "Date"), "count":("number", "Count")} columnnames = [ "date", "count" ] data_table = gviz_api.DataTable(description) userCnt = [] for uCnt in stats: logging.info('account:%s' % uCnt.to_xml()) entry = {"date": uCnt.date, "count":uCnt.count} userCnt.append(entry) data_table.LoadData(userCnt) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(data_table.ToJSonResponse(columns_order=(columnnames) , req_id=reqId)) return if not targetdate: today = datetime.date.today() yesterday=datetime.date.today() - datetime.timedelta(days=1) targetdate=yesterday else: try: targetdate=datetime.datetime.strptime(targetdate, '%Y-%m-%d').date() except: e = sys.exc_info()[1] logging.error('error formating date %s => %s' %(targetdate, e)) targetdate=datetime.date.today() - datetime.timedelta(days=1) logging.info('User stats feed') stats = UserStats.gql('WHERE date = :1 and count > 10 order by count desc', targetdate).fetch(50) if stats is None: logging.info('Not enough data for graph') self.repsonse.out.write('Not enough data for graph') return stats = [ x for x in stats if x is not None ] logging.info('retrieved %s stats' % len(stats)) description = {"account": ("string", "User"), "count":("number", "Count")} columnnames = [ "account", "count" ] data_table = gviz_api.DataTable(description) userCnt = [] for uCnt in stats: logging.info('account:%s' % uCnt.to_xml()) entry = {"account": uCnt.instapaper_account, "count":uCnt.count} userCnt.append(entry) data_table.LoadData(userCnt) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(data_table.ToJSonResponse(columns_order=(columnnames) , req_id=reqId))
def get(self, d): if d is None: logging.info('no date.exit') return date=datetime.datetime.strptime(d, '%Y-%m-%d').date() #if period == 'daily': # allUsers = UserStats.all() #else: # self.response.out.write('get outta here') # return allUsers = UserStats.gql('WHERE date = :1', date).fetch(1000) if not allUsers: logging.info('not stats for %s delete , exit' %d) return logging.info('total stats for %s delete %d' % (d, len(allUsers))) for u in allUsers: u.delete() logging.info('done')
def getBadge(self): targetdate=datetime.datetime.now().date() - datetime.timedelta(days=1) stats = UserStats.gql('WHERE date = :1 and count > 10 order by count desc', targetdate).fetch(3) logging.info('trophy badger: fetched stats %s' % len(stats)) stats = [ s.instapaper_account for s in stats if s is not None ] if stats is None or len(stats) == 0: logging.info('Not enough data for calc badge') return None if stats[0] == self.user: logging.info('User was number ONE user yesterday') return '1' if stats[1] == self.user: logging.info('User was number TWO user yesterday') return '2' if stats[2] == self.user: logging.info('User was number THREE user yesterday') return '3' logging.info('trophy badge %s: not initialized' % self.user ) return None
def get(self, period): date_ = self.request.get('date', None) if date_: date = datetime.datetime.strptime(date_, '%Y-%m-%d') else: date = datetime.datetime.now() - datetime.timedelta(days=1) logging.info('fetching stats for %s and %s' %(period,str(date.date()))) if period == 'daily': stats = UserStats.gql('WHERE date = :1 order by count desc', date).fetch(100) elif period == 'weekly': self.response.out.write('TODO') return else: self.response.out.write('Get outta here') return if not stats: self.response.out.write('not stats for %s and date %s retreived no data' %( period, date_)) return self.response.headers["Content-type"] = "application/json" self.response.out.write(simplejson.dumps(stats, default=lambda s: {'u':{'account':s.instapaper_account, 'cout': s.count}}))
def post(self): dt = self.request.get("date", None) cursor = self.request.get("last_cursor", None) logging.info("date from request %s " % dt) if dt is None: date = datetime.datetime.now().date() - datetime.timedelta(days=1) logging.info("aggregatine users from yesterday.") else: date = datetime.datetime.strptime(dt, "%Y-%m-%d").date() if date >= datetime.datetime.now().date(): logging.info("too early , wait") self.response.out.write("too early . wait") return memcache_key_sessions = ( "sessions_for_date_" + str(datetime.datetime.today().date()) + "_" + str(date) + "_" + str(cursor) ) cached_sessions = memcache.get(memcache_key_sessions) if cached_sessions: logging.info("getting from cache for date %s" % str(date)) sessions = cached_sessions else: sessions = SessionModel.getDailyDataWithOffset(date, cursor) logging.info("session batch size %d" % len(sessions)) memcache.set(memcache_key_sessions, sessions) if sessions is None: logging.info("no sessions for date %s" % str(date)) return for s in sessions: memcache_key_s = "user_detail_" + str(datetime.datetime.now().date()) + "_" + str(date) + "_" + str(s.key()) if memcache.get(memcache_key_s): logging.info("skippin processed key %s for date %s" % (s.key(), str(date))) continue # links stats add to queue taskqueue.add( queue_name="data-consolidation", url="/aggregate_data", params={"sessionKey": s.key(), "upper_limit_date": date}, ) # TODO also create tas cue for user consolidation userStats = UserStats.gql("WHERE instapaper_account = :1 and date = :2", s.instaright_account, date).get() if userStats is None: logging.info("no user stats for user: %s and date: %s" % (s.instaright_account, str(date))) userStats = UserStats() userStats.instapaper_account = s.instaright_account userStats.count = 1 userStats.date = date userStats.put() else: logging.info("updating user stats for user %s and date %s" % (s.instaright_account, str(date))) userStats.count = userStats.count + 1 userStats.put() user_detail = UserDetails.gql("WHERE instapaper_account = :1", s.instaright_account).get() if user_detail is None: logging.info("new user: %s" % s.instaright_account) user_detail = UserDetails() user_detail.instapaper_account = s.instaright_account user_detail.last_active_date = s.date user_detail.put() # task queue that gathers info try: fetch_task_url = "/user/" + urllib2.quote(s.instaright_account) + "/fetch" except: logging.warn("can't fetch info for user %s " % s.instaright_account) logging.info("adding task on url %s" % fetch_task_url) taskqueue.add(queue_name="user-info", url=fetch_task_url) else: logging.info("updating usage for user: %s" % s.instaright_account) user_detail.last_active_date = s.date user_detail.links_added = user_detail.links_added + 1 user_detail.put() memcache.set(memcache_key_s, s.key()) # IMPORTANT:delete daily badges for user memcache.delete("badge_" + s.instaright_account) logging.info("done for date %s" % str(date)) self.response.out.write("done for date %s" % str(date))