-
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
Showing
1 changed file
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import Dict | ||
|
||
import aiohttp | ||
import pandas as pd | ||
|
||
from .client import CloogyClient | ||
|
||
|
||
class AsyncCloogyClient(CloogyClient): | ||
def __init__(self, token: str, session: aiohttp.ClientSession): | ||
super(AsyncCloogyClient, self).__init__() | ||
self.token = token | ||
self.session = session | ||
|
||
async def get_readings_dataframe(self, granularity, tags, start, end, instants_type=None, metric='Read', rename_tags=False) -> pd.DataFrame: | ||
if rename_tags is True: | ||
raise NotImplementedError('No async implementation for renaming tags yet') | ||
|
||
df = await self.get_consumptions_dataframe( | ||
granularity=granularity, | ||
tags=tags, | ||
start=start, | ||
end=end, | ||
instants_type=instants_type | ||
) | ||
|
||
df = self._format_readings_dataframe(df, metric=metric, rename_tags=rename_tags, tags=tags) | ||
|
||
return df | ||
|
||
async def get_consumptions_dataframe(self, granularity, tags, start, end, instants_type=None) -> pd.DataFrame: | ||
cons = await self.get_consumptions( | ||
granularity=granularity, | ||
tags=tags, | ||
start=int(start.timestamp() * 1000), | ||
end=int(end.timestamp() * 1000), | ||
instants_type=instants_type | ||
) | ||
df = self._parse_consumptions_to_dataframe(cons=cons) | ||
return df | ||
|
||
async def get_consumptions(self, granularity, tags, start, end, instants_type=None) -> Dict: | ||
arguments = self._get_consumptions_arguments( | ||
granularity=granularity, | ||
start=start, | ||
end=end, | ||
tags=tags, | ||
instants_type=instants_type | ||
) | ||
arguments['headers'].update(self._headers) | ||
async with self.session.get(**arguments) as r: | ||
r.raise_for_status() | ||
return await r.json() |