Skip to content

Commit

Permalink
Sample based on last digest
Browse files Browse the repository at this point in the history
  • Loading branch information
G L committed Jul 28, 2024
1 parent 38025d7 commit d6b5728
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 22 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ coverage:

run:
PYTHONPATH=. python3 echopages/main.py

real:
export $(grep -v '^#' .env | xargs) && PYTHONPATH=. python3 echopages/main.py
3 changes: 2 additions & 1 deletion echopages/application/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def sample_contents(
) -> List[model.Content]:
with uow:
contents = uow.content_repo.get_all()
return content_sampler.sample(contents, number_of_units)
digests = uow.digest_repo.get_all()
return content_sampler.sample(digests, contents, number_of_units)


def generate_digest(
Expand Down
4 changes: 3 additions & 1 deletion echopages/domain/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def store_content_str(self, content_str: str) -> None:

class ContentSampler(abc.ABC):
@abc.abstractmethod
def sample(self, contents: List[Content], number_of_units: int) -> List[Content]:
def sample(
self, digests: List[Digest], contents: List[Content], number_of_units: int
) -> List[Content]:
raise NotImplementedError


Expand Down
27 changes: 19 additions & 8 deletions echopages/infrastructure/delivery/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,40 @@
from echopages.domain import model


class CountIndex:
value: int = 0


class SimpleContentSampler(model.ContentSampler):
def __init__(self) -> None:
super().__init__()

def sample(
self, contents: List[model.Content], number_of_units: int
self,
digests: List[model.Digest],
contents: List[model.Content],
number_of_units: int,
) -> List[model.Content]:
"""
Returns `number_of_units` content units starting from the current count index.
Returns `number_of_units` content units starting from the last content sent.
If there are not enough content units available, it loops back to the beginning of the list.
"""
if len(contents) == 0:
raise ValueError("No content units available")

# TODO: Indices are not a direct match to IDs, neither for digests nor for contents.

# Start sampling from the last content sent
if len(digests) == 0:
content_sampler_index = -1
else:
last_sent_content_id = max(digests[-1].content_ids)
content_sampler_index = [content.id for content in contents].index(
last_sent_content_id
)
print()

contents_length = len(contents)
sampled_contents = []

for _ in range(number_of_units):
sampled_contents.append(contents[CountIndex.value])
CountIndex.value = (CountIndex.value + 1) % contents_length
content_sampler_index = (content_sampler_index + 1) % contents_length
sampled_contents.append(contents[content_sampler_index])

return sampled_contents
2 changes: 1 addition & 1 deletion production.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DELIVERY_SYSTEM="PostmarkDigestDeliverySystem"
APP_EMAIL_ADDRESS=echopages@pachovit.com
NUMBER_OF_UNITS_PER_DIGEST=1
DAILY_TIME_OF_DIGEST="12:45"
DAILY_TIME_OF_DIGEST="13:45"
2 changes: 0 additions & 2 deletions tests/integration/test_file_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
get_sampler,
get_unit_of_work,
)
from echopages.infrastructure.delivery import samplers


def sample_content_data(id: int) -> Dict[str, str]:
Expand All @@ -25,7 +24,6 @@ def test_trigger_digest() -> None:
delivery_system = get_digest_delivery_system()

content_sampler = get_sampler()
samplers.CountIndex.value = 0

with uow:
assert len(uow.digest_repo.get_all()) == 0
Expand Down
28 changes: 25 additions & 3 deletions tests/unit/test_samplers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
from typing import List

from echopages.domain import model
from echopages.infrastructure.delivery import samplers


def test_simple_content_sampler() -> None:
samplers.CountIndex.value = 0
def test_simple_content_sampler_no_digests() -> None:
contents = [
model.Content(id=0, text="content unit 1"),
model.Content(id=1, text="content unit 2"),
model.Content(id=2, text="content unit 3"),
]
digests: List[model.Digest] = []

content_sampler = samplers.SimpleContentSampler()
sampled_contents = content_sampler.sample(contents, 8)
sampled_contents = content_sampler.sample(digests, contents, 8)

assert len(sampled_contents) == 8
for n in range(8):
assert sampled_contents[n].id == (n % 3)


def test_simple_content_sampler_previous_digests() -> None:
content_ids = [1, 3, 5]
contents = [
model.Content(id=1, text="content unit 1"),
model.Content(id=3, text="content unit 3"),
model.Content(id=5, text="content unit 5"),
]
digests = [
model.Digest(id=0, content_ids=[5]),
model.Digest(id=1, content_ids=[1, 3]),
]

content_sampler = samplers.SimpleContentSampler()
sampled_contents = content_sampler.sample(digests, contents, 8)

assert len(sampled_contents) == 8
for n in range(8):
assert sampled_contents[n].id == content_ids[(n + 2) % 3]
3 changes: 0 additions & 3 deletions tests/unit/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def test_generate_digest() -> None:
content_objects = setup_contents(uow, contents)

content_sampler = samplers.SimpleContentSampler()
samplers.CountIndex.value = 0

number_of_units = 2

Expand Down Expand Up @@ -168,7 +167,6 @@ def test_trigger_digest() -> None:
delivery_system = FakeDigestDeliverySystem()

content_sampler = samplers.SimpleContentSampler()
samplers.CountIndex.value = 0

# Given: 3 contents
contents = [sample_content_data(1), sample_content_data(2), sample_content_data(3)]
Expand Down Expand Up @@ -200,7 +198,6 @@ def test_all_flow() -> None:
digest_formatter = FakeDigestFormatter()
delivery_system = FakeDigestDeliverySystem()
content_sampler = samplers.SimpleContentSampler()
samplers.CountIndex.value = 0

# Populate contents
services.add_content(uow, sample_content_data(1))
Expand Down

0 comments on commit d6b5728

Please sign in to comment.