From 7fbe43174628567b994e22957fbcdffe48f23431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dra=C5=BEen=20Odoba=C5=A1i=C4=87?= Date: Thu, 27 Jun 2019 16:53:54 +0200 Subject: [PATCH] Calculate stats in the database Fixes: https://github.com/hotosm/tasking-manager/issues/1720 --- server/services/stats_service.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/server/services/stats_service.py b/server/services/stats_service.py index a4aa91bcf5..b2f0249005 100644 --- a/server/services/stats_service.py +++ b/server/services/stats_service.py @@ -259,21 +259,15 @@ def get_homepage_stats() -> HomePageStatsDTO: # current_app.logger.debug(total_area) # dto.total_area = total_area - tasks_mapped_area = 0 - tasks_mapped_sql = """select sum(ST_Area(geometry)) from public.tasks where task_status = 2""" - tasks_mapped_result = db.engine.execute(tasks_mapped_sql) - for rowproxy in tasks_mapped_result: - for tup in rowproxy: - tasks_mapped_area += tup - dto.total_mapped_area = tasks_mapped_area - - tasks_validated_area = 0 - tasks_validated_sql = """select sum(ST_Area(geometry)) from public.tasks where task_status = 4""" - tasks_validated_result = db.engine.execute(tasks_validated_sql) - for rowproxy in tasks_validated_result: - sum_proxy = [tup for tup in rowproxy if tup is not None] - tasks_validated_area += sum(sum_proxy) - dto.total_validated_area = tasks_validated_area + tasks_mapped_sql = "select coalesce(sum(ST_Area(geometry)), 0) as sum from public.tasks where task_status = %s" + tasks_mapped_result = db.engine.execute(tasks_mapped_sql, TaskStatus.MAPPED.value) + + dto.total_mapped_area = tasks_mapped_result.fetchone()['sum'] + + tasks_validated_sql = "select coalesce(sum(ST_Area(geometry)), 0) as sum from public.tasks where task_status = %s" + tasks_validated_result = db.engine.execute(tasks_validated_sql, TaskStatus.VALIDATED.value) + + dto.total_validated_area = tasks_validated_result.fetchone()['sum'] campaign_count = db.session.query(Project.campaign_tag, func.count(Project.campaign_tag))\ .group_by(Project.campaign_tag).all()