Skip to content

Commit

Permalink
Merge pull request #1162 from SEKOIA-IO/lv/tehtris_save_checkpoint
Browse files Browse the repository at this point in the history
Tehtris - save the progress
  • Loading branch information
squioc authored Nov 20, 2024
2 parents 9416ca2 + 778c99a commit 847aa0d
Show file tree
Hide file tree
Showing 6 changed files with 1,113 additions and 806 deletions.
6 changes: 6 additions & 0 deletions Tehtris/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 2024-11-07 - 1.16.0

### Changed

- Save the progress

## 2024-10-30 - 1.15.3

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Tehtris/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"name": "TEHTRIS",
"uuid": "1528d749-d353-4e38-ab1b-6e01d7595569",
"slug": "tehtris",
"version": "1.15.3",
"version": "1.16.0",
"categories": [
"Endpoint"
]
Expand Down
1,881 changes: 1,088 additions & 793 deletions Tehtris/poetry.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Tehtris/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ authors = []

[tool.poetry.dependencies]
python = ">=3.10,<3.12"
sekoia-automation-sdk = "^1.13.0"
orjson = "^3.7.7"
python-dateutil = "^2.8.2"
cachetools = "^5.4.0"
sekoia-automation-sdk = "^1.17.2"
orjson = "^3.10.11"
python-dateutil = "^2.9.0.post0"
cachetools = "^5.5.0"

[tool.poetry.dev-dependencies]
pytest = "*"
Expand Down
20 changes: 13 additions & 7 deletions Tehtris/tehtris_modules/trigger_tehtris_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import orjson
from cachetools import Cache, LRUCache
from dateutil.parser import isoparse
from sekoia_automation.checkpoint import CheckpointDatetime
from sekoia_automation.connector import Connector, DefaultConnectorConfiguration
from sekoia_automation.storage import PersistentJSON

from tehtris_modules import TehtrisModule
from tehtris_modules.client import ApiClient
Expand All @@ -31,7 +33,12 @@ class TehtrisEventConnector(Connector):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.from_date = datetime.now(timezone.utc) - timedelta(minutes=1)

self.cursor = CheckpointDatetime(
path=self._data_path, start_at=timedelta(minutes=1), ignore_older_than=timedelta(hours=1)
)
self.from_date = self.cursor.offset

self.fetch_events_limit = 100
self.events_cache: Cache = LRUCache(maxsize=1000) # TODO: is it enough to have 1000 event ids in cache?

Expand All @@ -40,11 +47,6 @@ def client(self):
return ApiClient(self.module.configuration.apikey)

def __fetch_next_events(self, from_date: datetime, offset: int):
# We don't retrieve messages older than one hour
one_hour_ago = datetime.now(timezone.utc) - timedelta(hours=1)
if from_date < one_hour_ago:
from_date = one_hour_ago

params = {
"fromDate": int(from_date.timestamp()),
"limit": self.fetch_events_limit,
Expand Down Expand Up @@ -137,6 +139,10 @@ def fetch_events(self) -> Generator[list, None, None]:
current_lag: int = 0
if most_recent_date_seen > self.from_date:
self.from_date = most_recent_date_seen

# save in context the most recent date seen
self.cursor.offset = self.from_date

delta_time = datetime.now(timezone.utc) - most_recent_date_seen
current_lag = int(delta_time.total_seconds())

Expand Down Expand Up @@ -192,5 +198,5 @@ def next_batch(self):
time.sleep(delta_sleep)

def run(self):
while True:
while self.running:
self.next_batch()
2 changes: 1 addition & 1 deletion Tehtris/tests/test_tehtris_event_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def fake_time():

@pytest.fixture
def patch_datetime_now(fake_time):
with patch("tehtris_modules.trigger_tehtris_events.datetime") as mock_datetime:
with patch("sekoia_automation.checkpoint.datetime") as mock_datetime:
mock_datetime.now.return_value = fake_time
mock_datetime.side_effect = lambda *args, **kw: datetime(*args, **kw)
yield mock_datetime
Expand Down

0 comments on commit 847aa0d

Please sign in to comment.