def get_query(self, *args): if args[0] == 'recipient': recipient = args[1].decode('utf8') recipients = list(set([recipient, recipient[:35]])) if len(args) > 2: year = int(args[2]) lines = SupportLine.query( SupportLine.year == year, SupportLine.recipient.IN(recipients)).order( SupportLine.code, SupportLine.kind, SupportLine.year) else: lines = SupportLine.query( SupportLine.recipient.IN(recipients)).order( SupportLine.code, SupportLine.kind, SupportLine.year) elif args[0] == 'kind': logging.error('SupportsApi:kind args=%r' % (args, )) kind = args[1] if len(args) > 2: code = args[2] if len(args) > 3: if args[3] == 'aggregated': lines = SupportLine.query( SupportLine.entity_kind == kind, SupportLine.prefixes == code) lines = self.aggregate_lines(lines, ['title']) else: year = int(args[3]) lines = SupportLine.query( SupportLine.entity_kind == kind, SupportLine.prefixes == code, SupportLine.year == year) logging.error('SupportsApi:kind args=%r' % (args, )) if len(args) > 4: if args[4] == 'aggregated': lines = self.aggregate_lines( lines, ['title', 'year']) else: assert (False) else: lines = SupportLine.query(SupportLine.entity_kind == kind, SupportLine.prefixes == code) else: lines = SupportLine.query(SupportLine.entity_kind == kind) else: code = args[0] if len(args) > 1: year = int(args[1]) lines = SupportLine.query(SupportLine.year == year, SupportLine.prefixes == code).order( SupportLine.recipient, SupportLine.kind, SupportLine.year) else: lines = SupportLine.query(SupportLine.prefixes == code).order( SupportLine.recipient, SupportLine.kind, SupportLine.year) return lines
def get_query(self,*args): if args[0] == 'recipient': recipient = args[1].decode('utf8') recipients = list(set([ recipient, recipient[:35] ])) if len(args)>2: year=int(args[2]) lines = SupportLine.query(SupportLine.year==year,SupportLine.recipient.IN(recipients)).order(SupportLine.code,SupportLine.kind,SupportLine.year) else: lines = SupportLine.query(SupportLine.recipient.IN(recipients)).order(SupportLine.code,SupportLine.kind,SupportLine.year) elif args[0] == 'entity': entity = args[1].decode('utf8') lines = SupportLine.query(SupportLine.entity_id==entity).order(-SupportLine.year) elif args[0] == 'kind': #logging.error('SupportsApi:kind args=%r' % (args,)) kind = args[1] if len(args)>2: code = args[2] if len(args)>3: if args[3] == 'aggregated': lines = SupportLine.query(SupportLine.entity_kind==kind,SupportLine.prefixes==code) lines = self.aggregate_lines(lines,['title']) else: year = int(args[3]) lines = SupportLine.query(SupportLine.entity_kind==kind,SupportLine.prefixes==code,SupportLine.year==year) #logging.error('SupportsApi:kind args=%r' % (args,)) if len(args)>4: if args[4] == 'aggregated': lines = self.aggregate_lines(lines,['title','year']) else: assert(False) else: lines = SupportLine.query(SupportLine.entity_kind==kind,SupportLine.prefixes==code) else: lines = SupportLine.query(SupportLine.entity_kind==kind) else: code = args[0] if len(args)>1: year=int(args[1]) lines = SupportLine.query(SupportLine.year==year,SupportLine.prefixes==code).order(SupportLine.recipient,SupportLine.kind,SupportLine.year) else: lines = SupportLine.query(SupportLine.prefixes==code).order(SupportLine.recipient,SupportLine.kind,SupportLine.year) return lines
def get_query(self,id): lines = [] if ALL_DIGITS.match(id) is not None: lines = Entity.query(Entity.id==id).fetch(1) else: lines = Entity.query(Entity.name==id).fetch(1) if len(lines)>0: ret = lines[0].to_dict() id = ret['id'] logging.debug('entity-api: id='+id) supports = SupportLine.query(SupportLine.entity_id==id).order(-SupportLine.year).fetch(1000) ret['supports'] = [x.to_dict() for x in supports] exemptions = MRExemptionRecord.query(MRExemptionRecord.entity_id==id).order(-MRExemptionRecord.start_date).fetch(1000) ret['exemptions'] = [x.to_dict() for x in exemptions] return ret else: return {}
def get_query(self, id): lines = [] if ALL_DIGITS.match(id) is not None: lines = Entity.query(Entity.id == id).fetch(1) else: lines = Entity.query(Entity.name == id).fetch(1) if len(lines) > 0: ret = lines[0].to_dict() id = ret['id'] logging.debug('entity-api: id=' + id) supports = SupportLine.query(SupportLine.entity_id == id).order( -SupportLine.year).fetch(1000) ret['supports'] = [x.to_dict() for x in supports] exemptions = MRExemptionRecord.query( MRExemptionRecord.entity_id == id).order( -MRExemptionRecord.start_date).fetch(1000) ret['exemptions'] = [x.to_dict() for x in exemptions] return ret else: return {}
def get(self, code, year): callback = self.request.get("callback") key = code + ":" + year data = memcache.get(key) if data is not None: ret = data.decode("zip") else: year = int(year) ## actual record rec = BudgetLine.query(BudgetLine.code == code, BudgetLine.year == year).fetch(1)[0] rec_data = rec.to_dict() ### async queries parent_codes = [prefix for prefix in rec.prefixes if prefix != code and prefix != "00"] parent_query = BudgetLine.query(BudgetLine.code.IN(parent_codes), BudgetLine.year == year).fetch_async() prefixes = [prefix for prefix in rec.prefixes if prefix != "00"] prefixes = [(len(prefix) / 2, prefix) for prefix in prefixes] hierarchy_queries = [ ( depth, BudgetLine.query( code_starts_with(BudgetLine, prefix), BudgetLine.year == year, BudgetLine.depth == depth ) .order(BudgetLine.code) .fetch_async(batch_size=100), ) for depth, prefix in prefixes ] history_query = BudgetLine.query(BudgetLine.code == code).fetch_async(batch_size=100) support_query = ( SupportLine.query(SupportLine.prefixes == code).order(SupportLine.year).fetch_async(batch_size=100) ) changes_query = ChangeLine.query(ChangeLine.prefixes == code).fetch_async(batch_size=100) ## parents parents = [record.to_dict() for record in parent_query.get_result()] ## hierarchy by_depth = [ (depth, [record.to_dict() for record in query.get_result()]) for depth, query in hierarchy_queries ] by_depth = dict(by_depth) ## history over the years history_recs = dict([(record.year, record) for record in history_query.get_result()]) history = [] for y in range(year, 1991, -1): rec = history_recs.get(y) inf = INFLATION[y] / INFLATION[2013] if rec is None: break to_add = {"year": rec.year, "title": rec.title} if rec.net_allocated is not None: to_add["net_allocated"] = (rec.net_allocated * inf,) if rec.net_revised is not None: to_add["net_revised"] = (rec.net_revised * inf,) if rec.net_used is not None: to_add["net_used"] = (rec.net_used * inf,) if rec.gross_allocated is not None: to_add["gross_allocated"] = (rec.gross_allocated * inf,) if rec.gross_revised is not None: to_add["gross_revised"] = (rec.gross_revised * inf,) if rec.gross_used is not None: to_add["gross_used"] = (rec.gross_used * inf,) history.append(to_add) ## supports support_recs = [record.to_dict() for record in support_query.get_result()] supports = {} for support in support_recs: support_code = support["code"] if support_code not in supports.keys(): supports[support_code] = { "code": support_code, "title": support["title"], "subject": support["subject"], "recepients": {}, } recepient = supports[support_code]["recepients"].setdefault(support["recepient"], {}) recepient.setdefault("sum", 0) recepient.setdefault("count", 0) recepient["sum"] += support["amount_used"] * INFLATION[support["year"]] / INFLATION[2013] recepient["count"] += 1 l = recepient.setdefault("kinds", {}).setdefault(support["kind"], []) l.append( { "year": support["year"], "num_supported": support["num_supported"], "amount_used": support["amount_used"], "amount_allocated": support["amount_allocated"], } ) ## changes changes = [ { "date": "-" if rec.date is None else rec.date.strftime("%d/%m/%Y"), "explanation": rec.explanation, "req_title": rec.req_title, "change_title": rec.change_title, "change_type_name": rec.change_type_name, "net_expense_diff": rec.net_expense_diff, "gross_expense_diff": rec.gross_expense_diff, "allocated_income_diff": rec.allocated_income_diff, "commitment_limit_diff": rec.commitment_limit_diff, "personnel_max_diff": rec.personnel_max_diff, } for rec in changes_query.get_result() ] ret = { "code": repr(code), "rec": rec_data, "parents": parents, "hierarchy": by_depth, "history": history, "supports": supports, "changes": changes, } ret = json.dumps(ret) memcache.add(key, ret.encode("zip"), 3660) self.response.headers["Content-Type"] = "application/json" if callback is not None and callback != "": ret = "%s(%s);" % (callback, ret) self.response.write(ret)
def get(self, code, year): callback = self.request.get('callback') key = code + ":" + year data = memcache.get(key) if data is not None: ret = data.decode("zip") else: year = int(year) ## actual record rec = BudgetLine.query(BudgetLine.code == code, BudgetLine.year == year).fetch(1)[0] rec_data = rec.to_dict() ### async queries parent_codes = [ prefix for prefix in rec.prefixes if prefix != code and prefix != "00" ] parent_query = BudgetLine.query( BudgetLine.code.IN(parent_codes), BudgetLine.year == year).fetch_async(batch_size=100) prefixes = [prefix for prefix in rec.prefixes if prefix != "00"] prefixes = [(len(prefix) / 2, prefix) for prefix in prefixes] hierarchy_queries = [ (depth, BudgetLine.query( code_starts_with(BudgetLine, prefix), BudgetLine.year == year, BudgetLine.depth == depth).order( BudgetLine.code).fetch_async(batch_size=500)) for depth, prefix in prefixes ] history_query = BudgetLine.query( BudgetLine.code == code).fetch_async(batch_size=500) support_query = SupportLine.query( SupportLine.prefixes == code).order( SupportLine.year).fetch_async(batch_size=500) changes_query = ChangeLine.query( ChangeLine.prefixes == code).order( -ChangeLine.year, -ChangeLine.date).fetch_async(batch_size=500) ## parents parents = [ record.to_dict() for record in parent_query.get_result() ] ## hierarchy by_depth = [(depth, [record.to_dict() for record in query.get_result()]) for depth, query in hierarchy_queries] by_depth = dict(by_depth) ## history over the years history_recs = dict([(record.year, record) for record in history_query.get_result()]) history = [] for y in range(year, 1991, -1): rec = history_recs.get(y) inf = INFLATION[y] / INFLATION[2013] if rec is None: break to_add = {'year': rec.year, 'title': rec.title} if rec.net_allocated is not None: to_add['net_allocated'] = rec.net_allocated * inf, if rec.net_revised is not None: to_add['net_revised'] = rec.net_revised * inf, if rec.net_used is not None: to_add['net_used'] = rec.net_used * inf, if rec.gross_allocated is not None: to_add['gross_allocated'] = rec.gross_allocated * inf, if rec.gross_revised is not None: to_add['gross_revised'] = rec.gross_revised * inf, if rec.gross_used is not None: to_add['gross_used'] = rec.gross_used * inf, history.append(to_add) ## supports support_recs = [ record.to_dict() for record in support_query.get_result() ] supports = {} for support in support_recs: support_code = support['code'] if support_code not in supports.keys(): supports[support_code] = { 'code': support_code, 'title': support['title'], 'subject': support['subject'], 'recipients': {} } recipient = supports[support_code]['recipients'].setdefault( support['recipient'], {}) recipient.setdefault('sum', 0) recipient.setdefault('count', 0) recipient['sum'] += support['amount_used'] * INFLATION[ support['year']] / INFLATION[2013] recipient['count'] += 1 l = recipient.setdefault('kinds', {}).setdefault(support['kind'], []) l.append({ 'year': support['year'], 'num_supported': support['num_supported'], 'amount_used': support['amount_used'], 'amount_allocated': support['amount_allocated'] }) ## changes changes = [{ 'year': rec.year, 'date': None if rec.date is None else rec.date.strftime("%d/%m/%Y"), 'explanation': rec.explanation, 'req_title': rec.req_title, 'change_title': rec.change_title, 'change_type_name': rec.change_type_name, 'net_expense_diff': rec.net_expense_diff, 'gross_expense_diff': rec.gross_expense_diff, 'allocated_income_diff': rec.allocated_income_diff, 'commitment_limit_diff': rec.commitment_limit_diff, 'personnel_max_diff': rec.personnel_max_diff } for rec in changes_query.get_result()] ret = { 'code': repr(code), 'rec': rec_data, 'parents': parents, 'hierarchy': by_depth, 'history': history, 'supports': supports, 'changes': changes } ret = json.dumps(ret) memcache.add(key, ret.encode("zip"), 86400) self.response.headers['Content-Type'] = 'application/json' if callback is not None and callback != "": ret = "%s(%s);" % (callback, ret) self.response.write(ret)
def get(self,code,year): callback = self.request.get('callback') key = code+":"+year data = memcache.get(key) if data is not None: ret = data.decode("zip") else: year = int(year) ## actual record rec = BudgetLine.query(BudgetLine.code==code, BudgetLine.year==year).fetch(1)[0] rec_data = rec.to_dict() ### async queries parent_codes = [ prefix for prefix in rec.prefixes if prefix != code and prefix != "00" ] parent_query = BudgetLine.query(BudgetLine.code.IN(parent_codes), BudgetLine.year==year).fetch_async(batch_size=100) prefixes = [ prefix for prefix in rec.prefixes if prefix != "00" ] prefixes = [ (len(prefix)/2, prefix) for prefix in prefixes ] hierarchy_queries = [ (depth, BudgetLine.query(code_starts_with(BudgetLine,prefix), BudgetLine.year==year, BudgetLine.depth==depth).order(BudgetLine.code).fetch_async(batch_size=500)) for depth, prefix in prefixes ] history_query = BudgetLine.query(BudgetLine.code==code).fetch_async(batch_size=500) support_query = SupportLine.query(SupportLine.prefixes==code).order(SupportLine.year).fetch_async(batch_size=500) changes_query = ChangeLine.query(ChangeLine.prefixes==code).order(-ChangeLine.year,-ChangeLine.date).fetch_async(batch_size=500) ## parents parents = [ record.to_dict() for record in parent_query.get_result() ] ## hierarchy by_depth = [ (depth,[ record.to_dict() for record in query.get_result() ]) for depth,query in hierarchy_queries ] by_depth = dict(by_depth) ## history over the years history_recs = dict([ (record.year,record) for record in history_query.get_result() ]) history = [] for y in range(year,1991,-1): rec = history_recs.get(y) inf = INFLATION[y] / INFLATION[2013] if rec is None: break to_add = { 'year': rec.year, 'title': rec.title } if rec.net_allocated is not None: to_add['net_allocated'] = rec.net_allocated*inf, if rec.net_revised is not None: to_add['net_revised'] = rec.net_revised*inf, if rec.net_used is not None: to_add['net_used'] = rec.net_used*inf, if rec.gross_allocated is not None: to_add['gross_allocated'] = rec.gross_allocated*inf, if rec.gross_revised is not None: to_add['gross_revised'] = rec.gross_revised*inf, if rec.gross_used is not None: to_add['gross_used'] = rec.gross_used*inf, history.append(to_add) ## supports support_recs = [ record.to_dict() for record in support_query.get_result() ] supports = {} for support in support_recs: support_code = support['code'] if support_code not in supports.keys(): supports[support_code] = {'code':support_code, 'title':support['title'], 'subject':support['subject'], 'recipients':{}} recipient = supports[support_code]['recipients'].setdefault(support['recipient'],{}) recipient.setdefault('sum',0) recipient.setdefault('count',0) recipient['sum'] += support['amount_used']*INFLATION[support['year']] / INFLATION[2013] recipient['count'] += 1 l = recipient.setdefault('kinds',{}).setdefault(support['kind'],[]) l.append({'year':support['year'], 'num_supported':support['num_supported'], 'amount_used':support['amount_used'], 'amount_allocated':support['amount_allocated']}) ## changes changes = [ {'year':rec.year, 'date':None if rec.date is None else rec.date.strftime("%d/%m/%Y"), 'explanation': rec.explanation, 'req_title': rec.req_title, 'change_title': rec.change_title, 'change_type_name': rec.change_type_name, 'net_expense_diff': rec.net_expense_diff, 'gross_expense_diff': rec.gross_expense_diff, 'allocated_income_diff': rec.allocated_income_diff, 'commitment_limit_diff': rec.commitment_limit_diff, 'personnel_max_diff': rec.personnel_max_diff } for rec in changes_query.get_result() ] ret = { 'code' : repr(code), 'rec' : rec_data, 'parents' : parents, 'hierarchy' : by_depth, 'history' : history, 'supports': supports, 'changes' : changes } ret = json.dumps(ret) memcache.add(key, ret.encode("zip"), 86400) self.response.headers['Content-Type'] = 'application/json' if callback is not None and callback != "": ret = "%s(%s);" % ( callback, ret ) self.response.write(ret)
def get_query(self,code,year=None,kids=None): lines = SupportLine.query(SupportLine.prefixes==code).order(SupportLine.recepient,SupportLine.kind,SupportLine.year) return lines