Skip to content
Snippets Groups Projects
Verified Commit b2e58107 authored by ck85nori's avatar ck85nori :railway_track:
Browse files

initial import

parents
No related branches found
No related tags found
No related merge requests found
*.db
*.csv
LICENSE 0 → 100644
ISC License
Copyright (c) 2017, Christian Krause
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
# Time Tracking Analysis
#!/usr/bin/env amm
import $ivy.`com.github.wookietreiber::scala-chart:0.5.1`, scalax.chart.api._
import $ivy.`com.github.tototoshi::scala-csv:1.3.4`, com.github.tototoshi.csv._
import $ivy.`org.typelevel::cats:0.9.0`
import cats._
import cats.instances.all._
import cats.syntax.all._
import java.time._
import sys.process._
import ammonite.ops._
val weekFields = temporal.WeekFields.of(java.util.Locale.getDefault)
// -------------------------------------------------------------------------------------------------
// debit
// -------------------------------------------------------------------------------------------------
class Debit(path: Path) {
val entries: List[(LocalDate,Double)] = for {
entry <- Seq("sqlite3", path.toNIO.toString, "select * from debit").lineStream.toList
parts = entry.split("\\|")
date = LocalDate.parse(parts(0))
hours = parts(1).toDouble
} yield date -> hours
val total = entries.foldLeft(0.0)(_ + _._2.toDouble)
val weekly: Map[String,List[Double]] = entries.groupBy({
case (date,_) =>
val year = date.getYear
val week = date.get(weekFields.weekOfWeekBasedYear)
f"$year%d-$week%02d"
}).mapValues(_.map(_._2))
def summary(): Unit = for {
(week,numbers) <- weekly.toSeq.sortBy(_._1)
hours = numbers.sum
} println(s"$week $hours")
}
// -------------------------------------------------------------------------------------------------
// credit
// -------------------------------------------------------------------------------------------------
class Credit(path: Path) {
case class TTEntry(start: LocalDateTime, end: LocalDateTime, duration: Double) {
override def toString: String = {
val d = (duration * 1000).round / 1000.0
s"""from $start to $end (duration=$d hours)"""
}
}
val entries = {
val reader = CSVReader.open(path.toIO)
val ttraw: List[List[String]] = reader.all().drop(1)
val formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
val ttx: List[TTEntry] = for {
entry <- ttraw
start = LocalDateTime.parse(entry(4), formatter)
end = LocalDateTime.parse(entry(5), formatter)
duration = entry(7).toDouble
} yield TTEntry(start, end, duration)
reader.close()
ttx
}
val total = entries.foldLeft(0.0)(_ + _.duration)
val weekly: Map[String,List[Double]] = entries.groupBy({ x =>
val year = x.start.getYear
val week = x.start.get(weekFields.weekOfWeekBasedYear)
f"$year%d-$week%02d"
}).mapValues(_.map(_.duration))
def summary: Unit = for {
(week,numbers) <- weekly.toSeq.sortBy(_._1)
hours = numbers.sum
} println(s"$week $hours")
}
// -------------------------------------------------------------------------------------------------
// balance
// -------------------------------------------------------------------------------------------------
class Balance(debit: Debit, credit: Credit) {
case class Rolling(week: String, debit: Double, credit: Double, balance: Double, change: Double)
def rolling: Seq[Rolling] = {
val xx: Map[String,List[Double]] =
debit.weekly.mapValues(_.sum).mapValues(_ :: Nil) |+|
credit.weekly.mapValues(_.sum).mapValues(_ :: Nil)
var b = 0.0
for {
(week,List(debit,credit)) <- xx.toSeq.sortBy(_._1)
change = credit - debit
} yield {
b += change
Rolling(week, debit, credit, b, change)
}
}
}
// -------------------------------------------------------------------------------------------------
// balance
// -------------------------------------------------------------------------------------------------
@main
def main(debitIn: Path, creditIn: Path) = {
val debit = new Debit(debitIn)
val credit = new Credit(creditIn)
val balance = new Balance(debit, credit)
for (r <- balance.rolling) {
val week = r.week
val debit = r.debit
val credit = r.credit
val balance = r.balance
val change = r.change
val cc = if (change < 0)
f"${Console.RED}${change.round}%5d${Console.RESET}"
else
f"${Console.GREEN}${change.round}%5d${Console.RESET}"
val cb = if (balance < 0)
f"${Console.RED}${balance.round}%5d${Console.RESET}"
else
f"${Console.GREEN}${balance.round}%5d${Console.RESET}"
println(f"$week ${debit.round}%5d ${credit.round}%5d $cc $cb")
}
println(s"""|
|total debit: ${debit.total} hours
|total credit: ${credit.total} hours
|
|balance: ${credit.total - debit.total} hours
|""".stripMargin)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment