Skip to content

Commit

Permalink
style(service): Simplify services usage
Browse files Browse the repository at this point in the history
  • Loading branch information
devsjc committed Dec 16, 2024
1 parent 0fb3b91 commit 1c8f409
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 369 deletions.
8 changes: 4 additions & 4 deletions src/nwp_consumer/internal/handlers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import datetime as dt
import logging

from returns.result import Failure
from returns.result import Failure, ResultE

from nwp_consumer.internal import ports, services

Expand Down Expand Up @@ -84,11 +84,11 @@ def run(self) -> int:
args = self.parser.parse_args()
match args.command:
case "consume":
service_result = services.Service.from_adaptors(
service_result = services.ConsumerService.from_adaptors(
model_adaptor=self.model_adaptor,
notification_adaptor=self.notification_adaptor,
)
result = service_result.do(
result: ResultE[str] = service_result.do(
consume_result
for service in service_result
for consume_result in service.consume(period=args.init_time)
Expand All @@ -98,7 +98,7 @@ def run(self) -> int:
return 1

case "archive":
service_result = services.Service.from_adaptors(
service_result = services.ConsumerService.from_adaptors(
model_adaptor=self.model_adaptor,
notification_adaptor=self.notification_adaptor,
)
Expand Down
3 changes: 1 addition & 2 deletions src/nwp_consumer/internal/ports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
in the `repositories` module.
"""

from .services import ConsumeUseCase, ArchiveUseCase
from .services import ConsumeUseCase
from .repositories import ModelRepository, NotificationRepository

__all__ = [
"ConsumeUseCase",
"ArchiveUseCase",
"ModelRepository",
"NotificationRepository",
]
57 changes: 7 additions & 50 deletions src/nwp_consumer/internal/ports/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
These interfaces define the signatures that *driving* actors must conform to
in order to interact with the core.
Also sometimes referred to as *primary ports*.
Sometimes referred to as *primary ports*.
"""

import abc
import datetime as dt

from returns.result import ResultE

from nwp_consumer.internal import entities
from .repositories import ModelRepository, NotificationRepository
from returns.methods import partition


class ConsumeUseCase(abc.ABC):
"""Interface for the consumer use case.
Expand All @@ -26,16 +22,15 @@ class ConsumeUseCase(abc.ABC):


@abc.abstractmethod
def consume(self, it: dt.datetime | None = None) -> ResultE[str]:
"""Consume NWP data to Zarr format for desired init time.
def consume(self, period: dt.datetime | dt.date | None = None) -> ResultE[str]:
"""Consume NWP data to Zarr format for desired time period.
Where possible the implementation should be as memory-efficient as possible.
The designs of the repository methods also enable parallel processing within
the implementation.
Args:
it: The initialization time for which to consume data.
If None, the latest available forecast should be consumed.
period: The period for which to gather init time data.
Returns:
The path to the produced Zarr store.
Expand All @@ -48,51 +43,13 @@ def consume(self, it: dt.datetime | None = None) -> ResultE[str]:
pass

@abc.abstractmethod
def postprocess(self, options: entities.PostProcessOptions) -> ResultE[str]:
"""Postprocess the produced Zarr according to given options."""
pass


class ArchiveUseCase(abc.ABC):
"""Interface for the archive use case.
Defines the business-critical methods for the following use cases:
- 'A user should be able to archive NWP data for a given time period.'
"""

@abc.abstractmethod
def archive(self, year: int, month: int) -> ResultE[str]:
"""Archive NWP data to Zarr format for the given month.
def archive(self, period: dt.date) -> ResultE[str]:
"""Archive NWP data to Zarr format for desired time period.
Args:
year: The year for which to archive data.
month: The month for which to archive data.
period: The period for which to gather init time data.
Returns:
The path to the produced Zarr store.
"""
pass

class InfoUseCase(abc.ABC):
"""Interface for the notification use case.
Defines the business-critical methods for the following use cases:
- 'A user should be able to retrieve information about the service.'
"""

@abc.abstractmethod
def available_models(self) -> list[str]:
"""Get a list of available models."""
pass

@abc.abstractmethod
def model_repository_info(self) -> str:
"""Get information about the model repository."""
pass

@abc.abstractmethod
def model_info(self) -> str:
"""Get information about the model."""
pass
4 changes: 0 additions & 4 deletions src/nwp_consumer/internal/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
"""

from .consumer_service import ConsumerService
from .archiver_service import ArchiverService
from .service import ConsumerService as Service

__all__ = [
"ConsumerService",
"ArchiverService",
"Service",
]
152 changes: 0 additions & 152 deletions src/nwp_consumer/internal/services/archiver_service.py

This file was deleted.

Loading

0 comments on commit 1c8f409

Please sign in to comment.