midas_fetcher 0.1.4

High-performance concurrent downloader for UK Met Office MIDAS Open weather data with intelligent caching and resumable downloads
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
//! File path generation and organization
//!
//! This module handles the generation of cache file paths based on dataset
//! information, organizing files by dataset, quality version, county, and station.

use std::path::{Path, PathBuf};

use crate::app::models::FileInfo;

/// Path generation utility for cache files
pub struct PathGenerator;

impl PathGenerator {
    /// Get the cache path for a file based on its dataset information
    ///
    /// Structure:
    /// - Capability/metadata files: {cache_root}/{dataset}/capability/{county}/{station}/{filename}
    /// - Data files: {cache_root}/{dataset}/{quality_version}/{county}/{station}/{filename}
    /// - Station metadata files: {cache_root}/{dataset}/station-metadata/{filename}
    /// - Change log files: {cache_root}/{dataset}/{filename}
    /// - Station log files: {cache_root}/{dataset}/station-log-files/{filename}
    pub fn get_file_path(cache_root: &Path, file_info: &FileInfo) -> PathBuf {
        let dataset = &file_info.dataset_info;
        let mut path = cache_root.to_path_buf();

        // Add dataset name (with manifest version if available)
        if let Some(version) = file_info.manifest_version {
            let versioned_name = format!("{}-{}", dataset.dataset_name, version);
            path.push(versioned_name);
        } else {
            path.push(&dataset.dataset_name);
        }

        // Determine path structure based on file type
        if let Some(ref file_type) = dataset.file_type {
            match file_type.as_str() {
                "capability" | "metadata" => {
                    // Capability and metadata files go in separate capability folder
                    path.push("capability");
                    Self::add_county_and_station_path(&mut path, dataset);
                }
                "station-metadata" => {
                    // Station metadata files go in station-metadata folder
                    path.push("station-metadata");
                    // Add filename directly and return early (no county/station structure)
                    path.push(&file_info.file_name);
                    return path;
                }
                "change-log" => {
                    // Change log files go directly in dataset root
                    path.push(&file_info.file_name);
                    return path;
                }
                "station-log" => {
                    // Station log files go in station-log-files folder
                    path.push("station-log-files");
                    // Add filename directly and return early (no county/station structure)
                    path.push(&file_info.file_name);
                    return path;
                }
                _ => {
                    // Data files use quality version structure
                    Self::add_quality_version_path(&mut path, dataset);
                    Self::add_county_and_station_path(&mut path, dataset);
                }
            }
        } else {
            // Fallback for files without file_type - use quality version
            Self::add_quality_version_path(&mut path, dataset);
            Self::add_county_and_station_path(&mut path, dataset);
        }

        // Add filename
        path.push(&file_info.file_name);

        path
    }

    /// Add quality version to path for data files
    fn add_quality_version_path(path: &mut PathBuf, dataset: &crate::app::models::DatasetFileInfo) {
        if let Some(qv) = &dataset.quality_version {
            path.push(qv.to_filename_format());
        } else {
            path.push("no-quality");
        }
    }

    /// Add county and station directories to path
    fn add_county_and_station_path(
        path: &mut PathBuf,
        dataset: &crate::app::models::DatasetFileInfo,
    ) {
        // Add county if available
        if let Some(county) = &dataset.county {
            path.push(county);
        } else {
            path.push("no-county");
        }

        // Add station if available
        if let Some(station_id) = &dataset.station_id {
            if let Some(station_name) = &dataset.station_name {
                path.push(format!("{}_{}", station_id, station_name));
            } else {
                path.push(station_id);
            }
        } else {
            path.push("no-station");
        }
    }

    /// Generate a path for a specific file type (used for testing)
    pub fn get_path_for_file_type(
        cache_root: &Path,
        dataset_name: &str,
        manifest_version: Option<u32>,
        file_type: &str,
        file_name: &str,
    ) -> PathBuf {
        let mut path = cache_root.to_path_buf();

        // Add dataset name (with manifest version if available)
        if let Some(version) = manifest_version {
            let versioned_name = format!("{}-{}", dataset_name, version);
            path.push(versioned_name);
        } else {
            path.push(dataset_name);
        }

        // Add file type specific path
        match file_type {
            "station-metadata" => {
                path.push("station-metadata");
            }
            "change-log" => {
                // Change log files go directly in dataset root
                path.push(file_name);
                return path;
            }
            "station-log" => {
                path.push("station-log-files");
            }
            _ => {
                // For other types, we would need more parameters
                // This is a simplified version for testing
                path.push(file_type);
            }
        }

        path.push(file_name);
        path
    }

    /// Check if a path represents a data file (has quality version structure)
    pub fn is_data_file_path(path: &Path) -> bool {
        // Look for quality version directory in the path components
        for component in path.components() {
            if let Some(dir_name) = component.as_os_str().to_str() {
                if dir_name.starts_with("qcv-") || dir_name == "no-quality" {
                    return true;
                }
            }
        }
        false
    }

    /// Check if a path represents a capability file
    pub fn is_capability_file_path(path: &Path) -> bool {
        // Look for capability directory in the path components
        for component in path.components() {
            if let Some(dir_name) = component.as_os_str().to_str() {
                if dir_name == "capability" {
                    return true;
                }
            }
        }
        false
    }

    /// Extract dataset name from a cache file path
    pub fn extract_dataset_name(cache_root: &Path, file_path: &Path) -> Option<String> {
        if let Ok(relative_path) = file_path.strip_prefix(cache_root) {
            if let Some(first_component) = relative_path.components().next() {
                let dataset_with_version = first_component.as_os_str().to_string_lossy();

                // Handle versioned dataset names (e.g., "uk-daily-temperature-obs-202507")
                if let Some(last_dash) = dataset_with_version.rfind('-') {
                    let potential_version = &dataset_with_version[last_dash + 1..];
                    if potential_version.chars().all(|c| c.is_ascii_digit()) {
                        return Some(dataset_with_version[..last_dash].to_string());
                    }
                }

                return Some(dataset_with_version.to_string());
            }
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::hash::Md5Hash;
    use crate::app::models::{DatasetFileInfo, QualityControlVersion};

    fn create_test_file_info() -> FileInfo {
        let hash = Md5Hash::from_hex("50c9d1c465f3cbff652be1509c2e2a4e").unwrap();
        let dataset_info = DatasetFileInfo {
            dataset_name: "uk-daily-temperature-obs".to_string(),
            version: "202407".to_string(),
            county: Some("devon".to_string()),
            station_id: Some("01381".to_string()),
            station_name: Some("twist".to_string()),
            quality_version: Some(QualityControlVersion::V1),
            year: Some("1980".to_string()),
            file_type: Some("data".to_string()),
        };

        FileInfo {
            hash,
            relative_path: "./data/test.csv".to_string(),
            file_name: "test.csv".to_string(),
            dataset_info,
            manifest_version: Some(202507),
            retry_count: 0,
            last_attempt: None,
            estimated_size: None,
            destination_path: PathBuf::from("/tmp/test.csv"),
        }
    }

    #[test]
    fn test_data_file_path_generation() {
        let cache_root = PathBuf::from("/cache");
        let file_info = create_test_file_info();
        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path = PathBuf::from(
            "/cache/uk-daily-temperature-obs-202507/qcv-1/devon/01381_twist/test.csv",
        );
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_capability_file_path_generation() {
        let cache_root = PathBuf::from("/cache");
        let mut file_info = create_test_file_info();
        file_info.dataset_info.file_type = Some("capability".to_string());
        file_info.file_name = "capability.csv".to_string();

        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path = PathBuf::from(
            "/cache/uk-daily-temperature-obs-202507/capability/devon/01381_twist/capability.csv",
        );
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_station_metadata_file_path_generation() {
        let cache_root = PathBuf::from("/cache");
        let mut file_info = create_test_file_info();
        file_info.dataset_info.file_type = Some("station-metadata".to_string());
        file_info.dataset_info.county = None;
        file_info.dataset_info.station_id = None;
        file_info.dataset_info.station_name = None;
        file_info.file_name = "station-metadata.csv".to_string();

        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path = PathBuf::from(
            "/cache/uk-daily-temperature-obs-202507/station-metadata/station-metadata.csv",
        );
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_change_log_file_path_generation() {
        let cache_root = PathBuf::from("/cache");
        let mut file_info = create_test_file_info();
        file_info.dataset_info.file_type = Some("change-log".to_string());
        file_info.file_name = "change-log.txt".to_string();

        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path = PathBuf::from("/cache/uk-daily-temperature-obs-202507/change-log.txt");
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_station_log_file_path_generation() {
        let cache_root = PathBuf::from("/cache");
        let mut file_info = create_test_file_info();
        file_info.dataset_info.file_type = Some("station-log".to_string());
        file_info.file_name = "station-log.txt".to_string();

        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path = PathBuf::from(
            "/cache/uk-daily-temperature-obs-202507/station-log-files/station-log.txt",
        );
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_file_without_manifest_version() {
        let cache_root = PathBuf::from("/cache");
        let mut file_info = create_test_file_info();
        file_info.manifest_version = None;

        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path =
            PathBuf::from("/cache/uk-daily-temperature-obs/qcv-1/devon/01381_twist/test.csv");
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_file_without_quality_version() {
        let cache_root = PathBuf::from("/cache");
        let mut file_info = create_test_file_info();
        file_info.dataset_info.quality_version = None;

        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path = PathBuf::from(
            "/cache/uk-daily-temperature-obs-202507/no-quality/devon/01381_twist/test.csv",
        );
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_file_without_county() {
        let cache_root = PathBuf::from("/cache");
        let mut file_info = create_test_file_info();
        file_info.dataset_info.county = None;

        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path = PathBuf::from(
            "/cache/uk-daily-temperature-obs-202507/qcv-1/no-county/01381_twist/test.csv",
        );
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_file_without_station() {
        let cache_root = PathBuf::from("/cache");
        let mut file_info = create_test_file_info();
        file_info.dataset_info.station_id = None;
        file_info.dataset_info.station_name = None;

        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path =
            PathBuf::from("/cache/uk-daily-temperature-obs-202507/qcv-1/devon/no-station/test.csv");
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_file_with_station_id_only() {
        let cache_root = PathBuf::from("/cache");
        let mut file_info = create_test_file_info();
        file_info.dataset_info.station_name = None;

        let path = PathGenerator::get_file_path(&cache_root, &file_info);

        let expected_path =
            PathBuf::from("/cache/uk-daily-temperature-obs-202507/qcv-1/devon/01381/test.csv");
        assert_eq!(path, expected_path);
    }

    #[test]
    fn test_is_data_file_path() {
        let data_path = PathBuf::from("/cache/dataset/qcv-1/devon/station/file.csv");
        assert!(PathGenerator::is_data_file_path(&data_path));

        let capability_path = PathBuf::from("/cache/dataset/capability/devon/station/file.csv");
        assert!(!PathGenerator::is_data_file_path(&capability_path));

        let metadata_path = PathBuf::from("/cache/dataset/station-metadata/file.csv");
        assert!(!PathGenerator::is_data_file_path(&metadata_path));
    }

    #[test]
    fn test_is_capability_file_path() {
        let capability_path = PathBuf::from("/cache/dataset/capability/devon/station/file.csv");
        assert!(PathGenerator::is_capability_file_path(&capability_path));

        let data_path = PathBuf::from("/cache/dataset/qcv-1/devon/station/file.csv");
        assert!(!PathGenerator::is_capability_file_path(&data_path));
    }

    #[test]
    fn test_extract_dataset_name() {
        let cache_root = PathBuf::from("/cache");

        // Test with versioned dataset name
        let file_path =
            PathBuf::from("/cache/uk-daily-temperature-obs-202507/qcv-1/devon/station/file.csv");
        let dataset_name = PathGenerator::extract_dataset_name(&cache_root, &file_path);
        assert_eq!(dataset_name, Some("uk-daily-temperature-obs".to_string()));

        // Test with non-versioned dataset name
        let file_path = PathBuf::from("/cache/simple-dataset/qcv-1/devon/station/file.csv");
        let dataset_name = PathGenerator::extract_dataset_name(&cache_root, &file_path);
        assert_eq!(dataset_name, Some("simple-dataset".to_string()));

        // Test with path outside cache root
        let file_path = PathBuf::from("/other/path/file.csv");
        let dataset_name = PathGenerator::extract_dataset_name(&cache_root, &file_path);
        assert_eq!(dataset_name, None);
    }

    #[test]
    fn test_get_path_for_file_type() {
        let cache_root = PathBuf::from("/cache");

        // Test station metadata
        let path = PathGenerator::get_path_for_file_type(
            &cache_root,
            "test-dataset",
            Some(202507),
            "station-metadata",
            "metadata.csv",
        );
        assert_eq!(
            path,
            PathBuf::from("/cache/test-dataset-202507/station-metadata/metadata.csv")
        );

        // Test change log
        let path = PathGenerator::get_path_for_file_type(
            &cache_root,
            "test-dataset",
            Some(202507),
            "change-log",
            "change.txt",
        );
        assert_eq!(path, PathBuf::from("/cache/test-dataset-202507/change.txt"));

        // Test station log
        let path = PathGenerator::get_path_for_file_type(
            &cache_root,
            "test-dataset",
            Some(202507),
            "station-log",
            "log.txt",
        );
        assert_eq!(
            path,
            PathBuf::from("/cache/test-dataset-202507/station-log-files/log.txt")
        );
    }
}