mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-10 07:22:37 -07:00
Make condition logic optional
* Implicit "and" between all conditions if logic is blank
This commit is contained in:
parent
0a1a691c73
commit
c67fa480a7
2 changed files with 37 additions and 33 deletions
|
@ -132,12 +132,9 @@
|
||||||
<div role="tabpanel" class="tab-pane" id="tabs-notify_conditions">
|
<div role="tabpanel" class="tab-pane" id="tabs-notify_conditions">
|
||||||
<label>Notification Conditions</label>
|
<label>Notification Conditions</label>
|
||||||
<p class="help-block">
|
<p class="help-block">
|
||||||
Add custom notification conditions.
|
Add custom conditions to filter out notifications.
|
||||||
<a href="#notify-text-sub-modal" data-toggle="modal">Click here</a> for a description of all the parameters.
|
<a href="#notify-text-sub-modal" data-toggle="modal">Click here</a> for a description of all the parameters.
|
||||||
</p>
|
</p>
|
||||||
<p class="help-block">
|
|
||||||
Note: Conditions are checked after the notification trigger and the notification will only be sent if the condition logic is satisfied.
|
|
||||||
</p>
|
|
||||||
<div id="condition-widget"></div>
|
<div id="condition-widget"></div>
|
||||||
<input type="hidden" name="custom_conditions" id="custom_conditions" />
|
<input type="hidden" name="custom_conditions" id="custom_conditions" />
|
||||||
|
|
||||||
|
@ -146,7 +143,8 @@
|
||||||
<input type="text" class="form-control" name="custom_conditions_logic" id="custom_conditions_logic" value="${notifier['custom_conditions_logic']}" required />
|
<input type="text" class="form-control" name="custom_conditions_logic" id="custom_conditions_logic" value="${notifier['custom_conditions_logic']}" required />
|
||||||
<div id="custom_conditions_logic_error" class="alert alert-danger" role="alert" style="padding-top: 5px; padding-bottom: 5px; margin: 0; display: none;"><i class="fa fa-exclamation-triangle" style="color: #a94442;"></i> <span></span></div>
|
<div id="custom_conditions_logic_error" class="alert alert-danger" role="alert" style="padding-top: 5px; padding-bottom: 5px; margin: 0; display: none;"><i class="fa fa-exclamation-triangle" style="color: #a94442;"></i> <span></span></div>
|
||||||
<p class="help-block">
|
<p class="help-block">
|
||||||
Enter the logic to use when evaluating the conditions (e.g. <span class="inline-pre">{1} and ({2} or {3})</span>).
|
Optional: Enter custom logic to use when evaluating the conditions (e.g. <span class="inline-pre">{1} and ({2} or {3})</span>).
|
||||||
|
Leave blank for implicit <span class="inline-pre">and</span> between all conditions.
|
||||||
</p>
|
</p>
|
||||||
<p class="help-block">
|
<p class="help-block">
|
||||||
Note: Only the keywords <span class="inline-pre">and</span>/<span class="inline-pre">or</span> and brackets <span class="inline-pre">()</span> are supported.
|
Note: Only the keywords <span class="inline-pre">and</span>/<span class="inline-pre">or</span> and brackets <span class="inline-pre">()</span> are supported.
|
||||||
|
|
|
@ -206,19 +206,21 @@ def notify_custom_conditions(notifier_id=None, parameters=None):
|
||||||
notifier_config = notifiers.get_notifier_config(notifier_id=notifier_id)
|
notifier_config = notifiers.get_notifier_config(notifier_id=notifier_id)
|
||||||
|
|
||||||
custom_conditions_logic = notifier_config['custom_conditions_logic']
|
custom_conditions_logic = notifier_config['custom_conditions_logic']
|
||||||
|
custom_conditions = json.loads(notifier_config['custom_conditions']) or []
|
||||||
|
|
||||||
if custom_conditions_logic:
|
if custom_conditions_logic or any(c for c in custom_conditions if c['value']):
|
||||||
logger.debug(u"Tautulli NotificationHandler :: Checking custom notification conditions for notifier_id %s." % notifier_id)
|
logger.debug(u"Tautulli NotificationHandler :: Checking custom notification conditions for notifier_id %s."
|
||||||
|
% notifier_id)
|
||||||
|
|
||||||
custom_conditions = json.loads(notifier_config['custom_conditions'])
|
logic_groups = None
|
||||||
|
if custom_conditions_logic:
|
||||||
try:
|
try:
|
||||||
# Parse and validate the custom conditions logic
|
# Parse and validate the custom conditions logic
|
||||||
logic_groups = helpers.parse_condition_logic_string(custom_conditions_logic, len(custom_conditions))
|
logic_groups = helpers.parse_condition_logic_string(custom_conditions_logic, len(custom_conditions))
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
logger.error(u"Tautulli NotificationHandler :: Unable to parse custom condition logic '%s': %s."
|
logger.error(u"Tautulli NotificationHandler :: Unable to parse custom condition logic '%s': %s."
|
||||||
% (custom_conditions_logic, e))
|
% (custom_conditions_logic, e))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
evaluated_conditions = [None] # Set condition {0} to None
|
evaluated_conditions = [None] # Set condition {0} to None
|
||||||
|
|
||||||
|
@ -227,10 +229,11 @@ def notify_custom_conditions(notifier_id=None, parameters=None):
|
||||||
operator = condition['operator']
|
operator = condition['operator']
|
||||||
values = condition['value']
|
values = condition['value']
|
||||||
parameter_type = condition['type']
|
parameter_type = condition['type']
|
||||||
|
parameter_value = parameters.get(parameter, "")
|
||||||
|
|
||||||
# Set blank conditions to None
|
# Set blank conditions to True (skip)
|
||||||
if not parameter or not operator or not values:
|
if not parameter or not operator or not values:
|
||||||
evaluated_conditions.append(None)
|
evaluated_conditions.append(True)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Make sure the condition values is in a list
|
# Make sure the condition values is in a list
|
||||||
|
@ -248,25 +251,25 @@ def notify_custom_conditions(notifier_id=None, parameters=None):
|
||||||
elif parameter_type == 'float':
|
elif parameter_type == 'float':
|
||||||
values = [float(v) for v in values]
|
values = [float(v) for v in values]
|
||||||
|
|
||||||
except Exception as e:
|
except ValueError as e:
|
||||||
logger.error(u"Tautulli NotificationHandler :: Unable to cast condition '%s' to type '%s'."
|
logger.error(u"Tautulli NotificationHandler :: Unable to cast condition '%s', values '%s', to type '%s'."
|
||||||
% (parameter, parameter_type))
|
% (parameter, values, parameter_type))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Cast the parameter value to the correct type
|
# Cast the parameter value to the correct type
|
||||||
try:
|
try:
|
||||||
if parameter_type == 'str':
|
if parameter_type == 'str':
|
||||||
parameter_value = unicode(parameters[parameter]).lower()
|
parameter_value = unicode(parameter_value).lower()
|
||||||
|
|
||||||
elif parameter_type == 'int':
|
elif parameter_type == 'int':
|
||||||
parameter_value = int(parameters[parameter])
|
parameter_value = int(parameter_value)
|
||||||
|
|
||||||
elif parameter_type == 'float':
|
elif parameter_type == 'float':
|
||||||
parameter_value = float(parameters[parameter])
|
parameter_value = float(parameter_value)
|
||||||
|
|
||||||
except Exception as e:
|
except ValueError as e:
|
||||||
logger.error(u"Tautulli NotificationHandler :: Unable to cast parameter '%s' to type '%s'."
|
logger.error(u"Tautulli NotificationHandler :: Unable to cast parameter '%s', value '%s', to type '%s'."
|
||||||
% (parameter, parameter_type))
|
% (parameter, parameter_value, parameter_type))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Check each condition
|
# Check each condition
|
||||||
|
@ -298,12 +301,15 @@ def notify_custom_conditions(notifier_id=None, parameters=None):
|
||||||
logger.warn(u"Tautulli NotificationHandler :: Invalid condition operator '%s'." % operator)
|
logger.warn(u"Tautulli NotificationHandler :: Invalid condition operator '%s'." % operator)
|
||||||
evaluated_conditions.append(None)
|
evaluated_conditions.append(None)
|
||||||
|
|
||||||
# Format and evaluate the logic string
|
if logic_groups:
|
||||||
try:
|
# Format and evaluate the logic string
|
||||||
evaluated_logic = helpers.eval_logic_groups_to_bool(logic_groups, evaluated_conditions)
|
try:
|
||||||
except Exception as e:
|
evaluated_logic = helpers.eval_logic_groups_to_bool(logic_groups, evaluated_conditions)
|
||||||
logger.error(u"Tautulli NotificationHandler :: Unable to evaluate custom condition logic: %s." % e)
|
except Exception as e:
|
||||||
return False
|
logger.error(u"Tautulli NotificationHandler :: Unable to evaluate custom condition logic: %s." % e)
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
evaluated_logic = all(evaluated_conditions[1:])
|
||||||
|
|
||||||
logger.debug(u"Tautulli NotificationHandler :: Custom condition evaluated to '%s'." % str(evaluated_logic))
|
logger.debug(u"Tautulli NotificationHandler :: Custom condition evaluated to '%s'." % str(evaluated_logic))
|
||||||
return evaluated_logic
|
return evaluated_logic
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue