Filters¶
A set o basic functions designed to be used in the filters section for the purpose of define conditions to enable or disable application logic.
Generic Functions¶
-
class
krules_core.base_functions.filters.Filter¶ Evaluates a boolean expression and returns its value. The best way to exploit it is to use it in combination with Argument Processors.
rulesdata = [ { rulename: "filtered-rules", subscibre_to: "k8s.resource.add", ruledata: { filters: [ Filter( lambda payload:( "my-label" in payload["object"]["metadata"].get("labels", {}) ) ), ] processing: [ ... ] } } ]
-
execute(value)¶ - Parameters
value – Boolean expression which will be evaluated
-
Acting on Subject¶
-
class
krules_core.base_functions.filters.SubjectNameMatch¶ Return True if the subject’s name matches the given regular expression
rulesdata = [ { rulename: "on-user-login-do-something", subscibre_to: "user-login", ruledata: { filters: [ SubjectNameMatch(r"^user\|(?P<user_id>.+)", payload_dest="user_id"), ] processing: [ ... ] } } ]
-
execute(regex, payload_dest='subject_match')¶ - Parameters
regex – Regular expression which will be evaluated
payload_dest – Name of the key in the payload where the value of any groups contained in the expression will be saved saved
-
-
class
krules_core.base_functions.filters.SubjectNameDoesNotMatch¶ Return True if the subject’s name does not match the given regular expression
-
execute(regex, **kwargs)¶ - Parameters
regex – Regular expression which will be evaluated
-
-
class
krules_core.base_functions.filters.CheckSubjectProperty¶ Returns True if the given subject property exists and, if provided, match the given value.
rulesdata = [ { rulename: "on-device-status-new-update-do-something", subscibre_to: "device-status-updated", ruledata: { filters: [ CheckSubjectProperty( "last_update", property_value=lambda payload:( lambda value: value > payload.get("update_time") ) ), ] processing: [ ... ] } } ]
-
execute(property_name, property_value=<function CheckSubjectProperty.<lambda>>, extended=False, use_cache=True)¶ - Parameters
property_name – The name of the property
property_value (optional) –
Could be the expected property value or a callable which takes the actual property value as unique parameter. It is possible to exploit the Argument Processors to get the expected property value using nested lambda (as in exmaple).
extended – If True, check extended property [default False]
use_cache – If False it checks the actual value on the storage backend bypassing the cached value [default True]
-
-
class
krules_core.base_functions.filters.OnSubjectPropertyChanged¶ Specific function to filter subject-property-changed event. This event is produced whenever a subject property changes and its data contains the property name, the new property value and the old one.
☁️ cloudevents.Event Validation: valid Context Attributes, specversion: 1.0 type: subject-property-changed source: my-ruleset subject: foo id: bd198e83-9d7e-4e93-a9ae-aa21a40383c6 time: 2020-06-16T08:16:57.340692Z datacontenttype: application/json Extensions, knativearrivaltime: 2020-06-16T08:16:57.346535873Z originid: bd198e83-9d7e-4e93-a9ae-aa21a40383c6 propertyname: count traceparent: 00-d571530282362927f824bae826e1fa36-a52fceb915060653-00 Data, { "property_name": "count", "old_value": 0, "value": 1 }It is important to note that the property name is also present also in the event’s extensions and can therefore be used as a filter in a Trigger to intercept changes only of a specific property.
apiVersion: eventing.knative.dev/v1 kind: Trigger metadata: name: on-count-change-trigger spec: filter: attributes: propertyname: count type: subject-property-changed subscriber: ref: apiVersion: v1 kind: Service name: on-count-change-handler
This function allows you to exec your rule only when a specific property changes and, optionally, only for a specific current or previous value of it.
rulesdata = [ { rulename: "on-tick-update-count", subscibre_to: "tick", ruledata: { filters: [ SetSubjectProperty("count", lambda x: x + 1), # Since in this case the event will be handled within the ruleset itself, # it is not necessary to define any trigger to intercept it ] processing: [ ... ] } }, { rulename: "on-new-count-do-something", subscibre_to: "subject-property-changed", ruledata: { filters: [ SubjectPropertyChanged("count"), # Exec processing section each time property count changes ] processing: [ ... ] } }, { rulename: "on-even-count-do-something", subscibre_to: "subject-property-changed", ruledata: { filters: [ SubjectPropertyChanged( "count", value=lambda v: v%2 == 0 ), # Exec processing section each time a new even value is assigned to property count ] processing: [ ... ] } }, { rulename: "on-count-passed-from-zero-to-new-value-do-something", subscibre_to: "subject-property-changed", ruledata: { filters: [ SubjectPropertyChanged("count", old_value=0), # Exec processing section each time property count changes and # its old value is equal to 0 ] processing: [ ... ] } }, { rulename: "on-count-passed-from-even-value-to-new-value-greater-then-ten", subscibre_to: "subject-property-changed", ruledata: { filters: [ SubjectPropertyChanged( "count", value=lambda v, o: v > 10 && o%2 == 1 ), # Exec processing section each time a new value greater than 10 is assigned to property count and its last value was odd ] processing: [ ... ] } }, ]
-
execute(property_name, value=<function OnSubjectPropertyChanged.<lambda>>, old_value=<function OnSubjectPropertyChanged.<lambda>>)¶ - Parameters
property_name – Name of the changed property. Could be a string, a callable with no args which returns the property name or a boolean callable which accepts the property name as unique parameter
value –
Could be the expected value or a boolean callable which takes as arguments just the new property value or the new value and the previous one. It is possible to exploit the Argument Processors to get the expected property value.
old_value –
Could be the expected previous property value or a boolean callable which takes as arguments the previous property value. It is possible to exploit the Argument Processors to get the expected old property value.
-
Acting on Payload¶
-
class
krules_core.base_functions.filters.PayloadMatch¶ Processes the payload with a given jsonpath expression to check its content.
# event payload = { # "user": "admin", # "skills": [{"id": 1, "rating": 85}, {"id": 2, "value": 53}, {"id": 3, "value": 98}]} # } rulesdata = [ { rulename: "on-admin-skills-updated-with-wrong-filter", subscibre_to: "user-skills-updated", ruledata: { filters: [ PayloadMatch("$.user", "admin"), # return False because with single_match = False match result is always a list ] processing: [ ... ] } }, { rulename: "on-admin-skills-updated-with-correct-filter", subscibre_to: "user-skills-updated", ruledata: { filters: [ PayloadMatch("$.user", "admin", single_match=True), # return True ] processing: [ ... ] } }, { rulename: "on-user-skills-updated-update-qualified-skills", subscibre_to: "user-skills-updated", ruledata: { filters: [ PayloadMatch("$.data[?@.rating>80]", lambda x: len(x) == 2, payload_dest="qualified_skills") ] processing: [ ... ] } }, ]
-
execute(jp_expr, match_value=<function PayloadMatch.<lambda>>, payload_dest=None, single_match=False)¶ - Parameters
jp_expr – Jsonpath expression which will be used to process the payload.
match_value – It can be both the expected value of the jsonpath expression processing or a boolean function that handles the expression result.
payload_dest – Payload key in which match result will be stored. If None, no result will be saved. [default None]
single_match – If True produces a single value as result, a list of values otherwise [default False]
-
-
class
krules_core.base_functions.filters.PayloadMatchOne¶ Extends PayloadMatch
# event payload = { # "user": "admin", # "skills": [{"id": 1, "rating": 85}, {"id": 2, "value": 53}, {"id": 3, "value": 98}]} # } rulesdata = [ { rulename: "on-admin-skills-updated-do-something", subscibre_to: "skills-updated", ruledata: { filters: [ PayloadMatchOne("$.user", "admin"), # return True, it is the same as PayloadMatch("$.user", "admin", single_match=True) ] processing: [ ... ] } } ]
-
execute(jp_expr, match_value=<function PayloadMatchOne.<lambda>>, payload_dest=None, **kwargs)¶ - Parameters
jp_expr – Jsonpath expression
payload_dest – Destination key in payload
-
2021, Airspot s.r.l. Sede Legale:Via Ormea 33 10125 Torino, TO Italy C.F. e P. IVA: 12141910013.
|
Powered by