@@ -567,21 +567,22 @@ def update_project_data(project_ids: list = []):
567567 logger .info ("Finished status update projects." )
568568
569569
570- def set_progress_in_firebase (project_id : str ):
571- """Update the project progress value in Firebase."""
572-
570+ def get_project_progress (project_id : str ) -> int :
571+ """
572+ Calculate overall project progress as the average progress for all groups.
573+ This is not hundred percent exact, since groups can have a different number of tasks
574+ but it is still "good enough" and gives almost correct progress.
575+ But it is easier to compute than considering the actual number of tasks per group.
576+
577+ NOTE: the cast to integer in postgres rounds decimals. This means that for 99.5%
578+ progress, we return 100% here. We should evaluate if this is what we want if/when
579+ we introduce automated project rotation upon completion (as the reported completion
580+ would happen 0.5% before actual completion).
581+ """
573582 pg_db = auth .postgresDB ()
574583 query = """
575- -- Calculate overall project progress as
576- -- the average progress for all groups.
577- -- This is not hundred percent exact,
578- -- since groups can have a different number of tasks
579- -- but it is still "good enough" and gives almost correct progress.
580- -- But it is easier to compute
581- -- than considering the actual number of tasks per group.
582584 select
583- project_id
584- ,avg(group_progress)::integer as progress
585+ avg(group_progress)::integer as progress
585586 from
586587 (
587588 -- Get all groups for this project and
@@ -605,46 +606,52 @@ def set_progress_in_firebase(project_id: str):
605606 -- even if more users than required submitted results.
606607 -- The verification number of a project is used here.
607608 select
608- r .group_id
609- ,r .project_id
609+ ms .group_id
610+ ,ms .project_id
610611 ,case
611612 when count(distinct user_id) >= p.verification_number then 100
612613 else 100 * count(distinct user_id) / p.verification_number
613614 end as group_progress
614- from results r , projects p
615- where r .project_id = p.project_id
616- group by group_id, r .project_id, p.verification_number
615+ from mapping_sessions ms , projects p
616+ where ms .project_id = p.project_id
617+ group by group_id, ms .project_id, p.verification_number
617618 ) bar
618619 on bar.group_id = g.group_id and bar.project_id = g.project_id
619620 where g.project_id = %s
620621 ) foo
621622 group by project_id
622623 """
623624 data = [project_id ]
624- progress = pg_db .retr_query (query , data )[0 ][1 ]
625+ return pg_db .retr_query (query , data )[0 ][0 ]
626+
627+
628+ def set_progress_in_firebase (project_id : str ):
629+ """Update the project progress value in Firebase."""
630+ progress = get_project_progress (project_id )
625631
626632 fb_db = auth .firebaseDB ()
627633 project_progress_ref = fb_db .reference (f"v2/projects/{ project_id } /progress" )
628634 project_progress_ref .set (progress )
629635 logger .info (f"set progress for project { project_id } : { progress } " )
630636
631637
632- def set_contributor_count_in_firebase (project_id : str ):
633- """Update the project contributors value in Firebase."""
634-
638+ def get_contributor_count_from_postgres (project_id : str ) -> int :
635639 pg_db = auth .postgresDB ()
636640 query = """
637641 select
638- project_id
639- ,count(distinct user_id) contributor_count
640- from results r
642+ count(distinct user_id)
643+ from mapping_sessions ms
641644 where
642645 project_id = %s
643- group by project_id
644646 """
645647 data = [project_id ]
646- contributor_count = pg_db .retr_query (query , data )[0 ][1 ]
648+ return pg_db .retr_query (query , data )[0 ][0 ]
649+
650+
651+ def set_contributor_count_in_firebase (project_id : str ):
652+ """Update the project contributors value in Firebase."""
647653
654+ contributor_count = get_contributor_count_from_postgres (project_id )
648655 fb_db = auth .firebaseDB ()
649656 project_progress_ref = fb_db .reference (f"v2/projects/{ project_id } /contributorCount" )
650657 project_progress_ref .set (contributor_count )
0 commit comments