From 3f1aab1883fbb5e42bd90309a13fb99e7df95e31 Mon Sep 17 00:00:00 2001 From: Jonas Kuche Date: Sun, 1 Apr 2018 17:42:30 +0200 Subject: [PATCH] Fix stats json Signed-off-by: Jonas Kuche --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/stats.rs | 26 ++++++++++++-------------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb0efbd..5ed1fdb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -769,7 +769,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "zlnk" -version = "0.4.0" +version = "0.4.1" dependencies = [ "dotenv 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "maxminddb 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index bcc7234..780bbb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zlnk" -version = "0.4.0" +version = "0.4.1" authors = ["Jonas Kuche "] [dependencies] diff --git a/src/stats.rs b/src/stats.rs index df97385..5b19a52 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -8,6 +8,7 @@ use woothee::parser::Parser; use env_loader::Env; use geo_locate_ip::GeoLocateIP; use serde_json::Value; +use std::collections::HashMap; pub struct Stats { pub refer: String, @@ -118,30 +119,27 @@ impl Stats { let _exists: String = connection.get(format!("short_{}", &short))?; let stats_key = &format!("stats_{}", short); let mut clicks: u64 = 0; - let mut refers: Vec = Vec::new(); - let mut browsers: Vec = Vec::new(); - let mut oss: Vec = Vec::new(); - let mut countries: Vec = Vec::new(); + let mut refers: HashMap = HashMap::new(); + let mut browsers: HashMap = HashMap::new(); + let mut oss: HashMap = HashMap::new(); + let mut countries: HashMap = HashMap::new(); let keys: Vec = connection.hkeys(stats_key)?; for key in keys.iter() { - let value = connection.hget(stats_key, key)?; + let value :u64 = connection.hget(stats_key, key)?; if key == "clicks" { clicks = value; } else { - let split: Vec<&str> = key.split('_').collect(); + let split: Vec<_> = key.split('_').collect(); let typ = split[0]; - let name = split[1]; - let val = json!({ - name: value - }); + let name = split[1].to_string(); if typ == "refer" { - refers.push(val); + refers.insert(name, value); } else if typ == "browser" { - browsers.push(val); + browsers.insert(name, value); } else if typ == "os" { - oss.push(val); + oss.insert(name, value); } else if typ == "country" { - countries.push(val); + countries.insert(name, value); } else { panic!("Invalid typ: {}!", typ); }