ggen-domain 3.2.0

Domain logic layer for ggen - pure business logic without CLI dependencies
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
//! Marketplace-MAPE-K Integration
//!
//! Bridges the marketplace domain with the MAPE-K autonomic control system,
//! enabling the marketplace to self-optimize through continuous monitoring,
//! analysis, planning, validation, and knowledge accumulation.

use super::guards::ValidationReceipt;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;

/// Marketplace observation - lightweight telemetry events
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MarketplaceObservation {
    /// Observation ID
    pub id: String,

    /// Type of observation
    pub obs_type: MarketplaceObservationType,

    /// Timestamp (milliseconds since epoch)
    pub timestamp: u64,

    /// Observation data
    pub data: serde_json::Value,

    /// Source component
    pub source: String,
}

/// Types of marketplace observations
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub enum MarketplaceObservationType {
    /// Validation receipt from guard execution
    Receipt,
    /// Performance metric
    Metric,
    /// Anomaly detection
    Anomaly,
}

/// Converts marketplace receipts to observations
pub struct ReceiptObserver;

impl ReceiptObserver {
    /// Convert validation receipt to observation
    pub fn receipt_to_observation(receipt: &ValidationReceipt) -> MarketplaceObservation {
        let now = get_timestamp();
        let critical_rate = if receipt.critical_total > 0 {
            (receipt.critical_passed as f64 / receipt.critical_total as f64) * 100.0
        } else {
            100.0
        };

        MarketplaceObservation {
            id: format!("receipt-{}-{}", receipt.package_id, now),
            obs_type: MarketplaceObservationType::Receipt,
            timestamp: now,
            data: serde_json::json!({
                "package_id": receipt.package_id,
                "version": receipt.version,
                "overall_score": receipt.overall_score,
                "production_ready": receipt.production_ready,
                "critical_rate": critical_rate,
                "guard_count": receipt.guard_results.len(),
                "validated_at": receipt.validated_at,
            }),
            source: "marketplace-guard-system".to_string(),
        }
    }

    /// Create performance metric observation
    pub fn create_metric_observation(
        component: &str, metric_name: &str, value: f64,
    ) -> MarketplaceObservation {
        let now = get_timestamp();

        MarketplaceObservation {
            id: format!("{}-{}-{}", component, metric_name, now),
            obs_type: MarketplaceObservationType::Metric,
            timestamp: now,
            data: serde_json::json!({
                "component": component,
                "metric": metric_name,
                "value": value,
            }),
            source: "marketplace-metrics".to_string(),
        }
    }

    /// Create anomaly observation
    pub fn create_anomaly_observation(
        component: &str, anomaly_type: &str,
    ) -> MarketplaceObservation {
        let now = get_timestamp();

        MarketplaceObservation {
            id: format!("{}-anomaly-{}", component, now),
            obs_type: MarketplaceObservationType::Anomaly,
            timestamp: now,
            data: serde_json::json!({
                "component": component,
                "anomaly_type": anomaly_type,
            }),
            source: "marketplace-anomaly-detector".to_string(),
        }
    }
}

/// Observation statistics
#[derive(Debug, Clone, Default)]
pub struct ObservationStats {
    /// Total observations ingested
    pub total_observations: usize,

    /// Observations by type
    pub by_type: std::collections::HashMap<String, usize>,

    /// Last observation timestamp
    pub last_observation_timestamp: u64,
}

/// Autonomous marketplace telemetry collector
pub struct AutonomicMarketplace {
    observations: Arc<Mutex<Vec<MarketplaceObservation>>>,
    stats: Arc<Mutex<ObservationStats>>,
    last_loop_timestamp: Arc<Mutex<u64>>,
}

impl AutonomicMarketplace {
    /// Create new autonomic marketplace
    pub fn new() -> Self {
        Self {
            observations: Arc::new(Mutex::new(Vec::new())),
            stats: Arc::new(Mutex::new(ObservationStats::default())),
            last_loop_timestamp: Arc::new(Mutex::new(0)),
        }
    }

    /// Ingest marketplace receipt into observation stream
    pub fn ingest_receipt(&self, receipt: &ValidationReceipt) {
        let obs = ReceiptObserver::receipt_to_observation(receipt);
        self.ingest_observation(obs);
    }

    /// Ingest performance metric
    pub fn ingest_metric(&self, component: &str, metric_name: &str, value: f64) {
        let obs = ReceiptObserver::create_metric_observation(component, metric_name, value);
        self.ingest_observation(obs);
    }

    /// Ingest anomaly detection
    pub fn ingest_anomaly(&self, component: &str, anomaly_type: &str) {
        let obs = ReceiptObserver::create_anomaly_observation(component, anomaly_type);
        self.ingest_observation(obs);
    }

    /// Internal: Record observation and update statistics
    fn ingest_observation(&self, obs: MarketplaceObservation) {
        let mut observations = self.observations.lock().unwrap();
        observations.push(obs.clone());

        let mut stats = self.stats.lock().unwrap();
        stats.total_observations += 1;
        stats.last_observation_timestamp = obs.timestamp;

        let type_name = format!("{:?}", obs.obs_type);
        *stats.by_type.entry(type_name).or_insert(0) += 1;
    }

    /// Get observations for processing by external MAPE-K system
    pub fn get_observations(&self, since_timestamp: u64) -> Vec<MarketplaceObservation> {
        let observations = self.observations.lock().unwrap();
        observations
            .iter()
            .filter(|o| o.timestamp > since_timestamp)
            .cloned()
            .collect()
    }

    /// Clear processed observations
    pub fn clear_observations(&self) {
        let mut observations = self.observations.lock().unwrap();
        observations.clear();
    }

    /// Get current observation statistics
    pub fn observation_stats(&self) -> ObservationStats {
        self.stats.lock().unwrap().clone()
    }

    /// Check health of marketplace
    pub fn health_check(&self) -> MarketplaceHealth {
        let stats = self.stats.lock().unwrap();

        let receipt_count = stats.by_type.get("Receipt").copied().unwrap_or(0);
        let metric_count = stats.by_type.get("Metric").copied().unwrap_or(0);
        let anomaly_count = stats.by_type.get("Anomaly").copied().unwrap_or(0);

        let status = if anomaly_count > 5 {
            AutonomicStatus::Degraded
        } else if anomaly_count > 0 {
            AutonomicStatus::Healthy
        } else {
            AutonomicStatus::Healthy
        };

        MarketplaceHealth {
            status,
            total_observations: stats.total_observations,
            receipt_count,
            metric_count,
            anomaly_count,
            last_observation_at: stats.last_observation_timestamp,
        }
    }

    /// Get last loop execution time
    pub fn last_loop_timestamp(&self) -> u64 {
        *self.last_loop_timestamp.lock().unwrap()
    }

    /// Record that autonomic loop was executed
    pub fn record_loop_execution(&self) {
        *self.last_loop_timestamp.lock().unwrap() = get_timestamp();
    }
}

/// Autonomic marketplace status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AutonomicStatus {
    /// System operating normally
    Healthy,

    /// System has issues but operational
    Degraded,

    /// System critical
    Critical,
}

/// Health check result for marketplace
#[derive(Debug, Clone)]
pub struct MarketplaceHealth {
    /// Overall autonomic status
    pub status: AutonomicStatus,

    /// Total observations processed
    pub total_observations: usize,

    /// Receipt observations
    pub receipt_count: usize,

    /// Metric observations
    pub metric_count: usize,

    /// Anomaly observations
    pub anomaly_count: usize,

    /// Last observation timestamp
    pub last_observation_at: u64,
}

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

/// Get current timestamp in milliseconds
fn get_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

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

    #[test]
    fn test_autonomic_marketplace_creation() {
        let autonomic = AutonomicMarketplace::new();
        let stats = autonomic.observation_stats();
        assert_eq!(stats.total_observations, 0);
    }

    #[test]
    fn test_receipt_to_observation() {
        let receipt = ValidationReceipt::new(
            "test-package".to_string(),
            "1.0.0".to_string(),
            "ggen-test".to_string(),
        );

        let obs = ReceiptObserver::receipt_to_observation(&receipt);
        assert_eq!(obs.source, "marketplace-guard-system");
        assert_eq!(obs.obs_type, MarketplaceObservationType::Receipt);
    }

    #[test]
    fn test_metric_observation() {
        let obs = ReceiptObserver::create_metric_observation("registry", "latency_ms", 45.5);
        assert_eq!(obs.obs_type, MarketplaceObservationType::Metric);
        assert_eq!(obs.source, "marketplace-metrics");
    }

    #[test]
    fn test_anomaly_observation() {
        let obs =
            ReceiptObserver::create_anomaly_observation("guard-validator", "high_failure_rate");
        assert_eq!(obs.obs_type, MarketplaceObservationType::Anomaly);
        assert_eq!(obs.source, "marketplace-anomaly-detector");
    }

    #[test]
    fn test_ingest_metric() {
        let autonomic = AutonomicMarketplace::new();
        autonomic.ingest_metric("registry", "latency_ms", 45.5);
        autonomic.ingest_metric("registry", "latency_ms", 52.3);
        autonomic.ingest_metric("validator", "latency_ms", 100.0);

        let stats = autonomic.observation_stats();
        assert_eq!(stats.total_observations, 3);
    }

    #[test]
    fn test_ingest_anomaly() {
        let autonomic = AutonomicMarketplace::new();
        autonomic.ingest_anomaly("guard-validator", "high_failure_rate");

        let stats = autonomic.observation_stats();
        assert_eq!(stats.total_observations, 1);
    }

    #[test]
    fn test_ingest_receipt() {
        let autonomic = AutonomicMarketplace::new();
        let receipt = ValidationReceipt::new(
            "pkg".to_string(),
            "1.0.0".to_string(),
            "ggen-test".to_string(),
        );

        autonomic.ingest_receipt(&receipt);

        let stats = autonomic.observation_stats();
        assert_eq!(stats.total_observations, 1);
    }

    #[test]
    fn test_get_observations() {
        let autonomic = AutonomicMarketplace::new();
        let now = get_timestamp();

        autonomic.ingest_metric("comp1", "metric1", 50.0);
        autonomic.ingest_metric("comp2", "metric2", 60.0);

        let obs = autonomic.get_observations(now - 1000);
        assert_eq!(obs.len(), 2);
    }

    #[test]
    fn test_clear_observations() {
        let autonomic = AutonomicMarketplace::new();
        autonomic.ingest_metric("comp1", "metric1", 50.0);

        let stats_before = autonomic.observation_stats();
        assert_eq!(stats_before.total_observations, 1);

        autonomic.clear_observations();

        let obs_after = autonomic.get_observations(0);
        assert_eq!(obs_after.len(), 0);
    }

    #[test]
    fn test_health_check() {
        let autonomic = AutonomicMarketplace::new();
        autonomic.ingest_metric("comp1", "metric1", 50.0);

        let health = autonomic.health_check();
        assert_eq!(health.status, AutonomicStatus::Healthy);
        assert_eq!(health.total_observations, 1);
        assert_eq!(health.metric_count, 1);
    }

    #[test]
    fn test_health_check_degraded() {
        let autonomic = AutonomicMarketplace::new();
        for i in 0..10 {
            autonomic.ingest_anomaly(&format!("comp{}", i), "issue");
        }

        let health = autonomic.health_check();
        assert_eq!(health.status, AutonomicStatus::Degraded);
        assert!(health.anomaly_count > 5);
    }

    #[test]
    fn test_record_loop_execution() {
        let autonomic = AutonomicMarketplace::new();
        assert_eq!(autonomic.last_loop_timestamp(), 0);

        autonomic.record_loop_execution();
        let timestamp = autonomic.last_loop_timestamp();
        assert!(timestamp > 0);
    }

    #[test]
    fn test_observation_stats_by_type() {
        let autonomic = AutonomicMarketplace::new();
        autonomic.ingest_metric("comp", "m", 1.0);
        autonomic.ingest_metric("comp", "m", 2.0);
        autonomic.ingest_anomaly("comp", "anom");

        let stats = autonomic.observation_stats();
        assert_eq!(stats.by_type.get("Metric").copied(), Some(2));
        assert_eq!(stats.by_type.get("Anomaly").copied(), Some(1));
    }
}