theo-heartbeat 0.1.0

A package that logs heartbeats for me.
Documentation

use iso8601_timestamp::Timestamp;

use redis::Commands;

fn iso_time() -> String {
    Timestamp::now_utc().format().to_string()
}

pub fn pulse(redis_url: &str, device: &str) -> i8 {
    let client = redis::Client::open(redis_url).unwrap();

    match client.get_connection() {
        Ok(mut connection) => {
            let time = iso_time();
            let ret = connection.hset::<&str, &str, String, i8>("sensors.heartbeats", device, time).unwrap();
            ret
        },
        Err(_) => {
            eprintln!("Unable to touch last updated, redis unavailable?");
            0
        }
    }
}

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

    #[test]
    fn test_iso_time() {
        let time = iso_time();
        let ts = Timestamp::now_utc().format().to_string();
        assert_eq!(time, ts);
    }

    #[test]
    fn test_pulse() {
        let client = redis::Client::open("redis://localhost:6379/3").unwrap();
        let mut conn = client.get_connection().expect("Failed to connect to redis");
        conn.del::<&str, i8>("sensors.heartbeats").expect("Failed to delete heartbeats key");

        let writes = pulse("redis://localhost:6379/3", "device");
        assert_eq!(1, writes);
    }
}