1 {-| 
    2 
    3 Print a histogram report.
    4 
    5 -}
    6 
    7 module Hledger.Cli.Histogram
    8 where
    9 import Data.List
   10 import Data.Maybe
   11 import Data.Ord
   12 import Text.Printf
   13 
   14 import Hledger.Cli.Options
   15 import Hledger.Data
   16 import Hledger.Reports
   17 import Prelude hiding (putStr)
   18 import Hledger.Utils.UTF8 (putStr)
   19 
   20 
   21 barchar = '*'
   22 
   23 -- | Print a histogram of some statistic per reporting interval, such as
   24 -- number of postings per day.
   25 histogram :: CliOpts -> Journal -> IO ()
   26 histogram CliOpts{reportopts_=reportopts_} j = do
   27   d <- getCurrentDay
   28   putStr $ showHistogram reportopts_ (optsToFilterSpec reportopts_ d) j
   29 
   30 showHistogram :: ReportOpts -> FilterSpec -> Journal -> String
   31 showHistogram opts filterspec j = concatMap (printDayWith countBar) spanps
   32     where
   33       i = intervalFromOpts opts
   34       interval | i == NoInterval = Days 1
   35                | otherwise = i
   36       span = datespan filterspec `orDatesFrom` journalDateSpan j
   37       spans = filter (DateSpan Nothing Nothing /=) $ splitSpan interval span
   38       spanps = [(s, filter (isPostingInDateSpan s) ps) | s <- spans]
   39       -- same as Register
   40       -- should count transactions, not postings ?
   41       ps = sortBy (comparing postingDate) $ filterempties $ filter matchapats $ filterdepth $ journalPostings j
   42       filterempties
   43           | empty_ opts = id
   44           | otherwise = filter (not . isZeroMixedAmount . pamount)
   45       matchapats = matchpats apats . paccount
   46       apats = acctpats filterspec
   47       filterdepth | interval == NoInterval = filter (\p -> accountNameLevel (paccount p) <= depth)
   48                   | otherwise = id
   49       depth = fromMaybe 99999 $ depth_ opts
   50 
   51 printDayWith f (DateSpan b _, ts) = printf "%s %s\n" (show $ fromJust b) (f ts)
   52 
   53 countBar ps = replicate (length ps) barchar