Skip to content

Commit 5044154

Browse files
committed
fix(detector): change dump data structure
1 parent 5701592 commit 5044154

File tree

6 files changed

+24
-32
lines changed

6 files changed

+24
-32
lines changed

commitlint.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const scope = [
88
"metrics",
99
"panic",
1010
"sentry-api",
11-
"graph"
11+
"graph",
12+
"detector"
1213
];
1314

1415
module.exports = {

controller/sentry/admin.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,37 +83,34 @@ def get_chart_data(self, sentry_id):
8383
threshold = project.detection_param["threshold"]
8484

8585
options = settings.DEFAULT_GRAPH_OPTION
86+
labels, series, signal, avg_filter, std_filter = list(zip(*project.detection_result))
8687
data = {
8788
"datasets": [
8889
{
8990
"label": "Series",
9091
"backgroundColor": "#36a2eb",
9192
"borderColor": "#36a2eb",
92-
"data": project.detection_result["series"],
93+
"data": series,
9394
"yAxisID": "series",
9495
},
9596
{
9697
"label": "Signal",
9798
"backgroundColor": "#ff6384",
9899
"borderColor": "#ff6384",
99-
"data": project.detection_result["signal"],
100+
"data": signal,
100101
"yAxisID": "signal",
101102
},
102103
{
103104
"label": "Threshold",
104105
"backgroundColor": "#9966ff",
105106
"borderColor": "#9966ff",
106107
"data": [
107-
avg_filter + threshold * std_filter
108-
for avg_filter, std_filter in zip(
109-
project.detection_result["avg_filter"],
110-
project.detection_result["std_filter"],
111-
)
108+
avg_filter + threshold * std_filter for avg_filter, std_filter in zip(avg_filter, std_filter)
112109
],
113110
"yAxisID": "series",
114111
},
115112
],
116-
"labels": project.detection_result["intervals"],
113+
"labels": labels,
117114
}
118115
return data, options
119116

controller/sentry/detector.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ def from_project(cls, project: Project) -> "SpikesDetector":
7171
"""
7272
return cls(**project.detection_param)
7373

74-
def compute_sentry(self, stats: dict) -> tuple[OrderedDict, dict[str, list]]:
74+
def compute_sentry(self, stats: dict) -> tuple[OrderedDict, list[tuple[str, int, int, float, float]]]:
7575
"""Method to compute from a sentry stats dict.
7676
7777
Args:
7878
stats (dict): The Sentry Stats
7979
8080
Returns:
8181
OrderedDict: Annotated signal
82-
dict[str, list]: Full algorithm results
82+
list[tuple[str, int, int, float, float]]: Full algorithm results
8383
8484
Raises:
8585
SentryNoOutcomeException: When there is no series with accepted outcome
@@ -93,14 +93,11 @@ def compute_sentry(self, stats: dict) -> tuple[OrderedDict, dict[str, list]]:
9393

9494
signal, avg_filter, std_filter = self.compute(series)
9595

96-
annotated_result = OrderedDict((date, signal) for date, signal in zip(stats["intervals"], signal))
97-
dump = {
98-
"signal": signal,
99-
"avg_filter": avg_filter,
100-
"std_filter": std_filter,
101-
"series": series,
102-
"intervals": stats["intervals"],
103-
}
96+
annotated_result = OrderedDict((date, sig) for date, sig in zip(stats["intervals"], signal))
97+
dump = []
98+
for data in zip(stats["intervals"], series, signal, avg_filter, std_filter):
99+
dump.append(data)
100+
104101
return annotated_result, dump
105102

106103
def compute(self, data) -> tuple[list[int], list[float], list[float]]:

controller/sentry/tests/data/example-1/expected.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
"2023-02-06T15:00:00Z": 1,
121121
"2023-02-06T16:00:00Z": 1,
122122
"2023-02-06T17:00:00Z": 0,
123-
"2023-02-06T18:00:00Z": 1,
123+
"2023-02-06T18:00:00Z": 0,
124124
"2023-02-06T19:00:00Z": 0,
125125
"2023-02-06T20:00:00Z": 0,
126126
"2023-02-06T21:00:00Z": 0,
@@ -136,7 +136,7 @@
136136
"2023-02-07T07:00:00Z": 0,
137137
"2023-02-07T08:00:00Z": 0,
138138
"2023-02-07T09:00:00Z": 0,
139-
"2023-02-07T10:00:00Z": 1,
139+
"2023-02-07T10:00:00Z": 0,
140140
"2023-02-07T11:00:00Z": 0,
141141
"2023-02-07T12:00:00Z": 0,
142142
"2023-02-07T13:00:00Z": 0,

controller/sentry/tests/test_admin.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,10 @@ def test_project_chart_no_context(super_call: Mock, admin_with_user):
400400
def test_project_chart(super_call: Mock, admin_with_user):
401401
site, request = admin_with_user
402402
project = Project(sentry_id="123")
403-
project.detection_result = {
404-
"signal": [0, 1],
405-
"avg_filter": [0, 1],
406-
"std_filter": [0, 2],
407-
"series": [0, 5],
408-
"intervals": ["a", "b"],
409-
}
403+
project.detection_result = [
404+
("a", 0, 0, 0, 0),
405+
("b", 5, 1, 1, 2),
406+
]
410407
project.save()
411408
super_call.return_value = MockResponse(context_data=True)
412409
response = site.change_view(request, project.sentry_id)
@@ -420,14 +417,14 @@ def test_project_chart(super_call: Mock, admin_with_user):
420417
"label": "Series",
421418
"backgroundColor": "#36a2eb",
422419
"borderColor": "#36a2eb",
423-
"data": [0, 5],
420+
"data": (0, 5),
424421
"yAxisID": "series",
425422
},
426423
{
427424
"label": "Signal",
428425
"backgroundColor": "#ff6384",
429426
"borderColor": "#ff6384",
430-
"data": [0, 1],
427+
"data": (0, 1),
431428
"yAxisID": "signal",
432429
},
433430
{
@@ -438,7 +435,7 @@ def test_project_chart(super_call: Mock, admin_with_user):
438435
"yAxisID": "series",
439436
},
440437
],
441-
"labels": ["a", "b"],
438+
"labels": ("a", "b"),
442439
},
443440
"options": settings.DEFAULT_GRAPH_OPTION,
444441
}

controller/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@
329329
DEFAULT_SPIKE_DETECTION_PARAM = {
330330
"lag": int(os.getenv("SPIKE_DETECTION_LAG", "48")),
331331
"threshold": int(os.getenv("SPIKE_DETECTION_THRESHOLD", "5")),
332-
"influence": float(os.getenv("SPIKE_DETECTION_INFLUENCE", "0")),
332+
"influence": float(os.getenv("SPIKE_DETECTION_INFLUENCE", "0.01")),
333333
}
334334

335335

0 commit comments

Comments
 (0)