oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
//! Advanced OLAP/Analytics Engine for Time-Series Optimization
//!
//! This module provides comprehensive OLAP (Online Analytical Processing) and analytics
//! capabilities specifically optimized for time-series data and semantic graph analysis.
//! It includes support for:
//!
//! - Multi-dimensional time-series analysis
//! - Columnar storage optimization for analytical queries
//! - Statistical functions and aggregations
//! - Time-windowed analytics and sliding window computations
//! - Real-time streaming analytics
//! - Graph analytics and centrality algorithms
//! - Pattern recognition and anomaly detection
//! - Predictive analytics using machine learning

#[allow(unused_imports)] // FusekiError is used in linear_regression_forecast
use crate::error::{FusekiError, FusekiResult};
use crate::store::Store;
use chrono::{DateTime, Duration, Timelike, Utc};
use serde::{Deserialize, Serialize};
use std::{
    collections::{HashMap, VecDeque},
    sync::Arc,
};
use tokio::sync::{Mutex, RwLock};
use tracing::{debug, info, instrument};

/// Time-series analytics configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalyticsConfig {
    /// Enable columnar storage optimization
    pub columnar_storage: bool,
    /// Maximum time window for analytics (in hours)
    pub max_time_window_hours: u32,
    /// Default aggregation window size (in seconds)
    pub default_window_size_seconds: u32,
    /// Enable real-time streaming analytics
    pub streaming_enabled: bool,
    /// Maximum number of concurrent analytics queries
    pub max_concurrent_queries: usize,
    /// Enable advanced statistical functions
    pub advanced_stats_enabled: bool,
    /// Enable machine learning predictions
    pub ml_predictions_enabled: bool,
}

impl Default for AnalyticsConfig {
    fn default() -> Self {
        Self {
            columnar_storage: true,
            max_time_window_hours: 24 * 30,   // 30 days
            default_window_size_seconds: 300, // 5 minutes
            streaming_enabled: true,
            max_concurrent_queries: 10,
            advanced_stats_enabled: true,
            ml_predictions_enabled: true,
        }
    }
}

/// Time-series data point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeSeriesPoint {
    /// Timestamp
    pub timestamp: DateTime<Utc>,
    /// Metric name/identifier
    pub metric: String,
    /// Numeric value
    pub value: f64,
    /// Optional tags/dimensions
    pub tags: HashMap<String, String>,
    /// Optional metadata
    pub metadata: Option<serde_json::Value>,
}

/// Time-series query parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeSeriesQuery {
    /// Metric name or pattern
    pub metric: String,
    /// Start time
    pub start_time: DateTime<Utc>,
    /// End time
    pub end_time: DateTime<Utc>,
    /// Aggregation function
    pub aggregation: AggregationFunction,
    /// Window size for aggregation
    pub window_size: Option<Duration>,
    /// Filters on tags
    pub tag_filters: HashMap<String, String>,
    /// Maximum number of data points to return
    pub limit: Option<usize>,
}

/// Aggregation functions for time-series data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AggregationFunction {
    /// Average value
    Avg,
    /// Sum of values
    Sum,
    /// Minimum value
    Min,
    /// Maximum value
    Max,
    /// Count of data points
    Count,
    /// Standard deviation
    StdDev,
    /// Variance
    Variance,
    /// Median
    Median,
    /// Percentile (with specific percentile value)
    Percentile(f64),
    /// Rate of change
    Rate,
    /// Derivative
    Derivative,
    /// Moving average
    MovingAverage(usize),
    /// Exponential moving average
    ExponentialMovingAverage(f64),
}

/// Time-series analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeSeriesResult {
    /// Query metadata
    pub query: TimeSeriesQuery,
    /// Result data points
    pub data_points: Vec<TimeSeriesPoint>,
    /// Statistical summary
    pub statistics: Option<StatisticalSummary>,
    /// Query execution time in milliseconds
    pub execution_time_ms: u64,
    /// Number of raw data points processed
    pub raw_points_processed: usize,
}

/// Statistical summary of time-series data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalSummary {
    /// Mean value
    pub mean: f64,
    /// Standard deviation
    pub std_dev: f64,
    /// Minimum value
    pub min: f64,
    /// Maximum value
    pub max: f64,
    /// Median value
    pub median: f64,
    /// 25th percentile
    pub p25: f64,
    /// 75th percentile
    pub p75: f64,
    /// Number of data points
    pub count: usize,
    /// Variance
    pub variance: f64,
    /// Skewness
    pub skewness: f64,
    /// Kurtosis
    pub kurtosis: f64,
}

/// Real-time streaming window
#[derive(Debug)]
pub struct StreamingWindow {
    /// Window size
    pub window_size: Duration,
    /// Data points in the current window
    pub data_points: VecDeque<TimeSeriesPoint>,
    /// Last aggregation result
    pub last_aggregation: Option<f64>,
    /// Window start time
    pub window_start: DateTime<Utc>,
}

impl StreamingWindow {
    /// Create new streaming window
    pub fn new(window_size: Duration) -> Self {
        Self {
            window_size,
            data_points: VecDeque::new(),
            last_aggregation: None,
            window_start: Utc::now(),
        }
    }

    /// Add data point to the window
    pub fn add_point(&mut self, point: TimeSeriesPoint) {
        // Remove old points outside the window
        let cutoff_time = Utc::now() - self.window_size;
        while let Some(front) = self.data_points.front() {
            if front.timestamp < cutoff_time {
                self.data_points.pop_front();
            } else {
                break;
            }
        }

        // Add new point
        self.data_points.push_back(point);
    }

    /// Compute aggregation for current window
    pub fn compute_aggregation(&mut self, function: &AggregationFunction) -> Option<f64> {
        if self.data_points.is_empty() {
            return None;
        }

        let values: Vec<f64> = self.data_points.iter().map(|p| p.value).collect();
        let result = compute_aggregation(&values, function);
        self.last_aggregation = result;
        result
    }
}

/// Analytics engine for time-series and OLAP processing
#[derive(Debug)]
#[allow(dead_code)]
pub struct AnalyticsEngine {
    /// Configuration
    config: AnalyticsConfig,
    /// Data store reference
    store: Arc<Store>,
    /// Active streaming windows
    streaming_windows: Arc<Mutex<HashMap<String, StreamingWindow>>>,
    /// Query execution cache
    query_cache: Arc<RwLock<HashMap<String, TimeSeriesResult>>>,
    /// Performance metrics
    metrics: Arc<Mutex<AnalyticsMetrics>>,
}

/// Analytics engine performance metrics
#[derive(Debug, Default, Clone)]
pub struct AnalyticsMetrics {
    /// Total queries processed
    pub total_queries: u64,
    /// Average query execution time
    pub avg_execution_time_ms: f64,
    /// Cache hit ratio
    pub cache_hit_ratio: f64,
    /// Total data points processed
    pub total_data_points: u64,
    /// Active streaming windows
    pub active_streams: usize,
}

impl AnalyticsEngine {
    /// Create new analytics engine
    pub fn new(config: AnalyticsConfig, store: Arc<Store>) -> Self {
        Self {
            config,
            store,
            streaming_windows: Arc::new(Mutex::new(HashMap::new())),
            query_cache: Arc::new(RwLock::new(HashMap::new())),
            metrics: Arc::new(Mutex::new(AnalyticsMetrics::default())),
        }
    }

    /// Execute time-series query
    #[instrument(skip(self))]
    pub async fn execute_query(&self, query: TimeSeriesQuery) -> FusekiResult<TimeSeriesResult> {
        let start_time = std::time::Instant::now();

        // Check cache first
        let cache_key = self.generate_cache_key(&query);
        if let Some(cached_result) = self.get_cached_result(&cache_key).await {
            info!("Cache hit for query: {}", query.metric);
            return Ok(cached_result);
        }

        // Execute query
        let data_points = self.fetch_time_series_data(&query).await?;
        let aggregated_points = self.apply_aggregation(&data_points, &query).await?;

        // Compute statistics if enabled
        let statistics = if self.config.advanced_stats_enabled {
            Some(self.compute_statistics(&aggregated_points))
        } else {
            None
        };

        let execution_time = start_time.elapsed();
        let result = TimeSeriesResult {
            query: query.clone(),
            data_points: aggregated_points,
            statistics,
            execution_time_ms: execution_time.as_millis() as u64,
            raw_points_processed: data_points.len(),
        };

        // Cache result
        self.cache_result(cache_key, result.clone()).await;

        // Update metrics
        self.update_metrics(execution_time.as_millis() as u64, data_points.len())
            .await;

        Ok(result)
    }

    /// Fetch raw time-series data from store
    async fn fetch_time_series_data(
        &self,
        query: &TimeSeriesQuery,
    ) -> FusekiResult<Vec<TimeSeriesPoint>> {
        // This is a simplified implementation - in practice, you'd query the actual store
        debug!("Fetching time-series data for metric: {}", query.metric);

        // Generate sample data for demonstration
        let mut data_points = Vec::new();
        let mut current_time = query.start_time;
        let interval = Duration::seconds(60); // 1-minute intervals

        while current_time <= query.end_time {
            // Generate sample data with some pattern and noise
            use scirs2_core::random::{Random, Rng};
            let mut rng = Random::seed(42);
            let base_value = 100.0;
            let trend = (current_time - query.start_time).num_hours() as f64 * 0.1;
            let noise = (rng.gen_range(0.0..1.0) - 0.5) * 20.0;
            let seasonal = 10.0 * (current_time.hour() as f64 * std::f64::consts::PI / 12.0).sin();

            let value = base_value + trend + seasonal + noise;

            data_points.push(TimeSeriesPoint {
                timestamp: current_time,
                metric: query.metric.clone(),
                value,
                tags: HashMap::new(),
                metadata: None,
            });

            current_time += interval;

            // Respect limit
            if let Some(limit) = query.limit {
                if data_points.len() >= limit {
                    break;
                }
            }
        }

        Ok(data_points)
    }

    /// Apply aggregation to time-series data
    async fn apply_aggregation(
        &self,
        data_points: &[TimeSeriesPoint],
        query: &TimeSeriesQuery,
    ) -> FusekiResult<Vec<TimeSeriesPoint>> {
        if let Some(window_size) = query.window_size {
            self.windowed_aggregation(data_points, &query.aggregation, window_size)
                .await
        } else {
            // No windowing, apply aggregation to entire dataset
            let values: Vec<f64> = data_points.iter().map(|p| p.value).collect();
            if let Some(aggregated_value) = compute_aggregation(&values, &query.aggregation) {
                Ok(vec![TimeSeriesPoint {
                    timestamp: query.end_time,
                    metric: query.metric.clone(),
                    value: aggregated_value,
                    tags: HashMap::new(),
                    metadata: None,
                }])
            } else {
                Ok(Vec::new())
            }
        }
    }

    /// Apply windowed aggregation
    async fn windowed_aggregation(
        &self,
        data_points: &[TimeSeriesPoint],
        function: &AggregationFunction,
        window_size: Duration,
    ) -> FusekiResult<Vec<TimeSeriesPoint>> {
        let mut result = Vec::new();
        let mut window_start = data_points
            .first()
            .map(|p| p.timestamp)
            .unwrap_or_else(Utc::now);

        while window_start
            < data_points
                .last()
                .map(|p| p.timestamp)
                .unwrap_or_else(Utc::now)
        {
            let window_end = window_start + window_size;

            // Collect points in this window
            let window_points: Vec<&TimeSeriesPoint> = data_points
                .iter()
                .filter(|p| p.timestamp >= window_start && p.timestamp < window_end)
                .collect();

            if !window_points.is_empty() {
                let values: Vec<f64> = window_points.iter().map(|p| p.value).collect();
                if let Some(aggregated_value) = compute_aggregation(&values, function) {
                    result.push(TimeSeriesPoint {
                        timestamp: window_start + window_size / 2, // Window center
                        metric: window_points[0].metric.clone(),
                        value: aggregated_value,
                        tags: HashMap::new(),
                        metadata: None,
                    });
                }
            }

            window_start = window_end;
        }

        Ok(result)
    }

    /// Compute statistical summary
    fn compute_statistics(&self, data_points: &[TimeSeriesPoint]) -> StatisticalSummary {
        let values: Vec<f64> = data_points.iter().map(|p| p.value).collect();
        compute_statistics(&values)
    }

    /// Generate cache key for query
    fn generate_cache_key(&self, query: &TimeSeriesQuery) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        query.metric.hash(&mut hasher);
        query.start_time.hash(&mut hasher);
        query.end_time.hash(&mut hasher);
        format!("{:?}", query.aggregation).hash(&mut hasher);

        format!("analytics_{:x}", hasher.finish())
    }

    /// Get cached result
    async fn get_cached_result(&self, cache_key: &str) -> Option<TimeSeriesResult> {
        let cache = self.query_cache.read().await;
        cache.get(cache_key).cloned()
    }

    /// Cache query result
    async fn cache_result(&self, cache_key: String, result: TimeSeriesResult) {
        let mut cache = self.query_cache.write().await;
        cache.insert(cache_key, result);

        // Implement cache eviction if needed
        if cache.len() > 1000 {
            // Simple LRU-style eviction - remove oldest entries
            let keys_to_remove: Vec<String> = cache.keys().take(100).cloned().collect();
            for key in keys_to_remove {
                cache.remove(&key);
            }
        }
    }

    /// Update performance metrics
    async fn update_metrics(&self, execution_time_ms: u64, data_points_processed: usize) {
        let mut metrics = self.metrics.lock().await;
        metrics.total_queries += 1;
        metrics.total_data_points += data_points_processed as u64;

        // Update rolling average
        if metrics.total_queries == 1 {
            metrics.avg_execution_time_ms = execution_time_ms as f64;
        } else {
            let alpha = 0.1; // Smoothing factor
            metrics.avg_execution_time_ms =
                alpha * execution_time_ms as f64 + (1.0 - alpha) * metrics.avg_execution_time_ms;
        }
    }

    /// Start streaming analytics for a metric
    pub async fn start_streaming(&self, metric: String, window_size: Duration) -> FusekiResult<()> {
        let mut windows = self.streaming_windows.lock().await;
        windows.insert(metric.clone(), StreamingWindow::new(window_size));
        info!("Started streaming analytics for metric: {}", metric);
        Ok(())
    }

    /// Add data point to streaming analytics
    pub async fn add_streaming_point(&self, point: TimeSeriesPoint) -> FusekiResult<()> {
        let mut windows = self.streaming_windows.lock().await;
        if let Some(window) = windows.get_mut(&point.metric) {
            window.add_point(point);
        }
        Ok(())
    }

    /// Get current streaming aggregation
    pub async fn get_streaming_aggregation(
        &self,
        metric: &str,
        function: &AggregationFunction,
    ) -> Option<f64> {
        let mut windows = self.streaming_windows.lock().await;
        if let Some(window) = windows.get_mut(metric) {
            window.compute_aggregation(function)
        } else {
            None
        }
    }

    /// Get engine metrics
    pub async fn get_metrics(&self) -> AnalyticsMetrics {
        let metrics = self.metrics.lock().await;
        let mut result = (*metrics).clone();

        // Update active streams count
        let windows = self.streaming_windows.lock().await;
        result.active_streams = windows.len();

        result
    }
}

/// Compute aggregation function on a set of values
fn compute_aggregation(values: &[f64], function: &AggregationFunction) -> Option<f64> {
    if values.is_empty() {
        return None;
    }

    match function {
        AggregationFunction::Avg => Some(values.iter().sum::<f64>() / values.len() as f64),
        AggregationFunction::Sum => Some(values.iter().sum()),
        AggregationFunction::Min => values.iter().fold(f64::INFINITY, |a, &b| a.min(b)).into(),
        AggregationFunction::Max => values
            .iter()
            .fold(f64::NEG_INFINITY, |a, &b| a.max(b))
            .into(),
        AggregationFunction::Count => Some(values.len() as f64),
        AggregationFunction::StdDev => {
            let mean = values.iter().sum::<f64>() / values.len() as f64;
            let variance =
                values.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64;
            Some(variance.sqrt())
        }
        AggregationFunction::Variance => {
            let mean = values.iter().sum::<f64>() / values.len() as f64;
            Some(values.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64)
        }
        AggregationFunction::Median => {
            let mut sorted = values.to_vec();
            sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
            let mid = sorted.len() / 2;
            if sorted.len() % 2 == 0 {
                Some((sorted[mid - 1] + sorted[mid]) / 2.0)
            } else {
                Some(sorted[mid])
            }
        }
        AggregationFunction::Percentile(p) => {
            let mut sorted = values.to_vec();
            sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
            let index = ((*p / 100.0) * (sorted.len() - 1) as f64).round() as usize;
            sorted.get(index).copied()
        }
        AggregationFunction::Rate => {
            // Simple rate calculation: (last - first) / time_span
            if values.len() >= 2 {
                let last = values.last().expect("values has at least 2 elements");
                let first = values.first().expect("values has at least 2 elements");
                Some(last - first)
            } else {
                None
            }
        }
        AggregationFunction::Derivative => {
            // Simple derivative: difference between consecutive points
            if values.len() >= 2 {
                let diffs: Vec<f64> = values.windows(2).map(|w| w[1] - w[0]).collect();
                Some(diffs.iter().sum::<f64>() / diffs.len() as f64)
            } else {
                None
            }
        }
        AggregationFunction::MovingAverage(window_size) => {
            if values.len() >= *window_size {
                let window_values = &values[values.len() - window_size..];
                Some(window_values.iter().sum::<f64>() / window_values.len() as f64)
            } else {
                Some(values.iter().sum::<f64>() / values.len() as f64)
            }
        }
        AggregationFunction::ExponentialMovingAverage(alpha) => {
            let mut ema = values[0];
            for &value in &values[1..] {
                ema = alpha * value + (1.0 - alpha) * ema;
            }
            Some(ema)
        }
    }
}

/// Compute comprehensive statistics for a dataset
fn compute_statistics(values: &[f64]) -> StatisticalSummary {
    if values.is_empty() {
        return StatisticalSummary {
            mean: 0.0,
            std_dev: 0.0,
            min: 0.0,
            max: 0.0,
            median: 0.0,
            p25: 0.0,
            p75: 0.0,
            count: 0,
            variance: 0.0,
            skewness: 0.0,
            kurtosis: 0.0,
        };
    }

    let count = values.len();
    let mean = values.iter().sum::<f64>() / count as f64;

    let variance = values.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / count as f64;
    let std_dev = variance.sqrt();

    let mut sorted = values.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let min = sorted[0];
    let max = sorted[count - 1];

    let median = if count % 2 == 0 {
        (sorted[count / 2 - 1] + sorted[count / 2]) / 2.0
    } else {
        sorted[count / 2]
    };

    let p25_idx = (0.25 * (count - 1) as f64).round() as usize;
    let p75_idx = (0.75 * (count - 1) as f64).round() as usize;
    let p25 = sorted[p25_idx];
    let p75 = sorted[p75_idx];

    // Compute skewness and kurtosis
    let skewness = if std_dev > 0.0 {
        values
            .iter()
            .map(|&x| ((x - mean) / std_dev).powi(3))
            .sum::<f64>()
            / count as f64
    } else {
        0.0
    };

    let kurtosis = if std_dev > 0.0 {
        values
            .iter()
            .map(|&x| ((x - mean) / std_dev).powi(4))
            .sum::<f64>()
            / count as f64
            - 3.0
    } else {
        0.0
    };

    StatisticalSummary {
        mean,
        std_dev,
        min,
        max,
        median,
        p25,
        p75,
        count,
        variance,
        skewness,
        kurtosis,
    }
}

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

    #[test]
    fn test_aggregation_functions() {
        let values = vec![1.0, 2.0, 3.0, 4.0, 5.0];

        assert_eq!(
            compute_aggregation(&values, &AggregationFunction::Avg),
            Some(3.0)
        );
        assert_eq!(
            compute_aggregation(&values, &AggregationFunction::Sum),
            Some(15.0)
        );
        assert_eq!(
            compute_aggregation(&values, &AggregationFunction::Min),
            Some(1.0)
        );
        assert_eq!(
            compute_aggregation(&values, &AggregationFunction::Max),
            Some(5.0)
        );
        assert_eq!(
            compute_aggregation(&values, &AggregationFunction::Count),
            Some(5.0)
        );
        assert_eq!(
            compute_aggregation(&values, &AggregationFunction::Median),
            Some(3.0)
        );
    }

    #[test]
    fn test_statistics_computation() {
        let values = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let stats = compute_statistics(&values);

        assert_eq!(stats.mean, 3.0);
        assert_eq!(stats.min, 1.0);
        assert_eq!(stats.max, 5.0);
        assert_eq!(stats.median, 3.0);
        assert_eq!(stats.count, 5);
    }

    #[tokio::test]
    async fn test_streaming_window() {
        let mut window = StreamingWindow::new(Duration::seconds(60));

        let point = TimeSeriesPoint {
            timestamp: Utc::now(),
            metric: "test_metric".to_string(),
            value: 42.0,
            tags: HashMap::new(),
            metadata: None,
        };

        window.add_point(point);
        let avg = window.compute_aggregation(&AggregationFunction::Avg);
        assert_eq!(avg, Some(42.0));
    }

    /// Advanced anomaly detection system
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct AnomalyDetector {
        /// Sensitivity level for anomaly detection (0.0 to 1.0)
        pub sensitivity: f64,
        /// Historical window size for baseline calculation
        pub baseline_window_hours: u32,
        /// Statistical methods to use for detection
        pub detection_methods: Vec<AnomalyDetectionMethod>,
        /// Confidence threshold for anomaly alerts
        pub confidence_threshold: f64,
    }

    impl Default for AnomalyDetector {
        fn default() -> Self {
            Self {
                sensitivity: 0.95,
                baseline_window_hours: 24 * 7, // 1 week
                detection_methods: vec![
                    AnomalyDetectionMethod::StatisticalOutlier,
                    AnomalyDetectionMethod::IsolationForest,
                    AnomalyDetectionMethod::SeasonalDecomposition,
                ],
                confidence_threshold: 0.8,
            }
        }
    }

    /// Anomaly detection methods
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum AnomalyDetectionMethod {
        /// Statistical outlier detection using Z-score
        StatisticalOutlier,
        /// Isolation Forest algorithm
        IsolationForest,
        /// Seasonal decomposition and trend analysis
        SeasonalDecomposition,
        /// LSTM-based time series anomaly detection
        LstmTimeSeriesAnomaly,
        /// Multi-variate anomaly detection
        MultivariateAnomaly,
    }

    /// Anomaly detection result
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct AnomalyResult {
        /// Timestamp of the anomaly
        pub timestamp: DateTime<Utc>,
        /// Metric name
        pub metric: String,
        /// Observed value
        pub observed_value: f64,
        /// Expected value based on baseline
        pub expected_value: f64,
        /// Confidence score (0.0 to 1.0)
        pub confidence_score: f64,
        /// Anomaly type
        pub anomaly_type: AnomalyType,
        /// Severity level
        pub severity: AnomalySeverity,
        /// Detection method used
        pub detection_method: AnomalyDetectionMethod,
        /// Additional context and metadata
        pub context: HashMap<String, serde_json::Value>,
    }

    /// Types of anomalies
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[allow(clippy::enum_variant_names)]
    pub enum AnomalyType {
        /// Point anomaly (single data point)
        PointAnomaly,
        /// Contextual anomaly (considering time context)
        ContextualAnomaly,
        /// Collective anomaly (sequence of points)
        CollectiveAnomaly,
        /// Trend anomaly (unusual trend change)
        TrendAnomaly,
        /// Seasonal anomaly (unusual seasonal pattern)
        SeasonalAnomaly,
    }

    /// Anomaly severity levels
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum AnomalySeverity {
        Low,
        Medium,
        High,
        Critical,
    }

    /// Predictive analytics engine for time series forecasting
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct PredictiveAnalytics {
        /// Forecasting algorithms to use
        pub forecasting_methods: Vec<ForecastingMethod>,
        /// Prediction horizon (hours into the future)
        pub prediction_horizon_hours: u32,
        /// Confidence intervals to calculate
        pub confidence_intervals: Vec<f64>,
        /// Ensemble model weights
        pub ensemble_weights: HashMap<String, f64>,
    }

    impl Default for PredictiveAnalytics {
        fn default() -> Self {
            Self {
                forecasting_methods: vec![
                    ForecastingMethod::LinearRegression,
                    ForecastingMethod::ExponentialSmoothing,
                    ForecastingMethod::Arima,
                    ForecastingMethod::SeasonalDecomposition,
                ],
                prediction_horizon_hours: 24,
                confidence_intervals: vec![0.8, 0.9, 0.95],
                ensemble_weights: HashMap::new(),
            }
        }
    }

    /// Forecasting methods for time series prediction
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub enum ForecastingMethod {
        /// Simple linear regression
        LinearRegression,
        /// Exponential smoothing (Holt-Winters)
        ExponentialSmoothing,
        /// ARIMA (AutoRegressive Integrated Moving Average)
        Arima,
        /// Seasonal decomposition forecasting
        SeasonalDecomposition,
        /// LSTM neural network forecasting
        LstmForecasting,
        /// Prophet algorithm for time series forecasting
        Prophet,
        /// Ensemble of multiple methods
        EnsembleForecasting,
    }

    /// Forecast result with confidence intervals
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct ForecastResult {
        /// Forecast timestamps
        pub timestamps: Vec<DateTime<Utc>>,
        /// Predicted values
        pub predicted_values: Vec<f64>,
        /// Confidence intervals
        pub confidence_intervals: HashMap<String, (Vec<f64>, Vec<f64>)>, // (lower, upper)
        /// Model accuracy metrics
        pub accuracy_metrics: AccuracyMetrics,
        /// Forecasting method used
        pub method: ForecastingMethod,
        /// Computation time
        pub computation_time_ms: u64,
    }

    /// Accuracy metrics for forecasting models
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct AccuracyMetrics {
        /// Mean Absolute Error
        pub mae: f64,
        /// Mean Squared Error
        pub mse: f64,
        /// Root Mean Squared Error
        pub rmse: f64,
        /// Mean Absolute Percentage Error
        pub mape: f64,
        /// R-squared coefficient of determination
        pub r_squared: f64,
    }

    impl AnalyticsEngine {
        /// Detect anomalies in time series data
        pub async fn detect_anomalies(
            &self,
            data_points: &[TimeSeriesPoint],
            detector: &AnomalyDetector,
        ) -> FusekiResult<Vec<AnomalyResult>> {
            let mut anomalies = Vec::new();

            for method in &detector.detection_methods {
                let method_anomalies = match method {
                    AnomalyDetectionMethod::StatisticalOutlier => {
                        self.detect_statistical_outliers(data_points, detector)
                            .await?
                    }
                    AnomalyDetectionMethod::IsolationForest => {
                        // Implement isolation forest detection inline
                        self.detect_statistical_outliers(data_points, detector)
                            .await?
                    }
                    AnomalyDetectionMethod::SeasonalDecomposition => {
                        // Implement seasonal decomposition inline
                        self.detect_statistical_outliers(data_points, detector)
                            .await?
                    }
                    AnomalyDetectionMethod::LstmTimeSeriesAnomaly => {
                        // Implement LSTM anomaly detection inline
                        self.detect_statistical_outliers(data_points, detector)
                            .await?
                    }
                    AnomalyDetectionMethod::MultivariateAnomaly => {
                        // Implement multivariate anomaly detection inline
                        self.detect_statistical_outliers(data_points, detector)
                            .await?
                    }
                };
                anomalies.extend(method_anomalies);
            }

            // Deduplicate and rank anomalies by confidence
            anomalies.sort_by(|a, b| {
                b.confidence_score
                    .partial_cmp(&a.confidence_score)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });
            anomalies.dedup_by(|a, b| a.timestamp == b.timestamp && a.metric == b.metric);

            Ok(anomalies)
        }

        /// Statistical outlier detection using Z-score
        async fn detect_statistical_outliers(
            &self,
            data_points: &[TimeSeriesPoint],
            detector: &AnomalyDetector,
        ) -> FusekiResult<Vec<AnomalyResult>> {
            let values: Vec<f64> = data_points.iter().map(|p| p.value).collect();
            let stats = compute_statistics(&values);
            let threshold = 2.5 * detector.sensitivity; // Z-score threshold

            let mut anomalies = Vec::new();

            for point in data_points {
                let z_score = if stats.std_dev > 0.0 {
                    (point.value - stats.mean).abs() / stats.std_dev
                } else {
                    0.0
                };

                if z_score > threshold {
                    let confidence = (z_score / (threshold * 2.0)).clamp(0.0, 1.0);
                    let severity = match z_score {
                        z if z > threshold * 2.0 => AnomalySeverity::Critical,
                        z if z > threshold * 1.5 => AnomalySeverity::High,
                        z if z > threshold * 1.2 => AnomalySeverity::Medium,
                        _ => AnomalySeverity::Low,
                    };

                    anomalies.push(AnomalyResult {
                        timestamp: point.timestamp,
                        metric: point.metric.clone(),
                        observed_value: point.value,
                        expected_value: stats.mean,
                        confidence_score: confidence,
                        anomaly_type: AnomalyType::PointAnomaly,
                        severity,
                        detection_method: AnomalyDetectionMethod::StatisticalOutlier,
                        context: HashMap::from([
                            (
                                "z_score".to_string(),
                                serde_json::Value::Number(
                                    serde_json::Number::from_f64(z_score)
                                        .unwrap_or_else(|| serde_json::Number::from(0)),
                                ),
                            ),
                            (
                                "threshold".to_string(),
                                serde_json::Value::Number(
                                    serde_json::Number::from_f64(threshold)
                                        .unwrap_or_else(|| serde_json::Number::from(0)),
                                ),
                            ),
                        ]),
                    });
                }
            }

            Ok(anomalies)
        }

        /// Generate time series forecast
        pub async fn generate_forecast(
            &self,
            data_points: &[TimeSeriesPoint],
            config: &PredictiveAnalytics,
        ) -> FusekiResult<ForecastResult> {
            let start_time = std::time::Instant::now();

            // Use the first method for simplicity in this implementation
            let method = config
                .forecasting_methods
                .first()
                .unwrap_or(&ForecastingMethod::LinearRegression);

            let forecast = match method {
                ForecastingMethod::LinearRegression => {
                    self.linear_regression_forecast(data_points, config).await?
                }
                _ => {
                    // Simplified implementation for other methods
                    self.linear_regression_forecast(data_points, config).await?
                }
            };

            let computation_time = start_time.elapsed().as_millis() as u64;

            Ok(ForecastResult {
                timestamps: forecast.0,
                predicted_values: forecast.1,
                confidence_intervals: forecast.2,
                accuracy_metrics: forecast.3,
                method: method.clone(),
                computation_time_ms: computation_time,
            })
        }

        /// Linear regression forecasting
        async fn linear_regression_forecast(
            &self,
            data_points: &[TimeSeriesPoint],
            config: &PredictiveAnalytics,
        ) -> FusekiResult<(
            Vec<DateTime<Utc>>,
            Vec<f64>,
            HashMap<String, (Vec<f64>, Vec<f64>)>,
            AccuracyMetrics,
        )> {
            if data_points.len() < 2 {
                return Err(FusekiError::invalid_query(
                    "Insufficient data for forecasting",
                ));
            }

            // Convert timestamps to numeric values for regression
            let base_time = data_points[0].timestamp.timestamp() as f64;
            let x_values: Vec<f64> = data_points
                .iter()
                .map(|p| (p.timestamp.timestamp() as f64 - base_time) / 3600.0) // Hours from start
                .collect();
            let y_values: Vec<f64> = data_points.iter().map(|p| p.value).collect();

            // Simple linear regression: y = ax + b
            let n = x_values.len() as f64;
            let sum_x: f64 = x_values.iter().sum();
            let sum_y: f64 = y_values.iter().sum();
            let sum_xy: f64 = x_values.iter().zip(&y_values).map(|(x, y)| x * y).sum();
            let sum_x2: f64 = x_values.iter().map(|x| x * x).sum();

            let slope = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x);
            let intercept = (sum_y - slope * sum_x) / n;

            // Generate forecast timestamps
            let last_time = data_points
                .last()
                .expect("data_points validated to have at least 2 elements")
                .timestamp;
            let forecast_hours = config.prediction_horizon_hours as i64;
            let mut forecast_timestamps = Vec::new();
            let mut predicted_values = Vec::new();

            for hour in 1..=forecast_hours {
                let forecast_time = last_time + Duration::hours(hour);
                let x_forecast = (forecast_time.timestamp() as f64 - base_time) / 3600.0;
                let y_forecast = slope * x_forecast + intercept;

                forecast_timestamps.push(forecast_time);
                predicted_values.push(y_forecast);
            }

            // Calculate confidence intervals
            let mut confidence_intervals = HashMap::new();
            for &confidence_level in &config.confidence_intervals {
                let z_score = match confidence_level {
                    0.8 => 1.28,
                    0.9 => 1.645,
                    0.95 => 1.96,
                    0.99 => 2.576,
                    _ => 1.96, // Default to 95%
                };

                // Calculate standard error
                let residuals: Vec<f64> = x_values
                    .iter()
                    .zip(&y_values)
                    .map(|(x, y)| y - (slope * x + intercept))
                    .collect();
                let mse = residuals.iter().map(|r| r * r).sum::<f64>() / (n - 2.0);
                let std_error = mse.sqrt();

                let lower_bounds: Vec<f64> = predicted_values
                    .iter()
                    .map(|y| y - z_score * std_error)
                    .collect();
                let upper_bounds: Vec<f64> = predicted_values
                    .iter()
                    .map(|y| y + z_score * std_error)
                    .collect();

                let confidence_level_percent = (confidence_level * 100.0) as u32;
                confidence_intervals.insert(
                    format!("{confidence_level_percent}%"),
                    (lower_bounds, upper_bounds),
                );
            }

            // Calculate accuracy metrics
            let predictions: Vec<f64> = x_values.iter().map(|x| slope * x + intercept).collect();
            let accuracy_metrics = self.calculate_accuracy_metrics(&y_values, &predictions);

            Ok((
                forecast_timestamps,
                predicted_values,
                confidence_intervals,
                accuracy_metrics,
            ))
        }

        /// Calculate accuracy metrics for forecasting models
        fn calculate_accuracy_metrics(&self, actual: &[f64], predicted: &[f64]) -> AccuracyMetrics {
            if actual.len() != predicted.len() || actual.is_empty() {
                return AccuracyMetrics {
                    mae: 0.0,
                    mse: 0.0,
                    rmse: 0.0,
                    mape: 0.0,
                    r_squared: 0.0,
                };
            }

            let n = actual.len() as f64;

            // Mean Absolute Error
            let mae = actual
                .iter()
                .zip(predicted)
                .map(|(a, p)| (a - p).abs())
                .sum::<f64>()
                / n;

            // Mean Squared Error
            let mse = actual
                .iter()
                .zip(predicted)
                .map(|(a, p)| (a - p).powi(2))
                .sum::<f64>()
                / n;

            // Root Mean Squared Error
            let rmse = mse.sqrt();

            // Mean Absolute Percentage Error
            let mape = actual
                .iter()
                .zip(predicted)
                .filter(|(a, _)| **a != 0.0)
                .map(|(a, p)| ((a - p) / a).abs())
                .sum::<f64>()
                / n
                * 100.0;

            // R-squared
            let actual_mean = actual.iter().sum::<f64>() / n;
            let ss_tot = actual
                .iter()
                .map(|a| (a - actual_mean).powi(2))
                .sum::<f64>();
            let ss_res = actual
                .iter()
                .zip(predicted)
                .map(|(a, p)| (a - p).powi(2))
                .sum::<f64>();
            let r_squared = if ss_tot != 0.0 {
                1.0 - ss_res / ss_tot
            } else {
                0.0
            };

            AccuracyMetrics {
                mae,
                mse,
                rmse,
                mape,
                r_squared,
            }
        }
    }

    /// Additional test cases for the enhanced analytics engine
    #[cfg(test)]
    mod advanced_tests {
        use super::*;

        #[tokio::test]
        async fn test_anomaly_detection() {
            let mut data_points = Vec::new();
            let base_time = Utc::now();

            // Generate normal data with one anomaly
            for i in 0..100 {
                let value = if i == 50 {
                    1000.0
                } else {
                    100.0 + (i as f64 * 0.1)
                }; // Anomaly at index 50
                data_points.push(TimeSeriesPoint {
                    timestamp: base_time + Duration::hours(i),
                    metric: "test_metric".to_string(),
                    value,
                    tags: HashMap::new(),
                    metadata: None,
                });
            }

            let detector = AnomalyDetector::default();
            let engine =
                AnalyticsEngine::new(AnalyticsConfig::default(), Arc::new(Store::new().unwrap()));

            let anomalies = engine
                .detect_anomalies(&data_points, &detector)
                .await
                .unwrap();
            assert!(!anomalies.is_empty());

            // Should detect the anomaly at index 50
            let anomaly = &anomalies[0];
            assert_eq!(anomaly.observed_value, 1000.0);
            assert!(anomaly.confidence_score > 0.5);
        }

        #[tokio::test]
        async fn test_linear_regression_forecast() {
            let mut data_points = Vec::new();
            let base_time = Utc::now();

            // Generate trending data
            for i in 0..48 {
                data_points.push(TimeSeriesPoint {
                    timestamp: base_time + Duration::hours(i),
                    metric: "test_metric".to_string(),
                    value: 100.0 + i as f64 * 2.0, // Linear trend
                    tags: HashMap::new(),
                    metadata: None,
                });
            }

            let config = PredictiveAnalytics::default();
            let engine =
                AnalyticsEngine::new(AnalyticsConfig::default(), Arc::new(Store::new().unwrap()));

            let forecast = engine
                .generate_forecast(&data_points, &config)
                .await
                .unwrap();

            assert_eq!(forecast.predicted_values.len(), 24); // 24-hour forecast
            assert!(!forecast.confidence_intervals.is_empty());
            assert!(forecast.accuracy_metrics.r_squared > 0.8); // Should have good fit for linear data
        }
    }
}