File size: 6,323 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
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
use chrono::prelude::*;
use chrono::Duration;
use std::path::Path;
use std::fs;

#[derive(Debug)]
pub struct AnalyticsTimestampFile {
    datetime: DateTime<Utc>
}

impl AnalyticsTimestampFile {
    pub fn is_expired(timestamp_file_path: &Path, minutes: u32) -> bool {
        let instance = match Self::load(&timestamp_file_path) {
            Ok(value) => value,
            Err(error) => {
                debug!("AnalyticsTimestampFile: error: {:?}", error);
                return true;
            }
        };
        instance.is_expired_minutes(minutes)
    }
    
    pub fn load(timestamp_file_path: &Path) -> anyhow::Result<Self> {
        if !timestamp_file_path.is_file() {
            return Err(anyhow::anyhow!("No timestamp file found at path {:?}", timestamp_file_path));
        }
        let contents: String = match fs::read_to_string(timestamp_file_path) {
            Ok(value) => value,
            Err(error) => {
                return Err(anyhow::anyhow!("Cannot load timestamp file. path: {:?} error: {:?}", timestamp_file_path, error));
            }
        };
        let datetime: DateTime<Utc> = Self::parse_utc_string(&contents)?;
        let instance = Self {
            datetime: datetime,
        };
        Ok(instance)
    }

    fn parse_utc_string(utc_string: &String) -> anyhow::Result<DateTime<Utc>> {
        let datetime: DateTime<FixedOffset> = match DateTime::parse_from_rfc3339(utc_string) {
            Ok(value) => value,
            Err(error) => {
                return Err(anyhow::anyhow!("Cannot parse timestamp file as UTC. path: {:?} error: {:?}", utc_string, error));
            }
        };
        let datetime: DateTime<Utc> = datetime.with_timezone(&Utc);
        Ok(datetime)
    }

    fn is_expired_inner(&self, now_datetime: DateTime<Utc>, duration: Duration) -> bool {
        (self.datetime + duration) <= now_datetime
    }

    pub fn is_expired_minutes(&self, minutes: u32) -> bool {
        self.is_expired_inner(Utc::now(), Duration::minutes(minutes as i64))
    }

    fn format_date(datetime: &DateTime<Utc>) -> String {
        datetime.to_rfc3339_opts(SecondsFormat::Secs, true).to_string()
    }

    fn save_datetime(timestamp_file_path: &Path, datetime: &DateTime<Utc>) -> anyhow::Result<()> {
        let contents = Self::format_date(datetime);
        fs::write(timestamp_file_path, contents)?;
        Ok(())
    }

    pub fn save_now(timestamp_file_path: &Path) -> anyhow::Result<()> {
        Self::save_datetime(timestamp_file_path, &Utc::now())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn test_10000_parse_utc_string_ok() {
        let date_str = "1999-03-24T21:59:33Z".to_string(); // release date of "the matrix"
        let dt: DateTime<Utc> = AnalyticsTimestampFile::parse_utc_string(&date_str).unwrap();
        assert_eq!(dt.year(), 1999);
        assert_eq!(dt.month(), 3);
        assert_eq!(dt.day(), 24);
        assert_eq!(dt.hour(), 21);
        assert_eq!(dt.minute(), 59);
    }

    #[test]
    fn test_10001_parse_utc_string_error() {
        static INPUT: &'static [&'static str] = &[
            "",
            "junk",
            "1999-03-24T21:59:33",  // Missing "Z" suffix
            "1999-03-24 21:59:33Z", // Missing "T" infix
            "1999-03-24T21:59:33Zjunk",
            "junk1999-03-24T21:59:33Z",
        ];
        for input in INPUT {
            AnalyticsTimestampFile::parse_utc_string(&input.to_string()).expect_err("is supposed to fail");
        }
    }

    #[test]
    fn test_10002_format() {
        let dt: DateTime<Utc> = Utc.with_ymd_and_hms(1999, 3, 24, 21, 59, 33).unwrap();
        let s = AnalyticsTimestampFile::format_date(&dt);
        assert_eq!(s, "1999-03-24T21:59:33Z"); // release date of "the matrix"
    }
    
    #[test]
    fn test_20000_save_datetime_create_new_file() -> anyhow::Result<()> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_20000_save_datetime_create_new_file");
        fs::create_dir(&basedir)?;
        let path: PathBuf = basedir.join("timestamp.asm");
        let dt: DateTime<Utc> = Utc.with_ymd_and_hms(1999, 3, 24, 21, 59, 33).unwrap();

        // Act
        AnalyticsTimestampFile::save_datetime(&path, &dt)?;

        // Assert
        let contents: String = fs::read_to_string(&path).unwrap();
        assert_eq!(contents, "1999-03-24T21:59:33Z"); // release date of "the matrix"
        Ok(())
    }

    #[test]
    fn test_20001_save_datetime_overwrite_existing_file() -> anyhow::Result<()> {
        // Arrange
        let tempdir = tempfile::tempdir().unwrap();
        let basedir = PathBuf::from(&tempdir.path()).join("test_20001_save_datetime_overwrite_existing_file");
        fs::create_dir(&basedir)?;
        let path: PathBuf = basedir.join("timestamp.asm");
        let dt: DateTime<Utc> = Utc.with_ymd_and_hms(1999, 3, 24, 21, 59, 33).unwrap();
        fs::write(&path, "overwrite me")?;

        // Act
        AnalyticsTimestampFile::save_datetime(&path, &dt)?;

        // Assert
        let contents: String = fs::read_to_string(&path).unwrap();
        assert_eq!(contents, "1999-03-24T21:59:33Z"); // release date of "the matrix"
        Ok(())
    }

    #[test]
    fn test_30000_is_expired_inner() {
        let date_str = "1999-03-24T00:00:00Z".to_string();
        let dt: DateTime<Utc> = AnalyticsTimestampFile::parse_utc_string(&date_str).unwrap();
        let timestamp = AnalyticsTimestampFile { datetime: dt };
        let now: DateTime<Utc> = Utc.with_ymd_and_hms(1999, 3, 24, 0, 1, 0).unwrap();
        assert_eq!(timestamp.is_expired_inner(now, Duration::minutes(30)), false);
        assert_eq!(timestamp.is_expired_inner(now, Duration::minutes(2)), false);
        assert_eq!(timestamp.is_expired_inner(now, Duration::minutes(1)), true);
        assert_eq!(timestamp.is_expired_inner(now, Duration::minutes(0)), true);
        assert_eq!(timestamp.is_expired_inner(now, Duration::seconds(120)), false);
        assert_eq!(timestamp.is_expired_inner(now, Duration::seconds(61)), false);
        assert_eq!(timestamp.is_expired_inner(now, Duration::seconds(60)), true);
        assert_eq!(timestamp.is_expired_inner(now, Duration::seconds(0)), true);
    }
}