perfgate-client 0.4.1

Client library for the perfgate baseline service
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
//! Fallback storage implementation.
//!
//! This module provides fallback storage when the server is unavailable.
//! It wraps the `BaselineClient` and falls back to local file storage on errors.

use crate::client::BaselineClient;
use crate::config::FallbackStorage;
use crate::error::ClientError;
use crate::types::*;
use std::path::PathBuf;
use tokio::fs;
use tracing::debug;

/// Client with fallback storage support.
///
/// This client wraps the main `BaselineClient` and provides automatic
/// fallback to local storage when the server is unavailable.
#[derive(Debug)]
pub struct FallbackClient {
    client: BaselineClient,
    fallback: Option<LocalFallbackStorage>,
}

impl FallbackClient {
    /// Creates a new fallback client.
    pub fn new(client: BaselineClient, fallback: Option<FallbackStorage>) -> Self {
        let local_fallback = fallback.map(|f| match f {
            FallbackStorage::Local { dir } => LocalFallbackStorage::new(dir),
        });

        Self {
            client,
            fallback: local_fallback,
        }
    }

    /// Gets the underlying client.
    pub fn inner(&self) -> &BaselineClient {
        &self.client
    }

    /// Gets the latest baseline with fallback support.
    ///
    /// First tries the server, then falls back to local storage if available.
    pub async fn get_latest_baseline(
        &self,
        project: &str,
        benchmark: &str,
    ) -> Result<BaselineRecord, ClientError> {
        match self.client.get_latest_baseline(project, benchmark).await {
            Ok(record) => Ok(record),
            Err(e) if e.is_connection_error() => {
                if let Some(fallback) = &self.fallback {
                    debug!(
                        project = %project,
                        benchmark = %benchmark,
                        "Server unavailable, falling back to local storage"
                    );
                    fallback.get_latest_baseline(project, benchmark).await
                } else {
                    Err(e)
                }
            }
            Err(e) => Err(e),
        }
    }

    /// Gets a specific baseline version with fallback support.
    pub async fn get_baseline_version(
        &self,
        project: &str,
        benchmark: &str,
        version: &str,
    ) -> Result<BaselineRecord, ClientError> {
        match self
            .client
            .get_baseline_version(project, benchmark, version)
            .await
        {
            Ok(record) => Ok(record),
            Err(e) if e.is_connection_error() => {
                if let Some(fallback) = &self.fallback {
                    debug!(
                        project = %project,
                        benchmark = %benchmark,
                        version = %version,
                        "Server unavailable, falling back to local storage"
                    );
                    fallback
                        .get_baseline_version(project, benchmark, version)
                        .await
                } else {
                    Err(e)
                }
            }
            Err(e) => Err(e),
        }
    }

    /// Uploads a baseline with fallback support.
    ///
    /// If the server is unavailable and fallback is configured, saves to local storage.
    pub async fn upload_baseline(
        &self,
        project: &str,
        request: &UploadBaselineRequest,
    ) -> Result<UploadBaselineResponse, ClientError> {
        match self.client.upload_baseline(project, request).await {
            Ok(response) => Ok(response),
            Err(e) if e.is_connection_error() => {
                if let Some(fallback) = &self.fallback {
                    debug!(
                        project = %project,
                        benchmark = %request.benchmark,
                        "Server unavailable, saving to local fallback storage"
                    );
                    fallback.save_baseline(project, request).await
                } else {
                    Err(e)
                }
            }
            Err(e) => Err(e),
        }
    }

    /// Lists baselines (server only, no fallback).
    pub async fn list_baselines(
        &self,
        project: &str,
        query: &ListBaselinesQuery,
    ) -> Result<ListBaselinesResponse, ClientError> {
        self.client.list_baselines(project, query).await
    }

    /// Deletes a baseline (server only, no fallback).
    pub async fn delete_baseline(
        &self,
        project: &str,
        benchmark: &str,
        version: &str,
    ) -> Result<(), ClientError> {
        self.client
            .delete_baseline(project, benchmark, version)
            .await
    }

    /// Promotes a baseline (server only, no fallback).
    pub async fn promote_baseline(
        &self,
        project: &str,
        benchmark: &str,
        request: &PromoteBaselineRequest,
    ) -> Result<PromoteBaselineResponse, ClientError> {
        self.client
            .promote_baseline(project, benchmark, request)
            .await
    }

    /// Checks server health.
    pub async fn health_check(&self) -> Result<HealthResponse, ClientError> {
        self.client.health_check().await
    }

    /// Returns true if the server is healthy.
    pub async fn is_healthy(&self) -> bool {
        self.client.is_healthy().await
    }

    /// Checks if fallback storage is available.
    pub fn has_fallback(&self) -> bool {
        self.fallback.is_some()
    }
}

/// Local filesystem fallback storage.
#[derive(Debug)]
pub struct LocalFallbackStorage {
    dir: PathBuf,
}

impl LocalFallbackStorage {
    /// Creates a new local fallback storage.
    pub fn new(dir: PathBuf) -> Self {
        Self { dir }
    }

    /// Gets the latest baseline from local storage.
    pub async fn get_latest_baseline(
        &self,
        project: &str,
        benchmark: &str,
    ) -> Result<BaselineRecord, ClientError> {
        let project_dir = self.dir.join(project);

        let mut entries = match fs::read_dir(&project_dir).await {
            Ok(entries) => entries,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                // Directory doesn't exist means no baselines
                return Err(ClientError::NotFoundError(format!(
                    "No baseline found for {}/{}",
                    project, benchmark
                )));
            }
            Err(e) => {
                return Err(ClientError::FallbackError(format!(
                    "Failed to read directory: {}",
                    e
                )));
            }
        };

        let mut latest: Option<(String, BaselineRecord)> = None;

        while let Some(entry) = entries
            .next_entry()
            .await
            .map_err(|e| ClientError::FallbackError(format!("Failed to read entry: {}", e)))?
        {
            let file_name = entry.file_name();
            let name = file_name.to_string_lossy();

            // Check if file matches pattern
            if name.starts_with(&format!("{}-", benchmark)) && name.ends_with(".json") {
                let path = entry.path();
                let content = fs::read_to_string(&path).await.map_err(|e| {
                    ClientError::FallbackError(format!("Failed to read file: {}", e))
                })?;

                let record: BaselineRecord =
                    serde_json::from_str(&content).map_err(ClientError::ParseError)?;

                // Compare by created_at timestamp
                match &latest {
                    None => latest = Some((name.to_string(), record)),
                    Some((_, existing)) => {
                        if record.created_at > existing.created_at {
                            latest = Some((name.to_string(), record));
                        }
                    }
                }
            }
        }

        latest.map(|(_, record)| record).ok_or_else(|| {
            ClientError::NotFoundError(format!("No baseline found for {}/{}", project, benchmark))
        })
    }

    /// Gets a specific baseline version from local storage.
    pub async fn get_baseline_version(
        &self,
        project: &str,
        benchmark: &str,
        version: &str,
    ) -> Result<BaselineRecord, ClientError> {
        let file_name = format!("{}-{}.json", benchmark, version);
        let path = self.dir.join(project).join(&file_name);

        let content = fs::read_to_string(&path).await.map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                ClientError::NotFoundError(format!(
                    "Baseline {}/{} not found in fallback storage",
                    benchmark, version
                ))
            } else {
                ClientError::FallbackError(format!("Failed to read file: {}", e))
            }
        })?;

        serde_json::from_str(&content).map_err(ClientError::ParseError)
    }

    /// Saves a baseline to local storage.
    pub async fn save_baseline(
        &self,
        project: &str,
        request: &UploadBaselineRequest,
    ) -> Result<UploadBaselineResponse, ClientError> {
        // Ensure directory exists
        let project_dir = self.dir.join(project);
        fs::create_dir_all(&project_dir).await.map_err(|e| {
            ClientError::FallbackError(format!("Failed to create directory: {}", e))
        })?;

        // Generate version if not provided
        let version = request
            .version
            .clone()
            .unwrap_or_else(|| chrono::Utc::now().format("%Y%m%d-%H%M%S").to_string());

        // Create a baseline record
        let now = chrono::Utc::now();
        let record = BaselineRecord {
            schema: "perfgate.baseline.v1".to_string(),
            id: format!("local_{}", uuid::Uuid::new_v4()),
            project: project.to_string(),
            benchmark: request.benchmark.clone(),
            version: version.clone(),
            git_ref: request.git_ref.clone(),
            git_sha: request.git_sha.clone(),
            receipt: request.receipt.clone(),
            metadata: request.metadata.clone(),
            tags: request.tags.clone(),
            created_at: now,
            updated_at: now,
            content_hash: "local".to_string(),
            source: BaselineSource::Upload,
            deleted: false,
        };

        // Write to file
        let file_name = format!("{}-{}.json", request.benchmark, version);
        let path = project_dir.join(&file_name);
        let content = serde_json::to_string_pretty(&record).map_err(ClientError::ParseError)?;

        fs::write(&path, content)
            .await
            .map_err(|e| ClientError::FallbackError(format!("Failed to write file: {}", e)))?;

        debug!(
            project = %project,
            benchmark = %request.benchmark,
            version = %version,
            path = %path.display(),
            "Saved baseline to local fallback storage"
        );

        Ok(UploadBaselineResponse {
            id: record.id,
            benchmark: request.benchmark.clone(),
            version,
            created_at: now,
            etag: "\"local\"".to_string(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{ClientConfig, RetryConfig};
    use perfgate_types::{BenchMeta, HostInfo, RunMeta, RunReceipt, Stats, ToolInfo, U64Summary};
    use tempfile::tempdir;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn create_test_receipt(benchmark: &str) -> RunReceipt {
        RunReceipt {
            schema: "perfgate.run.v1".to_string(),
            tool: ToolInfo {
                name: "perfgate".to_string(),
                version: "0.1.0".to_string(),
            },
            run: RunMeta {
                id: "test".to_string(),
                started_at: "2026-01-01T00:00:00Z".to_string(),
                ended_at: "2026-01-01T00:01:00Z".to_string(),
                host: HostInfo {
                    os: "linux".to_string(),
                    arch: "x86_64".to_string(),
                    cpu_count: Some(8),
                    memory_bytes: Some(16000000000),
                    hostname_hash: None,
                },
            },
            bench: BenchMeta {
                name: benchmark.to_string(),
                cwd: None,
                command: vec!["./bench.sh".to_string()],
                repeat: 5,
                warmup: 1,
                work_units: None,
                timeout_ms: None,
            },
            samples: vec![],
            stats: Stats {
                wall_ms: U64Summary {
                    median: 100,
                    min: 90,
                    max: 110,
                },
                cpu_ms: None,
                page_faults: None,
                ctx_switches: None,
                max_rss_kb: None,
                binary_bytes: None,
                throughput_per_s: None,
            },
        }
    }

    fn create_test_upload_request(benchmark: &str) -> UploadBaselineRequest {
        UploadBaselineRequest {
            benchmark: benchmark.to_string(),
            version: Some("v1.0.0".to_string()),
            git_ref: None,
            git_sha: None,
            receipt: create_test_receipt(benchmark),
            metadata: Default::default(),
            tags: vec![],
            normalize: false,
        }
    }

    #[tokio::test]
    async fn test_fallback_get_latest_from_server() {
        let mock_server = MockServer::start().await;
        let temp_dir = tempdir().unwrap();

        Mock::given(method("GET"))
            .and(path("/projects/test-project/baselines/my-bench/latest"))
            .respond_with(ResponseTemplate::new(200).set_body_json(BaselineRecord {
                schema: "perfgate.baseline.v1".to_string(),
                id: "bl_123".to_string(),
                project: "test-project".to_string(),
                benchmark: "my-bench".to_string(),
                version: "v1.0.0".to_string(),
                git_ref: None,
                git_sha: None,
                receipt: create_test_receipt("my-bench"),
                metadata: Default::default(),
                tags: vec![],
                created_at: chrono::Utc::now(),
                updated_at: chrono::Utc::now(),
                content_hash: "abc123".to_string(),
                source: BaselineSource::Upload,
                deleted: false,
            }))
            .mount(&mock_server)
            .await;

        let config = ClientConfig::new(mock_server.uri())
            .with_retry(RetryConfig {
                max_retries: 0,
                ..Default::default()
            })
            .with_fallback(FallbackStorage::local(temp_dir.path()));

        let client = BaselineClient::new(config).unwrap();
        let fallback_client = FallbackClient::new(client, None);

        let result = fallback_client
            .get_latest_baseline("test-project", "my-bench")
            .await
            .unwrap();

        assert_eq!(result.id, "bl_123");
    }

    #[tokio::test]
    async fn test_fallback_get_latest_from_local() {
        let temp_dir = tempdir().unwrap();

        // Create a local baseline file
        let project_dir = temp_dir.path().join("test-project");
        fs::create_dir_all(&project_dir).await.unwrap();

        let record = BaselineRecord {
            schema: "perfgate.baseline.v1".to_string(),
            id: "local_123".to_string(),
            project: "test-project".to_string(),
            benchmark: "my-bench".to_string(),
            version: "v1.0.0".to_string(),
            git_ref: None,
            git_sha: None,
            receipt: create_test_receipt("my-bench"),
            metadata: Default::default(),
            tags: vec![],
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
            content_hash: "abc123".to_string(),
            source: BaselineSource::Upload,
            deleted: false,
        };

        let file_path = project_dir.join("my-bench-v1.0.0.json");
        fs::write(&file_path, serde_json::to_string_pretty(&record).unwrap())
            .await
            .unwrap();

        // Use a non-existent server to trigger fallback
        let config = ClientConfig::new("http://localhost:59999")
            .with_retry(RetryConfig {
                max_retries: 0,
                ..Default::default()
            })
            .with_fallback(FallbackStorage::local(temp_dir.path()));

        let client = BaselineClient::new(config).unwrap();
        let fallback_client =
            FallbackClient::new(client, Some(FallbackStorage::local(temp_dir.path())));

        let result = fallback_client
            .get_latest_baseline("test-project", "my-bench")
            .await
            .unwrap();

        assert_eq!(result.id, "local_123");
    }

    #[tokio::test]
    async fn test_fallback_save_to_local() {
        let temp_dir = tempdir().unwrap();

        // Use a non-existent server to trigger fallback
        let config = ClientConfig::new("http://localhost:59999")
            .with_retry(RetryConfig {
                max_retries: 0,
                ..Default::default()
            })
            .with_fallback(FallbackStorage::local(temp_dir.path()));

        let client = BaselineClient::new(config).unwrap();
        let fallback_client =
            FallbackClient::new(client, Some(FallbackStorage::local(temp_dir.path())));

        let request = create_test_upload_request("my-bench");
        let response = fallback_client
            .upload_baseline("test-project", &request)
            .await
            .unwrap();

        assert!(response.id.starts_with("local_"));
        assert_eq!(response.benchmark, "my-bench");

        // Verify file was created
        let project_dir = temp_dir.path().join("test-project");
        let file_path = project_dir.join("my-bench-v1.0.0.json");
        assert!(file_path.exists());
    }

    #[tokio::test]
    async fn test_fallback_not_found_error() {
        let temp_dir = tempdir().unwrap();

        // Use a non-existent server to trigger fallback
        let config = ClientConfig::new("http://localhost:59999")
            .with_retry(RetryConfig {
                max_retries: 0,
                ..Default::default()
            })
            .with_fallback(FallbackStorage::local(temp_dir.path()));

        let client = BaselineClient::new(config).unwrap();
        let fallback_client =
            FallbackClient::new(client, Some(FallbackStorage::local(temp_dir.path())));

        let result = fallback_client
            .get_latest_baseline("test-project", "nonexistent")
            .await;

        assert!(matches!(result, Err(ClientError::NotFoundError(_))));
    }
}