@@ -98,6 +98,7 @@ def get_results(
9898 Parse timestamp as datetime object and add attribute "day" for each result.
9999 Return None if there are no results for this project.
100100 Otherwise, return dataframe.
101+ Include the 'ref' JSON field in integer results if it exists.
101102
102103 Parameters
103104 ----------
@@ -108,7 +109,10 @@ def get_results(
108109 if result_table == "mapping_sessions_results_geometry" :
109110 result_sql = "ST_AsGeoJSON(msr.result) as result"
110111 else :
111- result_sql = "msr.result"
112+ result_sql = """
113+ (msr.result->>'result')::int as result,
114+ msr.result->'ref' as ref
115+ """
112116
113117 sql_query = sql .SQL (
114118 f"""
@@ -504,6 +508,44 @@ def get_statistics_for_geometry_result_project(project_id: str):
504508 return project_stats_dict
505509
506510
511+ def unify_refs (ref_list ):
512+ if not ref_list :
513+ return None
514+ first_ref = json .dumps (ref_list [0 ], sort_keys = True )
515+ for r in ref_list [1 :]:
516+ if json .dumps (r , sort_keys = True ) != first_ref :
517+ return "multiple"
518+ return ref_list [0 ]
519+
520+
521+ def add_ref_to_agg_results (
522+ results_df : pd .DataFrame , agg_results_df : pd .DataFrame
523+ ) -> pd .DataFrame :
524+ """
525+ Add a 'ref' column to agg_results_df.
526+ If all user refs for a task are identical, use that ref.
527+ If refs differ, set ref to 'multiple'.
528+ """
529+
530+ # collect refs per task
531+ refs_per_task = (
532+ results_df .groupby (["project_id" , "group_id" , "task_id" ])["ref" ]
533+ .apply (list )
534+ .reset_index ()
535+ )
536+
537+ refs_per_task ["ref" ] = refs_per_task ["ref" ].apply (unify_refs )
538+
539+ # merge into agg_results_df
540+ agg_results_df = agg_results_df .merge (
541+ refs_per_task [["project_id" , "group_id" , "task_id" , "ref" ]],
542+ on = ["project_id" , "group_id" , "task_id" ],
543+ how = "left" ,
544+ )
545+
546+ return agg_results_df
547+
548+
507549def get_statistics_for_integer_result_project (
508550 project_id : str , project_info : pd .Series , generate_hot_tm_geometries : bool
509551) -> dict :
@@ -550,6 +592,9 @@ def get_statistics_for_integer_result_project(
550592 tasks_df ,
551593 project_info ["custom_options" ],
552594 )
595+
596+ agg_results_df = add_ref_to_agg_results (results_df , agg_results_df )
597+
553598 agg_results_df .to_csv (agg_results_filename , index_label = "idx" )
554599
555600 geojson_functions .gzipped_csv_to_gzipped_geojson (
0 commit comments