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
use async_graphql::Object;

use crate::{
    event::{Metric, MetricValue},
    sources::host_metrics,
};

pub struct MemoryMetrics(Vec<Metric>);

#[Object]
/// Host memory metrics
impl MemoryMetrics {
    /// Total bytes
    async fn total_bytes(&self) -> f64 {
        filter_host_metric(&self.0, "memory_total_bytes")
    }

    /// Free bytes
    async fn free_bytes(&self) -> f64 {
        filter_host_metric(&self.0, "memory_free_bytes")
    }

    /// Available bytes
    async fn available_bytes(&self) -> f64 {
        filter_host_metric(&self.0, "memory_available_bytes")
    }

    /// Active bytes (Linux/macOS only)
    async fn active_bytes(&self) -> Option<f64> {
        if cfg!(any(target_os = "linux", target_os = "macos")) {
            Some(filter_host_metric(&self.0, "memory_active_bytes"))
        } else {
            None
        }
    }

    /// Buffers bytes (Linux only)
    async fn buffers_bytes(&self) -> Option<f64> {
        if cfg!(target_os = "linux") {
            Some(filter_host_metric(&self.0, "memory_buffers_bytes"))
        } else {
            None
        }
    }

    /// Cached bytes (Linux only)
    async fn cached_bytes(&self) -> Option<f64> {
        if cfg!(target_os = "linux") {
            Some(filter_host_metric(&self.0, "memory_cached_bytes"))
        } else {
            None
        }
    }

    /// Shared bytes (Linux only)
    async fn shared_bytes(&self) -> Option<f64> {
        if cfg!(target_os = "linux") {
            Some(filter_host_metric(&self.0, "memory_shared_bytes"))
        } else {
            None
        }
    }

    /// Used bytes (Linux only)
    async fn used_bytes(&self) -> Option<f64> {
        if cfg!(target_os = "linux") {
            Some(filter_host_metric(&self.0, "memory_used_bytes"))
        } else {
            None
        }
    }

    /// Inactive bytes (macOS only)
    async fn inactive_bytes(&self) -> Option<f64> {
        if cfg!(target_os = "macos") {
            Some(filter_host_metric(&self.0, "memory_inactive_bytes"))
        } else {
            None
        }
    }

    /// Wired bytes (macOS only)
    async fn wired_bytes(&self) -> Option<f64> {
        if cfg!(target_os = "macos") {
            Some(filter_host_metric(&self.0, "memory_wired_bytes"))
        } else {
            None
        }
    }
}

pub struct SwapMetrics(Vec<Metric>);

#[Object]
impl SwapMetrics {
    /// Swap free bytes
    async fn free_bytes(&self) -> f64 {
        filter_host_metric(&self.0, "memory_swap_free_bytes")
    }

    /// Swap total bytes
    async fn total_bytes(&self) -> f64 {
        filter_host_metric(&self.0, "memory_swap_total_bytes")
    }

    /// Swap used bytes
    async fn used_bytes(&self) -> f64 {
        filter_host_metric(&self.0, "memory_swap_used_bytes")
    }

    /// Swapped in bytes total (not available on Windows)
    async fn swapped_in_bytes_total(&self) -> Option<f64> {
        if cfg!(not(windows)) {
            Some(filter_host_metric(&self.0, "memory_swapped_in_bytes_total"))
        } else {
            None
        }
    }

    /// Swapped out bytes total (not available on Windows)
    async fn swapped_out_bytes_total(&self) -> Option<f64> {
        if cfg!(not(windows)) {
            Some(filter_host_metric(
                &self.0,
                "memory_swapped_out_bytes_total",
            ))
        } else {
            None
        }
    }
}

pub struct CpuMetrics(Vec<Metric>);

#[Object]
impl CpuMetrics {
    /// CPU seconds total
    async fn cpu_seconds_total(&self) -> f64 {
        filter_host_metric(&self.0, "cpu_seconds_total")
    }
}

pub struct LoadAverageMetrics(Vec<Metric>);

#[Object]
impl LoadAverageMetrics {
    /// Load 1 average
    async fn load1(&self) -> f64 {
        filter_host_metric(&self.0, "load1")
    }

    /// Load 5 average
    async fn load5(&self) -> f64 {
        filter_host_metric(&self.0, "load5")
    }

    /// Load 15 average
    async fn load15(&self) -> f64 {
        filter_host_metric(&self.0, "load15")
    }
}

pub struct NetworkMetrics(Vec<Metric>);

#[Object]
impl NetworkMetrics {
    /// Total bytes received
    async fn receive_bytes_total(&self) -> f64 {
        filter_host_metric(&self.0, "network_receive_bytes_total")
    }

    /// Total errors received
    async fn receive_errs_total(&self) -> f64 {
        filter_host_metric(&self.0, "network_receive_errs_total")
    }

    /// Total packets received
    async fn receive_packets_total(&self) -> f64 {
        filter_host_metric(&self.0, "network_receive_packets_total")
    }

    /// Total bytes transmitted
    async fn transmit_bytes_total(&self) -> f64 {
        filter_host_metric(&self.0, "network_transmit_bytes_total")
    }

    /// Total errors transmitted
    async fn transmit_errs_total(&self) -> f64 {
        filter_host_metric(&self.0, "network_transmit_errs_total")
    }

    /// Total transmission packets dropped (Linux/Windows only)
    async fn transmit_packets_drop_total(&self) -> Option<f64> {
        if cfg!(any(target_os = "linux", windows)) {
            Some(filter_host_metric(
                &self.0,
                "network_transmit_packets_drop_total",
            ))
        } else {
            None
        }
    }

    /// Total transmission packets (Linux/Windows only)
    async fn transmit_packets_total(&self) -> Option<f64> {
        if cfg!(any(target_os = "linux", windows)) {
            Some(filter_host_metric(
                &self.0,
                "network_transmit_packets_total",
            ))
        } else {
            None
        }
    }
}

pub struct FileSystemMetrics(Vec<Metric>);

#[Object]
impl FileSystemMetrics {
    /// Free bytes
    async fn free_bytes(&self) -> f64 {
        filter_host_metric(&self.0, "filesystem_free_bytes")
    }

    /// Total bytes
    async fn total_bytes(&self) -> f64 {
        filter_host_metric(&self.0, "filesystem_total_bytes")
    }

    /// Used bytes
    async fn used_bytes(&self) -> f64 {
        filter_host_metric(&self.0, "filesystem_used_bytes")
    }
}

pub struct DiskMetrics(Vec<Metric>);

#[Object]
impl DiskMetrics {
    /// Total bytes read
    async fn read_bytes_total(&self) -> f64 {
        filter_host_metric(&self.0, "disk_read_bytes_total")
    }

    /// Total reads completed
    async fn reads_completed_total(&self) -> f64 {
        filter_host_metric(&self.0, "disk_reads_completed_total")
    }

    /// Total bytes written
    async fn written_bytes_total(&self) -> f64 {
        filter_host_metric(&self.0, "disk_written_bytes_total")
    }

    /// Total writes completed
    async fn writes_completed_total(&self) -> f64 {
        filter_host_metric(&self.0, "disk_writes_completed_total")
    }
}

pub struct HostMetrics(host_metrics::HostMetrics);

impl HostMetrics {
    /// Primes the host metrics pump by passing through a new `HostMetrics`
    pub fn new() -> Self {
        let config = host_metrics::HostMetricsConfig::default();
        Self(host_metrics::HostMetrics::new(config))
    }
}

#[Object]
/// Vector host metrics
impl HostMetrics {
    /// Memory metrics
    async fn memory(&self) -> MemoryMetrics {
        let mut buffer = self.0.buffer();
        self.0.memory_metrics(&mut buffer).await;
        MemoryMetrics(buffer.metrics)
    }

    /// Swap metrics
    async fn swap(&self) -> SwapMetrics {
        let mut buffer = self.0.buffer();
        self.0.swap_metrics(&mut buffer).await;
        SwapMetrics(buffer.metrics)
    }

    /// CPU metrics
    async fn cpu(&self) -> CpuMetrics {
        let mut buffer = self.0.buffer();
        self.0.cpu_metrics(&mut buffer).await;
        CpuMetrics(buffer.metrics)
    }

    /// Load average metrics (*nix only)
    async fn load_average(&self) -> Option<LoadAverageMetrics> {
        if cfg!(unix) {
            let mut buffer = self.0.buffer();
            self.0.loadavg_metrics(&mut buffer).await;
            Some(LoadAverageMetrics(buffer.metrics))
        } else {
            None
        }
    }

    /// Network metrics
    async fn network(&self) -> NetworkMetrics {
        let mut buffer = self.0.buffer();
        self.0.network_metrics(&mut buffer).await;
        NetworkMetrics(buffer.metrics)
    }

    /// Filesystem metrics
    async fn filesystem(&self) -> FileSystemMetrics {
        let mut buffer = self.0.buffer();
        self.0.filesystem_metrics(&mut buffer).await;
        FileSystemMetrics(buffer.metrics)
    }

    /// Disk metrics
    async fn disk(&self) -> DiskMetrics {
        let mut buffer = self.0.buffer();
        self.0.disk_metrics(&mut buffer).await;
        DiskMetrics(buffer.metrics)
    }
}

/// Filters a [`Vec<Metric>`] by name, returning the inner `value` or 0.00 if not found
fn filter_host_metric(metrics: &[Metric], name: &str) -> f64 {
    metrics
        .iter()
        .find(|m| matches!(m.namespace(), Some(n) if n == "host") && m.name() == name)
        .map(|m| match m.value() {
            MetricValue::Gauge { value } => *value,
            MetricValue::Counter { value } => *value,
            _ => 0.00,
        })
        .unwrap_or_else(|| 0.00)
}