Skip to content

Commit

Permalink
modify function's application to fit with the updated model
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewThien committed Dec 3, 2024
1 parent 2df9265 commit cc276a0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
28 changes: 15 additions & 13 deletions app/workers/RulesConceptsActivity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ def _create_concepts(
return concepts


def _transform_concepts(table_values: List[ScanReportValueDict], table_id: str) -> None:
def _transform_concepts(
table_values: List[ScanReportValueDict], table: ScanReportTable
) -> None:
"""
For each vocab, set "concept_id" and "standard_concept" in each entry in the vocab.
Transforms the values in place.
Expand Down Expand Up @@ -91,7 +93,7 @@ def _transform_concepts(table_values: List[ScanReportValueDict], table_id: str)
# Set to defaults, and skip all the remaining processing that a vocab would require
_set_defaults_for_none_vocab(value)
else:
_process_concepts_for_vocab(vocab, value, table_id)
_process_concepts_for_vocab(vocab, value, table)


def _set_defaults_for_none_vocab(entries: List[ScanReportValueDict]) -> None:
Expand All @@ -111,7 +113,7 @@ def _set_defaults_for_none_vocab(entries: List[ScanReportValueDict]) -> None:


def _process_concepts_for_vocab(
vocab: str, entries: List[ScanReportValueDict], table_id: str
vocab: str, entries: List[ScanReportValueDict], table: ScanReportTable
) -> None:
"""
Process concepts for a specific vocabulary.
Expand All @@ -127,7 +129,7 @@ def _process_concepts_for_vocab(
update_job(
JobStageType.BUILD_CONCEPTS_FROM_DICT,
StageStatusType.IN_PROGRESS,
scan_report_table_id=table_id,
scan_report_table=table,
details=f"Building concepts for {vocab} vocabulary",
)
logger.info(f"begin {vocab}")
Expand Down Expand Up @@ -271,7 +273,7 @@ def _handle_table(
# Add vocab id to each entry from the vocab dict
helpers.add_vocabulary_id_to_entries(table_values, vocab, table.name)

_transform_concepts(table_values, table.pk)
_transform_concepts(table_values, table)
logger.debug("finished standard concepts lookup")

concepts = _create_concepts(table_values)
Expand All @@ -285,37 +287,37 @@ def _handle_table(
update_job(
JobStageType.BUILD_CONCEPTS_FROM_DICT,
StageStatusType.COMPLETE,
scan_report_table_id=table.pk,
scan_report_table=table,
details=f"No concepts was created for table {table.name}. The data dict. may not be provided or the vocabs building function was called before.",
)
else:
update_job(
JobStageType.BUILD_CONCEPTS_FROM_DICT,
StageStatusType.COMPLETE,
scan_report_table_id=table.pk,
scan_report_table=table,
details=f"Created {len(concepts)} concepts for table {table.name}.",
)

# Starting the concepts reusing process
create_or_update_job(
JobStageType.REUSE_CONCEPTS,
StageStatusType.IN_PROGRESS,
scan_report_table_id=table.pk,
scan_report_table=table,
)
# handle reuse of concepts at field level
reuse_existing_field_concepts(table_fields, table.pk)
reuse_existing_field_concepts(table_fields, table)
update_job(
JobStageType.REUSE_CONCEPTS,
StageStatusType.IN_PROGRESS,
scan_report_table_id=table.pk,
scan_report_table=table,
details="Finished reusing concepts at field level. Reusing concepts at value level...",
)
# handle reuse of concepts at value level
reuse_existing_value_concepts(table_values, table.pk)
reuse_existing_value_concepts(table_values, table)
update_job(
JobStageType.REUSE_CONCEPTS,
StageStatusType.COMPLETE,
scan_report_table_id=table.pk,
scan_report_table=table,
details="Reusing concepts finished.",
)

Expand Down Expand Up @@ -343,6 +345,6 @@ def main(msg: Dict[str, str]):
create_or_update_job(
JobStageType.BUILD_CONCEPTS_FROM_DICT,
StageStatusType.IN_PROGRESS,
scan_report_table_id=table_id,
scan_report_table=table,
)
_handle_table(table, vocab_dictionary)
19 changes: 12 additions & 7 deletions app/workers/RulesConceptsActivity/reuse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from typing import Dict, List, Tuple, Union
from collections import defaultdict
from shared.mapping.models import ScanReportConcept, ScanReportField, ScanReportValue
from shared.mapping.models import (
ScanReportConcept,
ScanReportField,
ScanReportValue,
ScanReportTable,
)
from shared_code import db
from shared_code.logger import logger
from shared_code.models import (
Expand All @@ -25,7 +30,7 @@


def reuse_existing_value_concepts(
new_values_map: List[ScanReportValueDict], table_id: str
new_values_map: List[ScanReportValueDict], table: ScanReportTable
) -> None:
"""
This expects a dict of value names to ids which have been generated in a newly
Expand Down Expand Up @@ -169,7 +174,7 @@ def reuse_existing_value_concepts(
new_values_full_details,
value_details_to_value_and_concept_id_map,
content_type,
table_id,
table,
):
ScanReportConcept.objects.bulk_create(concepts_to_post)
logger.info("POST concepts all finished in reuse_existing_value_concepts")
Expand All @@ -178,7 +183,7 @@ def reuse_existing_value_concepts(


def reuse_existing_field_concepts(
new_fields_map: List[ScanReportFieldDict], table_id: str
new_fields_map: List[ScanReportFieldDict], table: ScanReportTable
) -> None:
"""
Creates new concepts associated to any field that matches the name of an existing
Expand Down Expand Up @@ -283,7 +288,7 @@ def reuse_existing_field_concepts(
new_fields_full_details,
existing_field_name_to_field_and_concept_id_map,
content_type,
table_id,
table,
):
ScanReportConcept.objects.bulk_create(concepts_to_post)
logger.info("POST concepts all finished in reuse_existing_field_concepts")
Expand All @@ -301,7 +306,7 @@ def select_concepts_to_post(
],
],
content_type: ScanReportConceptContentType,
table_id,
table: ScanReportTable,
) -> List[ScanReportConcept]:
"""
Depending on the content_type, generate a list of `ScanReportConcepts` to be created.
Expand Down Expand Up @@ -342,7 +347,7 @@ def select_concepts_to_post(
update_job(
JobStageType.REUSE_CONCEPTS,
StageStatusType.FAILED,
scan_report_table_id=table_id,
scan_report_table=table,
details=f"Reusing concepts failed: Unsupported content_type: {content_type}",
)
raise ValueError(f"Unsupported content_type: {content_type}")
Expand Down
7 changes: 4 additions & 3 deletions app/workers/RulesOrchestrator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
JobStageType,
StageStatusType,
)
from shared.mapping.models import ScanReportTable


def orchestrator_function(context: df.DurableOrchestrationContext):
Expand Down Expand Up @@ -49,7 +50,7 @@ def orchestrator_function(context: df.DurableOrchestrationContext):
create_or_update_job(
JobStageType.GENERATE_RULES,
StageStatusType.IN_PROGRESS,
scan_report_table_id=table_id,
scan_report_table=ScanReportTable.objects.get(id=table_id),
details=f"Generating mapping rules from {concepts_count} concepts found.",
)
# Fan out
Expand All @@ -65,7 +66,7 @@ def orchestrator_function(context: df.DurableOrchestrationContext):
update_job(
JobStageType.GENERATE_RULES,
StageStatusType.COMPLETE,
scan_report_table_id=table_id,
scan_report_table=ScanReportTable.objects.get(id=table_id),
details="Automatic mapping rules generation finished.",
)
return [result, results]
Expand All @@ -74,7 +75,7 @@ def orchestrator_function(context: df.DurableOrchestrationContext):
update_job(
JobStageType.GENERATE_RULES,
StageStatusType.FAILED,
scan_report_table_id=table_id,
scan_report_table=ScanReportTable.objects.get(id=table_id),
details=f"Rules Orchestrator function failed: {e}",
)
raise
Expand Down

0 comments on commit cc276a0

Please sign in to comment.