added orgminer staff validation through OA collab graph
This commit is contained in:
@@ -23,6 +23,7 @@ choice - nothing is cached in memory between requests.
|
||||
"""
|
||||
|
||||
from collections import defaultdict
|
||||
import asyncio
|
||||
from typing import Any, Callable, Optional
|
||||
from inse.integrations.elasticsearch import search
|
||||
from inse.utils import normalize_name
|
||||
@@ -215,10 +216,90 @@ async def get_orgminer_content(org_url: str,institution_id: str = DEFAULT_INSTIT
|
||||
index = resolve_es_index(institution_id)
|
||||
response = await search(query=query,index=index,cluster=ESCluster.INSE)
|
||||
orgminer_content = response["hits"]["hits"][0]["_source"] if response["hits"]["hits"] else {}
|
||||
classified_page_aggs = await get_classified_pages_aggs(org_url, institution_id)
|
||||
print("CLASSIFIED PAGE AGGS:", classified_page_aggs)
|
||||
return OrgminerContent.from_elastic_hit(orgminer_content)
|
||||
|
||||
|
||||
async def get_orgminer_content_verified_by_collab_graph(
|
||||
org_url: str,
|
||||
institution_id: str = DEFAULT_INSTITUTION_ID,
|
||||
) -> OrgminerContent:
|
||||
"""
|
||||
Return OrgMiner content with `verified_staff` containing only staff whose
|
||||
OpenAlex candidate(s) have a collaboration edge with candidate(s) of a
|
||||
different staff member in the same organization.
|
||||
|
||||
Returns None if the org does not exist.
|
||||
"""
|
||||
query = {"term": {"uri.keyword": org_url}}
|
||||
index = resolve_es_index(institution_id)
|
||||
|
||||
response = await search(query=query, index=index, cluster=ESCluster.INSE)
|
||||
hits = response.get("hits", {}).get("hits", [])
|
||||
if not hits:
|
||||
return None
|
||||
|
||||
orgminer_content = hits[0]["_source"]
|
||||
staff_list = orgminer_content.get("staff") or []
|
||||
|
||||
async def _resolve_staff(staff: dict) -> set[str]:
|
||||
name = staff.get("name")
|
||||
if not name:
|
||||
return set()
|
||||
|
||||
candidate_ids = await resolve_author_candidates(
|
||||
normalize_name(name),
|
||||
institution_id,
|
||||
)
|
||||
return set(candidate_ids)
|
||||
|
||||
async def _get_neighbor_ids(author_id: str) -> set[str]:
|
||||
collaborator_weights = await get_person_collaborator_weights(
|
||||
author_id,
|
||||
institution_id,
|
||||
)
|
||||
return set(collaborator_weights.keys())
|
||||
|
||||
candidate_sets = await asyncio.gather(
|
||||
*(_resolve_staff(staff) for staff in staff_list)
|
||||
)
|
||||
|
||||
async def _is_verified(staff_index: int) -> bool:
|
||||
own_ids = candidate_sets[staff_index]
|
||||
if not own_ids:
|
||||
return False
|
||||
|
||||
# Candidates belonging to other staff members only.
|
||||
other_ids = (
|
||||
set().union(
|
||||
*(
|
||||
candidate_ids
|
||||
for index, candidate_ids in enumerate(candidate_sets)
|
||||
if index != staff_index
|
||||
)
|
||||
)
|
||||
- own_ids
|
||||
)
|
||||
|
||||
if not other_ids:
|
||||
return False
|
||||
|
||||
neighbor_sets = await asyncio.gather(
|
||||
*(_get_neighbor_ids(author_id) for author_id in own_ids)
|
||||
)
|
||||
|
||||
return any(neighbor_ids & other_ids for neighbor_ids in neighbor_sets)
|
||||
|
||||
verified_flags = await asyncio.gather(
|
||||
*(_is_verified(index) for index in range(len(staff_list)))
|
||||
)
|
||||
|
||||
orgminer_content["verified_staff"] = [
|
||||
staff
|
||||
for staff, is_verified in zip(staff_list, verified_flags)
|
||||
if is_verified
|
||||
]
|
||||
|
||||
return OrgminerContent.from_elastic_hit(orgminer_content)
|
||||
async def get_classified_pages_aggs(org_url: str,institution_id: str = DEFAULT_INSTITUTION_ID,) -> list[CategoryBucket]:
|
||||
"""
|
||||
Returns the aggregated categories of the classified pages of the orgminer org.
|
||||
|
||||
@@ -48,11 +48,6 @@ class OrgminerStaff(BaseModel):
|
||||
)
|
||||
|
||||
|
||||
class OrgminerTag(BaseModel):
|
||||
score: Optional[float] = None
|
||||
value: Optional[str] = None
|
||||
|
||||
|
||||
class OrgminerContent(BaseModel):
|
||||
uri: Optional[str] = None
|
||||
|
||||
@@ -60,7 +55,7 @@ class OrgminerContent(BaseModel):
|
||||
news: List[OrgminerNews] = Field(default_factory=list)
|
||||
projects: List[OrgminerProject] = Field(default_factory=list)
|
||||
staff: List[OrgminerStaff] = Field(default_factory=list)
|
||||
tags: List[OrgminerTag] = Field(default_factory=list)
|
||||
verified_staff: List[OrgminerStaff] = Field(default_factory=list)
|
||||
|
||||
@classmethod
|
||||
def from_elastic_hit(cls, hit: dict[str, Any]) -> "OrgminerContent":
|
||||
@@ -84,9 +79,9 @@ class OrgminerContent(BaseModel):
|
||||
OrgminerStaff.from_staff_object(item)
|
||||
for item in source.get("staff", [])
|
||||
],
|
||||
tags=[
|
||||
OrgminerTag.model_validate(item)
|
||||
for item in source.get("tags", [])
|
||||
verified_staff=[
|
||||
OrgminerStaff.from_staff_object(item)
|
||||
for item in source.get("verified_staff", [])
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -71,7 +71,7 @@ async def get_hierarchy(
|
||||
@router.get(
|
||||
"/orgminer-content",
|
||||
operation_id="getOrgminerContent",
|
||||
response_model=OrgminerContent,
|
||||
response_model=OrgminerContent | None,
|
||||
summary="Returns the content of the orgminer org.",
|
||||
tags=[Tags.ORGMINER],
|
||||
)
|
||||
@@ -80,11 +80,12 @@ async def get_orgminer_content(
|
||||
session:SessionDep,
|
||||
access_token_metadata: Annotated[
|
||||
AccessTokenMetadata, CurrentUserAccessTokenPayloadDep
|
||||
]) -> OrgminerContent:
|
||||
]) -> OrgminerContent | None:
|
||||
institution_id = _get_account_institution_id(session, access_token_metadata.effective_account_id)
|
||||
if not institution_id:
|
||||
institution_id = DEFAULT_INSTITUTION_ID
|
||||
return await logic.get_orgminer_content(org_url,full_institution_url(institution_id))
|
||||
return await logic.get_orgminer_content_verified_by_collab_graph(org_url,full_institution_url(institution_id))
|
||||
#return await logic.get_orgminer_content(org_url,full_institution_url(institution_id))
|
||||
|
||||
@router.get(
|
||||
"/classified-pages-aggs",
|
||||
@@ -99,6 +100,9 @@ async def get_classified_pages_aggs(
|
||||
access_token_metadata: Annotated[
|
||||
AccessTokenMetadata, CurrentUserAccessTokenPayloadDep
|
||||
]) -> list[CategoryBucket]:
|
||||
"""
|
||||
Returns the aggregated categories of the classified pages of the orgminer org.
|
||||
"""
|
||||
institution_id = _get_account_institution_id(session, access_token_metadata.effective_account_id)
|
||||
if not institution_id:
|
||||
institution_id = DEFAULT_INSTITUTION_ID
|
||||
|
||||
Reference in New Issue
Block a user