Skip to content

Commit

Permalink
DB_ADDON:
Browse files Browse the repository at this point in the history
- Rework dbaddon_functions using parameters for zaehlerstand, verbrauch, wertehistorie and tagesmittel to get more flexibility
  • Loading branch information
sisamiwe committed Jul 11, 2024
1 parent fb7bac3 commit e42e21f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 106 deletions.
52 changes: 36 additions & 16 deletions db_addon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,37 @@ def get_query_parameters_from_db_addon_params() -> Union[dict, None]:
# reduce db_addon_params from item to (optional_params + mandatory_params)
new_db_addon_params = {k: v for k, v in db_addon_params.items() if k in (optional_params + mandatory_params)} if db_addon_params else {}

# check if parameter values are valid
if 'func' in new_db_addon_params:
if (db_addon_fct_cat == 'wertehistorie' and new_db_addon_params['func'] not in ALLOWED_MINMAX_FUNCS) or (db_addon_fct_cat == 'complex' and new_db_addon_params['func'] not in ALLOWED_QUERY_FUNCS):
self.logger.warning(f"Parameter 'func'={new_db_addon_params['timeframe']} of item '{item.property.path}' not valid. Item will be ignored.")
return None

if 'timeframe' in new_db_addon_params and new_db_addon_params['timeframe'] not in ALLOWED_QUERY_TIMEFRAMES:
self.logger.warning(f"Parameter 'timeframe'={new_db_addon_params['timeframe']} of item '{item.property.path}' not valid. Valid parameter values: {ALLOWED_QUERY_TIMEFRAMES}. Item will be ignored.")
return None

for param in ['start', 'end', 'threshold']:
if param in new_db_addon_params:
new_value = to_int(new_db_addon_params[param])
if new_value is None:
self.logger.warning(f"Parameter '{param}'={new_db_addon_params[param]} of item '{item.property.path}' not valid. Parameter value need to be integer. Item will be ignored.")
return None
else:
new_db_addon_params[param] = new_value

if 'year' in new_db_addon_params and not self._valid_year(new_db_addon_params['year']):
self.logger.warning(f"Parameter 'year'={new_db_addon_params['year']} of item '{item.property.path}' not valid. Item will be ignored.")
return None

if 'month' in new_db_addon_params and not self._valid_month(new_db_addon_params['month']):
self.logger.warning(f"Parameter 'month'={new_db_addon_params['month']} of item '{item.property.path}' not valid. Item will be ignored.")
return None

if 'result' in new_db_addon_params and new_db_addon_params['result'] not in ALLOWED_RESULT_TYPES:
self.logger.warning(f"Parameter 'result'={new_db_addon_params['result']} of item '{item.property.path}' not valid. Valid parameter values: {ALLOWED_RESULT_TYPES}. Item will be ignored.")
return None

# add additional params
if additional_params:
new_db_addon_params.update(additional_params)
Expand Down Expand Up @@ -1067,6 +1098,9 @@ def handle_ondemand(self, item: Item) -> None:
if isinstance(_result, list):
result = _result[0][1]

elif db_addon_fct_sub_cat == 'complex':
result = self._prepare_value_list(**params)

# handle all items of category 'summe'
elif db_addon_fct_cat == 'summe':
new_params = {}
Expand All @@ -1075,21 +1109,6 @@ def handle_ondemand(self, item: Item) -> None:
new_params.update({entry: params[entry]})
result = self._handle_temp_sums(func=db_addon_fct, database_item=database_item, year=params.get('year'), month=params.get('month'), ignore_value_list=params.get('ignore_value_list'), params=new_params)

# handle all items of category 'complex'
elif db_addon_fct_cat == 'complex':

# handle 'tagesmitteltemperatur'
if db_addon_fct == 'tagesmitteltemperatur':
result = self._prepare_value_list(**params)

# handle 'verbrauch'
elif db_addon_fct == 'verbrauch':
result = self._handle_verbrauch(params)

# handle 'zaehlerstand'
elif db_addon_fct == 'zaehlerstand':
result = self._handle_zaehlerstand(params)

# handle info functions
elif db_addon_fct == 'info_db_version':
result = self._get_db_version()
Expand Down Expand Up @@ -2011,7 +2030,6 @@ def vegetationstage() -> int:
"""provide number day counted as Frosttag with Tavg > 5°C"""
return _count(operator.ge, 'avg', 5)

# ToDo: check new coding
def _count(op, minmax: str, limit: int) -> int:
minmax_index = 2 if minmax == 'max' else 1
return sum(1 for entry in raw_data if op(entry[minmax_index], limit))
Expand Down Expand Up @@ -3141,3 +3159,5 @@ def split_sting_letters_numbers(string) -> list:

ALLOWED_QUERY_TIMEFRAMES = ['year', 'month', 'week', 'day', 'hour']
ALLOWED_MINMAX_FUNCS = ['min', 'max', 'avg']
ALLOWED_QUERY_FUNCS = ['avg', 'avg1', 'min', 'max', 'max1', 'sum', 'on', 'integrate', 'sum_max', 'sum_avg', 'sum_min_neg', 'diff_max', 'next', 'raw', 'first', 'last']
ALLOWED_RESULT_TYPES = ['total', 'month', 'day']
75 changes: 0 additions & 75 deletions db_addon/item_attributes.py

This file was deleted.

Loading

0 comments on commit e42e21f

Please sign in to comment.