Skip to content

Commit

Permalink
Refactor historical/graph page
Browse files Browse the repository at this point in the history
  • Loading branch information
Buried-In-Code committed Dec 22, 2023
1 parent 6c3424c commit f015eb2
Show file tree
Hide file tree
Showing 30 changed files with 883 additions and 701 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
rev: v0.1.9
hooks:
- id: ruff
- repo: https://github.com/executablebooks/mdformat
Expand Down Expand Up @@ -39,7 +39,7 @@ repos:
args:
- --markdown-linebreak-ext=md
- repo: https://github.com/psf/black
rev: 23.11.0
rev: 23.12.0
hooks:
- id: black
- repo: https://github.com/pappasam/toml-sort
Expand Down
2 changes: 1 addition & 1 deletion freyr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from freyr.console import CONSOLE

__version__ = "0.3.6"
__version__ = "0.4.0"


def get_cache_root() -> Path:
Expand Down
1 change: 0 additions & 1 deletion freyr/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,4 @@ async def validation_exception_handler(
"status": f"{status.value}: {status.phrase}",
"details": details,
},
headers=exc.headers,
)
4 changes: 2 additions & 2 deletions freyr/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from functools import cached_property
from typing import Self

from freyr.models import ReadingModel
from freyr.models.reading import Reading
from freyr.settings import Settings


class Constants:
@cached_property
def cache(self: Self) -> dict[str, ReadingModel]:
def cache(self: Self) -> dict[int, Reading]:
return {}

@cached_property
Expand Down
2 changes: 1 addition & 1 deletion freyr/database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ["sqlite_filepath"]
__all__ = []


from freyr import get_data_root
Expand Down
35 changes: 31 additions & 4 deletions freyr/database/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,65 @@

from pony.orm import Database, PrimaryKey, Required, Set, composite_key

from freyr.models import DeviceModel, ReadingModel
from freyr.models.device import Device as DeviceModel, DeviceEntry
from freyr.models.reading import Reading as ReadingModel, ReadingEntry

db = Database()


class Device(db.Entity):
_table_ = "devices"

device_id: int = PrimaryKey(int, auto=True)
id: int = PrimaryKey(int, auto=True) # noqa: A003
name: str = Required(str, unique=True)
readings: list["Reading"] = Set("Reading")

def to_entry_model(self: Self) -> DeviceEntry:
return DeviceEntry(
id=self.id,
name=self.name,
)

def to_model(self: Self) -> DeviceModel:
return DeviceModel(
id=self.id,
name=self.name,
readings=sorted({x.to_model() for x in self.readings}, reverse=True),
readings=sorted(
{
DeviceModel.Reading(
id=x.id,
timestamp=x.timestamp,
temperature=x.temperature,
humidity=x.humidity,
)
for x in self.readings
},
),
)


class Reading(db.Entity):
_table_ = "readings"

reading_id: int = PrimaryKey(int, auto=True)
id: int = PrimaryKey(int, auto=True) # noqa: A003
device: Device = Required(Device)
timestamp: datetime = Required(datetime)
temperature: Decimal = Required(Decimal)
humidity: Decimal = Required(Decimal)

composite_key(device, timestamp)

def to_entry_model(self: Self) -> ReadingEntry:
return ReadingEntry(
id=self.id,
timestamp=self.timestamp,
temperature=self.temperature,
humidity=self.humidity,
)

def to_model(self: Self) -> ReadingModel:
return ReadingModel(
id=self.id,
timestamp=self.timestamp,
temperature=self.temperature,
humidity=self.humidity,
Expand Down
70 changes: 0 additions & 70 deletions freyr/models.py

This file was deleted.

31 changes: 31 additions & 0 deletions freyr/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
__all__ = ["Summary"]

from datetime import datetime
from decimal import Decimal
from typing import Self

from pydantic import BaseModel, Field


class Summary(BaseModel):
class Reading(BaseModel):
timestamp: datetime
temperature: Decimal
humidity: Decimal

def __lt__(self: Self, other) -> int: # noqa: ANN001
if not isinstance(other, Summary.Reading):
raise NotImplementedError
return self.timestamp < other.timestamp

def __eq__(self: Self, other) -> bool: # noqa: ANN001
if not isinstance(other, Summary.Reading):
raise NotImplementedError
return self.timestamp == other.timestamp

def __hash__(self: Self) -> int:
return hash((type(self), self.timestamp))

highs: list[Reading] = Field(default_factory=list)
averages: list[Reading] = Field(default_factory=list)
lows: list[Reading] = Field(default_factory=list)
56 changes: 56 additions & 0 deletions freyr/models/device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
__all__ = ["Device", "DeviceEntry", "DeviceInput"]

from datetime import datetime
from decimal import Decimal
from typing import Self

from pydantic import BaseModel, Field


class BaseDevice(BaseModel):
name: str

def __lt__(self: Self, other) -> int: # noqa: ANN001
if not isinstance(other, BaseDevice):
raise NotImplementedError
return self.name.casefold() < other.name.casefold()

def __eq__(self: Self, other) -> bool: # noqa: ANN001
if not isinstance(other, BaseDevice):
raise NotImplementedError
return self.name.casefold() == other.name.casefold()

def __hash__(self: Self) -> int:
return hash((type(self), self.name.casefold()))


class Device(BaseDevice):
class Reading(BaseModel):
id: int # noqa: A003
timestamp: datetime
temperature: Decimal
humidity: Decimal

def __lt__(self: Self, other) -> int: # noqa: ANN001
if not isinstance(other, Device.Reading):
raise NotImplementedError
return self.timestamp < other.timestamp

def __eq__(self: Self, other) -> bool: # noqa: ANN001
if not isinstance(other, Device.Reading):
raise NotImplementedError
return self.timestamp == other.timestamp

def __hash__(self: Self) -> int:
return hash((type(self), self.timestamp))

id: int # noqa: A003
readings: list[Reading] = Field(default_factory=list)


class DeviceEntry(BaseDevice):
id: int # noqa: A003


class DeviceInput(BaseDevice):
pass
52 changes: 52 additions & 0 deletions freyr/models/reading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
__all__ = ["Reading", "ReadingEntry", "ReadingInput"]

from datetime import datetime
from decimal import Decimal
from typing import Self

from pydantic import BaseModel


class BaseReading(BaseModel):
temperature: Decimal
humidity: Decimal


class Reading(BaseReading):
id: int # noqa: A003
timestamp: datetime

def __lt__(self: Self, other) -> int: # noqa: ANN001
if not isinstance(other, Reading):
raise NotImplementedError
return self.timestamp < other.timestamp

def __eq__(self: Self, other) -> bool: # noqa: ANN001
if not isinstance(other, Reading):
raise NotImplementedError
return self.timestamp == other.timestamp

def __hash__(self: Self) -> int:
return hash((type(self), self.timestamp))


class ReadingEntry(BaseReading):
id: int # noqa: A003
timestamp: datetime

def __lt__(self: Self, other) -> int: # noqa: ANN001
if not isinstance(other, ReadingEntry):
raise NotImplementedError
return self.timestamp < other.timestamp

def __eq__(self: Self, other) -> bool: # noqa: ANN001
if not isinstance(other, ReadingEntry):
raise NotImplementedError
return self.timestamp == other.timestamp

def __hash__(self: Self) -> int:
return hash((type(self), self.timestamp))


class ReadingInput(BaseReading):
pass
Loading

0 comments on commit f015eb2

Please sign in to comment.