File size: 3,571 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use super::{AnalyticsDirectory, BatchProgramAnalyzerPlugin, BatchProgramAnalyzerContext};
use crate::common::create_csv_file;
use chrono::{DateTime, Utc, Datelike};
use std::path::PathBuf;
use std::error::Error;
use std::time::SystemTime;
use serde::Serialize;
use std::fs::{self, Metadata};

/// This analyzer determines when was the program files last modified and 
/// generates a `program_modified.csv` with this format:
/// 
/// ```
/// program id;modified
/// 4;20190115
/// 5;20190119
/// 6;20210316
/// 7;20181012
/// 8;20210118
/// 10;20210225
/// 12;20190115
/// ```
pub struct AnalyzeProgramModified {
    analytics_directory: AnalyticsDirectory,
    record_vec: Vec<Record>,
    error_count_metadata_without_modified: usize,
}

impl AnalyzeProgramModified {
    pub fn new(analytics_directory: AnalyticsDirectory) -> Self {
        Self {
            analytics_directory,
            record_vec: vec!(),
            error_count_metadata_without_modified: 0,
        }
    }

    fn append_record(&mut self, program_id: u32, modified: String) {
        let record = Record {
            program_id,
            modified,
        };
        self.record_vec.push(record);
    }

    /// Format timestamp as "20001231" or "19840101"
    fn format_timestamp(datetime: DateTime<Utc>) -> String {
        let year: u32 = i32::max(datetime.year(), 0) as u32;
        let month: u32 = datetime.month();
        let day: u32 = datetime.day();
        format!("{year}{month:02}{day:02}")
    }
}

impl BatchProgramAnalyzerPlugin for AnalyzeProgramModified {
    fn plugin_name(&self) -> &'static str {
        "AnalyzeProgramModified"
    }

    fn analyze(&mut self, context: &BatchProgramAnalyzerContext) -> Result<(), Box<dyn Error>> {
        let metadata: Metadata = fs::metadata(&context.program_path)?;
        let time: SystemTime = match metadata.modified() {
            Ok(value) => value,
            Err(error) => {
                if self.error_count_metadata_without_modified < 5 {
                    error!("AnalyzeProgramModified: Not supported on this platform. {:?}", error);
                }
                self.error_count_metadata_without_modified += 1;
                return Ok(());
            }
        };
        let datetime: DateTime<Utc> = DateTime::<Utc>::from(time);
        let s = Self::format_timestamp(datetime);
        self.append_record(context.program_id, s);
        Ok(())
    }

    fn save(&self) -> Result<(), Box<dyn Error>> {
        let mut records: Vec<Record> = self.record_vec.clone();
        records.sort_unstable_by_key(|item| (item.program_id));

        // Save as a CSV file
        let output_path: PathBuf = self.analytics_directory.program_modified_file();
        create_csv_file(&records, &output_path)?;
        Ok(())
    }

    fn human_readable_summary(&self) -> String {
        let mut s = format!("timestamps: {}", self.record_vec.len());
        if self.error_count_metadata_without_modified > 0 {
            s += &format!("\nNumber of times modified() could not be obtained: {}", self.error_count_metadata_without_modified);
        }
        s
    }
}


#[derive(Clone, Serialize)]
struct Record {
    #[serde(rename = "program id")]
    program_id: u32,
    modified: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;

    #[test]
    fn test_10000_obtain_timestamps() {
        let dt: DateTime<Utc> = Utc.with_ymd_and_hms(1999, 3, 9, 21, 59, 33).unwrap();
        let s = AnalyzeProgramModified::format_timestamp(dt);
        assert_eq!(s, "19990309");
    }
}