cardamon 0.2.0

Cardamon is a tool to help development teams measure the power consumption and carbon emissions of their software.
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
use crate::{
    dao::{
        self,
        pagination::{Page, Pages},
    },
    data::dataset::{Dataset, IterationMetrics},
};
use anyhow::Context;
use sea_orm::DatabaseConnection;
use tracing::trace;

#[derive(Debug)]
pub enum ScenarioSelection {
    All,
    One(String),
    InRun(String),
    InRange { from: i64, to: i64 },
    Search(String),
}

#[derive(Debug)]
pub enum RunSelection {
    All,
    InRange { from: i64, to: i64 },
    LastN(u64),
}

/// # DatasetBuilder
///
/// DatasetBuilder => DatasetRowPager => DatasetRows => DatasetColPager => DatasetBuilderFinal => Dataset
///
/// The DatasetBuilder allows you to construct a Dataset. There is one case that is not allowed. If you have multiple
/// scenarios (rows) you cannot `page` over runs (columns).
///
/// Example: scenario_runs_by_page("add_10_items", 3, 2)
///  ================================================================================
/// ||  scenarios   || run_1  | run_2  | run_3  |   run_4   |   run_5   |   run_6   ||
/// ||--------------||--------|--------|--------|-----------|-----------|-----------||
/// ||              ||        |        |        | ********************************* ||
/// || add_10_items || <data> | <data> | <data> | * <data>  |  <data>   |  <data> * ||
/// ||              ||        |        |        | ********************************* ||
///  ================================================================================
///
/// Example: last 3 runs of [add_10_items, add_10_users, checkout]
///  ============================================
/// ||  scenarios   || run_1  | run_2  | run_3  ||
/// ||--------------||--------------------------||
/// || add_10_items || <data> | <data> |        ||
/// || add_10_users ||        | <data> | <data> ||
/// || checkout     || <data> |        | <data> ||
///  ============================================
///

pub struct DatasetBuilder;
impl DatasetBuilder {
    pub fn new() -> Self {
        DatasetBuilder
    }

    /// Returns a single scenario.
    pub fn scenario(&self, scenario: &str) -> DatasetRowPager {
        DatasetRowPager {
            scenario_selection: ScenarioSelection::One(scenario.to_string()),
        }
    }

    /// Returns all scenarios.
    pub fn scenarios_all(&self) -> DatasetRowPager {
        DatasetRowPager {
            scenario_selection: ScenarioSelection::All,
        }
    }

    /// Returns all scenarios that were executed in a single run.
    pub fn scenarios_in_run(&self, run: i32) -> DatasetRowPager {
        DatasetRowPager {
            scenario_selection: ScenarioSelection::InRun(run.to_string()),
        }
    }

    /// Returns all scenarios that were executed at some time within the given time range.
    ///
    /// * Arguments
    /// - from: unix timestamp in millis
    /// - to: unix timestamp n millis
    pub fn scenarios_in_range(&self, from: i64, to: i64) -> DatasetRowPager {
        DatasetRowPager {
            scenario_selection: ScenarioSelection::InRange { from, to },
        }
    }

    /// Returns a DatasetRowPager all scenarios that match the given name. This function does not fetch these
    /// scenarios, it just defines the maximum set of scenarios which can be filtered in subsequent
    /// steps.
    pub fn scenarios_by_name(&self, name: &str) -> DatasetRowPager {
        DatasetRowPager {
            scenario_selection: ScenarioSelection::Search(name.to_string()),
        }
    }
}

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

/// The DatasetRowPager defines an incomplete Dataset which includes set of scenarios (rows)
/// without any runs.
///
/// It provides functions to select a subset within that range of scenarios.
pub struct DatasetRowPager {
    scenario_selection: ScenarioSelection,
}
impl DatasetRowPager {
    /// Returns a DatasetRows object which defined the full set of scenarios defined by this
    /// DatasetRowPager.
    pub fn all(self) -> DatasetRows {
        DatasetRows {
            scenario_selection: self.scenario_selection,
            scenario_page: None,
        }
    }

    /// Returns a DatasetRows object which defines a subset of the scenarios defined by this
    /// DatasetRowPager.
    pub fn page(self, page_size: u64, page_num: u64) -> DatasetRows {
        let scenario_page = Page {
            size: page_size,
            num: page_num,
        };

        DatasetRows {
            scenario_selection: self.scenario_selection,
            scenario_page: Some(scenario_page),
        }
    }
}

/// The DatasetRows defines an incomplete Dataet defining a set of scenarios (rows) without any
/// runs. This contains an optional Page object which defines some subset of this set of scenarios.
/// If no Page is provided then DatasetRows defines the full range of scenarios instead of a single
/// page within it.
///
/// Example: page 2 (page size = 2) of the rows containing 4 scenarios.
///  ================================
/// ||     scenarios    || runs ... ||
/// ||------------------||----------||
/// ||   add_10_items   ||          ||
/// ||   add_10_users   ||          ||
/// || **************** ||    ...   ||
/// || * checkout     * ||          ||
/// || * search_item  * ||          ||
/// || **************** ||          ||
///  ================================
///
pub struct DatasetRows {
    scenario_selection: ScenarioSelection,
    scenario_page: Option<Page>,
}
impl DatasetRows {
    /// Return a DataColPager which includes all the runs for this scenario.
    pub fn runs_all(self) -> DatasetColPager {
        DatasetColPager {
            scenario_selection: self.scenario_selection,
            scenario_page: self.scenario_page,
            run_selection: RunSelection::All,
        }
    }

    /// Return a DatasetColPager which includes only those runs which were executed within the
    /// given time range.
    ///
    /// * Arguments
    /// - from: unix timestamp in millis
    /// - to: unix timestamp in millis
    pub fn runs_in_range(self, from: i64, to: i64) -> DatasetColPager {
        DatasetColPager {
            scenario_selection: self.scenario_selection,
            scenario_page: self.scenario_page,
            run_selection: RunSelection::InRange { from, to },
        }
    }

    /// Returns a DatasetColPager which includes only the last `n` runs of the given scenario.
    ///
    /// * Arguments
    /// - n: number of runs to include.
    pub fn last_n_runs(self, n: u64) -> DatasetColPager {
        DatasetColPager {
            scenario_selection: self.scenario_selection,
            scenario_page: self.scenario_page,
            run_selection: RunSelection::LastN(n),
        }
    }
}

/// The DatasetColPager defines an incomplete Dataset which includes a single scenario (row) and
/// range of runs for that scenario.
///
/// It provides a single function to select a single page within that range of runs.
#[derive(Debug)]
pub struct DatasetColPager {
    scenario_selection: ScenarioSelection,
    scenario_page: Option<Page>,
    run_selection: RunSelection,
}
impl DatasetColPager {
    pub fn all(self) -> DatasetBuilderFinal {
        DatasetBuilderFinal {
            scenario_selection: self.scenario_selection,
            scenario_page: self.scenario_page,
            run_selection: self.run_selection,
            run_page: None,
        }
    }

    pub fn page(self, page_size: u64, page_num: u64) -> anyhow::Result<DatasetBuilderFinal> {
        trace!("page_size = {}", page_size);
        match self.scenario_selection {
            ScenarioSelection::One(_) => Ok(DatasetBuilderFinal {
                scenario_selection: self.scenario_selection,
                scenario_page: self.scenario_page,
                run_selection: self.run_selection,
                run_page: Some(Page {
                    size: page_size,
                    num: page_num,
                }),
            }),

            _ => Err(anyhow::anyhow!(
                "Unable to paginate over runs if multiple scenarios are selected."
            )),
        }
    }
}

pub struct DatasetBuilderFinal {
    scenario_selection: ScenarioSelection,
    scenario_page: Option<Page>,
    run_selection: RunSelection,
    run_page: Option<Page>,
}
impl DatasetBuilderFinal {
    async fn fetch_scenarios(
        &self,
        db: &DatabaseConnection,
    ) -> anyhow::Result<(Vec<String>, Pages)> {
        let (scenario_names, scenario_pages) = match &self.scenario_selection {
            ScenarioSelection::All => dao::scenario::fetch_all(&self.scenario_page, db).await,
            ScenarioSelection::One(name) => {
                let scenario_name = dao::scenario::fetch(name, db)
                    .await?
                    .context(format!("Error finding scenario with name {}", name))?;

                Ok((vec![scenario_name], Pages::NotRequired)) // if you only have one scenario then
                                                              // pages are not required!
            }
            ScenarioSelection::Search(name) => {
                dao::scenario::fetch_by_query(name, &self.scenario_page, db).await
            }
            ScenarioSelection::InRun(run) => {
                dao::scenario::fetch_in_run(run, &self.scenario_page, db).await
            }
            ScenarioSelection::InRange { from, to } => {
                dao::scenario::fetch_in_range(*from, *to, &self.scenario_page, db).await
            }
        }?;

        let scenario_names = scenario_names
            .iter()
            .map(|s| s.scenario_name.clone())
            .collect::<Vec<_>>();

        Ok((scenario_names, scenario_pages))
    }

    async fn all(&self, db: &DatabaseConnection) -> anyhow::Result<Dataset> {
        let (scenarios, total_scenarios) = self.fetch_scenarios(db).await?;

        let (iterations, total_runs) = match self.run_selection {
            RunSelection::All => dao::iteration::fetch_runs_all(&scenarios, None, db).await,

            RunSelection::InRange { from, to } => {
                dao::iteration::fetch_runs_in_range(&scenarios, from, to, None, db).await
            }

            RunSelection::LastN(n) => {
                dao::iteration::fetch_runs_last_n(&scenarios, n, None, db).await
            }
        }?;

        // marry up iterations with metrics
        // TODO: read from cache table first
        let mut iterations_with_metrics = vec![];
        for it in iterations {
            let metrics =
                dao::metrics::fetch_within(it.run_id, it.start_time, it.stop_time, db).await?;
            iterations_with_metrics.push(IterationMetrics::new(it, metrics));
        }
        // println!("\n {:?}", iterations_with_metrics);

        // TODO: cache the iterations/metrics data
        //

        Ok(Dataset::new(
            iterations_with_metrics,
            total_scenarios,
            total_runs,
        ))
    }

    async fn page(&self, page: &Page, db: &DatabaseConnection) -> anyhow::Result<Dataset> {
        let page = Page::new(page.size, page.num);
        let (scenarios, total_scenarios) = self.fetch_scenarios(db).await?;

        let (iterations, total_runs) = match self.run_selection {
            RunSelection::All => dao::iteration::fetch_runs_all(&scenarios, Some(page), db).await,

            RunSelection::InRange { from, to } => {
                dao::iteration::fetch_runs_in_range(&scenarios, from, to, Some(page), db).await
            }

            RunSelection::LastN(n) => {
                dao::iteration::fetch_runs_last_n(&scenarios, n, Some(page), db).await
            }
        }?;

        // marry up iterations with metrics
        // TODO: read from cache table first
        let mut iterations_with_metrics = vec![];
        for it in iterations {
            let metrics =
                dao::metrics::fetch_within(it.run_id, it.start_time, it.stop_time, db).await?;
            iterations_with_metrics.push(IterationMetrics::new(it, metrics));
        }

        // TODO: cache the iterations/metrics data
        //

        Ok(Dataset::new(
            iterations_with_metrics,
            total_scenarios,
            total_runs,
        ))
    }

    pub async fn build(&self, db: &DatabaseConnection) -> anyhow::Result<Dataset> {
        match &self.run_page {
            Some(page) => self.page(page, db).await,
            None => self.all(db).await,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        data::{dataset::LiveDataFilter, dataset_builder::DatasetBuilder},
        db_connect, db_migrate,
        tests::setup_fixtures,
    };
    use itertools::Itertools;
    use sea_orm::DatabaseConnection;

    async fn init_tests() -> anyhow::Result<DatabaseConnection> {
        let db = db_connect("sqlite::memory:", None).await?;
        db_migrate(&db).await?;
        setup_fixtures(
            &[
                "./fixtures/power_curves.sql",
                "./fixtures/cpus.sql",
                "./fixtures/runs.sql",
                "./fixtures/iterations.sql",
            ],
            &db,
        )
        .await?;

        Ok(db)
    }

    #[tokio::test]
    async fn scenarios_all() -> anyhow::Result<()> {
        let db = init_tests().await?;
        let dataset = DatasetBuilder::new()
            .scenarios_all()
            .all()
            .runs_all()
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);

        // there should be 3 scenarios in reverse chronological order
        // i.e. [scenario_3, scenario_2, scenario_1]
        let scenario_names = scenario_datasets
            .iter()
            .map(|dataset| dataset.scenario_name())
            .collect_vec();
        assert_eq!(
            scenario_names,
            vec!["scenario_3", "scenario_2", "scenario_1"]
        );

        Ok(())
    }

    #[tokio::test]
    async fn scenario() -> anyhow::Result<()> {
        let db = init_tests().await?;
        let dataset = DatasetBuilder::new()
            .scenario("scenario_2")
            .all()
            .runs_all()
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);

        // there should be one scenario (the one we selected) in reverse chronological order
        // ie. [scenario_2]
        let scenario_names = scenario_datasets
            .iter()
            .map(|dataset| dataset.scenario_name())
            .collect_vec();
        assert_eq!(scenario_names, vec!["scenario_2"]);

        Ok(())
    }

    #[tokio::test]
    async fn scenarios_in_run() -> anyhow::Result<()> {
        let db = init_tests().await?;
        let dataset = DatasetBuilder::new()
            .scenarios_in_run(2)
            .all()
            .runs_all()
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);

        // there should be two scenarios (the ones in run 2) in reverse chronological order
        // ie. [scenario_3, scenario_2]
        let scenario_names = scenario_datasets
            .iter()
            .map(|dataset| dataset.scenario_name())
            .collect_vec();
        assert_eq!(scenario_names, vec!["scenario_3", "scenario_2"]);

        Ok(())
    }

    #[tokio::test]
    async fn scenarios_in_range() -> anyhow::Result<()> {
        let db = init_tests().await?;
        let dataset = DatasetBuilder::new()
            .scenarios_in_range(1717507690000, 1717507699000)
            .all()
            .runs_all()
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);

        // there should be two scenarios (the ones in the given time range) in reverse
        // chronological order
        // ie. [scenario_3, scenario_2]
        let scenario_names = scenario_datasets
            .iter()
            .map(|dataset| dataset.scenario_name())
            .collect_vec();
        assert_eq!(scenario_names, vec!["scenario_3", "scenario_2"]);

        Ok(())
    }

    #[tokio::test]
    async fn scenarios_search() -> anyhow::Result<()> {
        let db = init_tests().await?;
        let dataset = DatasetBuilder::new()
            .scenarios_by_name("scenario")
            .all()
            .runs_all()
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);

        // there should be three scenarios (all matching "scenario") in reverse chronological order
        // ie. [scenario_3, scenario_2, scenario_1]
        let scenario_names = scenario_datasets
            .iter()
            .map(|dataset| dataset.scenario_name())
            .collect_vec();
        assert_eq!(
            scenario_names,
            vec!["scenario_3", "scenario_2", "scenario_1"]
        );

        Ok(())
    }

    #[tokio::test]
    async fn runs_all() -> anyhow::Result<()> {
        let db = init_tests().await?;

        // single scenario
        let dataset = DatasetBuilder::new()
            .scenario("scenario_3")
            .all()
            .runs_all()
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);
        let run_datasets = scenario_datasets.first().unwrap().by_run();

        // there should be three runs for scenario 3 returned in reverse chronological order
        // ie. [3, 2, 1]
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(run_ids, vec![3, 2, 1]);

        // multiple scenarios
        let dataset = DatasetBuilder::new()
            .scenarios_all()
            .all()
            .runs_all()
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);

        // there should be 3 runs for scenario_3, 2 for scenario_2 and 1 for scenario_1 in reverse
        // chronological order
        // ie. scenario_3 = [3,2,1], scenario_2 = [2,1], scenario_1 = [1]
        let scenario_dataset = scenario_datasets.first().unwrap();
        let run_datasets = scenario_dataset.by_run();
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(scenario_dataset.scenario_name(), "scenario_3");
        assert_eq!(run_ids, vec![3, 2, 1]);

        let scenario_dataset = scenario_datasets.get(1).unwrap();
        let run_datasets = scenario_dataset.by_run();
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(scenario_dataset.scenario_name(), "scenario_2");
        assert_eq!(run_ids, vec![2, 1]);

        let scenario_dataset = scenario_datasets.get(2).unwrap();
        let run_datasets = scenario_dataset.by_run();
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(scenario_dataset.scenario_name(), "scenario_1");
        assert_eq!(run_ids, vec![1]);

        Ok(())
    }

    #[tokio::test]
    async fn runs_in_range() -> anyhow::Result<()> {
        let db = init_tests().await?;

        // single scenario
        let dataset = DatasetBuilder::new()
            .scenario("scenario_3")
            .all()
            .runs_in_range(1717507690000, 1717507795000)
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);
        let run_datasets = scenario_datasets.first().unwrap().by_run();

        // there should be 2 runs for scenario 3 returned in reverse chronological order
        // ie. [3, 2]
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(run_ids, vec![3, 2]);

        // multiple scenarios
        let dataset = DatasetBuilder::new()
            .scenarios_all()
            .all()
            .runs_in_range(1717507690000, 1717507795000)
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);

        // there should be 2 runs for scenario_3 and 1 for scenario_2 in reverse chronological order
        // ie. scenario_3 = [3,2], scenario_2 = [2]
        let scenario_dataset = scenario_datasets.first().unwrap();
        let run_datasets = scenario_dataset.by_run();
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(scenario_dataset.scenario_name(), "scenario_3");
        assert_eq!(run_ids, vec![3, 2]);

        let scenario_dataset = scenario_datasets.get(1).unwrap();
        let run_datasets = scenario_dataset.by_run();
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(scenario_dataset.scenario_name(), "scenario_2");
        assert_eq!(run_ids, vec![2]);

        Ok(())
    }

    #[tokio::test]
    async fn runs_last_n() -> anyhow::Result<()> {
        let db = init_tests().await?;

        // single scenario
        let dataset = DatasetBuilder::new()
            .scenario("scenario_3")
            .all()
            .last_n_runs(2)
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);
        let run_datasets = scenario_datasets.first().unwrap().by_run();

        // there should be 2 runs for scenario 3 returned in reverse chronological order
        // ie. [3, 2]
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(run_ids, vec![3, 2]);

        // multiple scenarios
        let dataset = DatasetBuilder::new()
            .scenarios_all()
            .all()
            .last_n_runs(2)
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);

        // there should be 2 runs for scenario_3, 2 for scenario_2 and 1 for scenario_1 in reverse chronological order
        // ie. scenario_3 = [3,2], scenario_2 = [2,1], scenario_1 = [1]
        let scenario_dataset = scenario_datasets.first().unwrap();
        let run_datasets = scenario_dataset.by_run();
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(scenario_dataset.scenario_name(), "scenario_3");
        assert_eq!(run_ids, vec![3, 2]);

        let scenario_dataset = scenario_datasets.get(1).unwrap();
        let run_datasets = scenario_dataset.by_run();
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(scenario_dataset.scenario_name(), "scenario_2");
        assert_eq!(run_ids, vec![2, 1]);

        let scenario_dataset = scenario_datasets.get(2).unwrap();
        let run_datasets = scenario_dataset.by_run();
        let run_ids = run_datasets
            .iter()
            .map(|dataset| dataset.run_id())
            .collect_vec();
        assert_eq!(scenario_dataset.scenario_name(), "scenario_1");
        assert_eq!(run_ids, vec![1]);

        Ok(())
    }

    #[tokio::test]
    async fn iterations() -> anyhow::Result<()> {
        let db = init_tests().await?;

        // single scenario
        let dataset = DatasetBuilder::new()
            .scenario("scenario_3")
            .all()
            .last_n_runs(1)
            .all()
            .build(&db)
            .await?;
        let scenario_datasets = dataset.by_scenario(LiveDataFilter::IncludeLive);
        let run_datasets = scenario_datasets.first().unwrap().by_run();
        let run_dataset = run_datasets.first().unwrap();

        // there should be three runs for scenario 3 returned in reverse chronological order
        // ie. [3]
        let iteration_metrics = run_dataset.by_iteration();
        assert_eq!(iteration_metrics.len(), 3);

        Ok(())
    }
}