VolumeBasedCondition: simple serialization

This commit is contained in:
Klaas van Schelven
2024-01-05 18:48:05 +01:00
parent fee07ac1ba
commit 2eb555538b
3 changed files with 40 additions and 2 deletions
+2 -2
View File
@@ -11,13 +11,13 @@ You should get alerts for:
- Regressions
- Unmuting (could be due to volume)
The basic belief underlying this is: "you should care about the errors on your project". And therefor, you'll be
The basic belief underlying this is: "you should care about the errors on your project". And therefore, you'll be
alerted when they occur.
You may configure alerts for:
* **Volume-Based Rules:**
- _Any time_ more than 5 events per hour occur
- _Any time_ more than 5 events per the last 3 hours occur
- _First time_ more than 10 events per day
- _First time_ the total number of events exceeds 100
+12
View File
@@ -3,6 +3,7 @@ from datetime import datetime, timezone
from unittest import TestCase
from bugsink.period_counter import PeriodCounter, _prev_tup
from bugsink.volume_based_condition import VolumeBasedCondition
def apply_n(f, n, v):
@@ -117,3 +118,14 @@ class PeriodCounterTestCase(TestCase):
pc.inc(tp_2022)
self.assertEquals(2, wbt.calls)
self.assertEquals(1, wbf.calls) # unchanged
class VolumeBasedConditionTestCase(TestCase):
def test_serialization(self):
vbc = VolumeBasedCondition("any", "day", 1, 100)
json_str = vbc.to_json_str()
self.assertEquals('{"any_or_first": "any", "period": "day", "nr_of_periods": 1, "volume": 100}', json_str)
vbc2 = VolumeBasedCondition.from_json_str(json_str)
self.assertEquals(vbc, vbc2)
+26
View File
@@ -0,0 +1,26 @@
import json
class VolumeBasedCondition(object):
def __init__(self, any_or_first, period, nr_of_periods, volume):
self.any_or_first = any_or_first
self.period = period
self.nr_of_periods = nr_of_periods
self.volume = volume
@classmethod
def from_json_str(cls, json_str):
json_dict = json.loads(json_str)
return VolumeBasedCondition(
json_dict['any_or_first'],
json_dict['period'],
json_dict['nr_of_periods'],
json_dict['volume'],
)
def to_json_str(obj):
return json.dumps(obj.__dict__)
def __eq__(self, other):
return self.__dict__ == other.__dict__