Skip to content

Commit bedcae9

Browse files
committed
fix(detector): add minimum floor as parameters
1 parent 365ce02 commit bedcae9

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ENV PIP_DEFAULT_TIMEOUT=100 \
2323

2424
# get poetry
2525
RUN apk update \
26-
&& apk add --update --no-cache curl==7.88.1-r0 gcc=12.2.1_git20220924-r4 linux-headers=5.19.5-r0 build-base=0.5-r3 \
26+
&& apk add --update --no-cache curl==7.88.1-r1 gcc=12.2.1_git20220924-r4 linux-headers=5.19.5-r0 build-base=0.5-r3 \
2727
&& curl -sSL https://install.python-poetry.org | python -
2828

2929
RUN python -m venv "$VENV_PATH"

controller/sentry/admin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def get_chart_data(self, sentry_id):
8383
return None
8484

8585
threshold = project.detection_param["threshold"]
86+
floor = project.detection_param.get("floor", 50)
8687

8788
options = settings.DEFAULT_GRAPH_OPTION
8889
labels, series, signal, avg_filter, std_filter = list(zip(*project.detection_result))
@@ -107,7 +108,8 @@ def get_chart_data(self, sentry_id):
107108
"backgroundColor": "#9966ff",
108109
"borderColor": "#9966ff",
109110
"data": [
110-
avg_filter + threshold * std_filter for avg_filter, std_filter in zip(avg_filter, std_filter)
111+
max(floor, avg_filter + threshold * std_filter)
112+
for avg_filter, std_filter in zip(avg_filter, std_filter)
111113
],
112114
"yAxisID": "series",
113115
},
@@ -127,9 +129,9 @@ def save_model(self, request: "HttpRequest", obj: Project, form: "ModelForm[Proj
127129
form (ModelForm[Project]): form
128130
change (bool): change
129131
"""
132+
super().save_model(request, obj, form, change)
130133
if change:
131134
perform_detect.delay(obj.sentry_id)
132-
return super().save_model(request, obj, form, change)
133135

134136

135137
@admin.register(Event)

controller/sentry/detector.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,19 @@ class SpikesDetector:
4747
so the threshold can react to structural breaks quickly.
4848
"""
4949

50-
def __init__(self, lag: int = 48, threshold: int = 5, influence: float = 0) -> None:
50+
def __init__(self, lag: int = 48, threshold: int = 5, influence: float = 0, floor: int = 50) -> None:
5151
"""Init detector.
5252
5353
Args:
5454
lag (int): The lag
5555
threshold (int): The threshold
5656
influence (float): The influence
57+
floor (int): The minimum floor
5758
"""
5859
self.lag = lag
5960
self.threshold = threshold
6061
self.influence = influence
62+
self.floor = floor
6163

6264
@classmethod
6365
def from_project(cls, project: Project) -> "SpikesDetector":
@@ -119,7 +121,8 @@ def compute(self, data) -> tuple[list[int], list[float], list[float]]:
119121
std_filter[self.lag - 1] = stdev(data[: self.lag])
120122

121123
for i, item in enumerate(data[self.lag :], start=self.lag):
122-
if abs(item - avg_filter[i - 1]) > self.threshold * std_filter[i - 1]:
124+
threshold = max(self.floor, avg_filter[i - 1] + self.threshold * std_filter[i - 1])
125+
if item > threshold:
123126
signals.append(1 if item > avg_filter[i - 1] else 0)
124127
filtered_data[i] = self.influence * item + (1 - self.influence) * filtered_data[i - 1]
125128
else:

controller/sentry/tests/test_admin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def test_project_chart(super_call: Mock, admin_with_user):
418418
project = Project(sentry_id="123")
419419
project.detection_result = [
420420
("a", 0, 0, 0, 0),
421-
("b", 5, 1, 1, 2),
421+
("b", 55, 1, 1, 2),
422422
]
423423
project.save()
424424
super_call.return_value = MockResponse(context_data=True)
@@ -433,7 +433,7 @@ def test_project_chart(super_call: Mock, admin_with_user):
433433
"label": "Series",
434434
"backgroundColor": "#36a2eb",
435435
"borderColor": "#36a2eb",
436-
"data": (0, 5),
436+
"data": (0, 55),
437437
"yAxisID": "series",
438438
},
439439
{
@@ -447,7 +447,7 @@ def test_project_chart(super_call: Mock, admin_with_user):
447447
"label": "Threshold",
448448
"backgroundColor": "#9966ff",
449449
"borderColor": "#9966ff",
450-
"data": [0, 11],
450+
"data": [50, 50],
451451
"yAxisID": "series",
452452
},
453453
],

controller/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@
328328
"lag": int(os.getenv("SPIKE_DETECTION_LAG", "48")),
329329
"threshold": int(os.getenv("SPIKE_DETECTION_THRESHOLD", "5")),
330330
"influence": float(os.getenv("SPIKE_DETECTION_INFLUENCE", "0.01")),
331+
"floor": int(os.getenv("SPIKE_DETECTION_FLOOR", "50")),
331332
}
332333

333334

0 commit comments

Comments
 (0)