Skip to content

feat: Allow filtering events v2 by time #169

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 1 commit into from
Nov 16, 2020
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
51 changes: 22 additions & 29 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions sdcclient/monitor/_events_v2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from datetime import datetime

from sdcclient._common import _SdcCommon

Expand All @@ -8,7 +9,8 @@ def __init__(self, token="", sdc_url='https://app.sysdigcloud.com', ssl_verify=T
super().__init__(token, sdc_url, ssl_verify, custom_headers)
self.product = "SDC"

def get_events(self, name=None, category=None, direction='before', status=None, limit=100, pivot=None):
def get_events(self, name=None, category=None, direction='before', status=None, limit=100, pivot=None, from_s=None,
to_s=None):
'''**Description**
Returns the list of Sysdig Monitor events.

Expand All @@ -19,6 +21,8 @@ def get_events(self, name=None, category=None, direction='before', status=None,
- **status**: status of the event as list. Default: ['triggered', 'resolved', 'acknowledged', 'unacknowledged']
- **limit**: max number of events to retrieve. Default: 100.
- **pivot**: event id to use as pivot. Default: None.
- **from_s**: the unix timestamp in milliseconds or datetime object for the beginning of the events. Default: None.
- **to_s**: the unix timestamp in milliseconds or datetime object for the end of the events. Default: None.

**Success Return Value**
A dictionary containing the list of events.
Expand Down Expand Up @@ -46,6 +50,18 @@ def get_events(self, name=None, category=None, direction='before', status=None,
if direction not in ["before", "after"]:
return False, "Invalid direction '{}', must be either 'before' or 'after'".format(direction)

if from_s is not None and isinstance(from_s, datetime):
from_s = int(from_s.timestamp() * 1000)
if to_s is not None and isinstance(to_s, datetime):
to_s = int(to_s.timestamp() * 1000)

if to_s is None and from_s is not None or from_s is None and to_s is not None:
return False, "only one of 'from_s' or 'to_s' has been specified, both are required when filtering by time"

if to_s is not None and from_s is not None:
if int(to_s) < int(from_s):
return False, "'from_s' must be lower than 'to_s'"

options = {
'alertStatus': status,
'category': ','.join(category),
Expand All @@ -56,6 +72,8 @@ def get_events(self, name=None, category=None, direction='before', status=None,
'limit': str(limit),
'pivot': pivot,
'filter': name,
'from': from_s,
'to': to_s,
}
params = {k: v for k, v in options.items() if v is not None}
res = self.http.get(self.url + '/api/v2/events/', headers=self.hdrs, params=params, verify=self.ssl_verify)
Expand All @@ -78,7 +96,7 @@ def delete_event(self, event):
return [False, "Invalid event format"]

res = self.http.delete(self.url + '/api/v2/events/' + str(event['id']), headers=self.hdrs,
verify=self.ssl_verify)
verify=self.ssl_verify)
if not self._checkResponse(res):
return [False, self.lasterr]
return [True, None]
Expand Down Expand Up @@ -112,5 +130,5 @@ def post_event(self, name, description=None, severity=None, event_filter=None, t
'event': {k: v for k, v in options.items() if v is not None}
}
res = self.http.post(self.url + '/api/v2/events/', headers=self.hdrs, data=json.dumps(edata),
verify=self.ssl_verify)
verify=self.ssl_verify)
return self._request_result(res)
35 changes: 32 additions & 3 deletions specs/monitor/events_v2_spec.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import time
from datetime import datetime, timedelta

from expects import expect, have_key, contain, have_keys, be_empty, equal, be_false
from expects.matchers.built_in import have_len
from mamba import it, before, description
from expects import expect, have_key, contain, have_keys, be_empty, equal, be_false, be_above_or_equal, have_len
from mamba import it, before, context, description

from sdcclient.monitor import EventsClientV2
from specs import be_successful_api_call
Expand Down Expand Up @@ -83,6 +83,35 @@
expect((ok, res)).to(be_successful_api_call)
expect(res).to(have_key("events", have_len(1)))

with it("is able to retrieve the events from the last day"):
to_s = datetime.now()
from_s = to_s - timedelta(weeks=2)
ok, res = self.client.get_events(from_s=from_s, to_s=to_s)

expect((ok, res)).to(be_successful_api_call)
expect(res).to(have_key("events", have_len(be_above_or_equal(1))))

with context("but the from and to parameters are incorrectly specified"):
with it("returns an error if any of the parameters is specified but not the other"):
t = datetime.now() - timedelta(weeks=2)
ok1, res1 = self.client.get_events(from_s=t)
ok2, res2 = self.client.get_events(to_s=t)

expect((ok1, res1)).not_to(be_successful_api_call)
expect((ok2, res2)).not_to(be_successful_api_call)
expect(res1).to(equal("only one of 'from_s' or 'to_s' has been specified, "
"both are required when filtering by time"))
expect(res2).to(equal("only one of 'from_s' or 'to_s' has been specified, "
"both are required when filtering by time"))

with it("returns an error if they are specified in the wrong order"):
to_s = datetime.now()
from_s = to_s - timedelta(weeks=2)
ok, res = self.client.get_events(from_s=to_s, to_s=from_s)

expect((ok, res)).not_to(be_successful_api_call)
expect(res).to(equal("'from_s' must be lower than 'to_s'"))

with it("is able to remove the event from the feed"):
time.sleep(3) # Wait for the event to appear in the feed
_, res = self.client.get_events(category=["custom"])
Expand Down