Skip to content

Commit

Permalink
Calculate stats in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
dodobas committed Jun 27, 2019
1 parent 0143626 commit 7fbe431
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions server/services/stats_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7fbe431

Please sign in to comment.