Skip to content

Commit

Permalink
add local store only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Jan 28, 2024
1 parent fab3214 commit 215ad22
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
7 changes: 5 additions & 2 deletions klimalogger/store/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from injector import Module, provider, singleton

from .client import StoreClient
from .queue import QueueStore
from ..config import Config

log = logging.getLogger(__name__)
Expand All @@ -22,7 +21,11 @@ class StoreModule(Module):
def store_provider(self, config: Config) -> StoreClient:
log.info("store provider type: %s", config.store_type)

if config.store_type == 'queue':
if config.store_type == "file":
from .file import FileStore
return FileStore()
elif config.store_type == 'queue':
from .queue import QueueStore
return QueueStore(config)
else:
return influxdb_store_factory(config)
27 changes: 27 additions & 0 deletions klimalogger/store/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logging
import os
from datetime import datetime
from typing import List

from .client import StoreClient

log = logging.getLogger(__name__)


class FileStore(StoreClient):
target_folder = "/var/lib/klimalogger/data"

def store(self, data: List[dict]):
for entry in data:
timestamp = datetime.fromisoformat(entry["time"])
value = entry["fields"]["value"]
tags = entry["tags"]
measurement_type = tags["type"]
unit = tags["unit"]
sensor = tags["sensor"]
folder_name = f"{self.target_folder}/{timestamp:%Y}/{timestamp:%m}/"
file_name = f"{measurement_type}_{timestamp:%Y%m%d}.txt"

os.makedirs(folder_name, exist_ok=True)
with open(folder_name + '/' + file_name, 'a') as output_file:
output_file.write(f"{timestamp.isoformat()} {value} {unit} {sensor}\n")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "klimalogger"
version = "0.7.11"
version = "0.7.12"
authors = [
{ name = "Andreas Würl", email = "[email protected]" },
]
Expand Down

0 comments on commit 215ad22

Please sign in to comment.