def get(self): callers = {} cursor = None q = PhoneLog.all() while q is not None: # If the app stored a cursor during a previous request, use it. if cursor: q.with_cursor(cursor) logQuery = q.fetch(500) cursor = q.cursor() if len(logQuery) > 0: for e in logQuery: if e.phone.find('@gmail.com') > 0: caller = e.phone.split('/')[0] logging.debug('truncating %s to %s' % (e.phone,caller)) if caller not in callers: callers[caller] = 1 e.phone = caller e.put() else: logging.debug('nothing left!') break
def get(self): user = users.get_current_user() if user and users.is_current_user_admin(): greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" % (user.nickname(), users.create_logout_url("/"))) else: greeting = ("<a href=\"%s\">Sign in</a>." % users.create_login_url("/")) # do some analysis on the request history... total = 0 callers = dict() reqs = dict() cursor = None # Start a query for all Person entities. q = PhoneLog.all() while q is not None: # If the app stored a cursor during a previous request, use it. if cursor: q.with_cursor(cursor) logQuery = q.fetch(500) cursor = q.cursor() if len(logQuery) > 0: total += len(logQuery) logging.debug('parsing log entries %s' % total) for e in logQuery: if e.phone in callers: callers[e.phone] += 1 else: callers[e.phone] = 1 # add up all of the unique stop IDs requestString = e.body.split() if len(requestString) >= 2: stopID = requestString[1] elif len(requestString) > 0: stopID = requestString[0] if len(requestString) > 0 and stopID.isdigit() and len(stopID) == 4: if stopID in reqs: reqs[stopID] += 1 else: reqs[stopID] = 1 else: logging.debug('nothing left!') break # review the results and generate the data for the template caller_stats = [] sorted_callers = callers.items() sorted_callers.sort(key=itemgetter(1),reverse=True) for key,value in sorted_callers: caller_stats.append({'caller':key, 'counter':value, }) uniques = len(sorted_callers) # display some recent call history results = [] q = db.GqlQuery("SELECT * FROM PhoneLog ORDER BY date DESC") logQuery = q.fetch(30) if len(logQuery) > 0: for r in logQuery: results.append({'phone':r.phone, 'body':r.body, 'outboundSMS':r.outboundSMS, 'date':r.date,}) else: results.append({'phone':'empty', 'body':'empty', 'outboundSMS':'empty', 'date':'empty',}) logging.error("We couldn't find any history!?!") # add the counter to the template values template_values = {'greeting':greeting, 'total':total, 'uniques':uniques, 'callers':caller_stats, 'events':results, } # create a page that provides a form for sending an SMS message path = os.path.join(os.path.dirname(__file__), 'web/admin.html') self.response.out.write(template.render(path,template_values))