kaccy-db 0.2.0

Database layer for Kaccy Protocol - PostgreSQL, Redis, and distributed caching
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
//! Connection pool diagnostics and health monitoring utilities
//!
//! This module provides comprehensive diagnostics for database connection pools,
//! helping identify connection issues, pool exhaustion, and performance problems.

use crate::error::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;

/// Comprehensive connection pool diagnostics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoolDiagnostics {
    /// Current pool statistics
    pub stats: PoolStats,
    /// Connection health status
    pub health: ConnectionHealth,
    /// Recent connection issues
    pub issues: Vec<ConnectionIssue>,
    /// Recommendations for optimization
    pub recommendations: Vec<String>,
}

/// Current pool statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoolStats {
    /// Number of connections currently in use
    pub connections_active: u32,
    /// Number of idle connections in the pool
    pub connections_idle: u32,
    /// Maximum number of connections allowed
    pub connections_max: u32,
    /// Current pool utilization percentage
    pub utilization_percent: f64,
    /// Average connection acquisition time (ms)
    pub avg_acquisition_time_ms: Option<f64>,
}

/// Connection health status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionHealth {
    /// Whether the pool is healthy
    pub is_healthy: bool,
    /// Health status message
    pub status: String,
    /// Last successful connection timestamp
    pub last_successful_connection: Option<DateTime<Utc>>,
    /// Number of failed connection attempts in the last minute
    pub recent_failures: u32,
}

/// Connection issue record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionIssue {
    /// When the issue occurred
    pub timestamp: DateTime<Utc>,
    /// Issue type
    pub issue_type: IssueType,
    /// Issue description
    pub description: String,
    /// Severity level
    pub severity: Severity,
}

/// Type of connection issue
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum IssueType {
    /// Pool exhaustion (no available connections)
    PoolExhausted,
    /// Connection timeout
    Timeout,
    /// Connection failure
    ConnectionFailed,
    /// Slow query
    SlowQuery,
    /// High utilization
    HighUtilization,
}

/// Issue severity
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
    /// Informational
    Info,
    /// Warning
    Warning,
    /// Critical
    Critical,
}

/// Get comprehensive pool diagnostics
pub async fn get_pool_diagnostics(pool: &PgPool) -> Result<PoolDiagnostics> {
    let stats = get_pool_stats(pool);
    let health = check_connection_health(pool).await?;
    let issues = diagnose_issues(&stats, &health);
    let recommendations = generate_recommendations(&stats, &health, &issues);

    Ok(PoolDiagnostics {
        stats,
        health,
        issues,
        recommendations,
    })
}

/// Get current pool statistics
pub fn get_pool_stats(pool: &PgPool) -> PoolStats {
    let size = pool.size();
    let idle = pool.num_idle() as u32;
    let max = pool.options().get_max_connections();

    let active = size.saturating_sub(idle);
    let utilization = if max > 0 {
        (active as f64 / max as f64) * 100.0
    } else {
        0.0
    };

    PoolStats {
        connections_active: active,
        connections_idle: idle,
        connections_max: max,
        utilization_percent: utilization,
        avg_acquisition_time_ms: None, // Would need tracking
    }
}

/// Check connection health
pub async fn check_connection_health(pool: &PgPool) -> Result<ConnectionHealth> {
    // Try a simple query to check connectivity
    let result = sqlx::query_scalar::<_, i32>("SELECT 1")
        .fetch_one(pool)
        .await;

    let is_healthy = result.is_ok();
    let status = if is_healthy {
        "Healthy".to_string()
    } else {
        format!("Unhealthy: {}", result.unwrap_err())
    };

    Ok(ConnectionHealth {
        is_healthy,
        status,
        last_successful_connection: if is_healthy { Some(Utc::now()) } else { None },
        recent_failures: if is_healthy { 0 } else { 1 },
    })
}

/// Diagnose issues based on stats and health
fn diagnose_issues(stats: &PoolStats, health: &ConnectionHealth) -> Vec<ConnectionIssue> {
    let mut issues = Vec::new();
    let now = Utc::now();

    // Check for high utilization
    if stats.utilization_percent > 90.0 {
        issues.push(ConnectionIssue {
            timestamp: now,
            issue_type: IssueType::HighUtilization,
            description: format!(
                "Pool utilization is {:.1}% - consider increasing max connections",
                stats.utilization_percent
            ),
            severity: Severity::Warning,
        });
    }

    // Check for pool exhaustion
    if stats.connections_idle == 0 && stats.connections_active >= stats.connections_max {
        issues.push(ConnectionIssue {
            timestamp: now,
            issue_type: IssueType::PoolExhausted,
            description: "Connection pool is exhausted - all connections are in use".to_string(),
            severity: Severity::Critical,
        });
    }

    // Check for unhealthy state
    if !health.is_healthy {
        issues.push(ConnectionIssue {
            timestamp: now,
            issue_type: IssueType::ConnectionFailed,
            description: health.status.clone(),
            severity: Severity::Critical,
        });
    }

    issues
}

/// Generate recommendations based on diagnostics
fn generate_recommendations(
    stats: &PoolStats,
    health: &ConnectionHealth,
    issues: &[ConnectionIssue],
) -> Vec<String> {
    let mut recommendations = Vec::new();

    // Check if pool is too small
    if stats.utilization_percent > 80.0 {
        recommendations.push(format!(
            "Consider increasing max_connections from {} to {} for better headroom",
            stats.connections_max,
            stats.connections_max * 2
        ));
    }

    // Check if pool is too large
    if stats.utilization_percent < 20.0 && stats.connections_max > 10 {
        recommendations.push(format!(
            "Pool utilization is low ({:.1}%) - consider reducing max_connections to save resources",
            stats.utilization_percent
        ));
    }

    // Check for critical issues
    let has_critical = issues.iter().any(|i| i.severity == Severity::Critical);
    if has_critical {
        recommendations.push(
            "Critical issues detected - investigate immediately to prevent service disruption"
                .to_string(),
        );
    }

    // Connection health recommendations
    if !health.is_healthy {
        recommendations.push("Database connectivity issues detected - check network, credentials, and database status".to_string());
    }

    // If no idle connections but utilization is high
    if stats.connections_idle == 0 && stats.utilization_percent > 70.0 {
        recommendations.push(
            "No idle connections available - increase pool size or optimize query performance"
                .to_string(),
        );
    }

    recommendations
}

/// Get active connections from PostgreSQL
pub async fn get_active_connections(pool: &PgPool) -> Result<Vec<ActiveConnection>> {
    let connections =
        sqlx::query_as::<_, (i32, String, Option<String>, Option<DateTime<Utc>>, String)>(
            r#"
        SELECT
            pid,
            usename,
            application_name,
            query_start,
            state
        FROM pg_stat_activity
        WHERE datname = current_database()
        AND pid != pg_backend_pid()
        ORDER BY query_start DESC NULLS LAST
        "#,
        )
        .fetch_all(pool)
        .await?;

    Ok(connections
        .into_iter()
        .map(|c| ActiveConnection {
            pid: c.0,
            username: c.1,
            application_name: c.2,
            query_start: c.3,
            state: c.4,
            duration: c.3.map(|start| {
                let now = Utc::now();
                now.signed_duration_since(start).num_milliseconds() as u64
            }),
        })
        .collect())
}

/// Information about an active database connection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActiveConnection {
    /// Process ID
    pub pid: i32,
    /// Connected username
    pub username: String,
    /// Application name
    pub application_name: Option<String>,
    /// When the current query started
    pub query_start: Option<DateTime<Utc>>,
    /// Connection state
    pub state: String,
    /// Query duration in milliseconds
    pub duration: Option<u64>,
}

/// Find long-running queries
pub async fn find_long_running_queries(
    pool: &PgPool,
    threshold_ms: u64,
) -> Result<Vec<LongRunningQuery>> {
    let threshold_interval = format!("{} milliseconds", threshold_ms);

    let queries = sqlx::query_as::<_, (i32, String, String, DateTime<Utc>, String)>(
        r#"
        SELECT
            pid,
            usename,
            query,
            query_start,
            state
        FROM pg_stat_activity
        WHERE datname = current_database()
        AND state = 'active'
        AND query_start < NOW() - $1::interval
        AND query NOT LIKE '%pg_stat_activity%'
        ORDER BY query_start ASC
        "#,
    )
    .bind(threshold_interval)
    .fetch_all(pool)
    .await?;

    Ok(queries
        .into_iter()
        .map(|q| {
            let duration = Utc::now().signed_duration_since(q.3).num_milliseconds() as u64;

            LongRunningQuery {
                pid: q.0,
                username: q.1,
                query: q.2,
                started_at: q.3,
                duration_ms: duration,
                state: q.4,
            }
        })
        .collect())
}

/// Information about a long-running query
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LongRunningQuery {
    /// Process ID
    pub pid: i32,
    /// Username
    pub username: String,
    /// Query text
    pub query: String,
    /// When the query started
    pub started_at: DateTime<Utc>,
    /// Query duration in milliseconds
    pub duration_ms: u64,
    /// Query state
    pub state: String,
}

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

    #[test]
    fn test_pool_stats_structure() {
        let stats = PoolStats {
            connections_active: 5,
            connections_idle: 15,
            connections_max: 20,
            utilization_percent: 25.0,
            avg_acquisition_time_ms: Some(1.5),
        };

        assert_eq!(stats.connections_active, 5);
        assert_eq!(stats.utilization_percent, 25.0);
    }

    #[test]
    fn test_connection_health_structure() {
        let health = ConnectionHealth {
            is_healthy: true,
            status: "OK".to_string(),
            last_successful_connection: Some(Utc::now()),
            recent_failures: 0,
        };

        assert!(health.is_healthy);
        assert_eq!(health.recent_failures, 0);
    }

    #[test]
    fn test_connection_issue_structure() {
        let issue = ConnectionIssue {
            timestamp: Utc::now(),
            issue_type: IssueType::PoolExhausted,
            description: "Pool full".to_string(),
            severity: Severity::Critical,
        };

        assert_eq!(issue.issue_type, IssueType::PoolExhausted);
        assert_eq!(issue.severity, Severity::Critical);
    }

    #[test]
    fn test_diagnose_high_utilization() {
        let stats = PoolStats {
            connections_active: 19,
            connections_idle: 1,
            connections_max: 20,
            utilization_percent: 95.0,
            avg_acquisition_time_ms: None,
        };

        let health = ConnectionHealth {
            is_healthy: true,
            status: "OK".to_string(),
            last_successful_connection: Some(Utc::now()),
            recent_failures: 0,
        };

        let issues = diagnose_issues(&stats, &health);
        assert!(issues
            .iter()
            .any(|i| i.issue_type == IssueType::HighUtilization));
    }

    #[test]
    fn test_diagnose_pool_exhaustion() {
        let stats = PoolStats {
            connections_active: 20,
            connections_idle: 0,
            connections_max: 20,
            utilization_percent: 100.0,
            avg_acquisition_time_ms: None,
        };

        let health = ConnectionHealth {
            is_healthy: true,
            status: "OK".to_string(),
            last_successful_connection: Some(Utc::now()),
            recent_failures: 0,
        };

        let issues = diagnose_issues(&stats, &health);
        assert!(issues
            .iter()
            .any(|i| i.issue_type == IssueType::PoolExhausted));
        assert!(issues.iter().any(|i| i.severity == Severity::Critical));
    }

    #[test]
    fn test_generate_recommendations_high_utilization() {
        let stats = PoolStats {
            connections_active: 18,
            connections_idle: 2,
            connections_max: 20,
            utilization_percent: 90.0,
            avg_acquisition_time_ms: None,
        };

        let health = ConnectionHealth {
            is_healthy: true,
            status: "OK".to_string(),
            last_successful_connection: Some(Utc::now()),
            recent_failures: 0,
        };

        let issues = vec![];
        let recommendations = generate_recommendations(&stats, &health, &issues);

        assert!(!recommendations.is_empty());
        assert!(recommendations
            .iter()
            .any(|r| r.contains("increasing max_connections")));
    }

    #[test]
    fn test_generate_recommendations_low_utilization() {
        let stats = PoolStats {
            connections_active: 2,
            connections_idle: 18,
            connections_max: 20,
            utilization_percent: 10.0,
            avg_acquisition_time_ms: None,
        };

        let health = ConnectionHealth {
            is_healthy: true,
            status: "OK".to_string(),
            last_successful_connection: Some(Utc::now()),
            recent_failures: 0,
        };

        let issues = vec![];
        let recommendations = generate_recommendations(&stats, &health, &issues);

        assert!(recommendations
            .iter()
            .any(|r| r.contains("reducing max_connections")));
    }

    #[test]
    fn test_severity_ordering() {
        assert!(Severity::Info < Severity::Warning);
        assert!(Severity::Warning < Severity::Critical);
    }

    #[test]
    fn test_pool_diagnostics_serialization() {
        let diagnostics = PoolDiagnostics {
            stats: PoolStats {
                connections_active: 5,
                connections_idle: 5,
                connections_max: 10,
                utilization_percent: 50.0,
                avg_acquisition_time_ms: None,
            },
            health: ConnectionHealth {
                is_healthy: true,
                status: "OK".to_string(),
                last_successful_connection: None,
                recent_failures: 0,
            },
            issues: vec![],
            recommendations: vec![],
        };

        let json = serde_json::to_string(&diagnostics).unwrap();
        let deserialized: PoolDiagnostics = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.stats.connections_active, 5);
        assert!(deserialized.health.is_healthy);
    }
}