Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten command field types for the jsonpacker #130

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions flow/record/adapter/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
api_key=api_key,
)

self.json_packer = JsonRecordPacker()
self.json_packer = JsonRecordPacker(pack_descriptors=False)
self.queue: queue.Queue[Union[Record, StopIteration]] = queue.Queue()
self.event = threading.Event()
self.thread = threading.Thread(target=self.streaming_bulk_thread)
Expand All @@ -78,7 +78,7 @@ def __init__(

def record_to_document(self, record: Record, index: str) -> dict:
"""Convert a record to a Elasticsearch compatible document dictionary"""
rdict = record._asdict()
rdict = self.json_packer.pack_obj(record)

# Store record metadata under `_record_metadata`.
rdict_meta = {
Expand Down
4 changes: 4 additions & 0 deletions flow/record/jsonpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def pack_obj(self, obj):
elif field_type == "boolean" and isinstance(serial[field_name], int):
serial[field_name] = bool(serial[field_name])

# Flatten command type
elif field_type == "command" and isinstance(serial[field_name], fieldtypes.command):
serial[field_name] = serial[field_name]._join()

return serial
if isinstance(obj, RecordDescriptor):
serial = {
Expand Down
45 changes: 39 additions & 6 deletions tests/test_elastic_adapter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json

import pytest
Expand All @@ -10,18 +12,50 @@
[
("string", "field_one"),
("string", "field_two"),
("string", "field_three"),
],
)

AnotherRecord = RecordDescriptor(
"my/record",
[
("command", "field_one"),
("boolean", "field_two"),
("bytes", "field_three"),
],
)


@pytest.mark.parametrize(
"record",
"record, expected_output",
[
MyRecord("first", "record"),
MyRecord("second", "record"),
(
MyRecord("first", "record", "!"),
{
"field_one": "first",
"field_two": "record",
"field_three": "!",
},
),
(
MyRecord("second", "record", "!"),
{
"field_one": "second",
"field_two": "record",
"field_three": "!",
},
),
(
AnotherRecord("/bin/bash -c 'echo hello'", False, b"\x01\x02\x03\x04"),
{
"field_one": "/bin/bash -c 'echo hello'",
"field_two": False,
"field_three": "AQIDBA==",
},
),
],
)
def test_elastic_writer_metadata(record):
def test_elastic_writer_metadata(record: MyRecord | AnotherRecord, expected_output: dict) -> None:
options = {
"_meta_foo": "some value",
"_meta_bar": "another value",
Expand All @@ -34,8 +68,7 @@ def test_elastic_writer_metadata(record):
"_index": "some-index",
"_source": json.dumps(
{
"field_one": record.field_one,
"field_two": record.field_two,
**expected_output,
"_record_metadata": {
"descriptor": {
"name": "my/record",
Expand Down
17 changes: 17 additions & 0 deletions tests/test_json_packer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,20 @@ def test_record_pack_bool_regression() -> None:

# pack the json string back to a record and make sure it is the same as before
assert packer.unpack(data) == record


def test_record_pack_command_type() -> None:
TestRecord = RecordDescriptor(
"test/record_with_commands",
[
("command", "win_command"),
("command", "nix_command"),
],
)

record = TestRecord(win_command="foo.exe /H /E /L /O", nix_command="/bin/bash -c 'echo hello'")
packer = JsonRecordPacker()
data = packer.pack(record)

assert data.startswith('{"win_command": "foo.exe /H /E /L /O", "nix_command": "/bin/bash -c \'echo hello\'", ')
assert packer.unpack(data) == record
Loading