created data enrichment logic for EU projects
This commit is contained in:
@@ -0,0 +1,372 @@
|
||||
import json
|
||||
import logging
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
|
||||
from inse.config import get_settings
|
||||
from inse.integrations import elasticsearch
|
||||
from inse.model import ESCluster, ESIndex
|
||||
|
||||
# Import base models
|
||||
from inse.models.cordis_eu_project import CordisEuProject
|
||||
|
||||
# Import FP7 models
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_project import Fp7Project
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_organization import Fp7Organization
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_publication import Fp7Publication
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_project_ipr import Fp7ProjectIPR
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_report_summary import Fp7ReportSummary
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_topic import Fp7Topic
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_web_link import Fp7WebLink
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_eurosci_voc import Fp7EurosciVoc
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_legal_basis import Fp7LegalBasis
|
||||
from inse.models.projects_eu_cordis.fp7.fp7_project_details import Fp7ProjectDetails
|
||||
|
||||
# Import H2020 models
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_project import H2020Project
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_organization import H2020Organization
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_publication import H2020Publication
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_deliverable import H2020Deliverable
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_report_summary import H2020ReportSummary
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_topic import H2020Topic
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_web_link import H2020WebLink
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_eurosci_voc import H2020EurosciVoc
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_legal_basis import H2020LegalBasis
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_erc_pi import H2020ErcPI
|
||||
from inse.models.projects_eu_cordis.horizon2020.h2020_project_details import H2020ProjectDetails
|
||||
|
||||
# Import Horizon Europe models
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_project import HEUProject
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_organization import HEUOrganization
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_publication import HEUPublication
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_deliverable import HEUDeliverable
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_report_summary import HEUReportSummary
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_topic import HEUTopic
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_web_link import HEUWebLink
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_eurosci_voc import HEUEurosciVoc
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_legal_basis import HEULegalBasis
|
||||
from inse.models.projects_eu_cordis.horizon_europe.heu_project_details import HEUProjectDetails
|
||||
|
||||
settings = get_settings()
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(settings.logging_level)
|
||||
|
||||
|
||||
def get_enriched_cordis_project_by_id(
|
||||
project_id: str,
|
||||
) -> Optional[Union[Fp7ProjectDetails, H2020ProjectDetails, HEUProjectDetails]]:
|
||||
"""
|
||||
Fetches and enriches a single Cordis EU project by its ID across all program types.
|
||||
|
||||
Args:
|
||||
project_id: The project ID to fetch
|
||||
|
||||
Returns:
|
||||
The enriched detailed project model, or None if not found
|
||||
"""
|
||||
if not project_id:
|
||||
return None
|
||||
|
||||
# Step 1: Search for the project across all indices
|
||||
query = {"bool": {"filter": [{"term": {"id": project_id}}]}}
|
||||
|
||||
response = elasticsearch.search(
|
||||
query=query,
|
||||
index=ESIndex.CORDIS_EU_PROJECTS_WILDCARD,
|
||||
cluster=ESCluster.INSE,
|
||||
size=1,
|
||||
)
|
||||
|
||||
hits = response.get("hits", {}).get("hits", [])
|
||||
if not hits:
|
||||
logger.info(f"No project found with ID: {project_id}")
|
||||
return None
|
||||
|
||||
hit = hits[0]
|
||||
index = hit.get("_index", "")
|
||||
source = hit.get("_source", {})
|
||||
|
||||
# Determine program type from index name
|
||||
program_type = _determine_program_type_from_index(index)
|
||||
|
||||
# Enrich and return the project based on program type
|
||||
if program_type == "fp7":
|
||||
return _enrich_fp7_project(project_id, source)
|
||||
elif program_type == "h2020":
|
||||
return _enrich_h2020_project(project_id, source)
|
||||
elif program_type == "heu":
|
||||
return _enrich_heu_project(project_id, source)
|
||||
else:
|
||||
# Unknown type, return base model
|
||||
logger.warning(f"Unknown program type for index: {index}")
|
||||
return CordisEuProject.model_validate(source)
|
||||
|
||||
|
||||
def _determine_program_type_from_index(index: str) -> str:
|
||||
"""Determines the program type from the Elasticsearch index name."""
|
||||
index_lower = index.lower()
|
||||
if "fp7" in index_lower:
|
||||
return "fp7"
|
||||
elif "horizon_2020" or () in index_lower:
|
||||
return "h2020"
|
||||
elif "heu" in index_lower or "horizon_europe" in index_lower:
|
||||
return "heu"
|
||||
return "unknown"
|
||||
|
||||
|
||||
def _enrich_fp7_project(
|
||||
project_id: str,
|
||||
project_source: Dict[str, Any]
|
||||
) -> Optional[Fp7ProjectDetails]:
|
||||
"""Enriches a single FP7 project with all related data."""
|
||||
try:
|
||||
project = Fp7Project.model_validate(project_source)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to validate FP7 project {project_id}: {e}")
|
||||
return None
|
||||
|
||||
# Fetch all related data
|
||||
organizations = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_fp7_organizations",
|
||||
model_class=Fp7Organization
|
||||
)
|
||||
publications = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_fp7_publications",
|
||||
model_class=Fp7Publication
|
||||
)
|
||||
iprs = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_fp7_iprs",
|
||||
model_class=Fp7ProjectIPR
|
||||
)
|
||||
report_summaries = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_fp7_report_summaries",
|
||||
model_class=Fp7ReportSummary
|
||||
)
|
||||
topics_list = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_fp7_topics",
|
||||
model_class=Fp7Topic
|
||||
)
|
||||
web_links = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_fp7_web_links",
|
||||
model_class=Fp7WebLink
|
||||
)
|
||||
eurosci_voc = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_fp7_eurosci_voc",
|
||||
model_class=Fp7EurosciVoc
|
||||
)
|
||||
legal_basis_list = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_fp7_legal_basis",
|
||||
model_class=Fp7LegalBasis
|
||||
)
|
||||
|
||||
# Build and return enriched project
|
||||
return Fp7ProjectDetails.from_project_with_relations(
|
||||
project=project,
|
||||
organizations=organizations.get(project_id, []),
|
||||
publications=publications.get(project_id, []),
|
||||
iprs=iprs.get(project_id, []),
|
||||
report_summaries=report_summaries.get(project_id, []),
|
||||
topics_list=topics_list.get(project_id, []),
|
||||
web_links=web_links.get(project_id, []),
|
||||
eurosci_voc=eurosci_voc.get(project_id, []),
|
||||
legal_basis_list=legal_basis_list.get(project_id, []),
|
||||
)
|
||||
|
||||
|
||||
def _enrich_h2020_project(
|
||||
project_id: str,
|
||||
project_source: Dict[str, Any]
|
||||
) -> Optional[H2020ProjectDetails]:
|
||||
"""Enriches a single H2020 project with all related data."""
|
||||
try:
|
||||
project = H2020Project.model_validate(project_source)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to validate H2020 project {project_id}: {e}")
|
||||
return None
|
||||
|
||||
# Fetch all related data
|
||||
organizations = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_h2020_organizations",
|
||||
model_class=H2020Organization
|
||||
)
|
||||
publications = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_h2020_publications",
|
||||
model_class=H2020Publication
|
||||
)
|
||||
deliverables = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_h2020_deliverables",
|
||||
model_class=H2020Deliverable
|
||||
)
|
||||
report_summaries = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_h2020_report_summaries",
|
||||
model_class=H2020ReportSummary
|
||||
)
|
||||
topics_list = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_h2020_topics",
|
||||
model_class=H2020Topic
|
||||
)
|
||||
web_links = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_h2020_web_links",
|
||||
model_class=H2020WebLink
|
||||
)
|
||||
eurosci_voc = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_h2020_eurosci_voc",
|
||||
model_class=H2020EurosciVoc
|
||||
)
|
||||
legal_basis_list = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_h2020_legal_basis",
|
||||
model_class=H2020LegalBasis
|
||||
)
|
||||
erc_pis = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_h2020_erc_pis",
|
||||
model_class=H2020ErcPI
|
||||
)
|
||||
|
||||
# Build and return enriched project
|
||||
return H2020ProjectDetails.from_project_with_relations(
|
||||
project=project,
|
||||
organizations=organizations.get(project_id, []),
|
||||
publications=publications.get(project_id, []),
|
||||
deliverables=deliverables.get(project_id, []),
|
||||
report_summaries=report_summaries.get(project_id, []),
|
||||
topics_list=topics_list.get(project_id, []),
|
||||
web_links=web_links.get(project_id, []),
|
||||
eurosci_voc=eurosci_voc.get(project_id, []),
|
||||
legal_basis_list=legal_basis_list.get(project_id, []),
|
||||
erc_principal_investigators=erc_pis.get(project_id, []),
|
||||
)
|
||||
|
||||
|
||||
def _enrich_heu_project(
|
||||
project_id: str,
|
||||
project_source: Dict[str, Any]
|
||||
) -> Optional[HEUProjectDetails]:
|
||||
"""Enriches a single Horizon Europe project with all related data."""
|
||||
try:
|
||||
project = HEUProject.model_validate(project_source)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to validate Horizon Europe project {project_id}: {e}")
|
||||
return None
|
||||
|
||||
# Fetch all related data
|
||||
organizations = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_heu_organizations",
|
||||
model_class=HEUOrganization
|
||||
)
|
||||
publications = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_heu_publications",
|
||||
model_class=HEUPublication
|
||||
)
|
||||
deliverables = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_heu_deliverables",
|
||||
model_class=HEUDeliverable
|
||||
)
|
||||
report_summaries = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_heu_report_summaries",
|
||||
model_class=HEUReportSummary
|
||||
)
|
||||
topics_list = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_heu_topics",
|
||||
model_class=HEUTopic
|
||||
)
|
||||
web_links = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_heu_web_links",
|
||||
model_class=HEUWebLink
|
||||
)
|
||||
eurosci_voc = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_heu_eurosci_voc",
|
||||
model_class=HEUEurosciVoc
|
||||
)
|
||||
legal_basis_list = _fetch_related_entities(
|
||||
project_ids=[project_id],
|
||||
index_pattern="cordis_eu_heu_legal_basis",
|
||||
model_class=HEULegalBasis
|
||||
)
|
||||
|
||||
# Build and return enriched project
|
||||
return HEUProjectDetails.from_project_with_relations(
|
||||
project=project,
|
||||
organizations=organizations.get(project_id, []),
|
||||
publications=publications.get(project_id, []),
|
||||
deliverables=deliverables.get(project_id, []),
|
||||
report_summaries=report_summaries.get(project_id, []),
|
||||
topics_list=topics_list.get(project_id, []),
|
||||
web_links=web_links.get(project_id, []),
|
||||
eurosci_voc=eurosci_voc.get(project_id, []),
|
||||
legal_basis_list=legal_basis_list.get(project_id, []),
|
||||
)
|
||||
|
||||
|
||||
def _fetch_related_entities(
|
||||
project_ids: List[str],
|
||||
index_pattern: str,
|
||||
model_class: Any
|
||||
) -> Dict[str, List[Any]]:
|
||||
"""
|
||||
Fetches and groups related entities from Elasticsearch by project ID.
|
||||
|
||||
Args:
|
||||
project_ids: List of project IDs to fetch entities for
|
||||
index_pattern: The index pattern to search (supports wildcards)
|
||||
model_class: The Pydantic model class to validate against
|
||||
|
||||
Returns:
|
||||
Dictionary mapping project IDs to lists of entities
|
||||
"""
|
||||
query = {"bool": {"filter": [{"terms": {"projectID": project_ids}}]}}
|
||||
|
||||
try:
|
||||
response = elasticsearch.search(
|
||||
query=query,
|
||||
index=index_pattern,
|
||||
cluster=ESCluster.INSE,
|
||||
size=1
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to fetch from {index_pattern}: {e}")
|
||||
return {}
|
||||
|
||||
grouped = {}
|
||||
hits = response.get("hits", {}).get("hits", [])
|
||||
|
||||
for hit in hits:
|
||||
source = hit.get("_source", {})
|
||||
project_id = source.get("projectID")
|
||||
|
||||
if not project_id:
|
||||
continue
|
||||
|
||||
if project_id not in grouped:
|
||||
grouped[project_id] = []
|
||||
|
||||
try:
|
||||
entity = model_class.model_validate(source)
|
||||
grouped[project_id].append(entity)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to validate entity for project {project_id}: {e}")
|
||||
continue
|
||||
|
||||
return grouped
|
||||
Reference in New Issue
Block a user