aboutsummaryrefslogtreecommitdiff
path: root/ops
diff options
context:
space:
mode:
Diffstat (limited to 'ops')
-rw-r--r--ops/Cargo.toml1
-rw-r--r--ops/src/lib.rs1
-rw-r--r--ops/src/stats.rs26
3 files changed, 24 insertions, 4 deletions
diff --git a/ops/Cargo.toml b/ops/Cargo.toml
index f4bf082..6789aa2 100644
--- a/ops/Cargo.toml
+++ b/ops/Cargo.toml
@@ -9,6 +9,7 @@ authors = ["Till Höppner <till@hoeppner.ws>"]
[dependencies]
log = "0.3.5"
+chrono = "0.2.19"
ilc-base = "~0.2"
blist = "0.0.4"
bit-set = "0.3.0"
diff --git a/ops/src/lib.rs b/ops/src/lib.rs
index d5aa003..2ebe51f 100644
--- a/ops/src/lib.rs
+++ b/ops/src/lib.rs
@@ -3,6 +3,7 @@ extern crate log;
extern crate blist;
extern crate bit_set;
extern crate serde;
+extern crate chrono;
extern crate ilc_base;
mod ageset;
diff --git a/ops/src/stats.rs b/ops/src/stats.rs
index 49f4068..7e37a80 100644
--- a/ops/src/stats.rs
+++ b/ops/src/stats.rs
@@ -1,15 +1,21 @@
//! Per-nick word/line statistics
-
-use ilc_base::{self, Context, Decode, Event};
+use ilc_base::{self, Context, Decode, Event, Time};
use ilc_base::event::Type;
use std::collections::HashMap;
use std::io::BufRead;
+use chrono::{Datelike, NaiveDateTime, Timelike};
+
use serde::ser::{MapVisitor, Serialize, Serializer};
+pub type Day = [u32; 24];
+/// Weeks start on mondays.
+pub type Week = [Day; 7];
+
pub struct Stats {
pub freqs: HashMap<String, NickStat>,
+ pub week: Week,
}
impl Serialize for Stats {
@@ -22,6 +28,7 @@ impl Serialize for Stats {
where S: Serializer
{
try!(s.serialize_struct_elt("freqs", &self.0.freqs));
+ try!(s.serialize_struct_elt("week", &self.0.week));
Ok(None)
}
@@ -91,11 +98,19 @@ fn strip_nick(s: &str) -> &str {
/// Return all active nicks, with lines, words and words per lines counted.
pub fn stats(ctx: &Context, input: &mut BufRead, decoder: &mut Decode) -> ilc_base::Result<Stats> {
let mut freqs: HashMap<String, NickStat> = HashMap::new();
+ let mut week: Week = [[0; 24]; 7];
for e in decoder.decode(&ctx, input) {
let m = try!(e);
match m {
- Event { ty: Type::Msg { ref from, ref content, .. }, .. } => {
+ Event { ty: Type::Msg { ref from, ref content, .. }, ref time, .. } => {
+ if let &Time::Timestamp(stamp) = time {
+ let date = NaiveDateTime::from_timestamp(stamp, 0);
+ let dow = date.weekday().num_days_from_monday() as usize;
+ let hour = date.hour() as usize;
+ week[dow][hour] += 1;
+ }
+
let nick = strip_nick(from);
if freqs.contains_key(nick) {
let p: &mut NickStat = freqs.get_mut(nick).unwrap();
@@ -119,5 +134,8 @@ pub fn stats(ctx: &Context, input: &mut BufRead, decoder: &mut Decode) -> ilc_ba
}
}
- Ok(Stats { freqs: freqs })
+ Ok(Stats {
+ freqs: freqs,
+ week: week,
+ })
}