-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdao.py
More file actions
99 lines (83 loc) · 3.48 KB
/
Copy pathdao.py
File metadata and controls
99 lines (83 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2021 - 2026 Universität Tübingen, DKFZ, EMBL, and Universität zu Köln
# for the German Human Genome-Phenome Archive (GHGA)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""DAO translators for accessing the database."""
from hexkit.protocols.dao import DaoFactoryProtocol
from hexkit.protocols.daopub import DaoPublisher, DaoPublisherFactoryProtocol
from hexkit.providers.mongodb import MongoDbIndex
from pydantic import Field
from pydantic_settings import BaseSettings
from ets.core import models
from ets.core.models import AEMPack, AEMPackFailedEvent
from ets.ports.outbound.dao import (
FailedEventDao,
ModelDao,
RouteDao,
WorkflowDao,
)
async def get_persisted_model_dao(*, dao_factory: DaoFactoryProtocol) -> ModelDao:
"""Setup the Persisted Model DAO using the specified provider of the DaoFactoryProtocol."""
return await dao_factory.get_dao(
name="models", dto_model=models.Model, id_field="name"
)
async def get_workflow_dao(*, dao_factory: DaoFactoryProtocol) -> WorkflowDao:
"""Setup the Workflow DAO using the specified provider of the DaoFactoryProtocol."""
return await dao_factory.get_dao(
name="workflows", dto_model=models.Workflow, id_field="name"
)
async def get_route_dao(*, dao_factory: DaoFactoryProtocol) -> RouteDao:
"""Setup the Route DAO using the specified provider of the DaoFactoryProtocol."""
return await dao_factory.get_dao(
name="routes", dto_model=models.Route, id_field="name"
)
class AEMPackDaoConfig(BaseSettings):
"""Config for the AEMPack event publisher adapter."""
derived_aem_pack_topic: str = Field(
default=...,
description="Topic for events informing about derived AEMPacks.",
examples=["derived-aempacks"],
)
aem_pack_processing_event_topic: str = Field(
default=...,
description=(
"Topic for AEMPack processing-lifecycle (status) events, e.g. processing"
" failures, and later successes."
),
examples=["aempack-processing-events"],
)
async def get_aem_pack_dao(
*, dao_publisher_factory: DaoPublisherFactoryProtocol, topic: str
) -> DaoPublisher[AEMPack]:
"""Construct an outbox DAO for AEMPack objects."""
return await dao_publisher_factory.get_dao(
name="aem_packs",
id_field="id",
dto_model=AEMPack,
dto_to_event=lambda aem_pack: aem_pack.model_dump(mode="json"),
event_topic=topic,
autopublish=True,
indexes=[MongoDbIndex(fields={"pid": 1, "model_name": 1})],
)
async def get_failed_event_dao(
*, dao_publisher_factory: DaoPublisherFactoryProtocol, topic: str
) -> FailedEventDao:
"""Construct an outbox DAO for AEMPack processing-failure events."""
return await dao_publisher_factory.get_dao(
name="aem_pack_failed_events",
id_field="id",
dto_model=AEMPackFailedEvent,
dto_to_event=lambda event: event.model_dump(mode="json"),
event_topic=topic,
autopublish=True,
)