crawlkit-engine 2.0.0

High-performance Rust web crawler and SEO analysis toolkit with 28 analyzers, WASM plugin system, and enterprise features
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
use std::collections::HashMap;
use std::sync::Arc;

use parking_lot::RwLock;
use serde::{Deserialize, Serialize};

/// Tenant configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tenant {
    /// Tenant ID.
    pub id: String,
    /// Tenant name.
    pub name: String,
    /// Tenant plan (free, pro, enterprise).
    pub plan: TenantPlan,
    /// Maximum crawls per month.
    pub max_crawls: u64,
    /// Maximum pages per crawl.
    pub max_pages: usize,
    /// Maximum concurrent crawls.
    pub max_concurrent: usize,
    /// API rate limit (requests per minute).
    pub rate_limit: u32,
    /// Features enabled for this tenant.
    pub features: Vec<String>,
}

/// Tenant plan.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TenantPlan {
    Free,
    Pro,
    Enterprise,
}

/// Tenant manager for multi-tenant support.
pub struct TenantManager {
    tenants: Arc<RwLock<HashMap<String, Tenant>>>,
}

impl TenantManager {
    /// Create new tenant manager.
    #[must_use]
    pub fn new() -> Self {
        Self {
            tenants: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Add a tenant.
    pub fn add_tenant(&self, tenant: Tenant) {
        let mut tenants = self.tenants.write();
        tenants.insert(tenant.id.clone(), tenant);
    }

    /// Get a tenant by ID.
    #[must_use]
    pub fn get_tenant(&self, id: &str) -> Option<Tenant> {
        self.tenants.read().get(id).cloned()
    }

    /// List all tenants.
    #[must_use]
    pub fn list_tenants(&self) -> Vec<Tenant> {
        self.tenants.read().values().cloned().collect()
    }

    /// Remove a tenant.
    pub fn remove_tenant(&self, id: &str) -> bool {
        self.tenants.write().remove(id).is_some()
    }

    /// Get tenant count.
    #[must_use]
    pub fn count(&self) -> usize {
        self.tenants.read().len()
    }
}

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

/// Role definitions for RBAC.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Role {
    /// Role ID.
    pub id: String,
    /// Role name.
    pub name: String,
    /// Permissions granted by this role.
    pub permissions: Vec<String>,
}

/// Permission definitions.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Permission {
    /// Create crawls.
    CrawlCreate,
    /// Read crawl results.
    CrawlRead,
    /// Delete crawls.
    CrawlDelete,
    /// Manage API keys.
    ApiKeyManage,
    /// View analytics.
    AnalyticsView,
    /// Manage users.
    UserManage,
    /// Manage tenants.
    TenantManage,
    /// View audit logs.
    AuditView,
    /// Manage billing.
    BillingManage,
}

impl Permission {
    /// Get permission string representation.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Permission::CrawlCreate => "crawl:create",
            Permission::CrawlRead => "crawl:read",
            Permission::CrawlDelete => "crawl:delete",
            Permission::ApiKeyManage => "apikey:manage",
            Permission::AnalyticsView => "analytics:view",
            Permission::UserManage => "user:manage",
            Permission::TenantManage => "tenant:manage",
            Permission::AuditView => "audit:view",
            Permission::BillingManage => "billing:manage",
        }
    }

    /// Parse a permission from its string representation.
    ///
    /// Returns `None` if the string does not correspond to a known permission.
    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "crawl:create" => Some(Permission::CrawlCreate),
            "crawl:read" => Some(Permission::CrawlRead),
            "crawl:delete" => Some(Permission::CrawlDelete),
            "apikey:manage" => Some(Permission::ApiKeyManage),
            "analytics:view" => Some(Permission::AnalyticsView),
            "user:manage" => Some(Permission::UserManage),
            "tenant:manage" => Some(Permission::TenantManage),
            "audit:view" => Some(Permission::AuditView),
            "billing:manage" => Some(Permission::BillingManage),
            _ => None,
        }
    }
}

/// User with RBAC support.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    /// User ID.
    pub id: String,
    /// User email.
    pub email: String,
    /// User display name.
    pub name: String,
    /// Tenant ID.
    pub tenant_id: String,
    /// User roles.
    pub roles: Vec<String>,
    /// Whether user is active.
    pub active: bool,
}

/// RBAC manager for role-based access control.
pub struct RbacManager {
    roles: Arc<RwLock<HashMap<String, Role>>>,
    users: Arc<RwLock<HashMap<String, User>>>,
}

impl RbacManager {
    /// Create new RBAC manager.
    #[must_use]
    pub fn new() -> Self {
        let mut roles = HashMap::new();

        // Default roles
        roles.insert(
            "admin".to_string(),
            Role {
                id: "admin".to_string(),
                name: "Administrator".to_string(),
                permissions: Permission::iter().map(|p| p.as_str().to_string()).collect(),
            },
        );

        roles.insert(
            "user".to_string(),
            Role {
                id: "user".to_string(),
                name: "User".to_string(),
                permissions: vec![
                    Permission::CrawlCreate.as_str().to_string(),
                    Permission::CrawlRead.as_str().to_string(),
                    Permission::AnalyticsView.as_str().to_string(),
                ],
            },
        );

        roles.insert(
            "viewer".to_string(),
            Role {
                id: "viewer".to_string(),
                name: "Viewer".to_string(),
                permissions: vec![
                    Permission::CrawlRead.as_str().to_string(),
                    Permission::AnalyticsView.as_str().to_string(),
                ],
            },
        );

        Self {
            roles: Arc::new(RwLock::new(roles)),
            users: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Add a role.
    pub fn add_role(&self, role: Role) {
        let mut roles = self.roles.write();
        roles.insert(role.id.clone(), role);
    }

    /// Get a role by ID.
    #[must_use]
    pub fn get_role(&self, id: &str) -> Option<Role> {
        self.roles.read().get(id).cloned()
    }

    /// Add a user.
    pub fn add_user(&self, user: User) {
        let mut users = self.users.write();
        users.insert(user.id.clone(), user);
    }

    /// Get a user by ID.
    #[must_use]
    pub fn get_user(&self, id: &str) -> Option<User> {
        self.users.read().get(id).cloned()
    }

    /// Check if user has permission.
    #[must_use]
    pub fn has_permission(&self, user_id: &str, permission: &Permission) -> bool {
        let users = self.users.read();
        let roles = self.roles.read();

        if let Some(user) = users.get(user_id) {
            if !user.active {
                return false;
            }

            for role_id in &user.roles {
                if let Some(role) = roles.get(role_id) {
                    if role.permissions.contains(&permission.as_str().to_string()) {
                        return true;
                    }
                }
            }
        }

        false
    }

    /// Get all permissions for a user.
    #[must_use]
    pub fn get_permissions(&self, user_id: &str) -> Vec<String> {
        let users = self.users.read();
        let roles = self.roles.read();
        let mut permissions = Vec::new();

        if let Some(user) = users.get(user_id) {
            if !user.active {
                return permissions;
            }

            for role_id in &user.roles {
                if let Some(role) = roles.get(role_id) {
                    permissions.extend(role.permissions.clone());
                }
            }
        }

        permissions.sort();
        permissions.dedup();
        permissions
    }
}

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

/// SSO configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SsoConfig {
    /// SSO provider (saml, oidc).
    pub provider: SsoProvider,
    /// Provider URL.
    pub provider_url: String,
    /// Client ID.
    pub client_id: String,
    /// Client secret.
    pub client_secret: String,
    /// Callback URL.
    pub callback_url: String,
    /// Enabled SSO domains.
    pub enabled_domains: Vec<String>,
}

/// SSO provider type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SsoProvider {
    Saml,
    Oidc,
}

/// SSO manager for enterprise authentication.
pub struct SsoManager {
    configs: Arc<RwLock<Vec<SsoConfig>>>,
}

impl SsoManager {
    /// Create new SSO manager.
    #[must_use]
    pub fn new() -> Self {
        Self {
            configs: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Add SSO configuration.
    pub fn add_config(&self, config: SsoConfig) {
        let mut configs = self.configs.write();
        configs.push(config);
    }

    /// Get SSO config for a domain.
    #[must_use]
    pub fn get_config_for_domain(&self, domain: &str) -> Option<SsoConfig> {
        self.configs
            .read()
            .iter()
            .find(|c| c.enabled_domains.contains(&domain.to_string()))
            .cloned()
    }

    /// List all SSO configs.
    #[must_use]
    pub fn list_configs(&self) -> Vec<SsoConfig> {
        self.configs.read().clone()
    }
}

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

/// SLA monitoring.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlaConfig {
    /// Uptime target (percentage).
    pub uptime_target: f64,
    /// Response time target (ms).
    pub response_time_target: u64,
    /// Error rate target (percentage).
    pub error_rate_target: f64,
    /// Alert thresholds.
    pub alert_thresholds: SlaThresholds,
}

/// SLA alert thresholds.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SlaThresholds {
    /// Warning threshold (percentage of target).
    pub warning: f64,
    /// Critical threshold (percentage of target).
    pub critical: f64,
}

impl Default for SlaConfig {
    fn default() -> Self {
        Self {
            uptime_target: 99.9,
            response_time_target: 500,
            error_rate_target: 1.0,
            alert_thresholds: SlaThresholds {
                warning: 0.95,
                critical: 0.90,
            },
        }
    }
}

/// SLA monitor for tracking compliance.
pub struct SlaMonitor {
    config: SlaConfig,
    metrics: Arc<RwLock<SlaMetrics>>,
}

/// Current SLA metrics.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SlaMetrics {
    /// Total requests.
    pub total_requests: u64,
    /// Successful requests.
    pub successful_requests: u64,
    /// Failed requests.
    pub failed_requests: u64,
    /// Total response time (ms).
    pub total_response_time: u64,
    /// Uptime percentage.
    pub uptime: f64,
    /// Average response time (ms).
    pub avg_response_time: f64,
    /// Error rate (percentage).
    pub error_rate: f64,
}

impl SlaMonitor {
    /// Create new SLA monitor.
    #[must_use]
    pub fn new(config: SlaConfig) -> Self {
        Self {
            config,
            metrics: Arc::new(RwLock::new(SlaMetrics::default())),
        }
    }

    /// Create with default config.
    #[must_use]
    pub fn with_default_config() -> Self {
        Self::new(SlaConfig::default())
    }

    /// Record a successful request.
    pub fn record_success(&self, response_time_ms: u64) {
        let mut metrics = self.metrics.write();
        metrics.total_requests += 1;
        metrics.successful_requests += 1;
        metrics.total_response_time += response_time_ms;
        self.update_metrics(&mut metrics);
    }

    /// Record a failed request.
    pub fn record_failure(&self) {
        let mut metrics = self.metrics.write();
        metrics.total_requests += 1;
        metrics.failed_requests += 1;
        self.update_metrics(&mut metrics);
    }

    /// Update calculated metrics.
    fn update_metrics(&self, metrics: &mut SlaMetrics) {
        if metrics.total_requests > 0 {
            metrics.error_rate =
                (metrics.failed_requests as f64 / metrics.total_requests as f64) * 100.0;
            metrics.avg_response_time =
                metrics.total_response_time as f64 / metrics.total_requests as f64;
        }
    }

    /// Get current metrics.
    #[must_use]
    pub fn metrics(&self) -> SlaMetrics {
        self.metrics.read().clone()
    }

    /// Check if SLA is met.
    #[must_use]
    pub fn is_sla_met(&self) -> bool {
        let metrics = self.metrics.read();
        metrics.error_rate <= self.config.error_rate_target
            && metrics.avg_response_time <= self.config.response_time_target as f64
    }

    /// Get SLA status.
    #[must_use]
    pub fn status(&self) -> SlaStatus {
        let metrics = self.metrics.read();
        let error_rate_ratio = metrics.error_rate / self.config.error_rate_target;
        let response_time_ratio =
            metrics.avg_response_time / self.config.response_time_target as f64;

        let worst_ratio = error_rate_ratio.max(response_time_ratio);

        if worst_ratio <= self.config.alert_thresholds.critical {
            SlaStatus::Critical
        } else if worst_ratio <= self.config.alert_thresholds.warning {
            SlaStatus::Warning
        } else {
            SlaStatus::Healthy
        }
    }

    /// Get configuration.
    #[must_use]
    pub fn config(&self) -> &SlaConfig {
        &self.config
    }
}

impl Default for SlaMonitor {
    fn default() -> Self {
        Self::with_default_config()
    }
}

/// SLA status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SlaStatus {
    Healthy,
    Warning,
    Critical,
}

/// Helper trait for iterating permissions.
trait PermissionIter {
    fn iter() -> Box<dyn Iterator<Item = Permission>>;
}

impl PermissionIter for Permission {
    fn iter() -> Box<dyn Iterator<Item = Permission>> {
        Box::new(
            vec![
                Permission::CrawlCreate,
                Permission::CrawlRead,
                Permission::CrawlDelete,
                Permission::ApiKeyManage,
                Permission::AnalyticsView,
                Permission::UserManage,
                Permission::TenantManage,
                Permission::AuditView,
                Permission::BillingManage,
            ]
            .into_iter(),
        )
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_tenant_manager() {
        let manager = TenantManager::new();

        let tenant = Tenant {
            id: "tenant1".to_string(),
            name: "Acme Corp".to_string(),
            plan: TenantPlan::Enterprise,
            max_crawls: 1000,
            max_pages: 10000,
            max_concurrent: 10,
            rate_limit: 60,
            features: vec!["backlinks".to_string(), "rum".to_string()],
        };

        manager.add_tenant(tenant);
        assert_eq!(manager.count(), 1);
        assert!(manager.get_tenant("tenant1").is_some());
    }

    #[test]
    fn test_rbac_manager() {
        let manager = RbacManager::new();

        let user = User {
            id: "user1".to_string(),
            email: "test@example.com".to_string(),
            name: "Test User".to_string(),
            tenant_id: "tenant1".to_string(),
            roles: vec!["admin".to_string()],
            active: true,
        };

        manager.add_user(user);

        // Admin has all permissions
        assert!(manager.has_permission("user1", &Permission::CrawlCreate));
        assert!(manager.has_permission("user1", &Permission::TenantManage));
        assert!(manager.has_permission("user1", &Permission::BillingManage));

        // Test non-existent user
        assert!(!manager.has_permission("nonexistent", &Permission::CrawlCreate));
    }

    #[test]
    fn test_sla_monitor() {
        let monitor = SlaMonitor::with_default_config();

        // Record some successful requests
        for _ in 0..90 {
            monitor.record_success(100);
        }

        // Record some failures
        for _ in 0..10 {
            monitor.record_failure();
        }

        let metrics = monitor.metrics();
        assert_eq!(metrics.total_requests, 100);
        assert_eq!(metrics.successful_requests, 90);
        assert_eq!(metrics.failed_requests, 10);
        assert!((metrics.error_rate - 10.0).abs() < 0.01);
    }
}