| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
|
| 3 |
{-| |
| 4 |
|
| 5 |
Print a bar chart of posting activity per day, or other report interval. |
| 6 |
|
| 7 |
-} |
| 8 |
|
| 9 |
module Hledger.Cli.Commands.Activity |
| 10 |
where |
| 11 |
|
| 12 |
import Data.List (sortOn) |
| 13 |
import Text.Printf (printf) |
| 14 |
import Lens.Micro ((^.), set) |
| 15 |
|
| 16 |
import Hledger |
| 17 |
import Hledger.Cli.CliOptions |
| 18 |
|
| 19 |
activitymode = hledgerCommandMode |
| 20 |
$(embedFileRelative "Hledger/Cli/Commands/Activity.txt") |
| 21 |
[] |
| 22 |
cligeneralflagsgroups1 |
| 23 |
hiddenflags |
| 24 |
([], Just $ argsFlag "[QUERY]") |
| 25 |
|
| 26 |
barchar :: Char |
| 27 |
barchar = '*' |
| 28 |
|
| 29 |
-- | Print a bar chart of number of postings per report interval. |
| 30 |
activity :: CliOpts -> Journal -> IO () |
| 31 |
activity CliOpts{reportspec_=rspec} j = putStr $ showHistogram rspec j |
| 32 |
|
| 33 |
showHistogram :: ReportSpec -> Journal -> String |
| 34 |
showHistogram rspec@ReportSpec{_rsQuery=q} j = |
| 35 |
concatMap (printDayWith countBar) spanps |
| 36 |
where |
| 37 |
mspans = snd . reportSpan j $ case rspec ^. interval of |
| 38 |
NoInterval -> set interval (Days 1) rspec |
| 39 |
_ -> rspec |
| 40 |
spanps = case mspans of |
| 41 |
Nothing -> [] |
| 42 |
Just x -> map (\spn -> (spn, filter (postingInRange spn) ps)) $ dayPartitionToList x |
| 43 |
postingInRange (b, e) p = postingDate p >= b && postingDate p <= e |
| 44 |
-- same as Register |
| 45 |
-- should count transactions, not postings ? |
| 46 |
-- ps = sortBy (comparing postingDate) $ filterempties $ filter matchapats $ filterdepth $ journalPostings j |
| 47 |
ps = sortOn postingDate $ filter (q `matchesPosting`) $ journalPostings j |
| 48 |
|
| 49 |
printDayWith f ((b, _), ps) = printf "%s %s\n" (show b) (f ps) |
| 50 |
|
| 51 |
countBar ps = replicate (length ps) barchar |