Skip to content

Commit 4832566

Browse files
committed
fix:PRComments
1 parent 6b28ef6 commit 4832566

File tree

9 files changed

+175
-177
lines changed

9 files changed

+175
-177
lines changed

examples/alarm/alarm.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
from nisystemlink.clients.alarm import AlarmClient
55
from nisystemlink.clients.alarm.models import (
66
AlarmOrderBy,
7-
AlarmTransitionType,
8-
CreateAlarmTransition,
7+
AlarmSeverityLevel,
8+
ClearAlarmTransition,
99
CreateOrUpdateAlarmRequest,
1010
QueryAlarmsWithFilterRequest,
11-
TransitionInclusionOption,
11+
SetAlarmTransition,
1212
)
1313
from nisystemlink.clients.core import HttpConfiguration
1414

1515
# Setup the server configuration to point to your instance of SystemLink Enterprise
1616
server_configuration = HttpConfiguration(
17-
server_uri="https://yourserver.yourcompany.com",
18-
api_key="YourAPIKeyGeneratedFromSystemLink",
17+
server_uri="https://test-api.lifecyclesolutions.ni.com/",
18+
api_key="oVu4EpiijnlgwjPlY58lke8H1xv2XLuCo1QmAvveMI",
1919
)
2020
client = AlarmClient(configuration=server_configuration)
2121

@@ -26,10 +26,9 @@
2626
# Create an alarm with a SET transition
2727
create_request = CreateOrUpdateAlarmRequest(
2828
alarm_id=alarm_id,
29-
transition=CreateAlarmTransition(
30-
transition_type=AlarmTransitionType.SET,
29+
transition=SetAlarmTransition(
3130
occurred_at=datetime.now(),
32-
severity_level=3,
31+
severity_level=AlarmSeverityLevel.HIGH,
3332
condition="Temperature exceeded threshold",
3433
message="Temperature sensor reading: 85°C",
3534
),
@@ -39,14 +38,14 @@
3938

4039
# Get the alarm by its instance ID (the unique occurrence identifier)
4140
alarm = client.get_alarm(id)
41+
print(f"Retrieved alarm: {alarm.alarm_id}, Condition: {alarm.condition}")
4242

4343
# Update the alarm with a higher severity (same alarm_id, updates the same instance)
4444
update_request = CreateOrUpdateAlarmRequest(
4545
alarm_id=alarm_id,
46-
transition=CreateAlarmTransition(
47-
transition_type=AlarmTransitionType.SET,
46+
transition=SetAlarmTransition(
4847
occurred_at=datetime.now(),
49-
severity_level=5,
48+
severity_level=AlarmSeverityLevel.CRITICAL,
5049
condition="Temperature critically high",
5150
message="Temperature sensor reading: 95°C",
5251
),
@@ -55,25 +54,21 @@
5554

5655
# Query alarms with a filter (can filter by alarm_id to find all instances)
5756
query_request = QueryAlarmsWithFilterRequest(
58-
filter=f'alarmId="{alarm_id}"',
59-
transition_inclusion_option=TransitionInclusionOption.ALL,
57+
filter="alarmId=@0",
58+
substitutions=[alarm_id],
6059
order_by=AlarmOrderBy.UPDATED_AT,
6160
order_by_descending=True,
62-
return_count=True,
6361
)
6462
query_response = client.query_alarms(query_request)
6563

66-
# Acknowledge all queried alarms
67-
queried_alarm_ids = [alarm.instance_id for alarm in query_response.alarms]
68-
ack_response = client.acknowledge_alarms(ids=queried_alarm_ids)
64+
# Acknowledge the alarm
65+
client.acknowledge_alarms(ids=[id])
6966

7067
# Clear the alarm
7168
clear_request = CreateOrUpdateAlarmRequest(
7269
alarm_id=alarm_id,
73-
transition=CreateAlarmTransition(
74-
transition_type=AlarmTransitionType.CLEAR,
70+
transition=ClearAlarmTransition(
7571
occurred_at=datetime.now(),
76-
severity_level=0,
7772
condition="Temperature returned to normal",
7873
),
7974
)

nisystemlink/clients/alarm/_alarm_client.py

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

3-
from typing import List, Optional
3+
from typing import List
44

55
from nisystemlink.clients import core
66
from nisystemlink.clients.core._uplink._base_client import BaseClient
@@ -17,7 +17,7 @@
1717
)
1818
class AlarmClient(BaseClient):
1919

20-
def __init__(self, configuration: Optional[core.HttpConfiguration] = None):
20+
def __init__(self, configuration: core.HttpConfiguration | None = None):
2121
"""Initialize an instance.
2222
2323
Args:
@@ -39,7 +39,7 @@ def __init__(self, configuration: Optional[core.HttpConfiguration] = None):
3939
args=[Field("instanceIds"), Field("forceClear")],
4040
)
4141
def acknowledge_alarms(
42-
self, ids: List[str], force_clear: bool = False
42+
self, ids: List[str], *, force_clear: bool = False
4343
) -> models.AcknowledgeAlarmsResponse:
4444
"""Acknowledges one or more alarm instances by their instance IDs.
4545
@@ -77,6 +77,9 @@ def create_or_update_alarm(self, request: models.CreateOrUpdateAlarmRequest) ->
7777
7878
Raises:
7979
ApiException: if unable to communicate with the `/nialarm` Service or provided invalid arguments.
80+
A 409 Conflict error occurs when the request does not represent a valid transition
81+
for an existing alarm, such as attempting to clear an alarm which is already clear,
82+
or attempting to set an alarm which is already set at the given severity level.
8083
"""
8184
...
8285

nisystemlink/clients/alarm/models/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from ._acknowledge_alarms_response import AcknowledgeAlarmsResponse
2-
from ._alarm import Alarm, AlarmNote, AlarmTransition, AlarmTransitionType
2+
from ._alarm import Alarm, AlarmSeverityLevel, AlarmTransition, AlarmTransitionType
33
from ._create_or_update_alarm_request import (
4+
ClearAlarmTransition,
45
CreateAlarmTransition,
56
CreateOrUpdateAlarmRequest,
7+
SetAlarmTransition,
68
)
79
from ._delete_alarms_response import DeleteAlarmsResponse
810
from ._query_alarms_request import (

nisystemlink/clients/alarm/models/_alarm.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
from datetime import datetime
2-
from enum import Enum
3-
from typing import Dict, List, Optional
2+
from enum import Enum, IntEnum
3+
from typing import Any, Dict, List
44

55
from nisystemlink.clients.core._uplink._json_model import JsonModel
66

77

8+
class AlarmSeverityLevel(IntEnum):
9+
"""Well-known alarm severity levels.
10+
11+
The service supports custom severity levels greater than 4, but it is generally discouraged.
12+
The SystemLink Alarm UI only has display strings for severity levels in the range [1, 4].
13+
"""
14+
15+
CLEAR = -1
16+
"""Indicates an alarm clearance."""
17+
18+
LOW = 1
19+
"""Low severity level."""
20+
21+
MODERATE = 2
22+
"""Moderate severity level."""
23+
24+
HIGH = 3
25+
"""High severity level."""
26+
27+
CRITICAL = 4
28+
"""Critical severity level."""
29+
30+
831
class AlarmTransitionType(str, Enum):
932
"""Specifies a type of alarm transition."""
1033

@@ -32,7 +55,7 @@ class AlarmTransition(JsonModel):
3255
"""The severity of the transition.
3356
3457
Valid values for CLEAR transitions are [-1, -1].
35-
Valid values for SET transitions are [1, infinity].
58+
Valid values for SET transitions are [1, 2147483647].
3659
Note that the SystemLink Alarm UI only has display strings for SET severities in the range [1, 4].
3760
"""
3861

@@ -63,22 +86,6 @@ class AlarmTransition(JsonModel):
6386
"""
6487

6588

66-
class AlarmNote(JsonModel):
67-
"""Information about a particular alarm instance.
68-
69-
Such as a description of the root cause of the alarm.
70-
"""
71-
72-
note: str
73-
"""Information about a particular alarm instance, such as a description of the root cause of the alarm."""
74-
75-
created_at: Optional[datetime] = None
76-
"""The date and time when the note was created."""
77-
78-
user: Optional[str] = None
79-
"""The userId of the person who created the note."""
80-
81-
8289
class Alarm(JsonModel):
8390
"""An individual instance, or occurrence, of an alarm.
8491
@@ -125,13 +132,13 @@ class Alarm(JsonModel):
125132
(active=False). This field is automatically reset to false when the alarm's highestSeverityLevel field increases.
126133
"""
127134

128-
acknowledged_at: Optional[datetime]
135+
acknowledged_at: datetime | None
129136
"""The date and time when the alarm instance was acknowledged.
130137
131138
This field will be cleared when the alarm's highestSeverityLevel field increases.
132139
"""
133140

134-
acknowledged_by: Optional[str]
141+
acknowledged_by: str | None
135142
"""The userId of the individual who acknowledged the alarm.
136143
137144
This field will be cleared when the alarm's highestSeverityLevel field increases.
@@ -176,13 +183,13 @@ class Alarm(JsonModel):
176183
highest_severity_level: int
177184
"""The highest severity level that the alarm has ever been in."""
178185

179-
most_recent_set_occurred_at: Optional[datetime]
186+
most_recent_set_occurred_at: datetime | None
180187
"""The date and time of the most recent occurrence of a SET transition.
181188
182189
This property only considers transitions that cause an alarm state change.
183190
"""
184191

185-
most_recent_transition_occurred_at: Optional[datetime]
192+
most_recent_transition_occurred_at: datetime | None
186193
"""The date and time of the most recent occurrence of a transition.
187194
188195
This property only considers transitions that cause an alarm state change.
@@ -206,10 +213,10 @@ class Alarm(JsonModel):
206213
Alarms can be tagged with keywords to make it easier to find them with queries.
207214
"""
208215

209-
notes: List[AlarmNote]
216+
notes: List[Any]
210217
"""A collection of notes for a given alarm instance.
211218
212-
Notes are set by humans to record information such as the root cause of the alarm.
219+
Notes are not currently supported and this will always be an empty list.
213220
"""
214221

215222
properties: Dict[str, str]

nisystemlink/clients/alarm/models/_alarm_instances_partial_success.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Optional
1+
from typing import List
22

33
from nisystemlink.clients.core import ApiError
44
from nisystemlink.clients.core._uplink._json_model import JsonModel
@@ -10,5 +10,5 @@ class AlarmInstancesPartialSuccess(JsonModel):
1010
failed: List[str]
1111
"""The instanceIds that failed the operation."""
1212

13-
error: Optional[ApiError] = None
13+
error: ApiError | None = None
1414
"""The error that occurred during the operation."""

0 commit comments

Comments
 (0)