Skip to content

Commit bebea7b

Browse files
committed
handling invalid matches
1 parent 475e993 commit bebea7b

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

mapswipe_workers/mapswipe_workers/firebase_to_postgres/transfer_results.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def transfer_results(project_id_list=None):
5454
return project_id_list_transfered
5555

5656

57-
def transfer_results_for_project(project_id, results):
57+
def transfer_results_for_project(project_id, results, filter_mode: bool = False):
5858
"""Transfer the results for a specific project.
5959
Save results into an in-memory file.
6060
Copy the results to postgres.
@@ -90,18 +90,20 @@ def transfer_results_for_project(project_id, results):
9090
# results at relatively high speed.
9191
results_file = results_to_file(results, project_id)
9292
truncate_temp_results()
93-
save_results_to_postgres(results_file)
93+
save_results_to_postgres(results_file, project_id, filter_mode=filter_mode)
9494
except psycopg2.errors.ForeignKeyViolation as e:
9595
sentry.capture_exception(e)
9696
sentry.capture_message(
9797
"could not transfer results to postgres due to ForeignKeyViolation: "
98-
f"{project_id}"
98+
f"{project_id}; filter_mode={filter_mode}"
9999
)
100100
logger.exception(e)
101101
logger.warning(
102102
"could not transfer results to postgres due to ForeignKeyViolation: "
103-
f"{project_id}"
103+
f"{project_id}; filter_mode={filter_mode}"
104104
)
105+
if not filter_mode:
106+
transfer_results_for_project(project_id, results, filter_mode=True)
105107
except Exception as e:
106108
sentry.capture_exception(e)
107109
sentry.capture_message(f"could not transfer results to postgres: {project_id}")
@@ -259,14 +261,16 @@ def results_to_file(results, projectId):
259261
return results_file
260262

261263

262-
def save_results_to_postgres(results_file):
264+
def save_results_to_postgres(results_file, project_id, filter_mode: bool):
263265
"""
264266
Saves results to a temporary table in postgres
265267
using the COPY Statement of Postgres
266268
for a more efficient import into the database.
267269
Parameters
268270
----------
269271
results_file: io.StringIO
272+
filter_mode: boolean
273+
If true, try to filter out invalid results.
270274
"""
271275

272276
p_con = auth.postgresDB()
@@ -283,6 +287,23 @@ def save_results_to_postgres(results_file):
283287
p_con.copy_from(results_file, "results_temp", columns)
284288
results_file.close()
285289

290+
if filter_mode:
291+
logger.warn(f"trying to remove invalid tasks from {project_id}.")
292+
293+
filter_query = f"""
294+
DELETE FROM results_temp
295+
where task_id in (
296+
select task_id from results_temp where task_id not in (
297+
select r.task_id from results_temp r join (
298+
select * from tasks where project_id = '{project_id}'
299+
) as t
300+
on r.group_id = t.group_id
301+
and r.task_id = t.task_id
302+
)
303+
)
304+
"""
305+
p_con.query(filter_query)
306+
286307
query_insert_results = """
287308
INSERT INTO results
288309
SELECT * FROM results_temp

mapswipe_workers/tests/integration/test_transfer_results_invalid_results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_invalid_task_in_result(self):
4646
ref = fb_db.reference(
4747
f"v2/results/{self.project_id}/g115/test_build_area/results"
4848
)
49-
self.assertEqual(len(ref.get(shallow=True)), 253)
49+
self.assertIsNone(ref.get(shallow=True))
5050

5151

5252
if __name__ == "__main__":

0 commit comments

Comments
 (0)