Skip to content

Commit

Permalink
fig labeler tests processor
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrai2 committed Nov 8, 2024
1 parent 27cf5e7 commit a244347
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
43 changes: 10 additions & 33 deletions logprep/processor/labeler/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
from logprep.abc.processor import Processor
from logprep.processor.labeler.labeling_schema import LabelingSchema
from logprep.processor.labeler.rule import LabelerRule
from logprep.util.helper import add_field_to, get_dotted_field_value, add_and_overwrite
from logprep.util.helper import (
get_dotted_field_value,
add_batch_to_silent_fail,
)


class Labeler(Processor):
Expand Down Expand Up @@ -73,35 +76,9 @@ def setup(self):

def _apply_rules(self, event, rule):
"""Applies the rule to the current event"""
self._add_label_fields(event, rule)
self._add_label_values(event, rule)
self._convert_label_categories_to_sorted_list(event)

@staticmethod
def _add_label_fields(event: dict, rule: LabelerRule):
"""Prepares the event by adding empty label fields"""
add_field_to(event, "label", {})
for key in rule.label:
add_field_to(event, f"label.{key}", set())

@staticmethod
def _add_label_values(event: dict, rule: LabelerRule):
"""Adds the labels from the rule to the event"""
for key in rule.label:
label_key = f"label.{key}"
label = get_dotted_field_value(event, label_key)
if not isinstance(label, set):
label = set(label)
add_and_overwrite(event, label_key, label)
label.update(rule.label[key])

@staticmethod
def _convert_label_categories_to_sorted_list(event: dict):
label = get_dotted_field_value(event, "label")
if label is None:
return
for category in label:
category_key = f"label.{category}"
category_value = get_dotted_field_value(event, category_key)
sorted_category = sorted(list(category_value))
add_and_overwrite(event, category_key, sorted_category)
targets = [f"label.{key}" for key in rule.label.keys()]
contents = rule.label.values()
add_batch_to_silent_fail(event, targets, contents)
# convert sets into sorted lists
contents = [sorted(list(get_dotted_field_value(event, target))) for target in targets]
add_batch_to_silent_fail(event, targets, contents, overwrite_output_field=True)
15 changes: 15 additions & 0 deletions logprep/util/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ def add_batch_to(event, targets, contents, extends_lists=False, overwrite_output
raise FieldExistsWarning(event, unsuccessful_targets)


def add_batch_to_silent_fail(*args, **kwargs):
try:
add_batch_to(*args, **kwargs)
except FieldExistsWarning:
...


def add_field_to(
event,
target_field,
Expand Down Expand Up @@ -309,6 +316,14 @@ def add_and_overwrite(event, target_field, content, *_):
add_field_to(event, target_field, content, overwrite_output_field=True)


def add_and_overwrite_silent_fail(event, target_field, content, *_):
"""wrapper for add_field_to"""
try:
add_field_to(event, target_field, content, overwrite_output_field=True)
except FieldExistsWarning:
...


def append(event, target_field, content, separator):
"""appends to event"""
target_value = get_dotted_field_value(event, target_field)
Expand Down

0 comments on commit a244347

Please sign in to comment.