class Stats(Resource): def __init__(self): super(Resource, self).__init__() self.campaign_table = DynamoTable('campaigns') self.donation_table = DynamoTable('donations') @swagger.operation(notes='Get global stats for the site', nickname='Get Site Stats', parameters=[]) def get(self): """Get Site Stats Response: campaign_count - total number of campaigns campaign_active_count - total number of active campaigns campaign_matched_count - total number of matched campaigns campaign_cancelled_count - total number of cancelled campaigns campaign_total_cents - total amount of pledged cents (sum of matched and active campaigns only) donation_count - total number of donations total_donation_cents - total amount of donations in cents """ def campaign_count_func(items, result): """Method to count campaigns""" result["campaign_count"] += len(items) for item in items: if item["campaign_status"]["S"] == "active": result["campaign_active_count"] += 1 result["campaign_total_cents"] += int( item["match_cents"]["N"]) elif item["campaign_status"]["S"] == "matched": result["campaign_matched_count"] += 1 result["campaign_total_cents"] += int( item["match_cents"]["N"]) else: result["campaign_cancelled_count"] += 1 def donation_func(items, result): """Method to count campaigns""" for item in items: result["donation_count"] += 1 result["total_donation_cents"] += int( item["donation_cents"]["N"]) result = { "campaign_count": 0, "campaign_active_count": 0, "campaign_matched_count": 0, "campaign_cancelled_count": 0, "campaign_total_cents": 0, "donation_count": 0, "total_donation_cents": 0 } self.campaign_table.scan_table( campaign_count_func, result, "campaign_id, campaign_status, match_cents") self.donation_table.scan_table(donation_func, result, "donation_cents") return result, 200
def test_scan_table(self): """Method to test scanning a table""" def scan_func(items, input_val): for item in items: if item["campaign_status"]["S"] == "complete": input_val['count'] += 1 return input_val campaign_table = DynamoTable('campaigns') # Add a record for idx in range(0, 10): data = { "campaign_id": "my_campaign_{}".format(idx), "notified_on": arrow.utcnow().isoformat(), "campaign_status": "complete" } campaign_table.put_item(data) # Scan table result = {"count": 0} campaign_table.scan_table(scan_func, result, "campaign_status") self.assertEqual(result["count"], 10)