-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c222307
commit 42d234d
Showing
16 changed files
with
324 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,6 @@ def __init__(self): | |
async def start(self): | ||
pass | ||
|
||
|
||
@abstractmethod | ||
async def update_kwh(self, kwh: float): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
|
||
from prometheus_client.registry import Collector as Metrics | ||
from estimenergy.const import METRICS | ||
from estimenergy.helpers import get_current_datetime | ||
|
||
from estimenergy.models.collector_data import CollectorData | ||
from estimenergy.common import metric_registry | ||
|
||
|
||
class CollectorMetrics(Metrics): | ||
def __init__(self, collector: CollectorData): | ||
self.collector = collector | ||
self.metrics = { | ||
metric: metric.create_gauge() | ||
metric: metric.create_gauge(registry=metric_registry) | ||
for metric in METRICS | ||
} | ||
|
||
async def collect(self): | ||
return self.metrics.values() | ||
|
||
async def update_metrics(self): | ||
data = await self.collector.get_metrics() | ||
date = get_current_datetime() | ||
data = await self.collector.get_metrics(date) | ||
for metric in METRICS: | ||
self.metrics[metric].labels(name=self.collector.name, id=self.collector.id).set(data[metric.json_key]) | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,82 @@ | ||
import prometheus_client | ||
import pytest | ||
import pytest | ||
from httpx import AsyncClient | ||
from tortoise import Tortoise | ||
from prometheus_client.parser import text_string_to_metric_families | ||
import estimenergy | ||
from estimenergy.collectors.glow_collector import GlowCollector | ||
|
||
from estimenergy.main import app | ||
from estimenergy.common import metric_registry | ||
from estimenergy.models.collector_data import CollectorData | ||
|
||
DB_URL = "sqlite://:memory:" | ||
|
||
|
||
async def init_db(db_url, create_db: bool = False, schemas: bool = False) -> None: | ||
"""Initial database connection""" | ||
await Tortoise.init( | ||
db_url=db_url, modules={"models": ["estimenergy.models"]}, _create_db=create_db | ||
) | ||
if create_db: | ||
print(f"Database created! {db_url = }") | ||
if schemas: | ||
await Tortoise.generate_schemas() | ||
print("Success to generate schemas") | ||
|
||
|
||
async def init(db_url: str = DB_URL): | ||
await init_db(db_url, True, True) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def anyio_backend(): | ||
return "asyncio" | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
async def client(): | ||
async with AsyncClient(app=app, base_url="http://test") as client: | ||
print("Client is ready") | ||
yield client | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
@pytest.fixture(scope="function", autouse=True) | ||
async def initialize_tests(): | ||
await init() | ||
collectors = list(metric_registry._collector_to_names.keys()) | ||
for collector in collectors: | ||
metric_registry.unregister(collector) | ||
await Tortoise.init( | ||
db_url=DB_URL, modules={"models": ["estimenergy.models"]}, _create_db=True | ||
) | ||
await Tortoise.generate_schemas() | ||
yield | ||
await Tortoise._drop_databases() | ||
|
||
# @pytest.fixture(scope="function", autouse=True) | ||
# def test_config(monkeypatch): | ||
# monkeypatch.setenv("DB_PATH", "test.db") | ||
# monkeypatch.setenv("CONFIG_PATH", "test.config.yml") | ||
@pytest.fixture(scope="function") | ||
async def create_collector_metrics(): | ||
async def create_collector_metrics(collector_data: CollectorData): | ||
from estimenergy.metrics import CollectorMetrics | ||
collector_metrics = CollectorMetrics(collector_data) | ||
await collector_metrics.update_metrics() | ||
return collector_metrics | ||
|
||
return create_collector_metrics | ||
|
||
@pytest.fixture(scope="function") | ||
async def get_metric_value(client: AsyncClient): | ||
async def get_metric_value(metric_name: str, collector_name: str): | ||
response = await client.get("/metrics") | ||
assert response.status_code == 200 | ||
|
||
families = list(text_string_to_metric_families(response.text)) | ||
for family in families: | ||
if family.name == metric_name: | ||
for sample in family.samples: | ||
if sample.labels["name"] == collector_name: | ||
return sample.value | ||
|
||
return None | ||
|
||
return get_metric_value | ||
|
||
@pytest.fixture(scope="function") | ||
async def collector_data(): | ||
collector_data = await CollectorData.create( | ||
name="glow_test", | ||
host="0.0.0.0", | ||
port=0, | ||
password="", | ||
cost_per_kwh=1, | ||
base_cost_per_month=1, | ||
payment_per_month=100, | ||
billing_month=1, | ||
min_accuracy=0 | ||
) | ||
await collector_data.save() | ||
return collector_data | ||
|
||
@pytest.fixture(scope="function") | ||
async def glow_collector(collector_data): | ||
return GlowCollector(collector_data) |
Empty file.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.