Skip to content

Commit

Permalink
Merge pull request #108 from amosproj/84-backend-alert-api---backup-c…
Browse files Browse the repository at this point in the history
…reation-time-alert

Change Alert Structure
  • Loading branch information
chrisklg authored Nov 30, 2024
2 parents d712885 + 8659660 commit 44622ef
Show file tree
Hide file tree
Showing 32 changed files with 717 additions and 395 deletions.
2 changes: 1 addition & 1 deletion apps/analyzer/metadata_analyzer/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ def send_backup_data_batched(self, batch):
r.raise_for_status()

def create_alert(self, alert):
url = self.backend_url + "alerting"
url = self.backend_url + "alerting/size"
r = requests.post(url, json=alert)
r.raise_for_status()
10 changes: 4 additions & 6 deletions apps/analyzer/metadata_analyzer/simple_rule_based_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def _analyze_pair(self, result1, result2, bound):
return []

alert = {
"type": 0 if relative_change > 0 else 1,
"value": result2.data_size / 1_000_000,
"referenceValue": result1.data_size / 1_000_000,
"size": result2.data_size / 1_000_000,
"referenceSize": result1.data_size / 1_000_000,
"backupId": result2.uuid,
}
return [alert]
Expand All @@ -47,9 +46,8 @@ def _analyze_pair_diff(self, result1, result2):
return []

alert = {
"type": 0 if relative_change > 0 else 1,
"value": result2.data_size / 1_000_000,
"referenceValue": result1.data_size / 1_000_000,
"size": result2.data_size / 1_000_000,
"referenceSize": result1.data_size / 1_000_000,
"backupId": result2.uuid,
}

Expand Down
91 changes: 51 additions & 40 deletions apps/analyzer/tests/test_simple_rule_based_analyzer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from metadata_analyzer.simple_rule_based_analyzer import SimpleRuleBasedAnalyzer
from datetime import datetime

from metadata_analyzer.analyzer import Analyzer
from metadata_analyzer.models import Result
from metadata_analyzer.simple_rule_based_analyzer import SimpleRuleBasedAnalyzer
from tests.mock_backend import MockBackend
from tests.mock_database import MockDatabase
from datetime import datetime


def _create_mock_result(task, uuid, fdi_type, data_size, start_time):
mock_result = Result()
Expand All @@ -14,6 +16,7 @@ def _create_mock_result(task, uuid, fdi_type, data_size, start_time):
mock_result.start_time = start_time
return mock_result


def test_alert():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("foo", "2", "F", 121_000_000, datetime.fromisoformat("2000-01-02"))
Expand All @@ -25,12 +28,12 @@ def test_alert():
Analyzer.simple_rule_based_analysis(-1)

assert backend.alerts == [{
"type": 0,
"value": mock_result2.data_size / 1_000_000,
"referenceValue": mock_result1.data_size / 1_000_000,
"size": mock_result2.data_size / 1_000_000,
"referenceSize": mock_result1.data_size / 1_000_000,
"backupId": mock_result2.uuid
}]


def test_alerts_different_tasks():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("foo", "2", "F", 121_000_000, datetime.fromisoformat("2000-01-02"))
Expand All @@ -44,17 +47,16 @@ def test_alerts_different_tasks():
Analyzer.simple_rule_based_analysis(-1)

assert backend.alerts == [{
"type": 0,
"value": mock_result2.data_size / 1_000_000,
"referenceValue": mock_result1.data_size / 1_000_000,
"size": mock_result2.data_size / 1_000_000,
"referenceSize": mock_result1.data_size / 1_000_000,
"backupId": mock_result2.uuid
}, {
"type": 1,
"value": mock_result4.data_size / 1_000_000,
"referenceValue": mock_result3.data_size / 1_000_000,
"size": mock_result4.data_size / 1_000_000,
"referenceSize": mock_result3.data_size / 1_000_000,
"backupId": mock_result4.uuid
}]


def test_alert_backup_size_zero():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("foo", "2", "F", 0, datetime.fromisoformat("2000-01-02"))
Expand All @@ -66,12 +68,12 @@ def test_alert_backup_size_zero():
Analyzer.simple_rule_based_analysis(-1)

assert backend.alerts == [{
"type": 1,
"value": mock_result2.data_size / 1_000_000,
"referenceValue": mock_result1.data_size / 1_000_000,
"size": mock_result2.data_size / 1_000_000,
"referenceSize": mock_result1.data_size / 1_000_000,
"backupId": mock_result2.uuid
}]


def test_no_alert_size_diff_too_small():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("foo", "2", "F", 120_000_000, datetime.fromisoformat("2000-01-02"))
Expand All @@ -84,6 +86,7 @@ def test_no_alert_size_diff_too_small():

assert backend.alerts == []


def test_no_alert_wrong_type():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("foo", "2", "D", 121_000_000, datetime.fromisoformat("2000-01-02"))
Expand All @@ -96,6 +99,7 @@ def test_no_alert_wrong_type():

assert backend.alerts == []


def test_no_alert_different_tasks():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("bar", "2", "F", 121_000_000, datetime.fromisoformat("2000-01-02"))
Expand All @@ -108,6 +112,7 @@ def test_no_alert_different_tasks():

assert backend.alerts == []


def test_alert_limit():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("foo", "2", "F", 150_000_000, datetime.fromisoformat("2000-01-02"))
Expand All @@ -121,6 +126,7 @@ def test_alert_limit():

assert len(backend.alerts) == 1


# extremely large difference
def test_alert_backup_size_zero_diff():
mock_result1 = _create_mock_result("foo", "1", "D", 100_000_000, datetime.fromisoformat("2000-01-01"))
Expand All @@ -133,12 +139,12 @@ def test_alert_backup_size_zero_diff():
Analyzer.simple_rule_based_analysis_diff(1)

assert backend.alerts == [{
"type": 1,
"value": mock_result2.data_size / 1_000_000,
"referenceValue": mock_result1.data_size / 1_000_000,
"size": mock_result2.data_size / 1_000_000,
"referenceSize": mock_result1.data_size / 1_000_000,
"backupId": mock_result2.uuid
}]


# two decreasing diff backups (in the accepted range) with different full backups as base
def test_alert_backup_size_decrease_ok_diff():
mock_result1 = _create_mock_result("foo", "1", "D", 100_000_000, datetime.fromisoformat("2000-01-01"))
Expand All @@ -153,6 +159,7 @@ def test_alert_backup_size_decrease_ok_diff():

assert backend.alerts == []


# two decreasing diff backups (in the accepted range) with same full backup as base
def test_alert_backup_size_decrease_nok_diff():
mock_result1 = _create_mock_result("foo", "1", "D", 100_000_000, datetime.fromisoformat("2000-01-01"))
Expand All @@ -165,13 +172,13 @@ def test_alert_backup_size_decrease_nok_diff():
Analyzer.simple_rule_based_analysis_diff(1)

assert backend.alerts == [{
"type": 1,
"value": mock_result2.data_size / 1_000_000,
"referenceValue": mock_result1.data_size / 1_000_000,
"size": mock_result2.data_size / 1_000_000,
"referenceSize": mock_result1.data_size / 1_000_000,
"backupId": mock_result2.uuid
}]

# two decreasing diff backups (not in the accepted range) with same full backup as base

# two decreasing diff backups (not in the accepted range) with same full backup as base
def test_alert_backup_size_decrease_large_nok_diff():
mock_result1 = _create_mock_result("foo", "1", "D", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("foo", "2", "D", 1_000_000, datetime.fromisoformat("2000-01-03"))
Expand All @@ -183,12 +190,12 @@ def test_alert_backup_size_decrease_large_nok_diff():
Analyzer.simple_rule_based_analysis_diff(1)

assert backend.alerts == [{
"type": 1,
"value": mock_result2.data_size / 1_000_000,
"referenceValue": mock_result1.data_size / 1_000_000,
"size": mock_result2.data_size / 1_000_000,
"referenceSize": mock_result1.data_size / 1_000_000,
"backupId": mock_result2.uuid
}]


# two decreasing diff backups (not in the accepted range) with different full backups as base
def test_alert_backup_size_decrease_large_ok_diff():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
Expand All @@ -204,6 +211,7 @@ def test_alert_backup_size_decrease_large_ok_diff():

assert backend.alerts == []


# two increasing diff backups (not in the accepted range) with same full backups as base
def test_alert_backup_size_increase_large_nok_diff():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
Expand All @@ -217,12 +225,12 @@ def test_alert_backup_size_increase_large_nok_diff():
Analyzer.simple_rule_based_analysis_diff(1)

assert backend.alerts == [{
"type": 0,
"value": mock_result3.data_size / 1_000_000,
"referenceValue": mock_result2.data_size / 1_000_000,
"size": mock_result3.data_size / 1_000_000,
"referenceSize": mock_result2.data_size / 1_000_000,
"backupId": mock_result3.uuid
}]


# two increasing diff backups (not in the accepted range) with different full backups as base
def test_alert_backup_size_increase_large_ok_diff():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
Expand All @@ -239,6 +247,8 @@ def test_alert_backup_size_increase_large_ok_diff():
assert backend.alerts == []

# multiple decreasing diff backups (not in the accepted range) with same full backups as base


def test_alert_backup_size_complex_nok_diff():
mock_result1 = _create_mock_result("foo", "1", "F", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("foo", "2", "D", 1_000_000, datetime.fromisoformat("2000-01-02"))
Expand All @@ -248,19 +258,20 @@ def test_alert_backup_size_complex_nok_diff():
mock_result6 = _create_mock_result("foo", "6", "D", 101_000_000, datetime.fromisoformat("2000-01-06"))
mock_result7 = _create_mock_result("foo", "7", "D", 1_000_000, datetime.fromisoformat("2000-01-07"))

database = MockDatabase([mock_result1, mock_result2, mock_result3, mock_result4, mock_result5, mock_result6, mock_result7])
database = MockDatabase(
[mock_result1, mock_result2, mock_result3, mock_result4, mock_result5, mock_result6, mock_result7])
backend = MockBackend()
simple_rule_based_analyzer = SimpleRuleBasedAnalyzer(backend, 0.2, 0.2, 0.2, 0.2)
Analyzer.init(database, backend, None, simple_rule_based_analyzer)
Analyzer.simple_rule_based_analysis_diff(1)

assert backend.alerts == [{
"type": 1,
"value": mock_result7.data_size / 1_000_000,
"referenceValue": mock_result6.data_size / 1_000_000,
"size": mock_result7.data_size / 1_000_000,
"referenceSize": mock_result6.data_size / 1_000_000,
"backupId": mock_result7.uuid
}]


# large increase of inc size
def test_alert_backup_size_zero_inc():
mock_result1 = _create_mock_result("foo", "1", "I", 100_000_000, datetime.fromisoformat("2000-01-01"))
Expand All @@ -273,34 +284,35 @@ def test_alert_backup_size_zero_inc():
Analyzer.simple_rule_based_analysis_inc(1)

assert backend.alerts == [{
"type": 1,
"value": mock_result2.data_size / 1_000_000,
"referenceValue": mock_result1.data_size / 1_000_000,
"size": mock_result2.data_size / 1_000_000,
"referenceSize": mock_result1.data_size / 1_000_000,
"backupId": mock_result2.uuid
}]


# irregular backup times that should not be alerted
def test_alert_backup_size_irregular_inc():
mock_result1 = _create_mock_result("foo", "1", "I", 100_000_000, datetime.fromisoformat("2000-01-01"))
mock_result2 = _create_mock_result("foo", "2", "I", 0, datetime.fromisoformat("2000-01-08"))
mock_result3 = _create_mock_result("foo", "3", "I", 100_000_000, datetime.fromisoformat("2000-01-09"))
mock_result4 = _create_mock_result("foo", "4", "I", 100_000_000, datetime.fromisoformat("2000-01-10"))

database = MockDatabase([mock_result1, mock_result2, mock_result3,mock_result4])
database = MockDatabase([mock_result1, mock_result2, mock_result3, mock_result4])
backend = MockBackend()
simple_rule_based_analyzer = SimpleRuleBasedAnalyzer(backend, 0.2, 0.2, 0.2, 0.2)
Analyzer.init(database, backend, None, simple_rule_based_analyzer)
Analyzer.simple_rule_based_analysis_inc(1)

assert backend.alerts == []


# irregular backup sizes
def test_alert_backup_size_irregularSize_inc():
mock_result1 = _create_mock_result("foo", "1", "I", 100_000_000, datetime.fromisoformat("2000-01-07"))
mock_result2 = _create_mock_result("foo", "2", "I", 100_000_000, datetime.fromisoformat("2000-01-08"))
mock_result3 = _create_mock_result("foo", "3", "I", 72_000_000, datetime.fromisoformat("2000-01-09"))
mock_result4 = _create_mock_result("foo", "4", "I", 100_000_000, datetime.fromisoformat("2000-01-10"))
avg = (mock_result1.data_size + mock_result2.data_size + mock_result3.data_size + mock_result4.data_size)/4
avg = (mock_result1.data_size + mock_result2.data_size + mock_result3.data_size + mock_result4.data_size) / 4

database = MockDatabase([mock_result1, mock_result2, mock_result3, mock_result4])
backend = MockBackend()
Expand All @@ -309,8 +321,7 @@ def test_alert_backup_size_irregularSize_inc():
Analyzer.simple_rule_based_analysis_inc(1)

assert backend.alerts == [{
"type": 1,
"value":72,
"referenceValue": avg / 1_000_000,
"size": 72,
"referenceSize": avg / 1_000_000,
"backupId": mock_result3.uuid
}]
}]
Loading

0 comments on commit 44622ef

Please sign in to comment.