Skip to main content

fakecloud_application_autoscaling/
state.rs

1//! In-memory state for Application Auto Scaling.
2
3use std::collections::BTreeMap;
4use std::sync::Arc;
5
6use chrono::{DateTime, Utc};
7use parking_lot::RwLock;
8use serde::{Deserialize, Serialize};
9
10pub type SharedApplicationAutoScalingState = Arc<RwLock<ApplicationAutoScalingAccounts>>;
11
12#[derive(Debug, Default, Serialize, Deserialize)]
13pub struct ApplicationAutoScalingAccounts {
14    pub accounts: BTreeMap<String, AccountState>,
15}
16
17impl ApplicationAutoScalingAccounts {
18    pub fn new() -> Self {
19        Self::default()
20    }
21}
22
23#[derive(Debug, Default, Serialize, Deserialize)]
24pub struct AccountState {
25    /// Keyed by (ServiceNamespace, ResourceId, ScalableDimension).
26    pub scalable_targets: BTreeMap<TargetKey, ScalableTarget>,
27    /// Keyed by (ServiceNamespace, ResourceId, ScalableDimension, PolicyName).
28    pub scaling_policies: BTreeMap<PolicyKey, ScalingPolicy>,
29    /// Keyed by (ServiceNamespace, ResourceId, ScalableDimension, ScheduledActionName).
30    pub scheduled_actions: BTreeMap<ScheduledKey, ScheduledAction>,
31    /// Scaling activities, newest first.
32    pub scaling_activities: Vec<ScalingActivity>,
33    /// Tags keyed by ARN.
34    pub tags: BTreeMap<String, BTreeMap<String, String>>,
35}
36
37pub type TargetKey = (String, String, String);
38pub type PolicyKey = (String, String, String, String);
39pub type ScheduledKey = (String, String, String, String);
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct ScalableTarget {
43    pub arn: String,
44    pub service_namespace: String,
45    pub resource_id: String,
46    pub scalable_dimension: String,
47    pub min_capacity: i32,
48    pub max_capacity: i32,
49    pub role_arn: String,
50    pub creation_time: DateTime<Utc>,
51    pub suspended_state: Option<SuspendedState>,
52    pub predicted_capacity: Option<i32>,
53}
54
55#[derive(Debug, Clone, Default, Serialize, Deserialize)]
56pub struct SuspendedState {
57    pub dynamic_scaling_in_suspended: Option<bool>,
58    pub dynamic_scaling_out_suspended: Option<bool>,
59    pub scheduled_scaling_suspended: Option<bool>,
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct ScalingPolicy {
64    pub arn: String,
65    pub policy_name: String,
66    pub service_namespace: String,
67    pub resource_id: String,
68    pub scalable_dimension: String,
69    pub policy_type: String,
70    pub creation_time: DateTime<Utc>,
71    pub step_scaling_policy_configuration: Option<serde_json::Value>,
72    pub target_tracking_scaling_policy_configuration: Option<serde_json::Value>,
73    pub predictive_scaling_policy_configuration: Option<serde_json::Value>,
74    pub alarms: Vec<Alarm>,
75    /// Last time the watcher applied a scale action through this policy.
76    /// Honours the policy's cooldown so we don't thrash capacity.
77    #[serde(default)]
78    pub last_applied_at: Option<DateTime<Utc>>,
79}
80
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct Alarm {
83    pub alarm_name: String,
84    pub alarm_arn: String,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct ScheduledAction {
89    pub arn: String,
90    pub scheduled_action_name: String,
91    pub service_namespace: String,
92    pub resource_id: String,
93    pub scalable_dimension: Option<String>,
94    pub schedule: String,
95    pub timezone: Option<String>,
96    pub start_time: Option<DateTime<Utc>>,
97    pub end_time: Option<DateTime<Utc>>,
98    pub scalable_target_action: Option<ScalableTargetAction>,
99    pub creation_time: DateTime<Utc>,
100    /// Last time the executor fired this scheduled action. Used to
101    /// dedupe within the same minute so a cron-every-minute schedule
102    /// doesn't double-fire when the ticker runs more than once during
103    /// the same wall-clock minute.
104    #[serde(default)]
105    pub last_fired_at: Option<DateTime<Utc>>,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct ScalableTargetAction {
110    pub min_capacity: Option<i32>,
111    pub max_capacity: Option<i32>,
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct ScalingActivity {
116    pub activity_id: String,
117    pub service_namespace: String,
118    pub resource_id: String,
119    pub scalable_dimension: String,
120    pub description: String,
121    pub cause: String,
122    pub start_time: DateTime<Utc>,
123    pub end_time: Option<DateTime<Utc>>,
124    pub status_code: String,
125    pub status_message: Option<String>,
126    pub details: Option<String>,
127    pub not_scaled_reasons: Vec<NotScaledReason>,
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub struct NotScaledReason {
132    pub code: String,
133    pub max_capacity: Option<i32>,
134    pub min_capacity: Option<i32>,
135    pub current_capacity: Option<i32>,
136}