Calculator
This presentations goal it to introduce the features of the Calculator and how to configure it.
The challenge
I want calculate with field values.
from this:
[71]:
document = {
'message': {
"time_in_ms": 0.333
}
}
to this:
[72]:
expected = {
'message': {
"time_in_ns": 333000
}
}
Create rule and processor
create the rule:
[73]:
import sys
sys.path.append("../../../../../")
import tempfile
from pathlib import Path
rule_yaml = """---
filter: "message.time_in_ms"
calculator:
target_field: message.time_in_ns
calc: trunc(${message.time_in_ms} * 10e5)
delete_source_fields: true
"""
rule_path = Path(tempfile.gettempdir()) / "calculator"
rule_path.mkdir(exist_ok=True)
rule_file = rule_path / "calculation.yml"
rule_file.write_text(rule_yaml)
[73]:
153
create the processor config:
[74]:
processor_config = {
"mycalculator":{
"type": "calculator",
"rules": [str(rule_path), "/dev"],
}
}
create the processor with the factory:
[75]:
from unittest import mock
from logprep.factory import Factory
mock_logger = mock.MagicMock()
calculator = Factory.create(processor_config)
calculator
[75]:
calculator
Process event
[76]:
from copy import deepcopy
mydocument = deepcopy(document)
print(f"before: {mydocument}")
calculator.process(mydocument)
print(f"after: {mydocument}")
print(mydocument == expected)
before: {'message': {'time_in_ms': 0.333}}
after: {'message': {'time_in_ns': 333000}}
True