Skip to content

fix empty result filter for latest_version error when serach result is empty #160

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 2 commits into from
Apr 4, 2025
Merged
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
1 change: 1 addition & 0 deletions changelog/160.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix error in `~stixpy.net.client.StixQueryResponse.filter_for_latest_version` if the result table was empty.
38 changes: 24 additions & 14 deletions stixpy/net/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,30 @@

class StixQueryResponse(QueryResponse):
def filter_for_latest_version(self, allow_uncompleted=False):
self["tidx"] = range(len(self))
grouped_res = self.group_by(
["Start Time", "End Time", "Instrument", "Level", "DataType", "DataProduct", "Request ID"]
)
keep = np.zeros(len(self), dtype=bool)
for key, group in zip(grouped_res.groups.keys, grouped_res.groups):
group.sort("Ver")
if not allow_uncompleted:
incomplete = np.char.endswith(group["Ver"].data, "U")
keep[group[~incomplete][-1]["tidx"]] = True
else:
keep[group[-1]["tidx"]] = True
self.remove_column("tidx")
self.remove_rows(np.where(~keep))
r"""
Filter the response to only include the most recent versions of results.

Parameters
----------
allow_uncompleted
Include incomplete version (e.g. V02U)

"""
if len(self) > 0 and "Start Time" in self.columns:
self["_tidx"] = range(len(self))
grouped_res = self.group_by(
["Start Time", "End Time", "Instrument", "Level", "DataType", "DataProduct", "Request ID"]
)
keep = np.zeros(len(self), dtype=bool)
for key, group in zip(grouped_res.groups.keys, grouped_res.groups):
group.sort("Ver")
if not allow_uncompleted:
incomplete = np.char.endswith(group["Ver"].data, "U")
keep[group[~incomplete][-1]["_tidx"]] = True
else:
keep[group[-1]["_tidx"]] = True
self.remove_column("_tidx")
self.remove_rows(np.where(~keep))


class STIXClient(GenericClient):
Expand Down
6 changes: 6 additions & 0 deletions stixpy/net/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ def test_search_version_and(clientlocal):
assert len(res) == 5


def test_search_latest_version_empty(clientlocal):
res = clientlocal.search(a.Time("2023-01-01T00:00", "2023-01-01T23:59"), a.Instrument.stix)
res.filter_for_latest_version()
assert len(res) == 0


def test_search_latest_version(clientlocal):
res = clientlocal.search(a.Time("2022-01-01T00:00", "2022-01-01T23:59"), a.Instrument.stix)
res.filter_for_latest_version(allow_uncompleted=True)
Expand Down
Loading