issun 0.10.0

A mini game engine for logic-focused games - Build games in ISSUN (一寸) of time
Documentation
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
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Metrics registry and aggregation logic

use crate::resources::Resource;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};

use super::types::*;

/// Configuration for metrics system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsConfig {
    /// Maximum number of values to keep per metric (for windowed aggregation)
    pub max_values_per_metric: usize,

    /// Enable automatic periodic snapshots
    pub enable_periodic_snapshots: bool,

    /// Snapshot period (in game time units, e.g., days)
    pub snapshot_period: u64,

    /// Enable automatic reporting
    pub enable_auto_report: bool,

    /// Report period (in game time units)
    pub report_period: u64,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            max_values_per_metric: 1000,
            enable_periodic_snapshots: false,
            snapshot_period: 1, // Daily
            enable_auto_report: false,
            report_period: 7, // Weekly
        }
    }
}

impl Resource for MetricsConfig {}

/// Registry of all metrics in the game
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsRegistry {
    /// Metric definitions
    definitions: HashMap<MetricId, MetricDefinition>,

    /// Recent values for each metric (windowed)
    values: HashMap<MetricId, VecDeque<MetricValue>>,

    /// Configuration
    config: MetricsConfig,

    /// Last snapshot timestamp
    last_snapshot_time: u64,

    /// Last report timestamp
    last_report_time: u64,
}

impl Default for MetricsRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl MetricsRegistry {
    /// Create a new metrics registry
    pub fn new() -> Self {
        Self {
            definitions: HashMap::new(),
            values: HashMap::new(),
            config: MetricsConfig::default(),
            last_snapshot_time: 0,
            last_report_time: 0,
        }
    }

    /// Create a registry with custom config
    pub fn with_config(config: MetricsConfig) -> Self {
        Self {
            definitions: HashMap::new(),
            values: HashMap::new(),
            config,
            last_snapshot_time: 0,
            last_report_time: 0,
        }
    }

    /// Get the current configuration
    pub fn config(&self) -> &MetricsConfig {
        &self.config
    }

    /// Set the configuration
    pub fn set_config(&mut self, config: MetricsConfig) {
        self.config = config;
    }

    /// Define a new metric
    pub fn define(&mut self, definition: MetricDefinition) {
        self.definitions.insert(definition.id.clone(), definition);
    }

    /// Record a metric value
    pub fn record(&mut self, value: MetricValue) -> Result<(), String> {
        // Check if metric is defined
        if !self.definitions.contains_key(&value.metric_id) {
            return Err(format!("Metric {} not defined", value.metric_id.as_str()));
        }

        let values = self.values.entry(value.metric_id.clone()).or_default();
        values.push_back(value);

        // Enforce window size
        while values.len() > self.config.max_values_per_metric {
            values.pop_front();
        }

        Ok(())
    }

    /// Remove a metric and all its values
    pub fn remove(&mut self, metric_id: &MetricId) {
        self.definitions.remove(metric_id);
        self.values.remove(metric_id);
    }

    /// Clear all metrics and values
    pub fn clear(&mut self) {
        self.definitions.clear();
        self.values.clear();
    }

    /// Get all values for all metrics (for snapshot creation)
    pub fn all_values(&self) -> impl Iterator<Item = (&MetricId, &VecDeque<MetricValue>)> {
        self.values.iter()
    }

    /// Get aggregated metric for a specific period
    pub fn aggregate(
        &self,
        metric_id: &MetricId,
        aggregation: AggregationType,
        period_start: u64,
        period_end: u64,
    ) -> Option<AggregatedMetric> {
        let values = self.values.get(metric_id)?;

        // Filter values in period
        let filtered: Vec<&MetricValue> = values
            .iter()
            .filter(|v| v.timestamp >= period_start && v.timestamp <= period_end)
            .collect();

        if filtered.is_empty() {
            return None;
        }

        let result = match aggregation {
            AggregationType::Sum => filtered.iter().map(|v| v.value).sum(),
            AggregationType::Count => filtered.len() as f64,
            AggregationType::Average => {
                filtered.iter().map(|v| v.value).sum::<f64>() / filtered.len() as f64
            }
            AggregationType::Min => filtered
                .iter()
                .map(|v| v.value)
                .fold(f64::INFINITY, f64::min),
            AggregationType::Max => filtered
                .iter()
                .map(|v| v.value)
                .fold(f64::NEG_INFINITY, f64::max),
            AggregationType::Last => filtered.last().map(|v| v.value).unwrap_or(0.0),
            AggregationType::P50 => calculate_percentile(&filtered, 0.5),
            AggregationType::P95 => calculate_percentile(&filtered, 0.95),
            AggregationType::P99 => calculate_percentile(&filtered, 0.99),
            AggregationType::Rate => {
                let duration = period_end.saturating_sub(period_start).max(1);
                filtered.iter().map(|v| v.value).sum::<f64>() / duration as f64
            }
        };

        Some(AggregatedMetric::new(
            metric_id.clone(),
            aggregation,
            result,
            filtered.len(),
            period_start,
            period_end,
        ))
    }

    /// Get all values for a metric (within window)
    pub fn get_values(&self, metric_id: &MetricId) -> Option<&VecDeque<MetricValue>> {
        self.values.get(metric_id)
    }

    /// Get metric definition
    pub fn get_definition(&self, metric_id: &MetricId) -> Option<&MetricDefinition> {
        self.definitions.get(metric_id)
    }

    /// List all defined metrics
    pub fn list_metrics(&self) -> Vec<&MetricDefinition> {
        self.definitions.values().collect()
    }

    /// Get last snapshot time
    pub fn last_snapshot_time(&self) -> u64 {
        self.last_snapshot_time
    }

    /// Set last snapshot time
    pub fn set_last_snapshot_time(&mut self, time: u64) {
        self.last_snapshot_time = time;
    }

    /// Get last report time
    pub fn last_report_time(&self) -> u64 {
        self.last_report_time
    }

    /// Set last report time
    pub fn set_last_report_time(&mut self, time: u64) {
        self.last_report_time = time;
    }

    /// Check if snapshot is due
    pub fn is_snapshot_due(&self, current_time: u64) -> bool {
        if !self.config.enable_periodic_snapshots {
            return false;
        }
        current_time >= self.last_snapshot_time + self.config.snapshot_period
    }

    /// Check if report is due
    pub fn is_report_due(&self, current_time: u64) -> bool {
        if !self.config.enable_auto_report {
            return false;
        }
        current_time >= self.last_report_time + self.config.report_period
    }
}

/// Helper function to calculate percentile
fn calculate_percentile(values: &[&MetricValue], percentile: f64) -> f64 {
    if values.is_empty() {
        return 0.0;
    }

    let mut sorted: Vec<f64> = values.iter().map(|v| v.value).collect();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let index = ((sorted.len() as f64 - 1.0) * percentile) as usize;
    sorted[index]
}

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

    #[test]
    fn test_registry_creation() {
        let registry = MetricsRegistry::new();
        assert_eq!(registry.config.max_values_per_metric, 1000);
    }

    #[test]
    fn test_define_metric() {
        let mut registry = MetricsRegistry::new();
        let definition =
            MetricDefinition::new("test", "Test", "Test metric", MetricType::Counter, "count");

        registry.define(definition);
        assert!(registry.get_definition(&MetricId::new("test")).is_some());
    }

    #[test]
    fn test_record_metric() {
        let mut registry = MetricsRegistry::new();
        let definition =
            MetricDefinition::new("test", "Test", "Test metric", MetricType::Counter, "count");
        registry.define(definition);

        let value = MetricValue::new(MetricId::new("test"), 42.0, 100);
        registry.record(value).unwrap();

        let values = registry.get_values(&MetricId::new("test")).unwrap();
        assert_eq!(values.len(), 1);
        assert_eq!(values[0].value, 42.0);
    }

    #[test]
    fn test_aggregate_sum() {
        let mut registry = MetricsRegistry::new();
        let definition =
            MetricDefinition::new("test", "Test", "Test metric", MetricType::Counter, "count");
        registry.define(definition);

        registry
            .record(MetricValue::new(MetricId::new("test"), 10.0, 1))
            .unwrap();
        registry
            .record(MetricValue::new(MetricId::new("test"), 20.0, 2))
            .unwrap();
        registry
            .record(MetricValue::new(MetricId::new("test"), 30.0, 3))
            .unwrap();

        let agg = registry
            .aggregate(&MetricId::new("test"), AggregationType::Sum, 0, 10)
            .unwrap();
        assert_eq!(agg.value, 60.0);
        assert_eq!(agg.count, 3);
    }

    #[test]
    fn test_aggregate_average() {
        let mut registry = MetricsRegistry::new();
        let definition =
            MetricDefinition::new("test", "Test", "Test metric", MetricType::Counter, "count");
        registry.define(definition);

        registry
            .record(MetricValue::new(MetricId::new("test"), 10.0, 1))
            .unwrap();
        registry
            .record(MetricValue::new(MetricId::new("test"), 20.0, 2))
            .unwrap();
        registry
            .record(MetricValue::new(MetricId::new("test"), 30.0, 3))
            .unwrap();

        let agg = registry
            .aggregate(&MetricId::new("test"), AggregationType::Average, 0, 10)
            .unwrap();
        assert_eq!(agg.value, 20.0);
    }

    #[test]
    fn test_aggregate_min_max() {
        let mut registry = MetricsRegistry::new();
        let definition =
            MetricDefinition::new("test", "Test", "Test metric", MetricType::Counter, "count");
        registry.define(definition);

        registry
            .record(MetricValue::new(MetricId::new("test"), 10.0, 1))
            .unwrap();
        registry
            .record(MetricValue::new(MetricId::new("test"), 50.0, 2))
            .unwrap();
        registry
            .record(MetricValue::new(MetricId::new("test"), 30.0, 3))
            .unwrap();

        let min = registry
            .aggregate(&MetricId::new("test"), AggregationType::Min, 0, 10)
            .unwrap();
        assert_eq!(min.value, 10.0);

        let max = registry
            .aggregate(&MetricId::new("test"), AggregationType::Max, 0, 10)
            .unwrap();
        assert_eq!(max.value, 50.0);
    }

    #[test]
    fn test_aggregate_percentile() {
        let mut registry = MetricsRegistry::new();
        let definition = MetricDefinition::new(
            "test",
            "Test",
            "Test metric",
            MetricType::Histogram,
            "value",
        );
        registry.define(definition);

        for i in 1..=100 {
            registry
                .record(MetricValue::new(MetricId::new("test"), i as f64, i))
                .unwrap();
        }

        let p50 = registry
            .aggregate(&MetricId::new("test"), AggregationType::P50, 0, 200)
            .unwrap();
        assert!((p50.value - 50.0).abs() < 1.0);

        let p95 = registry
            .aggregate(&MetricId::new("test"), AggregationType::P95, 0, 200)
            .unwrap();
        assert!((p95.value - 95.0).abs() < 1.0);
    }

    #[test]
    fn test_windowed_aggregation() {
        let mut registry = MetricsRegistry::with_config(MetricsConfig {
            max_values_per_metric: 5,
            ..Default::default()
        });
        let definition =
            MetricDefinition::new("test", "Test", "Test metric", MetricType::Counter, "count");
        registry.define(definition);

        // Record 10 values
        for i in 1..=10 {
            registry
                .record(MetricValue::new(MetricId::new("test"), i as f64, i))
                .unwrap();
        }

        // Should only keep last 5
        let values = registry.get_values(&MetricId::new("test")).unwrap();
        assert_eq!(values.len(), 5);
        assert_eq!(values[0].value, 6.0); // First value should be 6
        assert_eq!(values[4].value, 10.0); // Last value should be 10
    }

    #[test]
    fn test_period_filtering() {
        let mut registry = MetricsRegistry::new();
        let definition =
            MetricDefinition::new("test", "Test", "Test metric", MetricType::Counter, "count");
        registry.define(definition);

        registry
            .record(MetricValue::new(MetricId::new("test"), 10.0, 1))
            .unwrap();
        registry
            .record(MetricValue::new(MetricId::new("test"), 20.0, 5))
            .unwrap();
        registry
            .record(MetricValue::new(MetricId::new("test"), 30.0, 10))
            .unwrap();

        // Only include values from timestamp 3 to 7
        let agg = registry
            .aggregate(&MetricId::new("test"), AggregationType::Sum, 3, 7)
            .unwrap();
        assert_eq!(agg.value, 20.0); // Only the middle value
        assert_eq!(agg.count, 1);
    }

    #[test]
    fn test_is_snapshot_due() {
        let mut registry = MetricsRegistry::with_config(MetricsConfig {
            enable_periodic_snapshots: true,
            snapshot_period: 10,
            ..Default::default()
        });

        assert!(registry.is_snapshot_due(10));
        registry.set_last_snapshot_time(10);
        assert!(!registry.is_snapshot_due(15));
        assert!(registry.is_snapshot_due(20));
    }
}