Skip to content

Commit

Permalink
Merge pull request #27 from EuleMitKeule/develop
Browse files Browse the repository at this point in the history
fix: influx and prometheus data services
  • Loading branch information
EuleMitKeule authored Apr 30, 2023
2 parents 794160f + af45253 commit fc36c95
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 106 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ poetry.lock
*.db-journal
*.db
*.db-shm
*.db-wal
*.db-wal
appdata
75 changes: 33 additions & 42 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,47 +59,38 @@
"instanceLimit": 1,
},
},
// {
// "label": "Format",
// "type": "shell",
// "command": "poetry run format",
// "detail": "Run code formatting tools.",
// "isBackground": true,
// "presentation": {
// "echo": false,
// "reveal": "silent",
// "focus": true,
// "panel": "dedicated",
// "showReuseMessage": true,
// "clear": true,
// "close": false
// },
// "problemMatcher": [],
// "promptOnClose": false,
// "runOptions": {
// "instanceLimit": 1,
// },
// },
// {
// "label": "Security",
// "type": "shell",
// "command": "poetry run security",
// "detail": "Run security scanning tools.",
// "isBackground": true,
// "presentation": {
// "echo": false,
// "reveal": "silent",
// "focus": true,
// "panel": "dedicated",
// "showReuseMessage": true,
// "clear": true,
// "close": false
// },
// "problemMatcher": [],
// "promptOnClose": false,
// "runOptions": {
// "instanceLimit": 1,
// },
// }
{
"label": "Build Docker",
"detail": "Build Docker Image.",
"type": "shell",
"command": "docker-compose -f docker-compose.dev.yml build",
"problemMatcher": [],
"promptOnClose": false,
"runOptions": {
"instanceLimit": 1,
},
},
{
"label": "Deploy Stack",
"detail": "Deploy Stack with docker-compose.",
"type": "shell",
"command": "docker-compose -f docker-compose.dev.yml up",
"problemMatcher": [],
"promptOnClose": false,
"runOptions": {
"instanceLimit": 1,
},
},
{
"label": "Deploy Data Services",
"detail": "Deploy Data Services with docker-compose.",
"type": "shell",
"command": "docker-compose -f docker-compose.dev.yml up estimenergy-postgresql estimenergy-influxdb estimenergy-prometheus --remove-orphans",
"problemMatcher": [],
"promptOnClose": false,
"runOptions": {
"instanceLimit": 1,
},
}
]
}
72 changes: 72 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
services:
estimenergy:
build:
context: .
dockerfile: Dockerfile
container_name: estimenergy
restart: unless-stopped
networks:
- estimenergy
ports:
- 12321:12321
volumes:
- ./appdata/config:/config
- /etc/localtime:/etc/localtime:ro

estimenergy-postgresql:
image: postgres:15.2-alpine
container_name: estimenergy-postgresql
restart: unless-stopped
networks:
- estimenergy
ports:
- 5432:5432
user: 0:0
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_INITDB_ARGS=--encoding=UTF8
volumes:
- ./appdata/postgresql:/var/lib/postgresql/data
- /etc/localtime:/etc/localtime:ro

estimenergy-influxdb:
image: influxdb:2.7.0-alpine
container_name: estimenergy-influxdb
restart: unless-stopped
networks:
- estimenergy
ports:
- 8086:8086
environment:
- DOCKER_INFLUXDB_INIT_USERNAME=${INFLUXDB_USERNAME}
- DOCKER_INFLUXDB_INIT_PASSWORD=${INFLUXDB_PASSWORD}
- DOCKER_INFLUXDB_INIT_ORG=${INFLUXDB_ORG}
- DOCKER_INFLUXDB_INIT_BUCKET=${INFLUXDB_BUCKET}
- DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=${INFLUXDB_ADMIN_TOKEN}
- DOCKER_INFLUXDB_REPORTING_DISABLED=true
- DOCKER_INFLUXDB_INIT_MODE=setup
- DOCKER_INFLUXDB_HTTP_AUTH_ENABLED=true
volumes:
- ./appdata/influxdb:/var/lib/influxdb2
- /etc/localtime:/etc/localtime:ro

estimenergy-prometheus:
image: prom/prometheus:v2.43.0
container_name: estimenergy-prometheus
restart: unless-stopped
user: 0:0
networks:
- estimenergy
command:
- --storage.tsdb.retention.time=5y
- --storage.tsdb.retention.size=1TB
volumes:
- ./appdata/prometheus:/prometheus
- /etc/localtime:/etc/localtime:ro

networks:
estimenergy:
driver: bridge
name: estimenergy
8 changes: 4 additions & 4 deletions estimenergy/devices/base_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def increment(
self,
metric: Metric,
value: float,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
):
"""Increment a metric in the database."""

Expand All @@ -64,7 +64,7 @@ async def decrement(
self,
metric: Metric,
value: float,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
):
"""Decrement a metric in the database."""

Expand All @@ -78,7 +78,7 @@ async def write(
self,
metric: Metric,
value: float,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
):
"""Write a metric to the database."""

Expand All @@ -90,7 +90,7 @@ async def write(

async def update(
self,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
):
"""Calculate metrics based on other metrics."""

Expand Down
22 changes: 14 additions & 8 deletions estimenergy/devices/glow_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ async def on_connect():

await self.api.subscribe_states(self.__state_changed)

async def on_connect_error():
async def on_connect_error(error: Exception):
_ = error
with Session(db_engine) as session:
self.device_config.is_connected = False
session.add(self.device_config)
Expand Down Expand Up @@ -152,20 +153,22 @@ def __state_changed(self, state: SensorState):
loop.create_task(self.__on_total_kwh_changed(state.state))

async def __on_total_kwh_changed(self, value: float):
current_timezone = datetime.datetime.now().astimezone().tzinfo

if self.last_kwh is None or self.last_time is None:
self.last_kwh = value
self.last_time = datetime.datetime.now()
self.last_time = datetime.datetime.now(tz=current_timezone)
return

if value < self.last_kwh:
self.last_kwh = value
logger.warning("Detected a reset of the total kWh counter.")
return

time = datetime.datetime.now()
value_dt = datetime.datetime.now(tz=current_timezone)

kwh_increase = value - self.last_kwh
time_increase_us = (time - self.last_time).microseconds
time_increase_us = (value_dt - self.last_time).microseconds
us_per_day = 1000 * 1000 * 60 * 60 * 24
accuracy_increase = time_increase_us / us_per_day

Expand All @@ -176,19 +179,22 @@ async def __on_total_kwh_changed(self, value: float):
await self.increment(
Metric(MetricType.ENERGY, MetricPeriod.DAY, False, False),
kwh_increase,
time,
value_dt,
)
await self.increment(
Metric(MetricType.ACCURACY, MetricPeriod.DAY, False, False),
accuracy_increase,
time,
value_dt,
)

await self.update()
await self.update(value_dt)

self.last_kwh = value

async def __on_power_changed(self, value: float):
current_timezone = datetime.datetime.now().astimezone().tzinfo
value_dt = datetime.datetime.now(tz=current_timezone)

await self.write(
Metric(MetricType.POWER, MetricPeriod.TOTAL, False, False), value
Metric(MetricType.POWER, MetricPeriod.TOTAL, False, False), value, value_dt
)
4 changes: 2 additions & 2 deletions estimenergy/influx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from estimenergy.config import config

if config.influx_config:
influx_client = InfluxDBClient(
influx_client: InfluxDBClient = InfluxDBClient(
url=config.influx_config.url,
token=config.influx_config.token.get_secret_value(),
token=config.influx_config.token,
org=config.influx_config.org,
)
else:
Expand Down
2 changes: 1 addition & 1 deletion estimenergy/models/config/influx_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

class InfluxConfig(BaseModel):
url: str
token: SecretStr
token: str
org: str
bucket: str
14 changes: 7 additions & 7 deletions estimenergy/services/data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, device_config: DeviceConfig, config: Config):
async def last(
self,
metric: Metric,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
) -> float:
"""Return the last value for a metric."""

Expand All @@ -33,7 +33,7 @@ async def write(
self,
metric: Metric,
value: float,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
):
"""Write a metric to the database."""

Expand All @@ -46,7 +46,7 @@ async def increment(
self,
metric: Metric,
value: float,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
):
"""Increment a metric in the database."""

Expand All @@ -61,7 +61,7 @@ async def decrement(
self,
metric: Metric,
value: float,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
):
"""Decrement a metric in the database."""

Expand All @@ -81,7 +81,7 @@ def supported_metrics(self) -> list[Metric]:
async def _last(
self,
metric: Metric,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
) -> float:
"""Get the last value for a metric."""

Expand All @@ -90,13 +90,13 @@ async def _write(
self,
metric: Metric,
value: float,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
):
"""Write a metric to the database."""

@abstractmethod
async def update(
self,
value_dt: datetime.datetime = datetime.datetime.now(),
value_dt: datetime.datetime,
):
"""Update metrics."""
Loading

0 comments on commit fc36c95

Please sign in to comment.