-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
268 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-FileCopyrightText: 2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from data_rentgen.consumer.extractors.location import location_for_job | ||
|
||
__all__ = [ | ||
"location_for_job", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# SPDX-FileCopyrightText: 2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from urllib.parse import urlparse | ||
|
||
from data_rentgen.consumer.openlineage.job import OpenLineageJob | ||
from data_rentgen.consumer.openlineage.run_facets import OpenLineageParentJob | ||
from data_rentgen.dto import LocationDTO | ||
|
||
|
||
def location_for_job(job: OpenLineageJob | OpenLineageParentJob) -> LocationDTO: | ||
url = urlparse(job.namespace) | ||
scheme = url.scheme or "unknown" | ||
netloc = url.netloc or url.path | ||
return LocationDTO( | ||
type=scheme, | ||
name=netloc, | ||
urls=[f"{scheme}://{netloc}"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
# SPDX-FileCopyrightText: 2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from faststream import Logger | ||
from faststream import Depends | ||
from faststream.kafka import KafkaRouter | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from data_rentgen.consumer.extractors import location_for_job | ||
from data_rentgen.consumer.openlineage.run_event import OpenLineageRunEvent | ||
from data_rentgen.dependencies import Stub | ||
from data_rentgen.dto.location import LocationDTO | ||
from data_rentgen.services.uow import UnitOfWork | ||
|
||
router = KafkaRouter() | ||
|
||
|
||
def get_unit_of_work(session: AsyncSession = Depends(Stub(AsyncSession))) -> UnitOfWork: | ||
return UnitOfWork(session) | ||
|
||
|
||
@router.subscriber("input.runs") | ||
async def runs_handler(msg: OpenLineageRunEvent, logger: Logger): | ||
logger.info("Successfully handled, %s", msg) | ||
async def runs_handler(event: OpenLineageRunEvent, unit_of_work: UnitOfWork = Depends(get_unit_of_work)): | ||
job_location = location_for_job(event.job) | ||
|
||
parent_location: LocationDTO | None = None | ||
parent_run_facet = event.run.facets.get("parent", None) | ||
if parent_run_facet: | ||
parent_location = location_for_job(parent_run_facet.job) | ||
|
||
async with unit_of_work: | ||
await unit_of_work.location.get_or_create(job_location) | ||
if parent_location: | ||
await unit_of_work.location.get_or_create(parent_location) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# SPDX-FileCopyrightText: 2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from sqlalchemy import select | ||
from sqlalchemy.orm import selectinload | ||
|
||
from data_rentgen.db.models import Address, Location | ||
from data_rentgen.db.repositories.base import Repository | ||
from data_rentgen.dto import LocationDTO | ||
|
||
|
||
class LocationRepository(Repository[Location]): | ||
async def get_or_create(self, location: LocationDTO) -> Location: | ||
by_name = select(Location).where(Location.type == location.type, Location.name == location.name) | ||
by_addresses = ( | ||
select(Location) | ||
.join(Location.addresses) | ||
.where(Location.type == location.type, Address.url.in_(location.urls)) | ||
) | ||
statement = ( | ||
select(Location).from_statement(by_name.union(by_addresses)).options(selectinload(Location.addresses)) | ||
) | ||
|
||
result = await self._session.scalar(statement) | ||
changed: bool = False | ||
if not result: | ||
result = Location(type=location.type, name=location.name) | ||
self._session.add(result) | ||
changed = True | ||
|
||
existing_urls = {address.url for address in result.addresses} | ||
new_urls = set(location.urls) - existing_urls | ||
for url in new_urls: | ||
# currently we automatically add new addresses, but not delete old ones | ||
result.addresses.append(Address(url=url)) | ||
changed = True | ||
|
||
if changed: | ||
await self._session.flush() | ||
return result |
2 changes: 1 addition & 1 deletion
2
data_rentgen/server/dependencies/__init__.py → data_rentgen/dependencies/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# SPDX-FileCopyrightText: 2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from data_rentgen.server.dependencies.stub import Stub | ||
from data_rentgen.dependencies.stub import Stub | ||
|
||
__all__ = ["Stub"] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# SPDX-FileCopyrightText: 2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from data_rentgen.dto.location import LocationDTO | ||
from data_rentgen.dto.pagination import PaginationDTO | ||
|
||
__all__ = [ | ||
"LocationDTO", | ||
"PaginationDTO", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# SPDX-FileCopyrightText: 2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class LocationDTO: | ||
type: str | ||
name: str | ||
urls: list[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
data_rentgen/server/services/__init__.py → data_rentgen/services/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# SPDX-FileCopyrightText: 2024 MTS PJSC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from data_rentgen.server.services.uow import UnitOfWork | ||
from data_rentgen.services.uow import UnitOfWork | ||
|
||
__all__ = ["UnitOfWork"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.