crate_activity/
align_and_normalize.rs

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
crate::ix!();

pub fn align_and_normalize_data(
    version_downloads: &[VersionDownload],
    full_date_range:   &[NaiveDate],

) -> Vec<i64> {

    let downloads_map: HashMap<NaiveDate, i64> = version_downloads
        .iter()
        .map(|d| (*d.date(), *d.downloads())) // Dereference both the date and the downloads
        .collect();

    full_date_range
        .iter()
        .map(|date| *downloads_map.get(date).unwrap_or(&0)) // Fill missing dates with 0
        .collect()
}

pub fn debug_alignment(
    crate_a: &str,
    crate_b: &str,
    aligned_a: &[i64],
    aligned_b: &[i64],
) {
    println!("Aligned data for {} and {}:", crate_a, crate_b);
    println!("  Aligned A: {:?}", aligned_a);
    println!("  Aligned B: {:?}", aligned_b);
}

#[cfg(test)]
mod alignment_and_normalization_tests {
    use super::*;
    use chrono::NaiveDate;

    #[test]
    fn test_empty_input() {
        let version_downloads = [];
        let full_date_range = [];
        let result = align_and_normalize_data(&version_downloads, &full_date_range);
        assert_eq!(result, Vec::<i64>::new(), "Empty input should produce empty output.");
    }

    #[test]
    fn test_full_date_range_matches_data() {
        let version_downloads = [
            VersionDownloadBuilder::default()
                .version(1_i64)
                .downloads(100_i64)
                .date(NaiveDate::from_ymd_opt(2024, 12, 1).unwrap())
                .build()
                .unwrap(),
            VersionDownloadBuilder::default()
                .version(1_i64)
                .downloads(200_i64)
                .date(NaiveDate::from_ymd_opt(2024, 12, 2).unwrap())
                .build()
                .unwrap(),
        ];
        let full_date_range = vec![
            NaiveDate::from_ymd_opt(2024, 12, 1).unwrap(),
            NaiveDate::from_ymd_opt(2024, 12, 2).unwrap(),
        ];
        let result = align_and_normalize_data(&version_downloads, &full_date_range);
        assert_eq!(result, vec![100, 200], "All dates should align correctly.");
    }

    #[test]
    fn test_full_date_range_extends_beyond_data() {
        let version_downloads = [
            VersionDownloadBuilder::default()
                .version(1_i64)
                .downloads(200_i64)
                .date(NaiveDate::from_ymd_opt(2024, 12, 2).unwrap())
                .build()
                .unwrap(),
        ];
        let full_date_range = vec![
            NaiveDate::from_ymd_opt(2024, 12, 1).unwrap(),
            NaiveDate::from_ymd_opt(2024, 12, 2).unwrap(),
            NaiveDate::from_ymd_opt(2024, 12, 3).unwrap(),
        ];
        let result = align_and_normalize_data(&version_downloads, &full_date_range);
        assert_eq!(
            result,
            vec![0, 200, 0],
            "Missing dates should be filled with 0."
        );
    }

    #[test]
    fn test_full_date_range_subset_of_data() {
        let version_downloads = [
            VersionDownloadBuilder::default()
                .version(1_i64)
                .downloads(100_i64)
                .date(NaiveDate::from_ymd_opt(2024, 12, 1).unwrap())
                .build()
                .unwrap(),
            VersionDownloadBuilder::default()
                .version(1_i64)
                .downloads(200_i64)
                .date(NaiveDate::from_ymd_opt(2024, 12, 2).unwrap())
                .build()
                .unwrap(),
            VersionDownloadBuilder::default()
                .version(1_i64)
                .downloads(300_i64)
                .date(NaiveDate::from_ymd_opt(2024, 12, 3).unwrap())
                .build()
                .unwrap(),
        ];
        let full_date_range = vec![
            NaiveDate::from_ymd_opt(2024, 12, 2).unwrap(),
            NaiveDate::from_ymd_opt(2024, 12, 3).unwrap(),
        ];
        let result = align_and_normalize_data(&version_downloads, &full_date_range);
        assert_eq!(
            result,
            vec![200, 300],
            "Only values within the full_date_range should be included."
        );
    }

    #[test]
    fn test_duplicate_dates_in_input() {
        let version_downloads = [
            VersionDownloadBuilder::default()
                .version(1_i64)
                .downloads(100_i64)
                .date(NaiveDate::from_ymd_opt(2024, 12, 2).unwrap())
                .build()
                .unwrap(),
            VersionDownloadBuilder::default()
                .version(1_i64)
                .downloads(200_i64)
                .date(NaiveDate::from_ymd_opt(2024, 12, 2).unwrap())
                .build()
                .unwrap(), // Duplicate date
        ];
        let full_date_range = vec![
            NaiveDate::from_ymd_opt(2024, 12, 2).unwrap(),
        ];
        let result = align_and_normalize_data(&version_downloads, &full_date_range);
        assert_eq!(
            result,
            vec![200],
            "The most recent value for a duplicate date should be used."
        );
    }
}