Added funding visualization for institution cordis eu projects
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from enum import StrEnum
|
||||
from typing import List, Literal, Optional
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
@@ -8,6 +8,24 @@ from inse.models.eu_project import EuProject
|
||||
from inse.models.cordis_eu_project import CordisEuProject
|
||||
|
||||
|
||||
class ProjectType(StrEnum):
|
||||
BUND = "bund"
|
||||
EU = "eu"
|
||||
DFG = "dfg"
|
||||
|
||||
|
||||
class ProjectsSortBy(StrEnum):
|
||||
TITLE = "title"
|
||||
FUNDING_START = "funding_start"
|
||||
FUNDING_END = "funding_end"
|
||||
FUNDING_AMOUNT = "funding_amount"
|
||||
|
||||
|
||||
class SortDirection(StrEnum):
|
||||
ASC = "asc"
|
||||
DESC = "desc"
|
||||
|
||||
|
||||
class Page(BaseModel):
|
||||
page: int = Field(default=0, description="The current page number.")
|
||||
size: int = Field(default=20, description="The requested page size.")
|
||||
@@ -78,7 +96,7 @@ class DfgProjectPage(Page):
|
||||
|
||||
|
||||
class InstitutionFunderSummary(BaseModel):
|
||||
type: Literal["bund", "eu", "dfg"] = Field(
|
||||
type: ProjectType = Field(
|
||||
description="The type of funding data (e.g., 'bund', 'eu', 'dfg')."
|
||||
)
|
||||
funding_per_year: dict = Field(
|
||||
@@ -91,24 +109,6 @@ class InstitutionFunderSummary(BaseModel):
|
||||
)
|
||||
|
||||
|
||||
class ProjectType(StrEnum):
|
||||
BUND = "bund"
|
||||
EU = "eu"
|
||||
DFG = "dfg"
|
||||
|
||||
|
||||
class ProjectsSortBy(StrEnum):
|
||||
TITLE = "title"
|
||||
FUNDING_START = "funding_start"
|
||||
FUNDING_END = "funding_end"
|
||||
FUNDING_AMOUNT = "funding_amount"
|
||||
|
||||
|
||||
class SortDirection(StrEnum):
|
||||
ASC = "asc"
|
||||
DESC = "desc"
|
||||
|
||||
|
||||
class ProjectsFilteringProperties(BaseModel):
|
||||
project_type: ProjectType = Field(description="The type of projects to filter by.")
|
||||
page: int = Field(description="The page number for pagination.")
|
||||
|
||||
@@ -80,7 +80,7 @@ async def search_bund_projects(
|
||||
"/institution/bund/funding-over-time",
|
||||
summary="Get funding over time for bund projects.",
|
||||
description="Returns the funding over time for bund projects associated with the specified institution.",
|
||||
operation_id="getBundFundingOverTime",
|
||||
operation_id="getInstitutionBundProjectsFundingOverTime",
|
||||
responses={
|
||||
status.HTTP_200_OK: {
|
||||
"description": "Successful Response",
|
||||
@@ -90,21 +90,21 @@ async def search_bund_projects(
|
||||
},
|
||||
response_model_by_alias=False,
|
||||
)
|
||||
async def get_bund_funding_over_time(
|
||||
async def get_institution_bund_projects_funding_over_time(
|
||||
bund_search_client: Annotated[
|
||||
BundProjectsSearchClient, Depends(get_bund_search_client)
|
||||
],
|
||||
institutions_search_client: Annotated[
|
||||
InstitutionsSearchClient, Depends(get_institutions_search_client)
|
||||
],
|
||||
institution_id: Annotated[
|
||||
raw_oa_institution_id: Annotated[
|
||||
str, Query(description="The institution ID to filter by.")
|
||||
],
|
||||
) -> InstitutionFunderSummary:
|
||||
return await projects_service.get_institution_bund_funding_over_time(
|
||||
return await projects_service.get_institution_bund_projects_funding_over_time(
|
||||
bund_search_client=bund_search_client,
|
||||
institutions_search_client=institutions_search_client,
|
||||
institution_id=institution_id,
|
||||
raw_oa_institution_id=raw_oa_institution_id,
|
||||
)
|
||||
|
||||
|
||||
@@ -139,6 +139,36 @@ async def search_cordis_eu_projects(
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/institution/eu/funding-over-time",
|
||||
summary="Get funding over time for EU projects.",
|
||||
description="Returns the funding over time for EU projects associated with the specified institution.",
|
||||
operation_id="getInstitutionCordisEuProjectsFundingOverTime",
|
||||
responses={
|
||||
status.HTTP_200_OK: {
|
||||
"description": "Successful Response",
|
||||
"model": InstitutionFunderSummary,
|
||||
},
|
||||
status.HTTP_404_NOT_FOUND: {"description": "Institution not found"},
|
||||
},
|
||||
response_model_by_alias=False,
|
||||
)
|
||||
async def get_institution_cordis_eu_projects_funding_over_time(
|
||||
eu_search_client: Annotated[EuProjectsSearchClient, Depends(get_eu_search_client)],
|
||||
institutions_search_client: Annotated[
|
||||
InstitutionsSearchClient, Depends(get_institutions_search_client)
|
||||
],
|
||||
raw_oa_institution_id: Annotated[
|
||||
str, Query(description="The institution ID to filter by.")
|
||||
],
|
||||
) -> InstitutionFunderSummary:
|
||||
return await projects_service.get_institution_cordis_eu_projects_funding_over_time(
|
||||
eu_search_client=eu_search_client,
|
||||
institutions_search_client=institutions_search_client,
|
||||
raw_oa_institution_id=raw_oa_institution_id,
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/person/dfg/search",
|
||||
summary="Get DFG projects.",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import datetime
|
||||
import logging
|
||||
from typing import Dict, List, Optional
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from inse.projects.projects_search_client import (
|
||||
BundProjectsSearchClient,
|
||||
@@ -13,6 +13,7 @@ from inse.projects.model import (
|
||||
DfgProjectPage,
|
||||
EuProjectPage,
|
||||
InstitutionFunderSummary,
|
||||
ProjectType,
|
||||
ProjectsFilteringProperties,
|
||||
)
|
||||
from inse.institutions.institutions_search_client import (
|
||||
@@ -204,17 +205,45 @@ async def get_dfg_projects_by_author_id(
|
||||
return response
|
||||
|
||||
|
||||
async def get_institution_bund_funding_over_time(
|
||||
async def get_institution_cordis_eu_projects_funding_over_time(
|
||||
eu_search_client: EuProjectsSearchClient,
|
||||
institutions_search_client: InstitutionsSearchClient,
|
||||
raw_oa_institution_id: str,
|
||||
) -> InstitutionFunderSummary:
|
||||
|
||||
cordis_eu_project_page = await search_cordis_eu_projects(
|
||||
eu_search_client,
|
||||
institutions_search_client,
|
||||
ProjectsFilteringProperties(
|
||||
project_type=ProjectType.EU,
|
||||
raw_oa_institution_id=raw_oa_institution_id,
|
||||
page=0,
|
||||
size=10000,
|
||||
),
|
||||
)
|
||||
|
||||
return _calculate_funding_over_time(
|
||||
project_type=ProjectType.EU,
|
||||
projects=cordis_eu_project_page.elements,
|
||||
funding_start_attr="startDate",
|
||||
funding_end_attr="endDate",
|
||||
funding_amount_attr="totalCost",
|
||||
)
|
||||
|
||||
|
||||
async def get_institution_bund_projects_funding_over_time(
|
||||
bund_search_client: BundProjectsSearchClient,
|
||||
institutions_search_client: InstitutionsSearchClient,
|
||||
institution_id: str,
|
||||
raw_oa_institution_id: str,
|
||||
) -> InstitutionFunderSummary:
|
||||
logger.info(f"Fetching funding over time for institution_id={institution_id}")
|
||||
logger.info(
|
||||
f"Fetching funding over time for institution_id={raw_oa_institution_id}"
|
||||
)
|
||||
institution_aliases = institutions_search_client.get_institution_aliases_by_id(
|
||||
institution_id
|
||||
raw_oa_institution_id
|
||||
)
|
||||
logger.debug(
|
||||
f"Found institution aliases for institution_id={institution_id}: {institution_aliases}"
|
||||
f"Found institution aliases for institution_id={raw_oa_institution_id}: {institution_aliases}"
|
||||
)
|
||||
response = await bund_search_client.search_bund_projects(
|
||||
page=0,
|
||||
@@ -222,14 +251,36 @@ async def get_institution_bund_funding_over_time(
|
||||
institution_names=institution_aliases,
|
||||
)
|
||||
|
||||
return _calculate_funding_over_time(
|
||||
project_type=ProjectType.BUND,
|
||||
projects=response.elements,
|
||||
funding_start_attr="funding_start",
|
||||
funding_end_attr="funding_end",
|
||||
funding_amount_attr="grant_amount",
|
||||
)
|
||||
|
||||
|
||||
def _calculate_funding_over_time(
|
||||
project_type: ProjectType,
|
||||
projects: List[Any],
|
||||
funding_start_attr: str,
|
||||
funding_end_attr: str,
|
||||
funding_amount_attr: str,
|
||||
) -> InstitutionFunderSummary:
|
||||
funding_per_year: Dict[int, float] = {}
|
||||
for project in response.elements:
|
||||
funding_start = project.funding_start
|
||||
funding_end = project.funding_end
|
||||
funding_amount = project.grant_amount
|
||||
|
||||
for project in projects:
|
||||
funding_start = getattr(project, funding_start_attr)
|
||||
funding_end = getattr(project, funding_end_attr)
|
||||
funding_amount = getattr(project, funding_amount_attr)
|
||||
if not funding_start or not funding_end or not funding_amount:
|
||||
continue
|
||||
|
||||
if hasattr(funding_start, "date"):
|
||||
funding_start = funding_start.date()
|
||||
if hasattr(funding_end, "date"):
|
||||
funding_end = funding_end.date()
|
||||
|
||||
delta = (funding_end - funding_start).days + 1
|
||||
if delta <= 0:
|
||||
continue
|
||||
@@ -271,7 +322,7 @@ async def get_institution_bund_funding_over_time(
|
||||
annual_average_over_period = 0.0
|
||||
|
||||
return InstitutionFunderSummary(
|
||||
type="bund",
|
||||
type=project_type,
|
||||
funding_per_year=sorted_funding_per_year,
|
||||
annual_average_over_period=annual_average_over_period,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user