Skip to content

Commit 5b55135

Browse files
authored
feat: Allow filtering events v2 by time (#169)
1 parent 5e72ba9 commit 5b55135

File tree

3 files changed

+75
-35
lines changed

3 files changed

+75
-35
lines changed

poetry.lock

+22-29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdcclient/monitor/_events_v2.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from datetime import datetime
23

34
from sdcclient._common import _SdcCommon
45

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

11-
def get_events(self, name=None, category=None, direction='before', status=None, limit=100, pivot=None):
12+
def get_events(self, name=None, category=None, direction='before', status=None, limit=100, pivot=None, from_s=None,
13+
to_s=None):
1214
'''**Description**
1315
Returns the list of Sysdig Monitor events.
1416
@@ -19,6 +21,8 @@ def get_events(self, name=None, category=None, direction='before', status=None,
1921
- **status**: status of the event as list. Default: ['triggered', 'resolved', 'acknowledged', 'unacknowledged']
2022
- **limit**: max number of events to retrieve. Default: 100.
2123
- **pivot**: event id to use as pivot. Default: None.
24+
- **from_s**: the unix timestamp in milliseconds or datetime object for the beginning of the events. Default: None.
25+
- **to_s**: the unix timestamp in milliseconds or datetime object for the end of the events. Default: None.
2226
2327
**Success Return Value**
2428
A dictionary containing the list of events.
@@ -46,6 +50,18 @@ def get_events(self, name=None, category=None, direction='before', status=None,
4650
if direction not in ["before", "after"]:
4751
return False, "Invalid direction '{}', must be either 'before' or 'after'".format(direction)
4852

53+
if from_s is not None and isinstance(from_s, datetime):
54+
from_s = int(from_s.timestamp() * 1000)
55+
if to_s is not None and isinstance(to_s, datetime):
56+
to_s = int(to_s.timestamp() * 1000)
57+
58+
if to_s is None and from_s is not None or from_s is None and to_s is not None:
59+
return False, "only one of 'from_s' or 'to_s' has been specified, both are required when filtering by time"
60+
61+
if to_s is not None and from_s is not None:
62+
if int(to_s) < int(from_s):
63+
return False, "'from_s' must be lower than 'to_s'"
64+
4965
options = {
5066
'alertStatus': status,
5167
'category': ','.join(category),
@@ -56,6 +72,8 @@ def get_events(self, name=None, category=None, direction='before', status=None,
5672
'limit': str(limit),
5773
'pivot': pivot,
5874
'filter': name,
75+
'from': from_s,
76+
'to': to_s,
5977
}
6078
params = {k: v for k, v in options.items() if v is not None}
6179
res = self.http.get(self.url + '/api/v2/events/', headers=self.hdrs, params=params, verify=self.ssl_verify)
@@ -78,7 +96,7 @@ def delete_event(self, event):
7896
return [False, "Invalid event format"]
7997

8098
res = self.http.delete(self.url + '/api/v2/events/' + str(event['id']), headers=self.hdrs,
81-
verify=self.ssl_verify)
99+
verify=self.ssl_verify)
82100
if not self._checkResponse(res):
83101
return [False, self.lasterr]
84102
return [True, None]
@@ -112,5 +130,5 @@ def post_event(self, name, description=None, severity=None, event_filter=None, t
112130
'event': {k: v for k, v in options.items() if v is not None}
113131
}
114132
res = self.http.post(self.url + '/api/v2/events/', headers=self.hdrs, data=json.dumps(edata),
115-
verify=self.ssl_verify)
133+
verify=self.ssl_verify)
116134
return self._request_result(res)

specs/monitor/events_v2_spec.py

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import os
22
import time
3+
from datetime import datetime, timedelta
34

4-
from expects import expect, have_key, contain, have_keys, be_empty, equal, be_false
5-
from expects.matchers.built_in import have_len
6-
from mamba import it, before, description
5+
from expects import expect, have_key, contain, have_keys, be_empty, equal, be_false, be_above_or_equal, have_len
6+
from mamba import it, before, context, description
77

88
from sdcclient.monitor import EventsClientV2
99
from specs import be_successful_api_call
@@ -83,6 +83,35 @@
8383
expect((ok, res)).to(be_successful_api_call)
8484
expect(res).to(have_key("events", have_len(1)))
8585

86+
with it("is able to retrieve the events from the last day"):
87+
to_s = datetime.now()
88+
from_s = to_s - timedelta(weeks=2)
89+
ok, res = self.client.get_events(from_s=from_s, to_s=to_s)
90+
91+
expect((ok, res)).to(be_successful_api_call)
92+
expect(res).to(have_key("events", have_len(be_above_or_equal(1))))
93+
94+
with context("but the from and to parameters are incorrectly specified"):
95+
with it("returns an error if any of the parameters is specified but not the other"):
96+
t = datetime.now() - timedelta(weeks=2)
97+
ok1, res1 = self.client.get_events(from_s=t)
98+
ok2, res2 = self.client.get_events(to_s=t)
99+
100+
expect((ok1, res1)).not_to(be_successful_api_call)
101+
expect((ok2, res2)).not_to(be_successful_api_call)
102+
expect(res1).to(equal("only one of 'from_s' or 'to_s' has been specified, "
103+
"both are required when filtering by time"))
104+
expect(res2).to(equal("only one of 'from_s' or 'to_s' has been specified, "
105+
"both are required when filtering by time"))
106+
107+
with it("returns an error if they are specified in the wrong order"):
108+
to_s = datetime.now()
109+
from_s = to_s - timedelta(weeks=2)
110+
ok, res = self.client.get_events(from_s=to_s, to_s=from_s)
111+
112+
expect((ok, res)).not_to(be_successful_api_call)
113+
expect(res).to(equal("'from_s' must be lower than 'to_s'"))
114+
86115
with it("is able to remove the event from the feed"):
87116
time.sleep(3) # Wait for the event to appear in the feed
88117
_, res = self.client.get_events(category=["custom"])

0 commit comments

Comments
 (0)