Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 532d755

Browse files
authored
Improve annotation of the CloudWatch store (#13511)
1 parent cf08694 commit 532d755

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

localstack-core/localstack/services/cloudwatch/models.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import datetime
22

3-
from localstack.aws.api.cloudwatch import CompositeAlarm, DashboardBody, MetricAlarm, StateValue
3+
from localstack.aws.api.cloudwatch import (
4+
AlarmHistoryItem,
5+
CompositeAlarm,
6+
DashboardBody,
7+
MetricAlarm,
8+
StateValue,
9+
)
410
from localstack.services.stores import (
511
AccountRegionBundle,
612
BaseStore,
@@ -72,6 +78,8 @@ class LocalStackDashboard:
7278
dashboard_name: str
7379
dashboard_arn: str
7480
dashboard_body: DashboardBody
81+
last_modified: datetime.datetime
82+
size: int
7583

7684
def __init__(
7785
self, account_id: str, region: str, dashboard_name: str, dashboard_body: DashboardBody
@@ -99,7 +107,7 @@ class CloudWatchStore(BaseStore):
99107

100108
# Contains all the Alarm Histories. Per documentation, an alarm history is retained even if the alarm is deleted,
101109
# making it necessary to save this at store level
102-
histories: list[dict] = LocalAttribute(default=list)
110+
histories: list[dict[str, AlarmHistoryItem]] = LocalAttribute(default=list)
103111

104112
dashboards: dict[str, LocalStackDashboard] = LocalAttribute(default=dict)
105113

localstack-core/localstack/services/cloudwatch/provider_v2.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from localstack.aws.api.cloudwatch import (
1010
AccountId,
1111
ActionPrefix,
12+
AlarmHistoryItem,
1213
AlarmName,
1314
AlarmNamePrefix,
1415
AlarmNames,
@@ -760,7 +761,7 @@ def _update_state(
760761
self,
761762
context: RequestContext,
762763
alarm: LocalStackAlarm,
763-
state_value: str,
764+
state_value: StateValue,
764765
state_reason: str,
765766
state_reason_data: dict = None,
766767
):
@@ -780,18 +781,17 @@ def _update_state(
780781
"stateReasonData": state_reason_data,
781782
},
782783
}
783-
store.histories.append(
784-
{
785-
"Timestamp": timestamp_millis(alarm.alarm["StateUpdatedTimestamp"]),
786-
"HistoryItemType": HistoryItemType.StateUpdate,
787-
"AlarmName": alarm.alarm["AlarmName"],
788-
"HistoryData": json.dumps(history_data),
789-
"HistorySummary": f"Alarm updated from {old_state} to {state_value}",
790-
"AlarmType": "MetricAlarm"
791-
if isinstance(alarm, LocalStackMetricAlarm)
792-
else "CompositeAlarm",
793-
}
784+
alarm_history_item = AlarmHistoryItem(
785+
Timestamp=alarm.alarm["StateUpdatedTimestamp"],
786+
HistoryItemType=HistoryItemType.StateUpdate,
787+
AlarmName=alarm.alarm["AlarmName"],
788+
HistoryData=json.dumps(history_data),
789+
HistorySummary=f"Alarm updated from {old_state} to {state_value}",
790+
AlarmType="MetricAlarm"
791+
if isinstance(alarm, LocalStackMetricAlarm)
792+
else "CompositeAlarm",
794793
)
794+
store.histories.append(alarm_history_item)
795795
alarm.alarm["StateValue"] = state_value
796796
alarm.alarm["StateReason"] = state_reason
797797
if state_reason_data:
@@ -837,15 +837,10 @@ def describe_alarm_history(
837837
if alarm_name:
838838
history = [h for h in history if h["AlarmName"] == alarm_name]
839839

840-
def _get_timestamp(input: dict):
841-
if timestamp_string := input.get("Timestamp"):
842-
return datetime.datetime.fromisoformat(timestamp_string)
843-
return None
844-
845840
if start_date:
846-
history = [h for h in history if (date := _get_timestamp(h)) and date >= start_date]
841+
history = [h for h in history if (date := h.get("Timestamp")) and date >= start_date]
847842
if end_date:
848-
history = [h for h in history if (date := _get_timestamp(h)) and date <= end_date]
843+
history = [h for h in history if (date := h.get("Timestamp")) and date <= end_date]
849844
return DescribeAlarmHistoryOutput(AlarmHistoryItems=history)
850845

851846
def _evaluate_composite_alarms(self, context: RequestContext, triggering_alarm):

0 commit comments

Comments
 (0)