Skip to content

Commit 4785b07

Browse files
committed
fix:PRComments
1 parent 2a8b11e commit 4785b07

File tree

5 files changed

+79
-67
lines changed

5 files changed

+79
-67
lines changed

examples/alarm/alarm.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@
3030
transition=SetAlarmTransition(
3131
occurred_at=datetime.now(),
3232
severity_level=AlarmSeverityLevel.HIGH,
33-
condition="Temperature exceeded threshold",
33+
value="85",
34+
condition="Greater than 80",
35+
short_text="Temperature is high",
36+
detail_text="Temperature sensor reading is 85°C (higher than the configured threshold of 80°C)",
3437
),
3538
)
3639
# Returns instance_id - a server-generated unique identifier for this specific alarm occurrence
3740
id = client.create_or_update_alarm(create_request)
3841

3942
# Get the alarm by its instance ID (the unique occurrence identifier)
40-
if id:
41-
alarm = client.get_alarm(id)
43+
alarm = client.get_alarm(instance_id=id)
4244
print(f"Retrieved alarm: {alarm.alarm_id}, Condition: {alarm.condition}")
4345

4446
# Update the alarm with a higher severity (same alarm_id, updates the same instance)
@@ -47,7 +49,10 @@
4749
transition=SetAlarmTransition(
4850
occurred_at=datetime.now(),
4951
severity_level=AlarmSeverityLevel.CRITICAL,
50-
condition="Temperature critically high",
52+
value="95",
53+
condition="Greater than 90",
54+
short_text="Temperature is critical",
55+
detail_text="Temperature sensor reading is 95°C (higher than the configured threshold of 90°C)",
5156
),
5257
)
5358
client.create_or_update_alarm(update_request)
@@ -71,9 +76,8 @@
7176
for transition in alarm.transitions:
7277
print(f"- {transition.transition_type}: {transition.condition}")
7378

74-
if id:
75-
# Acknowledge the alarm
76-
client.acknowledge_alarms(ids=[id])
79+
# Acknowledge the alarm
80+
client.acknowledge_alarms(instance_ids=[id])
7781

7882
# Clear the alarm with 409 conflict handling - Method 1: Manual exception handling
7983
# A 409 Conflict response indicates that the requested transition would not change the alarm's state.
@@ -105,6 +109,5 @@
105109
else:
106110
print(f"Alarm cleared successfully: {result}")
107111

108-
if id:
109-
# Delete the alarm by its instance ID
110-
client.delete_alarm(id)
112+
# Delete the alarm by its instance ID
113+
client.delete_alarm(instance_id=id)

nisystemlink/clients/alarm/_alarm_client.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Implementation of Alarm Client"""
22

3-
from typing import List
3+
from typing import List, Literal, overload
44

55
from nisystemlink.clients import core
66
from nisystemlink.clients.core._uplink._base_client import BaseClient
@@ -39,12 +39,12 @@ def __init__(self, configuration: core.HttpConfiguration | None = None):
3939
args=[Field("instanceIds"), Field("forceClear")],
4040
)
4141
def acknowledge_alarms(
42-
self, ids: List[str], *, force_clear: bool = False
42+
self, instance_ids: List[str], *, force_clear: bool = False
4343
) -> models.AcknowledgeAlarmsResponse:
4444
"""Acknowledges one or more alarm instances by their instance IDs.
4545
4646
Args:
47-
ids: List of instance IDs (unique occurrence identifiers) of the alarms to acknowledge.
47+
instance_ids: List of instance IDs (unique occurrence identifiers) of the alarms to acknowledge.
4848
These are the server-generated IDs returned when creating/updating alarms,
4949
not the user-defined alarm_id.
5050
force_clear: Whether or not the affected alarms should have their clear field set to true.
@@ -59,6 +59,22 @@ def acknowledge_alarms(
5959
"""
6060
...
6161

62+
@overload
63+
def create_or_update_alarm( # noqa: E704
64+
self,
65+
request: models.CreateOrUpdateAlarmRequest,
66+
*,
67+
ignore_conflict: Literal[False] = False,
68+
) -> str: ...
69+
70+
@overload
71+
def create_or_update_alarm( # noqa: E704
72+
self,
73+
request: models.CreateOrUpdateAlarmRequest,
74+
*,
75+
ignore_conflict: Literal[True],
76+
) -> str | None: ...
77+
6278
def create_or_update_alarm(
6379
self,
6480
request: models.CreateOrUpdateAlarmRequest,
@@ -91,15 +107,12 @@ def create_or_update_alarm(
91107
or attempting to set an alarm which is already set at the given severity level.
92108
This error can be suppressed by setting ignore_conflict=True.
93109
"""
94-
if ignore_conflict:
95-
try:
96-
return self._create_or_update_alarm(request)
97-
except core.ApiException as e:
98-
if e.http_status_code == 409:
99-
return None
100-
raise
101-
else:
110+
try:
102111
return self._create_or_update_alarm(request)
112+
except core.ApiException as e:
113+
if ignore_conflict and e.http_status_code == 409:
114+
return None
115+
raise
103116

104117
@post("instances", return_key="instanceId")
105118
def _create_or_update_alarm(
@@ -109,11 +122,11 @@ def _create_or_update_alarm(
109122
...
110123

111124
@get("instances/{instance_id}", args=[Path("instance_id")])
112-
def get_alarm(self, id: str) -> models.Alarm:
125+
def get_alarm(self, instance_id: str) -> models.Alarm:
113126
"""Gets an alarm by its instance_id.
114127
115128
Args:
116-
id: The unique instance ID (occurrence identifier) of the alarm to retrieve.
129+
instance_id: The unique instance ID (occurrence identifier) of the alarm to retrieve.
117130
This is the server-generated ID returned from create_or_update_alarm(),
118131
not the user-defined alarm_id.
119132
@@ -126,11 +139,11 @@ def get_alarm(self, id: str) -> models.Alarm:
126139
...
127140

128141
@delete("instances/{instance_id}", args=[Path("instance_id")])
129-
def delete_alarm(self, id: str) -> None:
142+
def delete_alarm(self, instance_id: str) -> None:
130143
"""Deletes an alarm by its instance_id.
131144
132145
Args:
133-
id: The unique instance ID (occurrence identifier) of the alarm to delete.
146+
instance_id: The unique instance ID (occurrence identifier) of the alarm to delete.
134147
This is the server-generated ID returned from create_or_update_alarm(),
135148
not the user-defined alarm_id.
136149
@@ -140,11 +153,11 @@ def delete_alarm(self, id: str) -> None:
140153
...
141154

142155
@post("delete-instances-by-instance-id", args=[Field("instanceIds")])
143-
def delete_alarms(self, ids: List[str]) -> models.DeleteAlarmsResponse:
156+
def delete_alarms(self, instance_ids: List[str]) -> models.DeleteAlarmsResponse:
144157
"""Deletes multiple alarm instances by their instance IDs.
145158
146159
Args:
147-
ids: List of instance IDs (unique occurrence identifiers) of the alarms to delete.
160+
instance_ids: List of instance IDs (unique occurrence identifiers) of the alarms to delete.
148161
These are the server-generated IDs returned when creating/updating alarms,
149162
not the user-defined alarm_id.
150163

nisystemlink/clients/alarm/models/_alarm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AlarmSeverityLevel(IntEnum):
1313
"""
1414

1515
CLEAR = -1
16-
"""Indicates an alarm clearance."""
16+
"""Severity level for cleared alarms."""
1717

1818
LOW = 1
1919
"""Low severity level."""
@@ -57,6 +57,7 @@ class AlarmTransition(JsonModel):
5757
Valid values for CLEAR transitions are [-1, -1].
5858
Valid values for SET transitions are [1, 2147483647].
5959
Note that the SystemLink Alarm UI only has display strings for SET severities in the range [1, 4].
60+
The AlarmSeverityLevel enum provides values for standard severity levels.
6061
"""
6162

6263
value: str

nisystemlink/clients/alarm/models/_create_or_update_alarm_request.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from nisystemlink.clients.core._uplink._json_model import JsonModel
55
from pydantic import model_validator
66

7-
from ._alarm import AlarmTransitionType
7+
from ._alarm import AlarmSeverityLevel, AlarmTransitionType
88

99

1010
class CreateAlarmTransition(JsonModel):
@@ -22,6 +22,7 @@ class CreateAlarmTransition(JsonModel):
2222
Valid values for CLEAR transitions are [-1, -1].
2323
Valid values for SET transitions are [1, 2147483647].
2424
Note that the SystemLink Alarm UI only has display strings for SET severities in the range [1, 4].
25+
The AlarmSeverityLevel enum provides values for standard severity levels.
2526
"""
2627

2728
value: str | None = None
@@ -74,12 +75,12 @@ class ClearAlarmTransition(CreateAlarmTransition):
7475
"""
7576

7677
transition_type: AlarmTransitionType = AlarmTransitionType.CLEAR
77-
severity_level: int | None = -1
78+
severity_level: int | None = AlarmSeverityLevel.CLEAR
7879

7980
@model_validator(mode="after")
8081
def _set_clear_defaults(self) -> "ClearAlarmTransition":
8182
self.transition_type = AlarmTransitionType.CLEAR
82-
self.severity_level = -1
83+
self.severity_level = AlarmSeverityLevel.CLEAR
8384
return self
8485

8586

@@ -103,7 +104,11 @@ class CreateOrUpdateAlarmRequest(JsonModel):
103104
"""
104105

105106
transition: CreateAlarmTransition
106-
"""Contains information about a transition used to create or update an instance of an alarm."""
107+
"""Contains information about a transition used to create or update an instance of an alarm.
108+
109+
Consider using SetAlarmTransition to trigger an alarm or ClearAlarmTransition to clear an alarm.
110+
These convenience classes automatically set the appropriate transition type and severity level.
111+
"""
107112

108113
notification_strategy_ids: List[str] | None = None
109114
"""The IDs of the notification strategies which should be triggered.

0 commit comments

Comments
 (0)