alice-edge 0.1.0

Embedded Model Generator - Don't send data, send the law
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! テレメトリ / 監視ハブ
//!
//! 複数エッジデバイスのメトリクス集約と健全性監視。

use std::collections::HashMap;

/// テレメトリ設定。
#[derive(Debug, Clone)]
pub struct TelemetryConfig {
    /// デバイス ID。
    pub device_id: String,
    /// 報告間隔 (秒)。
    pub report_interval_secs: u64,
    /// イベントバッファサイズ。
    pub buffer_size: usize,
    /// ハートビート間隔 (秒)。
    pub heartbeat_interval_secs: u64,
}

impl Default for TelemetryConfig {
    fn default() -> Self {
        Self {
            device_id: String::new(),
            report_interval_secs: 60,
            buffer_size: 1000,
            heartbeat_interval_secs: 30,
        }
    }
}

/// デバイスメトリクス。
#[derive(Debug, Clone)]
pub struct DeviceMetrics {
    /// CPU 使用率 (0.0-100.0)。
    pub cpu_usage: f32,
    /// メモリ使用率 (0.0-100.0)。
    pub memory_usage: f32,
    /// 温度 (摂氏)。
    pub temperature: f32,
    /// 稼働時間 (秒)。
    pub uptime_secs: u64,
    /// ディスク使用率 (0.0-100.0)。
    pub disk_usage: f32,
    /// ネットワーク送信バイト数。
    pub network_tx_bytes: u64,
    /// ネットワーク受信バイト数。
    pub network_rx_bytes: u64,
}

impl Default for DeviceMetrics {
    fn default() -> Self {
        Self {
            cpu_usage: 0.0,
            memory_usage: 0.0,
            temperature: 0.0,
            uptime_secs: 0,
            disk_usage: 0.0,
            network_tx_bytes: 0,
            network_rx_bytes: 0,
        }
    }
}

/// テレメトリイベント。
#[derive(Debug, Clone)]
pub enum TelemetryEvent {
    /// ハートビート。
    Heartbeat {
        /// デバイス ID。
        device_id: String,
        /// タイムスタンプ (Unix 秒)。
        timestamp: u64,
    },
    /// アラート。
    Alert {
        /// デバイス ID。
        device_id: String,
        /// 重大度。
        severity: AlertSeverity,
        /// メッセージ。
        message: String,
        /// タイムスタンプ (Unix 秒)。
        timestamp: u64,
    },
    /// メトリクス報告。
    Metric {
        /// デバイス ID。
        device_id: String,
        /// メトリクスデータ。
        metrics: DeviceMetrics,
        /// タイムスタンプ (Unix 秒)。
        timestamp: u64,
    },
}

/// アラート重大度。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlertSeverity {
    /// 情報。
    Info,
    /// 警告。
    Warning,
    /// 危険。
    Critical,
}

impl core::fmt::Display for AlertSeverity {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Info => write!(f, "Info"),
            Self::Warning => write!(f, "Warning"),
            Self::Critical => write!(f, "Critical"),
        }
    }
}

/// 健全性ステータス。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HealthStatus {
    /// 正常。
    Healthy,
    /// 劣化。
    Degraded,
    /// 危険。
    Critical,
    /// 不明。
    Unknown,
}

impl core::fmt::Display for HealthStatus {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Healthy => write!(f, "Healthy"),
            Self::Degraded => write!(f, "Degraded"),
            Self::Critical => write!(f, "Critical"),
            Self::Unknown => write!(f, "Unknown"),
        }
    }
}

/// デバイス状態。
#[derive(Debug, Clone)]
struct DeviceState {
    /// 最終ハートビート (Unix 秒)。
    last_heartbeat: u64,
    /// 最新メトリクス。
    metrics: Option<DeviceMetrics>,
    /// 健全性。
    health: HealthStatus,
    /// アラート数。
    alert_count: u64,
}

/// テレメトリハブ (複数デバイス管理)。
#[derive(Debug)]
pub struct TelemetryHub {
    /// デバイス ID → 状態。
    devices: HashMap<String, DeviceState>,
    /// イベントバッファ。
    events: Vec<TelemetryEvent>,
    /// バッファ最大サイズ。
    buffer_size: usize,
    /// ハートビートタイムアウト (秒)。
    heartbeat_timeout_secs: u64,
}

impl Default for TelemetryHub {
    fn default() -> Self {
        Self::new(1000, 120)
    }
}

impl TelemetryHub {
    /// 新しいテレメトリハブを作成。
    #[must_use]
    pub fn new(buffer_size: usize, heartbeat_timeout_secs: u64) -> Self {
        Self {
            devices: HashMap::new(),
            events: Vec::with_capacity(buffer_size.min(1024)),
            buffer_size,
            heartbeat_timeout_secs,
        }
    }

    /// イベントを記録。
    pub fn record(&mut self, event: TelemetryEvent) {
        match &event {
            TelemetryEvent::Heartbeat {
                device_id,
                timestamp,
            } => {
                let state = self
                    .devices
                    .entry(device_id.clone())
                    .or_insert(DeviceState {
                        last_heartbeat: 0,
                        metrics: None,
                        health: HealthStatus::Unknown,
                        alert_count: 0,
                    });
                state.last_heartbeat = *timestamp;
                state.health = HealthStatus::Healthy;
            }
            TelemetryEvent::Alert {
                device_id,
                severity,
                ..
            } => {
                let state = self
                    .devices
                    .entry(device_id.clone())
                    .or_insert(DeviceState {
                        last_heartbeat: 0,
                        metrics: None,
                        health: HealthStatus::Unknown,
                        alert_count: 0,
                    });
                state.alert_count += 1;
                if *severity == AlertSeverity::Critical {
                    state.health = HealthStatus::Critical;
                } else if state.health != HealthStatus::Critical {
                    state.health = HealthStatus::Degraded;
                }
            }
            TelemetryEvent::Metric {
                device_id,
                metrics,
                timestamp,
            } => {
                let state = self
                    .devices
                    .entry(device_id.clone())
                    .or_insert(DeviceState {
                        last_heartbeat: 0,
                        metrics: None,
                        health: HealthStatus::Unknown,
                        alert_count: 0,
                    });
                state.metrics = Some(metrics.clone());
                state.last_heartbeat = *timestamp;
                // 温度に基づく健全性判定
                state.health = if metrics.temperature > 85.0 {
                    HealthStatus::Critical
                } else if metrics.temperature > 70.0 || metrics.cpu_usage > 90.0 {
                    HealthStatus::Degraded
                } else {
                    HealthStatus::Healthy
                };
            }
        }

        if self.events.len() < self.buffer_size {
            self.events.push(event);
        }
    }

    /// バッファをフラッシュし、イベントを返す。
    pub fn flush(&mut self) -> Vec<TelemetryEvent> {
        core::mem::take(&mut self.events)
    }

    /// デバイスの健全性を取得。
    #[must_use]
    pub fn device_health(&self, device_id: &str) -> HealthStatus {
        self.devices
            .get(device_id)
            .map_or(HealthStatus::Unknown, |s| s.health)
    }

    /// デバイスのメトリクスを取得。
    #[must_use]
    pub fn device_metrics(&self, device_id: &str) -> Option<&DeviceMetrics> {
        self.devices.get(device_id).and_then(|s| s.metrics.as_ref())
    }

    /// 登録デバイス数。
    #[must_use]
    pub fn device_count(&self) -> usize {
        self.devices.len()
    }

    /// タイムアウトしたデバイスを検出。
    #[must_use]
    pub fn timed_out_devices(&self, now_unix: u64) -> Vec<String> {
        self.devices
            .iter()
            .filter(|(_, s)| {
                s.last_heartbeat > 0
                    && now_unix.saturating_sub(s.last_heartbeat) > self.heartbeat_timeout_secs
            })
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// バッファ内イベント数。
    #[must_use]
    pub const fn event_count(&self) -> usize {
        self.events.len()
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_config() {
        let config = TelemetryConfig::default();
        assert_eq!(config.report_interval_secs, 60);
        assert_eq!(config.buffer_size, 1000);
    }

    #[test]
    fn default_metrics() {
        let m = DeviceMetrics::default();
        assert!(m.cpu_usage.abs() < f32::EPSILON);
        assert_eq!(m.uptime_secs, 0);
    }

    #[test]
    fn alert_severity_display() {
        assert_eq!(AlertSeverity::Info.to_string(), "Info");
        assert_eq!(AlertSeverity::Warning.to_string(), "Warning");
        assert_eq!(AlertSeverity::Critical.to_string(), "Critical");
    }

    #[test]
    fn health_status_display() {
        assert_eq!(HealthStatus::Healthy.to_string(), "Healthy");
        assert_eq!(HealthStatus::Degraded.to_string(), "Degraded");
        assert_eq!(HealthStatus::Critical.to_string(), "Critical");
        assert_eq!(HealthStatus::Unknown.to_string(), "Unknown");
    }

    #[test]
    fn hub_record_heartbeat() {
        let mut hub = TelemetryHub::new(100, 60);
        hub.record(TelemetryEvent::Heartbeat {
            device_id: "dev1".into(),
            timestamp: 1000,
        });
        assert_eq!(hub.device_count(), 1);
        assert_eq!(hub.device_health("dev1"), HealthStatus::Healthy);
    }

    #[test]
    fn hub_record_metric() {
        let mut hub = TelemetryHub::new(100, 60);
        hub.record(TelemetryEvent::Metric {
            device_id: "dev1".into(),
            metrics: DeviceMetrics {
                cpu_usage: 50.0,
                temperature: 45.0,
                ..DeviceMetrics::default()
            },
            timestamp: 1000,
        });
        assert_eq!(hub.device_health("dev1"), HealthStatus::Healthy);
        assert!(hub.device_metrics("dev1").is_some());
    }

    #[test]
    fn hub_high_temperature_degraded() {
        let mut hub = TelemetryHub::new(100, 60);
        hub.record(TelemetryEvent::Metric {
            device_id: "hot".into(),
            metrics: DeviceMetrics {
                temperature: 75.0,
                ..DeviceMetrics::default()
            },
            timestamp: 1000,
        });
        assert_eq!(hub.device_health("hot"), HealthStatus::Degraded);
    }

    #[test]
    fn hub_critical_temperature() {
        let mut hub = TelemetryHub::new(100, 60);
        hub.record(TelemetryEvent::Metric {
            device_id: "burn".into(),
            metrics: DeviceMetrics {
                temperature: 90.0,
                ..DeviceMetrics::default()
            },
            timestamp: 1000,
        });
        assert_eq!(hub.device_health("burn"), HealthStatus::Critical);
    }

    #[test]
    fn hub_alert_critical() {
        let mut hub = TelemetryHub::new(100, 60);
        hub.record(TelemetryEvent::Alert {
            device_id: "dev1".into(),
            severity: AlertSeverity::Critical,
            message: "Overheating".into(),
            timestamp: 1000,
        });
        assert_eq!(hub.device_health("dev1"), HealthStatus::Critical);
    }

    #[test]
    fn hub_flush() {
        let mut hub = TelemetryHub::new(100, 60);
        hub.record(TelemetryEvent::Heartbeat {
            device_id: "dev1".into(),
            timestamp: 1000,
        });
        assert_eq!(hub.event_count(), 1);
        let events = hub.flush();
        assert_eq!(events.len(), 1);
        assert_eq!(hub.event_count(), 0);
    }

    #[test]
    fn hub_timed_out() {
        let mut hub = TelemetryHub::new(100, 60);
        hub.record(TelemetryEvent::Heartbeat {
            device_id: "old".into(),
            timestamp: 100,
        });
        hub.record(TelemetryEvent::Heartbeat {
            device_id: "new".into(),
            timestamp: 500,
        });
        let timed_out = hub.timed_out_devices(500);
        assert!(timed_out.contains(&"old".to_string()));
        assert!(!timed_out.contains(&"new".to_string()));
    }

    #[test]
    fn hub_unknown_device() {
        let hub = TelemetryHub::new(100, 60);
        assert_eq!(hub.device_health("nonexistent"), HealthStatus::Unknown);
        assert!(hub.device_metrics("nonexistent").is_none());
    }

    #[test]
    fn hub_default() {
        let hub = TelemetryHub::default();
        assert_eq!(hub.device_count(), 0);
    }
}