-
Notifications
You must be signed in to change notification settings - Fork 379
Modified Streaming and Generating Custom Search Command #407
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e37c691
Changes added to preserve the custom field
vmalaviya-splunk ae817d9
Update README.md
vmalaviya-splunk 0875ba9
Update README.md
vmalaviya-splunk 35a8ff1
Update internals.py
vmalaviya-splunk 114f2e8
Update search_command.py
vmalaviya-splunk a622420
Update internals.py
vmalaviya-splunk c4995a6
Update internals.py
vmalaviya-splunk c6ec689
Merged fieldnames
vmalaviya-splunk b957136
Fixed: test failed due to fieldname merged
vmalaviya-splunk d20f194
Update internals.py
vmalaviya-splunk d695207
add gen_record() method for create a new record
akaila-splunk 0003f65
Update internals.py
vmalaviya-splunk c8f793d
Update search_command.py
akaila-splunk aa3bab7
added test case for generating CSC and updated README.md
akaila-splunk cc17181
updated search_command.py file
akaila-splunk 834f570
Update README.md
vmalaviya-splunk f847b41
Update README.md
vmalaviya-splunk 1ffab11
Update test_streaming_command.py
vmalaviya-splunk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,71 @@ def stream(self, records): | |
output = chunky.ChunkedDataStream(ofile) | ||
getinfo_response = output.read_chunk() | ||
assert getinfo_response.meta["type"] == "streaming" | ||
|
||
|
||
def test_field_preservation_negative(): | ||
@Configuration() | ||
class TestStreamingCommand(StreamingCommand): | ||
|
||
def stream(self, records): | ||
for index, record in enumerate(records): | ||
if index % 2 != 0: | ||
record["odd_field"] = True | ||
else: | ||
record["even_field"] = True | ||
yield record | ||
|
||
cmd = TestStreamingCommand() | ||
ifile = io.BytesIO() | ||
ifile.write(chunky.build_getinfo_chunk()) | ||
data = list() | ||
for i in range(0, 10): | ||
data.append({"in_index": str(i)}) | ||
ifile.write(chunky.build_data_chunk(data, finished=True)) | ||
ifile.seek(0) | ||
ofile = io.BytesIO() | ||
cmd._process_protocol_v2([], ifile, ofile) | ||
ofile.seek(0) | ||
output_iter = chunky.ChunkedDataStream(ofile).__iter__() | ||
output_iter.next() | ||
output_records = [i for i in output_iter.next().data] | ||
|
||
# Assert that count of records having "odd_field" is 0 | ||
assert len(list(filter(lambda r: "odd_field" in r, output_records))) == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really nice demonstration of the issue! |
||
|
||
# Assert that count of records having "even_field" is 10 | ||
assert len(list(filter(lambda r: "even_field" in r, output_records))) == 10 | ||
|
||
|
||
def test_field_preservation_positive(): | ||
@Configuration() | ||
class TestStreamingCommand(StreamingCommand): | ||
|
||
def stream(self, records): | ||
for index, record in enumerate(records): | ||
if index % 2 != 0: | ||
self.add_field(record, "odd_field", True) | ||
else: | ||
self.add_field(record, "even_field", True) | ||
yield record | ||
|
||
cmd = TestStreamingCommand() | ||
ifile = io.BytesIO() | ||
ifile.write(chunky.build_getinfo_chunk()) | ||
data = list() | ||
for i in range(0, 10): | ||
data.append({"in_index": str(i)}) | ||
ifile.write(chunky.build_data_chunk(data, finished=True)) | ||
ifile.seek(0) | ||
ofile = io.BytesIO() | ||
cmd._process_protocol_v2([], ifile, ofile) | ||
ofile.seek(0) | ||
output_iter = chunky.ChunkedDataStream(ofile).__iter__() | ||
output_iter.next() | ||
output_records = [i for i in output_iter.next().data] | ||
|
||
# Assert that count of records having "odd_field" is 10 | ||
assert len(list(filter(lambda r: "odd_field" in r, output_records))) == 10 | ||
|
||
# Assert that count of records having "even_field" is 10 | ||
assert len(list(filter(lambda r: "even_field" in r, output_records))) == 10 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should return current_record or else the documentation is incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @Bre77 - I think in this case the documentation needs to be updated, the
current_record
should be updated with the new field without the need to return since it's essentially pass-by-reference for this usage (I always go back to this article when I need to refresh my memory: https://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/).So if you all agree can we fix the README.md above to the following?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And to make sure that example we provide is working ^ that's probably a good implementation to add when adding tests for the StreamingCommand / add_field case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed my implementation to using pass by reference (as per the suggested doco update) and everything works as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @Bre77
I've updated the README accordingly.