pipedash-core 0.2.2

Framework-agnostic core library for Pipedash
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
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use dashmap::DashSet;
use tokio::sync::Semaphore;
use tokio::time::timeout;

const MAX_CONCURRENT_PROVIDER_FETCHES: usize = 10;

use super::metrics_service::MetricsService;
use super::provider_service::ProviderService;
use crate::domain::{
    DomainError,
    DomainResult,
    PaginatedRunHistory,
    Pipeline,
    PipelineRun,
    TriggerParams,
};
use crate::event::{
    CacheInvalidationReason,
    CoreEvent,
    EventBus,
};
use crate::infrastructure::database::Repository;
use crate::infrastructure::deduplication::{
    hash_pipeline_run,
    hash_request,
    RequestDeduplicator,
};

pub struct PipelineService {
    repository: Arc<Repository>,
    provider_service: Arc<ProviderService>,
    metrics_service: Option<Arc<MetricsService>>,
    event_bus: Arc<dyn EventBus>,
    deduplicator: Arc<RequestDeduplicator<Vec<Pipeline>>>,
    run_deduplicator: Arc<RequestDeduplicator<Vec<PipelineRun>>>,
    cache_write_tracker: Arc<DashSet<String>>,
}

impl PipelineService {
    pub fn new(
        repository: Arc<Repository>, provider_service: Arc<ProviderService>,
        metrics_service: Option<Arc<MetricsService>>, event_bus: Arc<dyn EventBus>,
    ) -> Self {
        Self {
            repository,
            provider_service,
            metrics_service,
            event_bus,
            deduplicator: Arc::new(RequestDeduplicator::new()),
            run_deduplicator: Arc::new(RequestDeduplicator::new()),
            cache_write_tracker: Arc::new(DashSet::new()),
        }
    }

    async fn update_provider_status_and_emit(
        &self, provider_id: i64, success: bool, error: Option<String>,
    ) {
        if let Ok(changed) = self
            .repository
            .update_provider_fetch_status(provider_id, success, error)
            .await
        {
            if changed {
                self.event_bus.emit(CoreEvent::ProvidersChanged).await;
            }
        }
    }

    pub async fn fetch_pipelines(&self, provider_id: Option<i64>) -> DomainResult<Vec<Pipeline>> {
        if let Err(_e) = self.repository.clear_workflow_parameters_cache().await {}

        if let Some(pid) = provider_id {
            let provider = self.provider_service.get_provider(pid).await?;

            let result = timeout(Duration::from_secs(30), provider.fetch_pipelines()).await;

            match result {
                Ok(Ok(pipelines)) => {
                    self.repository
                        .update_pipelines_cache(pid, &pipelines)
                        .await?;

                    self.update_provider_status_and_emit(pid, true, None).await;

                    self.event_bus
                        .emit(CoreEvent::PipelineCacheInvalidated {
                            provider_id: Some(pid),
                            reason: CacheInvalidationReason::Fetch,
                        })
                        .await;

                    let timestamp = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_millis() as i64;

                    self.event_bus
                        .emit(CoreEvent::PipelinesUpdated {
                            pipelines: pipelines.clone(),
                            provider_id: Some(pid),
                            timestamp,
                        })
                        .await;

                    Ok(pipelines)
                }
                Ok(Err(e)) => {
                    let error_msg = format!("{e}");
                    self.update_provider_status_and_emit(pid, false, Some(error_msg))
                        .await;

                    Err(e)
                }
                Err(_elapsed) => {
                    let error_msg = "Connection timeout - provider did not respond".to_string();
                    self.update_provider_status_and_emit(pid, false, Some(error_msg.clone()))
                        .await;

                    Err(DomainError::ProviderError(error_msg))
                }
            }
        } else {
            let provider_summaries = self.provider_service.list_providers().await?;

            let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_PROVIDER_FETCHES));

            let futures: Vec<_> = provider_summaries
                .into_iter()
                .map(|summary| {
                    let semaphore = semaphore.clone();
                    let provider_service = self.provider_service.clone();
                    let deduplicator = self.deduplicator.clone();
                    let repository = self.repository.clone();
                    let event_bus = self.event_bus.clone();
                    let provider_id = summary.id;
                    async move {
                        let _permit = semaphore.acquire().await.expect("semaphore closed");
                        let request_id = hash_request(provider_id, "fetch_pipelines");

                        let result = timeout(
                            Duration::from_secs(30),
                            deduplicator.deduplicate(request_id, || async {
                                let provider = provider_service.get_provider(provider_id).await?;
                                provider.fetch_pipelines().await
                            }),
                        )
                        .await;

                        match result {
                            Ok(Ok(pipelines)) => {
                                if let Ok(changed) = repository
                                    .update_provider_fetch_status(provider_id, true, None)
                                    .await
                                {
                                    if changed {
                                        event_bus.emit(CoreEvent::ProvidersChanged).await;
                                    }
                                }

                                Ok((provider_id, pipelines))
                            }
                            Ok(Err(e)) => {
                                let error_msg = format!("{e}");
                                if let Ok(changed) = repository
                                    .update_provider_fetch_status(
                                        provider_id,
                                        false,
                                        Some(error_msg),
                                    )
                                    .await
                                {
                                    if changed {
                                        event_bus.emit(CoreEvent::ProvidersChanged).await;
                                    }
                                }

                                Err(e)
                            }
                            Err(_elapsed) => {
                                let error_msg =
                                    "Connection timeout - provider did not respond".to_string();
                                if let Ok(changed) = repository
                                    .update_provider_fetch_status(
                                        provider_id,
                                        false,
                                        Some(error_msg.clone()),
                                    )
                                    .await
                                {
                                    if changed {
                                        event_bus.emit(CoreEvent::ProvidersChanged).await;
                                    }
                                }

                                Err(DomainError::ProviderError(error_msg))
                            }
                        }
                    }
                })
                .collect();

            let results =
                timeout(Duration::from_secs(60), futures::future::join_all(futures)).await;

            let results = match results {
                Ok(results) => results,
                Err(_) => {
                    tracing::warn!("Overall provider fetch timeout (60s exceeded)");
                    return Err(DomainError::ProviderError(
                        "Overall provider fetch timeout (60s exceeded)".into(),
                    ));
                }
            };

            let mut all_pipelines = Vec::new();
            for result in results {
                match result {
                    Ok((provider_id, pipelines)) => {
                        self.repository
                            .update_pipelines_cache(provider_id, &pipelines)
                            .await?;

                        self.event_bus
                            .emit(CoreEvent::PipelineCacheInvalidated {
                                provider_id: Some(provider_id),
                                reason: CacheInvalidationReason::Fetch,
                            })
                            .await;

                        all_pipelines.extend(pipelines);
                    }
                    Err(_e) => {}
                }
            }

            let timestamp = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_millis() as i64;

            self.event_bus
                .emit(CoreEvent::PipelinesUpdated {
                    pipelines: all_pipelines.clone(),
                    provider_id: None,
                    timestamp,
                })
                .await;

            Ok(all_pipelines)
        }
    }

    pub async fn get_cached_pipelines(
        &self, provider_id: Option<i64>,
    ) -> DomainResult<Vec<Pipeline>> {
        self.repository.get_cached_pipelines(provider_id).await
    }

    pub async fn fetch_pipelines_lazy(
        &self, provider_id: Option<i64>, page: usize, page_size: usize,
    ) -> DomainResult<pipedash_plugin_api::PaginatedResponse<Pipeline>> {
        use pipedash_plugin_api::PaginatedResponse;

        if let Some(pid) = provider_id {
            let provider = self.provider_service.get_provider(pid).await?;
            let result = provider.fetch_pipelines_paginated(page, page_size).await?;

            if page == 1 {
                self.repository
                    .update_pipelines_cache(pid, &result.items)
                    .await?;
            }

            Ok(result)
        } else {
            let all_pipelines = self.fetch_pipelines(None).await?;
            let total_count = all_pipelines.len();
            let start = (page - 1) * page_size;
            let end = start + page_size;

            let pipelines = if start < total_count {
                all_pipelines[start..end.min(total_count)].to_vec()
            } else {
                Vec::new()
            };

            Ok(PaginatedResponse::new(
                pipelines,
                page,
                page_size,
                total_count,
            ))
        }
    }

    pub async fn fetch_run_history(
        &self, pipeline_id: &str, limit: usize,
    ) -> DomainResult<Vec<PipelineRun>> {
        let cached_pipelines = self.repository.get_cached_pipelines(None).await?;
        let pipeline = cached_pipelines
            .iter()
            .find(|p| p.id == pipeline_id)
            .ok_or_else(|| DomainError::PipelineNotFound(pipeline_id.to_string()))?;

        let provider_id = pipeline.provider_id;
        let pipeline_id_owned = pipeline_id.to_string();

        let request_id = hash_request(
            provider_id,
            &format!("fetch_runs_{}_{}", pipeline_id, limit),
        );

        let provider_service = self.provider_service.clone();
        let run_deduplicator = self.run_deduplicator.clone();

        let api_runs = run_deduplicator
            .deduplicate(request_id, || async move {
                let provider = provider_service.get_provider(provider_id).await?;
                provider.fetch_run_history(&pipeline_id_owned, limit).await
            })
            .await?;

        let cached_with_hashes = self
            .repository
            .get_cached_runs_with_hashes(pipeline_id)
            .await?;

        let mut api_map: HashMap<i64, (PipelineRun, String)> = HashMap::new();
        for run in &api_runs {
            let status_str = run.status.as_str();
            let hash = hash_pipeline_run(
                run.run_number,
                status_str,
                run.branch.as_deref(),
                &run.started_at.to_rfc3339(),
                run.duration_seconds,
                run.commit_sha.as_deref(),
            );
            api_map.insert(run.run_number, (run.clone(), hash));
        }

        let mut new_runs = Vec::new();
        let mut changed_runs = Vec::new();
        let mut deleted_run_numbers = Vec::new();

        for (run_number, (run, api_hash)) in &api_map {
            if let Some((_, cached_hash)) = cached_with_hashes.get(run_number) {
                if api_hash != cached_hash {
                    changed_runs.push(run.clone());
                }
            } else {
                new_runs.push(run.clone());
            }
        }

        let min_api_run_number = api_runs
            .iter()
            .map(|r| r.run_number)
            .min()
            .unwrap_or(i64::MAX);
        for run_number in cached_with_hashes.keys() {
            if *run_number >= min_api_run_number && !api_map.contains_key(run_number) {
                deleted_run_numbers.push(*run_number);
            }
        }

        if !new_runs.is_empty() || !changed_runs.is_empty() || !deleted_run_numbers.is_empty() {
            self.repository
                .merge_run_cache(pipeline_id, new_runs, changed_runs, deleted_run_numbers)
                .await?;

            self.event_bus
                .emit(CoreEvent::RunHistoryCacheInvalidated {
                    pipeline_id: Some(pipeline_id.to_string()),
                })
                .await;
        }

        Ok(api_runs)
    }

    pub async fn fetch_run_history_paginated(
        &self, pipeline_id: &str, page: usize, page_size: usize,
    ) -> DomainResult<PaginatedRunHistory> {
        let start_idx = (page - 1) * page_size;
        let end_idx = start_idx + page_size;

        let cached_count = self.repository.get_cached_run_count(pipeline_id).await?;

        if cached_count >= end_idx {
            let runs = self
                .repository
                .get_paginated_runs(pipeline_id, page, page_size)
                .await?;

            let is_complete = false;
            let total_pages = cached_count.div_ceil(page_size);

            if page == 1 {
                if let Some(metrics_service) = &self.metrics_service {
                    let pipeline_id_clone = pipeline_id.to_string();
                    let metrics_service_clone = metrics_service.clone();
                    let event_bus = self.event_bus.clone();
                    let repository = self.repository.clone();

                    tokio::spawn(async move {
                        match repository
                            .get_cached_run_history(&pipeline_id_clone, 10000)
                            .await
                        {
                            Ok(all_cached_runs) => {
                                match metrics_service_clone
                                    .extract_and_store_metrics(&pipeline_id_clone, &all_cached_runs)
                                    .await
                                {
                                    Ok(count) => {
                                        if count > 0 {
                                            event_bus
                                                .emit(CoreEvent::MetricsGenerated {
                                                    pipeline_id: pipeline_id_clone,
                                                })
                                                .await;
                                        }
                                    }
                                    Err(e) => {
                                        tracing::error!(
                                            error = %e,
                                            pipeline_id = %pipeline_id_clone,
                                            "Failed to extract metrics from cached runs"
                                        );
                                    }
                                }
                            }
                            Err(e) => {
                                tracing::warn!(
                                    error = %e,
                                    pipeline_id = %pipeline_id_clone,
                                    "Failed to load cached runs for metrics extraction"
                                );
                            }
                        }
                    });
                }
            } // end if page == 1

            return Ok(PaginatedRunHistory {
                runs,
                total_count: cached_count,
                has_more: !is_complete,
                is_complete,
                page,
                page_size,
                total_pages,
            });
        }

        let needed_runs = page * page_size;

        let github_pages_needed = (needed_runs / 100) + 1;
        let fetch_limit = (github_pages_needed * 100).min(1000);

        tracing::debug!(
            page = page,
            page_size = page_size,
            needed_runs = needed_runs,
            fetch_limit = fetch_limit,
            "Smart pagination: fetching only what's needed"
        );

        let all_runs = self.fetch_run_history(pipeline_id, fetch_limit).await?;
        let total_count = all_runs.len();

        let is_complete = total_count < fetch_limit;

        let repository = self.repository.clone();
        let pipeline_id_for_cache = pipeline_id.to_string();
        let runs_to_cache = all_runs.clone();
        let cache_tracker = self.cache_write_tracker.clone();
        tokio::spawn(async move {
            if !cache_tracker.insert(pipeline_id_for_cache.clone()) {
                return;
            }
            if let Err(e) = repository
                .cache_run_history(&pipeline_id_for_cache, &runs_to_cache)
                .await
            {
                tracing::warn!(error = %e, pipeline_id = %pipeline_id_for_cache, "Failed to cache run history");
            }
            cache_tracker.remove(&pipeline_id_for_cache);
        });

        if let Some(metrics_service) = &self.metrics_service {
            let pipeline_id_clone = pipeline_id.to_string();
            let runs_clone = all_runs.clone();
            let metrics_service_clone = metrics_service.clone();
            let event_bus = self.event_bus.clone();

            tokio::spawn(async move {
                match metrics_service_clone
                    .extract_and_store_metrics(&pipeline_id_clone, &runs_clone)
                    .await
                {
                    Ok(count) => {
                        if count > 0 {
                            event_bus
                                .emit(CoreEvent::MetricsGenerated {
                                    pipeline_id: pipeline_id_clone,
                                })
                                .await;
                        }
                    }
                    Err(e) => {
                        tracing::error!(
                            error = %e,
                            pipeline_id = %pipeline_id_clone,
                            "Failed to extract and store metrics"
                        );
                    }
                }
            });
        }

        let runs = if start_idx < total_count {
            let end = end_idx.min(total_count);
            all_runs[start_idx..end].to_vec()
        } else {
            Vec::new()
        };

        let total_pages = total_count.div_ceil(page_size);

        Ok(PaginatedRunHistory {
            runs,
            total_count,
            has_more: !is_complete,
            is_complete,
            page,
            page_size,
            total_pages,
        })
    }

    pub async fn fetch_run_details(
        &self, pipeline_id: &str, run_number: i64,
    ) -> DomainResult<PipelineRun> {
        let cached_pipelines = self.repository.get_cached_pipelines(None).await?;
        let pipeline = cached_pipelines
            .iter()
            .find(|p| p.id == pipeline_id)
            .ok_or_else(|| DomainError::PipelineNotFound(pipeline_id.to_string()))?;

        let provider = self
            .provider_service
            .get_provider(pipeline.provider_id)
            .await?;
        provider.fetch_run_details(pipeline_id, run_number).await
    }

    pub async fn trigger_pipeline(&self, params: TriggerParams) -> DomainResult<String> {
        let cached_pipelines = self.repository.get_cached_pipelines(None).await?;
        let pipeline = cached_pipelines
            .iter()
            .find(|p| p.id == params.workflow_id)
            .ok_or_else(|| DomainError::PipelineNotFound(params.workflow_id.clone()))?;

        let provider = self
            .provider_service
            .get_provider(pipeline.provider_id)
            .await?;
        let result = provider.trigger_pipeline(params.clone()).await?;

        self.event_bus
            .emit(CoreEvent::RunTriggered {
                workflow_id: params.workflow_id,
            })
            .await;

        Ok(result)
    }

    pub async fn cancel_run(&self, pipeline_id: &str, run_number: i64) -> DomainResult<()> {
        let cached_pipelines = self.repository.get_cached_pipelines(None).await?;
        let pipeline = cached_pipelines
            .iter()
            .find(|p| p.id == pipeline_id)
            .ok_or_else(|| DomainError::PipelineNotFound(pipeline_id.to_string()))?;

        let provider = self
            .provider_service
            .get_provider(pipeline.provider_id)
            .await?;
        provider.cancel_run(pipeline_id, run_number).await?;

        self.event_bus
            .emit(CoreEvent::RunCancelled {
                pipeline_id: pipeline_id.to_string(),
            })
            .await;

        Ok(())
    }

    pub async fn refresh_all(&self) -> DomainResult<()> {
        self.fetch_pipelines(None).await?;
        Ok(())
    }

    pub async fn clear_run_history_cache(&self, pipeline_id: &str) {
        let _ = self.repository.clear_cached_run_history(pipeline_id).await;
    }

    pub async fn clear_all_run_history_caches(&self) {
        let _ = self.repository.clear_all_run_history_cache().await;
    }

    pub async fn get_pipelines_cache_count(&self) -> DomainResult<i64> {
        self.repository.get_pipelines_cache_count().await
    }

    pub async fn get_run_history_cache_count(&self) -> DomainResult<i64> {
        self.repository.get_run_history_cache_count().await
    }

    pub async fn get_workflow_params_cache_count(&self) -> DomainResult<i64> {
        self.repository.get_workflow_params_cache_count().await
    }

    pub async fn clear_pipelines_cache(&self) -> DomainResult<usize> {
        self.repository.clear_pipelines_cache().await
    }

    pub async fn clear_workflow_params_cache(&self) -> DomainResult<()> {
        self.repository.clear_workflow_parameters_cache().await
    }

    pub async fn clear_all_caches_atomic(&self) -> DomainResult<()> {
        self.repository.clear_all_caches_atomic().await
    }

    pub async fn invalidate_run_cache(&self, pipeline_id: &str) {
        let _ = self.repository.clear_cached_run_history(pipeline_id).await;
    }

    pub async fn get_cached_run_history(
        &self, pipeline_id: &str, limit: usize,
    ) -> DomainResult<Vec<PipelineRun>> {
        self.repository
            .get_cached_run_history(pipeline_id, limit)
            .await
    }
}