Skip to content

Commit 061527d

Browse files
authored
Merge pull request #208 from OpenUpSA/elastic-bottleneck
flatten isolate data
2 parents 0136a3d + e09026c commit 061527d

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

helpers.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,24 @@ def bulk_send_to_elastic(documents):
9393

9494
bulk_lines = []
9595
for doc in documents:
96-
doc_id = doc.get("id")
97-
action = {"index": {"_id": doc_id}} if doc_id else {"index": {}}
98-
bulk_lines.append(json.dumps(action))
99-
# Use json_serial for datetime/date serialization
100-
bulk_lines.append(json.dumps(doc, default=json_serial))
96+
# Flatten isolate_data if present (same logic as send_to_elastic2)
97+
if 'isolate_data' in doc and doc['isolate_data']:
98+
isolate_data = doc['isolate_data']
99+
if isinstance(isolate_data, str):
100+
try:
101+
isolate_data = json.loads(isolate_data)
102+
except Exception:
103+
isolate_data = {}
104+
if isinstance(isolate_data, dict):
105+
for key, value in isolate_data.items():
106+
if key not in doc:
107+
doc[key] = value
108+
del doc['isolate_data']
109+
doc_id = doc.get("id")
110+
action = {"index": {"_id": doc_id}} if doc_id else {"index": {}}
111+
bulk_lines.append(json.dumps(action))
112+
# Use json_serial for datetime/date serialization
113+
bulk_lines.append(json.dumps(doc, default=json_serial))
101114
bulk_data = "\n".join(bulk_lines) + "\n"
102115

103116
url = f"{settings.ELASTICSEARCH_URL}/{settings.ELASTICSEARCH_INDEX}/_bulk"

test/test_submissions_validation.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -976,11 +976,13 @@ def test_validate_submission_sends_validated_isolates_to_elasticsearch(
976976
setup_tsv_rows(tsv_rows)
977977

978978
url = get_validation_endpoint(public_project1["id"], submission_id)
979-
response = make_request(client, "POST", url, token=org1_admin_token)
980-
981-
assert response.status_code == 200
982-
# Verify send_to_elastic2 was called for each validated isolate
983-
assert mock_validation_stack["elastic"].call_count == 2
979+
with patch("app.bulk_send_to_elastic") as mock_bulk:
980+
response = make_request(client, "POST", url, token=org1_admin_token)
981+
assert response.status_code == 200
982+
# Verify bulk_send_to_elastic was called once with both isolates
983+
mock_bulk.assert_called_once()
984+
args, kwargs = mock_bulk.call_args
985+
assert len(args[0]) == 2
984986

985987
finally:
986988
cleanup_submission(submission_id)

0 commit comments

Comments
 (0)