Compare commits

12 Commits

Author SHA1 Message Date
Bel LaPointe
12bdf43721 accept --precision to change logging 2024-02-21 14:28:50 -07:00
Bel LaPointe
a693776a59 oops rust evidently will do some rounding for prints 2023-12-19 11:25:06 -05:00
Bel LaPointe
d97be3123b add -v/--verbose 2023-12-18 08:24:28 -07:00
Bel LaPointe
f6da50ff6f fix computing duration for each task 2023-12-18 08:24:20 -07:00
Bel LaPointe
09e1c57f32 when building day's log, skip stubs 2023-12-13 13:22:12 -07:00
Bel LaPointe
fcb144b437 --log=--log || --since=... 2023-12-11 16:09:18 -07:00
Bel LaPointe
e6ceef2ead add -c/--clock 2023-12-11 08:25:01 -07:00
Bel LaPointe
7463ca2069 default since is today 2023-12-07 09:14:44 -08:00
Bel LaPointe
1235b38636 timestamps from local timezone instead of utc 2023-12-07 09:09:26 -08:00
Bel LaPointe
cf47c63bd7 --since as local timezone 2023-12-07 09:09:17 -08:00
Bel LaPointe
0f9711aaa1 per task gets 1.1 hours, per day gets rounded 1 hours 2023-11-29 06:38:24 -07:00
Bel LaPointe
326b79a3b1 dont do things by default 2023-11-29 06:31:50 -07:00

View File

@@ -4,7 +4,7 @@ use std::fs::File;
use std::time::{SystemTime, UNIX_EPOCH, Duration};
use std::ops::{Add, Sub};
use clap::Parser;
use chrono::DateTime;
use chrono::{TimeZone, Local, Timelike};
#[derive(Debug, Parser)]
struct Flags {
@@ -22,15 +22,31 @@ struct Flags {
#[arg(short = 't', long = "tag")]
tag: Option<String>,
#[arg(short = 'c', long = "clock")]
clock: bool,
#[arg(short = 'v', long = "verbose")]
verbose: bool,
#[arg(short = 'p', long = "precision")]
precision: Option<u32>,
}
fn main() {
let flags = Flags::parse();
let mut flags = Flags::parse();
flags.log = flags.log || flags.since.is_some();
clock(&flags.f, &flags.clock).unwrap();
add(&flags.f, &flags.add, &flags.tag).unwrap();
log(&flags.f, &flags.since).unwrap();
log(&flags.f, &flags.log, &flags.since, &flags.verbose, &flags.precision.unwrap_or(0)).unwrap();
}
println!("{:?}", flags);
fn clock(f: &String, clock: &bool) -> Result<(), String> {
match clock {
true => add(&f, &Some("".to_string()), &None),
false => Ok(()),
}
}
fn add(f: &String, x: &Option<String>, tag: &Option<String>) -> Result<(), String> {
@@ -51,52 +67,87 @@ fn add(f: &String, x: &Option<String>, tag: &Option<String>) -> Result<(), Strin
#[derive(Debug, Serialize, Clone)]
struct Log {
t: String,
d: u8,
d: f32,
xs: Vec<LogX>,
}
#[derive(Debug, Serialize, Clone)]
struct LogX {
d: u8,
d: f32,
x: String,
}
fn log(f: &String, since: &Option<String>) -> Result<(), String> {
fn log(f: &String, enabled: &bool, since: &Option<String>, verbose: &bool, precision: &u32) -> Result<(), String> {
if !enabled {
return Ok(());
}
let since = parse_time(since)?;
if *verbose {
eprintln!("since = {} ({})", system_time_to_unix_seconds(&since), timestamp(&system_time_to_unix_seconds(&since)));
}
let tsheet = load(&f)?;
let tsheet = tsheet.since(since);
let tsheet = tsheet.sorted();
if *verbose {
eprintln!("tsheet = {:?}", &tsheet);
}
let mut result = vec![];
let mut curr = Log{t: "".to_string(), d: 0, xs: vec![]};
let mut curr = Log{t: "".to_string(), d: 0.0, xs: vec![]};
for i in 0..tsheet.xs.len() {
let x = &tsheet.xs[i];
if *verbose {
eprintln!("{} != {}?", &curr.t, x.timestamp());
}
if curr.t != x.timestamp() {
if curr.xs.len() > 0 {
if *verbose {
eprintln!("push {:?}", &curr.xs);
}
result.push(curr.clone());
}
curr.xs.truncate(0);
curr.t = x.timestamp();
curr.d = 0;
curr.d = 0.0;
}
let d = match curr.xs.len() {
0 if x.x.len() == 0 => 0,
0 => 1,
_ => ((tsheet.xs[i].t - tsheet.xs[i-1].t + 60*30) / (60*60)) as u8,
};
let mut d = 1.0;
if x.x.len() == 0 {
d = 0.0;
} else if i > 0 {
d = ((tsheet.xs[i].t - tsheet.xs[i-1].t) as f32 / (60.0*60.0)) as f32;
}
if *verbose {
eprintln!("d={} x='{}'", &x.x, &d);
}
match x.x.len() {
0 => {},
_ => {
curr.t = x.timestamp();
curr.xs.push(LogX{d: d, x: x.x.clone()});
},
};
}
if curr.xs.len() > 0 {
if *verbose {
eprintln!("push {:?} (final)", &curr.xs);
}
result.push(curr.clone());
}
for i in result.iter_mut() {
i.d = i.xs.iter().map(|x| x.d).sum();
if *verbose {
eprintln!("{} = {:?}", &i.d, &i.xs.iter().map(|x| x.d).collect::<Vec<_>>());
}
}
for log in result {
for x in log.xs {
if x.x.len() > 0 {
println!("{} ({}) {} ({})", log.t, log.d, x.x, x.d);
match precision {
0 => println!("{} ({:.0}) {} ({:.1})", log.t, log.d, x.x, x.d),
1 => println!("{} ({:.1}) {} ({:.2})", log.t, log.d, x.x, x.d),
_ => println!("{} ({:.2}) {} ({:.3})", log.t, log.d, x.x, x.d),
}
}
}
}
@@ -107,19 +158,17 @@ fn parse_time(since: &Option<String>) -> Result<SystemTime, String> {
match since {
Some(since) => {
match chrono::NaiveDate::parse_from_str(since, "%Y-%m-%d") {
Ok(dt) => {
Ok(nd) => {
let ndt = nd.and_hms_opt(1, 1, 1).unwrap();
let dt = Local.from_local_datetime(&ndt).unwrap();
Ok(UNIX_EPOCH.add(
Duration::from_secs(
dt.and_hms_opt(1, 1, 1)
.unwrap()
.timestamp() as u64
)
Duration::from_secs(dt.timestamp() as u64)
))
},
Err(msg) => Err(format!("failed to parse {}: {}", since, msg)),
}
},
None => Ok(SystemTime::now().sub(Duration::from_secs(60*60*24*7))),
None => Ok(SystemTime::now().sub(Duration::from_secs(Local::now().hour() as u64*60*60))),
}
}
@@ -221,14 +270,22 @@ impl X {
}
fn timestamp(&self) -> String {
let dt = DateTime::from_timestamp(self.t, 0).unwrap();
dt.format("%Y-%m-%d").to_string()
timestamp(&self.t)
}
}
fn system_time_to_unix_seconds(st: &SystemTime) -> i64 {
st.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64
}
fn timestamp(t: &i64) -> String {
let dt = Local.timestamp_opt(*t, 0).unwrap();
dt.format("%Y-%m-%d").to_string()
}
fn new_x(t: SystemTime, x: String, tag: String) -> X {
X{
t: t.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64,
t: system_time_to_unix_seconds(&t),
x: x,
tag: tag,
}