domain-status 0.2.1

Concurrent URL status checker that captures comprehensive metadata in SQLite.
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
//! Status server data structures.

use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use tokio::sync::Semaphore;

use crate::error_handling::ProcessingStats;
use crate::runtime_metrics::RuntimeMetrics;
use crate::utils::TimingStats;

/// High-level scan lifecycle phase for live status.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ScanPhase {
    /// Reading input and processing URLs.
    Scanning = 0,
    /// Input exhausted; waiting for in-flight tasks (or aborting on drain timeout).
    Draining = 1,
    /// Computing final stats and shutting down helpers.
    Finalizing = 2,
}

impl ScanPhase {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Scanning => "scanning",
            Self::Draining => "draining",
            Self::Finalizing => "finalizing",
        }
    }

    fn from_u8(value: u8) -> Self {
        match value {
            1 => Self::Draining,
            2 => Self::Finalizing,
            _ => Self::Scanning,
        }
    }
}

/// Atomic wrapper around [`ScanPhase`].
#[derive(Debug)]
pub struct AtomicPhase {
    value: AtomicU8,
}

impl AtomicPhase {
    pub fn new(phase: ScanPhase) -> Self {
        Self {
            value: AtomicU8::new(phase as u8),
        }
    }

    pub fn set(&self, phase: ScanPhase) {
        self.value.store(phase as u8, Ordering::SeqCst);
    }

    pub fn get(&self) -> ScanPhase {
        ScanPhase::from_u8(self.value.load(Ordering::SeqCst))
    }
}

impl Default for AtomicPhase {
    fn default() -> Self {
        Self::new(ScanPhase::Scanning)
    }
}

/// Tracks recent finished-URL throughput for windowed rate / ETA.
#[derive(Debug)]
pub struct ThroughputWindow {
    inner: Mutex<ThroughputWindowInner>,
}

#[derive(Debug)]
struct ThroughputWindowInner {
    prev_at: Instant,
    prev_finished: usize,
    rate: f64,
}

impl ThroughputWindow {
    pub fn new() -> Self {
        Self {
            inner: Mutex::new(ThroughputWindowInner {
                prev_at: Instant::now(),
                prev_finished: 0,
                rate: 0.0,
            }),
        }
    }

    /// Observe current finished count; returns the latest windowed rate (URLs/sec).
    ///
    /// Updates when at least ~1s has elapsed since the previous sample so scrapes
    /// denser than that reuse the last computed rate.
    pub fn observe(&self, finished: usize) -> f64 {
        let Ok(mut guard) = self.inner.lock() else {
            return 0.0;
        };
        let now = Instant::now();
        let dt = now.duration_since(guard.prev_at).as_secs_f64();
        if dt >= 1.0 {
            let delta = finished.saturating_sub(guard.prev_finished);
            #[allow(clippy::cast_precision_loss)]
            {
                guard.rate = delta as f64 / dt;
            }
            guard.prev_at = now;
            guard.prev_finished = finished;
        } else if finished < guard.prev_finished {
            // Counter reset (should not happen mid-run); resync baseline.
            guard.prev_finished = finished;
            guard.prev_at = now;
            guard.rate = 0.0;
        }
        guard.rate
    }
}

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

/// Shared state for the status server
#[derive(Clone)]
pub struct StatusState {
    /// Total lines in the input file (non-empty, non-comment lines)
    pub total_urls: Arc<AtomicUsize>,
    /// Total URLs that have entered processing or early-skip paths
    pub total_urls_attempted: Arc<AtomicUsize>,
    /// URLs that produced a persisted `url_status` row (matches finalize / `runs`)
    pub successful_urls: Arc<AtomicUsize>,
    pub failed_urls: Arc<AtomicUsize>,
    /// URLs skipped (invalid, SSRF-unsafe, or mid-process intentional skip)
    pub skipped_urls: Arc<AtomicUsize>,
    pub start_time: Arc<Instant>,
    pub error_stats: Arc<ProcessingStats>,
    /// Timing statistics for performance monitoring
    pub timing_stats: Option<Arc<TimingStats>>,
    /// Request-per-second limiter for configured rate reporting
    pub request_limiter: Option<Arc<crate::initialization::RateLimiter>>,
    /// Runtime retry/degradation counters
    pub runtime_metrics: Arc<RuntimeMetrics>,
    /// Run identifier for correlating metrics with database and logs
    pub run_id: Option<String>,
    /// Run start time as Unix timestamp in seconds
    pub run_start_time_unix_secs: Option<f64>,
    /// Current scan lifecycle phase
    pub phase: Arc<AtomicPhase>,
    /// Windowed throughput tracker shared across scrapes
    pub throughput_window: Arc<ThroughputWindow>,
    /// Configured concurrency limit (`max_concurrency`)
    pub max_concurrency: Option<usize>,
    /// Live semaphore for in-use concurrency reporting
    pub semaphore: Option<Arc<Semaphore>>,
}

/// JSON response for `/status` endpoint
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct StatusResponse {
    pub total_urls: usize,
    pub total_urls_attempted: usize,
    pub successful_urls: usize,
    pub failed_urls: usize,
    pub skipped_urls: usize,
    pub active_urls: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pending_urls: Option<usize>,
    /// Finished (`successful + failed + skipped`) / total × 100
    pub percentage_complete: f64,
    /// Dispatched (`attempted`) / total × 100
    pub percentage_dispatched: f64,
    pub elapsed_seconds: f64,
    /// Lifetime finished URLs per second
    pub rate_per_second: f64,
    /// Recent windowed finished URLs per second (~1s+ samples)
    pub windowed_rate_per_second: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub eta_seconds: Option<f64>,
    /// `scanning` | `draining` | `finalizing`
    pub phase: String,
    /// Configured request-rate limit when a limiter is enabled (not measured RPS)
    pub current_rps: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub concurrency_limit: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub concurrency_in_use: Option<usize>,
    pub retried_requests: usize,
    pub non_retriable_failures: usize,
    /// Persisted `url_partial_failures` rows (does not increment `failed_urls`).
    pub partial_failures: usize,
    /// Subset with `error_type` = `Satellite insert error`.
    pub satellite_insert_errors: usize,
    pub errors: ErrorCounts,
    pub warnings: WarningCounts,
    pub info: InfoCounts,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timing: Option<TimingSummary>,
}

/// Timing summary for status endpoint
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct TimingSummary {
    pub count: u64,
    pub averages: TimingMetrics,
}

/// Timing metrics in milliseconds
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct TimingMetrics {
    pub http_request_ms: u64,
    pub dns_forward_ms: u64,
    pub dns_reverse_ms: u64,
    pub dns_additional_ms: u64,
    pub tls_handshake_ms: u64,
    pub html_parsing_ms: u64,
    pub tech_detection_ms: u64,
    pub geoip_lookup_ms: u64,
    pub whois_lookup_ms: u64,
    pub total_ms: u64,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct ErrorCounts {
    pub total: usize,
    pub timeout: usize,
    pub connection_error: usize,
    pub http_error: usize,
    pub dns_error: usize,
    pub tls_error: usize,
    pub parse_error: usize,
    pub other_error: usize,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct WarningCounts {
    pub total: usize,
    pub missing_meta_description: usize,
    pub missing_title: usize,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct InfoCounts {
    pub total: usize,
    pub http_redirect: usize,
    pub https_redirect: usize,
    pub bot_detection_403: usize,
    pub multiple_redirects: usize,
}

/// Helper to build a minimal [`StatusState`] for tests.
#[cfg(test)]
pub(crate) fn test_status_state(
    total: usize,
    attempted: usize,
    successful: usize,
    failed: usize,
    skipped: usize,
) -> StatusState {
    StatusState {
        total_urls: Arc::new(AtomicUsize::new(total)),
        total_urls_attempted: Arc::new(AtomicUsize::new(attempted)),
        successful_urls: Arc::new(AtomicUsize::new(successful)),
        failed_urls: Arc::new(AtomicUsize::new(failed)),
        skipped_urls: Arc::new(AtomicUsize::new(skipped)),
        start_time: Arc::new(Instant::now()),
        error_stats: Arc::new(ProcessingStats::new()),
        timing_stats: None,
        request_limiter: None,
        runtime_metrics: Arc::new(RuntimeMetrics::default()),
        run_id: None,
        run_start_time_unix_secs: None,
        phase: Arc::new(AtomicPhase::default()),
        throughput_window: Arc::new(ThroughputWindow::new()),
        max_concurrency: None,
        semaphore: None,
    }
}

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

    #[test]
    fn test_status_state_atomic_operations() {
        let state = test_status_state(100, 0, 0, 0, 0);
        state.successful_urls.fetch_add(1, Ordering::SeqCst);
        assert_eq!(state.successful_urls.load(Ordering::SeqCst), 1);
        state.failed_urls.fetch_add(5, Ordering::SeqCst);
        assert_eq!(state.failed_urls.load(Ordering::SeqCst), 5);
    }

    #[test]
    fn test_status_state_clone() {
        let state = test_status_state(50, 0, 10, 2, 0);
        let cloned = state.clone();
        cloned.successful_urls.fetch_add(1, Ordering::SeqCst);
        assert_eq!(state.successful_urls.load(Ordering::SeqCst), 11);
    }

    #[test]
    fn test_status_response_serialization() {
        let response = StatusResponse {
            total_urls: 100,
            total_urls_attempted: 55,
            successful_urls: 50,
            failed_urls: 5,
            skipped_urls: 0,
            active_urls: 0,
            pending_urls: Some(45),
            percentage_complete: 50.0,
            percentage_dispatched: 55.0,
            elapsed_seconds: 30.5,
            rate_per_second: 1.64,
            windowed_rate_per_second: 2.0,
            eta_seconds: Some(20.0),
            phase: "scanning".to_string(),
            current_rps: Some(20),
            concurrency_limit: Some(50),
            concurrency_in_use: Some(10),
            retried_requests: 4,
            non_retriable_failures: 2,
            partial_failures: 0,
            satellite_insert_errors: 0,
            errors: ErrorCounts {
                total: 5,
                timeout: 2,
                connection_error: 1,
                http_error: 1,
                dns_error: 0,
                tls_error: 1,
                parse_error: 0,
                other_error: 0,
            },
            warnings: WarningCounts {
                total: 2,
                missing_meta_description: 1,
                missing_title: 1,
            },
            info: InfoCounts {
                total: 10,
                http_redirect: 5,
                https_redirect: 3,
                bot_detection_403: 1,
                multiple_redirects: 1,
            },
            timing: None,
        };

        let json = serde_json::to_string(&response).expect("Failed to serialize StatusResponse");
        assert!(json.contains("\"total_urls\":100"));
        assert!(json.contains("\"successful_urls\":50"));
        assert!(json.contains("\"skipped_urls\":0"));
        assert!(json.contains("\"percentage_complete\":50"));
        assert!(json.contains("\"phase\":\"scanning\""));
    }

    #[test]
    fn test_status_response_pending_urls_optional() {
        let response = StatusResponse {
            total_urls: 100,
            total_urls_attempted: 100,
            successful_urls: 100,
            failed_urls: 0,
            skipped_urls: 0,
            active_urls: 0,
            pending_urls: None,
            percentage_complete: 100.0,
            percentage_dispatched: 100.0,
            elapsed_seconds: 60.0,
            rate_per_second: 1.67,
            windowed_rate_per_second: 0.0,
            eta_seconds: Some(0.0),
            phase: "finalizing".to_string(),
            current_rps: None,
            concurrency_limit: None,
            concurrency_in_use: None,
            retried_requests: 0,
            non_retriable_failures: 0,
            partial_failures: 0,
            satellite_insert_errors: 0,
            errors: ErrorCounts {
                total: 0,
                timeout: 0,
                connection_error: 0,
                http_error: 0,
                dns_error: 0,
                tls_error: 0,
                parse_error: 0,
                other_error: 0,
            },
            warnings: WarningCounts {
                total: 0,
                missing_meta_description: 0,
                missing_title: 0,
            },
            info: InfoCounts {
                total: 0,
                http_redirect: 0,
                https_redirect: 0,
                bot_detection_403: 0,
                multiple_redirects: 0,
            },
            timing: None,
        };

        let json = serde_json::to_string(&response).expect("Failed to serialize");
        assert!(!json.contains("pending_urls"));
    }

    #[test]
    fn test_atomic_phase_transitions() {
        let phase = AtomicPhase::new(ScanPhase::Scanning);
        assert_eq!(phase.get(), ScanPhase::Scanning);
        phase.set(ScanPhase::Draining);
        assert_eq!(phase.get().as_str(), "draining");
        phase.set(ScanPhase::Finalizing);
        assert_eq!(phase.get().as_str(), "finalizing");
    }

    #[test]
    fn test_timing_summary_serialization() {
        let timing = TimingSummary {
            count: 100,
            averages: TimingMetrics {
                http_request_ms: 150,
                dns_forward_ms: 20,
                dns_reverse_ms: 15,
                dns_additional_ms: 10,
                tls_handshake_ms: 50,
                html_parsing_ms: 30,
                tech_detection_ms: 25,
                geoip_lookup_ms: 5,
                whois_lookup_ms: 100,
                total_ms: 415,
            },
        };

        let json = serde_json::to_string(&timing).expect("Failed to serialize TimingSummary");
        assert!(json.contains("\"count\":100"));
        assert!(json.contains("\"http_request_ms\":150"));
        assert!(json.contains("\"total_ms\":415"));
    }
}