Skip to content

Instantly share code, notes, and snippets.

@chan-lee
Created August 22, 2014 07:20
Show Gist options
  • Save chan-lee/f0b2e1227f579ee25379 to your computer and use it in GitHub Desktop.
Save chan-lee/f0b2e1227f579ee25379 to your computer and use it in GitHub Desktop.
send json from appengine with python to google charts api
from collections import namedtuple
class AdminLogsByTeamHandler(AdminHandler): # get
@admin_required
def get(self, team_id):
team_key = ndb.Key(Team, int(team_id))
Quantity = namedtuple("Quantity", ["date", "account"])
resolution_by_day = {}
logs = Log.query(Log.team == team_key).fetch()
for log in logs:
date = log.created.date().isoformat()
account = log.sender.id()
q = Quantity(date=date, account=account)
if q in resolution_by_day:
resolution_by_day[q] += 1
else:
resolution_by_day[q] = 1
# make 2d array
date_set = set(d for d, a in resolution_by_day)
account_set = set(a for d, a in resolution_by_day)
result = []
# make header
header = ['Date']
for account in account_set:
a = Account.get_by_id(int(account))
name = a.name if a is not None else account
header.append(name)
result.append(header)
# make rows
for date in date_set:
row = [date]
for account in account_set:
q = Quantity(date=date, account=account)
if q in resolution_by_day:
row.append(resolution_by_day[q])
else :
row.append(0)
result.append(row)
self.response_object(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment