Skip to content

SDK-23: check if developer added custom map method #622

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

Open
wants to merge 3 commits into
base: develop
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
12 changes: 8 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
python: [ 3.7, 3.9, 3.13]
include:
- python: "3.7"
os: ubuntu-22.04
- python: "3.9"
os: ubuntu-latest
- python: "3.13"
os: ubuntu-latest
splunk-version:
- "8.1"
- "8.2"
- "latest"
fail-fast: false
fail-fast: false

steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion splunklib/searchcommands/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def write_record(self, record):

def write_records(self, records):
self._ensure_validity()
records = list(records)
records = [] if records is NotImplemented else list(records)
write_record = self._write_record
for record in records:
write_record(record)
Expand Down
21 changes: 13 additions & 8 deletions splunklib/searchcommands/reporting_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,26 @@ def map(self, records):
"""
return NotImplemented

def prepare(self):

phase = self.phase
def _has_custom_method(self, method_name):
method = getattr(self.__class__, method_name, None)
base_method = getattr(ReportingCommand, method_name, None)
return callable(method) and (method is not base_method)

if phase == 'map':
# noinspection PyUnresolvedReferences
self._configuration = self.map.ConfigurationSettings(self)
def prepare(self):
if self.phase == 'map':
if self._has_custom_method('map'):
phase_method = getattr(self.__class__, 'map')
self._configuration = phase_method.ConfigurationSettings(self)
else:
self._configuration = self.ConfigurationSettings(self)
return

if phase == 'reduce':
if self.phase == 'reduce':
streaming_preop = chain((self.name, 'phase="map"', str(self._options)), self.fieldnames)
self._configuration.streaming_preop = ' '.join(streaming_preop)
return

raise RuntimeError(f'Unrecognized reporting command phase: {json_encode_string(str(phase))}')
raise RuntimeError(f'Unrecognized reporting command phase: {json_encode_string(str(self.phase))}')

def reduce(self, records):
""" Override this method to produce a reporting data structure.
Expand Down
39 changes: 39 additions & 0 deletions tests/searchcommands/test_reporting_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,42 @@ def reduce(self, records):
data = list(data_chunk.data)
assert len(data) == 1
assert int(data[0]['sum']) == sum(range(0, 10))


def test_simple_reporting_command_with_map():
@searchcommands.Configuration()
class MapAndReduceReportingCommand(searchcommands.ReportingCommand):
def map(self, records):
for record in records:
record["value"] = str(int(record["value"]) * 2)
yield record

def reduce(self, records):
total = 0
for record in records:
total += int(record["value"])
yield {"sum": total}

cmd = MapAndReduceReportingCommand()
ifile = io.BytesIO()

input_data = [{"value": str(i)} for i in range(5)]

mapped_data = list(cmd.map(input_data))

ifile.write(chunky.build_getinfo_chunk())
ifile.write(chunky.build_data_chunk(mapped_data))
ifile.seek(0)

ofile = io.BytesIO()
cmd._process_protocol_v2([], ifile, ofile)

ofile.seek(0)
chunk_stream = chunky.ChunkedDataStream(ofile)
chunk_stream.read_chunk()
data_chunk = chunk_stream.read_chunk()
assert data_chunk.meta['finished'] is True

result = list(data_chunk.data)
expected_sum = sum(i * 2 for i in range(5))
assert int(result[0]["sum"]) == expected_sum