29
29
import click
30
30
import psutil
31
31
32
- from simvue .api .objects .alert .base import AlertBase
33
32
from simvue .api .objects .alert .fetch import Alert
34
33
from simvue .api .objects .folder import Folder
35
34
from simvue .exception import SimvueRunError
@@ -695,6 +694,7 @@ def init(
695
694
self ._sv_obj .tags = tags
696
695
self ._sv_obj .metadata = (metadata or {}) | git_info (os .getcwd ()) | environment ()
697
696
self ._sv_obj .heartbeat_timeout = timeout
697
+ self ._sv_obj .alerts = []
698
698
self ._sv_obj .notifications = notification
699
699
700
700
if self ._status == "running" :
@@ -1642,48 +1642,26 @@ def add_alerts(
1642
1642
try :
1643
1643
if alerts := Alert .get (offline = self ._user_config .run .mode == "offline" ):
1644
1644
for alert in alerts :
1645
- if alert .name in names :
1646
- ids .append (alert .id )
1645
+ if alert [1 ].name in names :
1646
+ ids .append (alert [1 ].id )
1647
+ else :
1648
+ self ._error ("No existing alerts" )
1649
+ return False
1647
1650
except RuntimeError as e :
1648
1651
self ._error (f"{ e .args [0 ]} " )
1649
1652
return False
1650
- else :
1651
- self ._error ("No existing alerts" )
1652
- return False
1653
1653
elif not names and not ids :
1654
1654
self ._error ("Need to provide alert ids or alert names" )
1655
1655
return False
1656
1656
1657
1657
# Avoid duplication
1658
- self ._sv_obj .alerts = list (set (self ._sv_obj .alerts + [ids ]))
1658
+ _deduplicated = list (set (self ._sv_obj .alerts + ids ))
1659
+ self ._sv_obj .alerts = _deduplicated
1659
1660
self ._sv_obj .commit ()
1660
1661
1661
- return False
1662
-
1663
- def _attach_alert_to_run (self , alert : AlertBase ) -> str | None :
1664
- # Check if the alert already exists
1665
- _alert_id : str | None = None
1666
-
1667
- for _ , _existing_alert in Alert .get (
1668
- offline = self ._user_config .run .mode == "offline"
1669
- ):
1670
- if _existing_alert .compare (alert ):
1671
- _alert_id = _existing_alert .id
1672
- logger .info ("Existing alert found with id: %s" , _existing_alert .id )
1673
- break
1674
-
1675
- if not _alert_id :
1676
- alert .commit ()
1677
- _alert_id = alert .id
1678
-
1679
- self ._sv_obj .alerts = [_alert_id ]
1680
-
1681
- self ._sv_obj .commit ()
1682
-
1683
- return _alert_id
1662
+ return True
1684
1663
1685
1664
@skip_if_failed ("_aborted" , "_suppress_errors" , None )
1686
- @check_run_initialised
1687
1665
@pydantic .validate_call
1688
1666
def create_metric_range_alert (
1689
1667
self ,
@@ -1701,6 +1679,7 @@ def create_metric_range_alert(
1701
1679
] = "average" ,
1702
1680
notification : typing .Literal ["email" , "none" ] = "none" ,
1703
1681
trigger_abort : bool = False ,
1682
+ attach_to_run : bool = True ,
1704
1683
) -> str | None :
1705
1684
"""Creates a metric range alert with the specified name (if it doesn't exist)
1706
1685
and applies it to the current run. If alert already exists it will
@@ -1730,6 +1709,8 @@ def create_metric_range_alert(
1730
1709
whether to notify on trigger, by default "none"
1731
1710
trigger_abort : bool, optional
1732
1711
whether this alert can trigger a run abort, default False
1712
+ attach_to_run : bool, optional
1713
+ whether to attach this alert to the current run, default True
1733
1714
1734
1715
Returns
1735
1716
-------
@@ -1751,10 +1732,12 @@ def create_metric_range_alert(
1751
1732
offline = self ._user_config .run .mode == "offline" ,
1752
1733
)
1753
1734
_alert .abort = trigger_abort
1754
- return self ._attach_alert_to_run (_alert )
1735
+ _alert .commit ()
1736
+ if attach_to_run :
1737
+ self .add_alerts (ids = [_alert .id ])
1738
+ return _alert .id
1755
1739
1756
1740
@skip_if_failed ("_aborted" , "_suppress_errors" , None )
1757
- @check_run_initialised
1758
1741
@pydantic .validate_call
1759
1742
def create_metric_threshold_alert (
1760
1743
self ,
@@ -1771,6 +1754,7 @@ def create_metric_threshold_alert(
1771
1754
] = "average" ,
1772
1755
notification : typing .Literal ["email" , "none" ] = "none" ,
1773
1756
trigger_abort : bool = False ,
1757
+ attach_to_run : bool = True ,
1774
1758
) -> str | None :
1775
1759
"""Creates a metric threshold alert with the specified name (if it doesn't exist)
1776
1760
and applies it to the current run. If alert already exists it will
@@ -1798,6 +1782,8 @@ def create_metric_threshold_alert(
1798
1782
whether to notify on trigger, by default "none"
1799
1783
trigger_abort : bool, optional
1800
1784
whether this alert can trigger a run abort, default False
1785
+ attach_to_run : bool, optional
1786
+ whether to attach this alert to the current run, default True
1801
1787
1802
1788
Returns
1803
1789
-------
@@ -1817,11 +1803,14 @@ def create_metric_threshold_alert(
1817
1803
notification = notification ,
1818
1804
offline = self ._user_config .run .mode == "offline" ,
1819
1805
)
1806
+
1820
1807
_alert .abort = trigger_abort
1821
- return self ._attach_alert_to_run (_alert )
1808
+ _alert .commit ()
1809
+ if attach_to_run :
1810
+ self .add_alerts (ids = [_alert .id ])
1811
+ return _alert .id
1822
1812
1823
1813
@skip_if_failed ("_aborted" , "_suppress_errors" , None )
1824
- @check_run_initialised
1825
1814
@pydantic .validate_call
1826
1815
def create_event_alert (
1827
1816
self ,
@@ -1832,6 +1821,7 @@ def create_event_alert(
1832
1821
frequency : pydantic .PositiveInt = 1 ,
1833
1822
notification : typing .Literal ["email" , "none" ] = "none" ,
1834
1823
trigger_abort : bool = False ,
1824
+ attach_to_run : bool = True ,
1835
1825
) -> str | None :
1836
1826
"""Creates an events alert with the specified name (if it doesn't exist)
1837
1827
and applies it to the current run. If alert already exists it will
@@ -1849,6 +1839,8 @@ def create_event_alert(
1849
1839
whether to notify on trigger, by default "none"
1850
1840
trigger_abort : bool, optional
1851
1841
whether this alert can trigger a run abort
1842
+ attach_to_run : bool, optional
1843
+ whether to attach this alert to the current run, default True
1852
1844
1853
1845
Returns
1854
1846
-------
@@ -1865,10 +1857,12 @@ def create_event_alert(
1865
1857
offline = self ._user_config .run .mode == "offline" ,
1866
1858
)
1867
1859
_alert .abort = trigger_abort
1868
- return self ._attach_alert_to_run (_alert )
1860
+ _alert .commit ()
1861
+ if attach_to_run :
1862
+ self .add_alerts (ids = [_alert .id ])
1863
+ return _alert .id
1869
1864
1870
1865
@skip_if_failed ("_aborted" , "_suppress_errors" , None )
1871
- @check_run_initialised
1872
1866
@pydantic .validate_call
1873
1867
def create_user_alert (
1874
1868
self ,
@@ -1877,6 +1871,7 @@ def create_user_alert(
1877
1871
description : str | None = None ,
1878
1872
notification : typing .Literal ["email" , "none" ] = "none" ,
1879
1873
trigger_abort : bool = False ,
1874
+ attach_to_run : bool = True ,
1880
1875
) -> None :
1881
1876
"""Creates a user alert with the specified name (if it doesn't exist)
1882
1877
and applies it to the current run. If alert already exists it will
@@ -1892,6 +1887,8 @@ def create_user_alert(
1892
1887
whether to notify on trigger, by default "none"
1893
1888
trigger_abort : bool, optional
1894
1889
whether this alert can trigger a run abort, default False
1890
+ attach_to_run : bool, optional
1891
+ whether to attach this alert to the current run, default True
1895
1892
1896
1893
Returns
1897
1894
-------
@@ -1906,7 +1903,10 @@ def create_user_alert(
1906
1903
offline = self ._user_config .run .mode == "offline" ,
1907
1904
)
1908
1905
_alert .abort = trigger_abort
1909
- return self ._attach_alert_to_run (_alert )
1906
+ _alert .commit ()
1907
+ if attach_to_run :
1908
+ self .add_alerts (ids = [_alert .id ])
1909
+ return _alert .id
1910
1910
1911
1911
@skip_if_failed ("_aborted" , "_suppress_errors" , False )
1912
1912
@check_run_initialised
0 commit comments