{-# LANGUAGE TemplateHaskell #-}

{-|

Print a bar chart of posting activity per day, or other report interval.

-}

module Hledger.Cli.Commands.Activity
where

import Data.List (sortOn)
import Text.Printf (printf)
import Lens.Micro ((^.), set)

import Hledger
import Hledger.Cli.CliOptions

activitymode :: Mode RawOpts
activitymode = [Char]
-> [Flag RawOpts]
-> [([Char], [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
  $(embedFileRelative "Hledger/Cli/Commands/Activity.txt")
  []
  [([Char], [Flag RawOpts])
generalflagsgroup1]
  [Flag RawOpts]
hiddenflags
  ([], forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ [Char] -> Arg RawOpts
argsFlag [Char]
"[QUERY]")

barchar :: Char
barchar :: Char
barchar = Char
'*'

-- | Print a bar chart of number of postings per report interval.
activity :: CliOpts -> Journal -> IO ()
activity :: CliOpts -> Journal -> IO ()
activity CliOpts{reportspec_ :: CliOpts -> ReportSpec
reportspec_=ReportSpec
rspec} Journal
j = [Char] -> IO ()
putStr forall a b. (a -> b) -> a -> b
$ ReportSpec -> Journal -> [Char]
showHistogram ReportSpec
rspec Journal
j

showHistogram :: ReportSpec -> Journal -> String
showHistogram :: ReportSpec -> Journal -> [Char]
showHistogram rspec :: ReportSpec
rspec@ReportSpec{_rsQuery :: ReportSpec -> Query
_rsQuery=Query
q} Journal
j =
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall {t} {t} {t}.
(PrintfArg t, PrintfType t) =>
(t -> t) -> (DateSpan, t) -> t
printDayWith forall {t :: * -> *} {a}. Foldable t => t a -> [Char]
countBar) [(DateSpan, [Posting])]
spanps
  where
    spans :: [DateSpan]
spans = forall a. (a -> Bool) -> [a] -> [a]
filter (Maybe Day -> Maybe Day -> DateSpan
DateSpan forall a. Maybe a
Nothing forall a. Maybe a
Nothing forall a. Eq a => a -> a -> Bool
/=) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. Journal -> ReportSpec -> (DateSpan, [DateSpan])
reportSpan Journal
j forall a b. (a -> b) -> a -> b
$ case ReportSpec
rspec forall s a. s -> Getting a s a -> a
^. forall c. HasReportOptsNoUpdate c => Lens' c Interval
interval of
      Interval
NoInterval -> forall s t a b. ASetter s t a b -> b -> s -> t
set forall c. HasReportOptsNoUpdate c => Lens' c Interval
interval (Int -> Interval
Days Int
1) ReportSpec
rspec
      Interval
_ -> ReportSpec
rspec
    spanps :: [(DateSpan, [Posting])]
spanps = [(DateSpan
s, forall a. (a -> Bool) -> [a] -> [a]
filter (DateSpan -> Posting -> Bool
isPostingInDateSpan DateSpan
s) [Posting]
ps) | DateSpan
s <- [DateSpan]
spans]
    -- same as Register
    -- should count transactions, not postings ?
    -- ps = sortBy (comparing postingDate) $ filterempties $ filter matchapats $ filterdepth $ journalPostings j
    ps :: [Posting]
ps = forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn Posting -> Day
postingDate forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (Query
q Query -> Posting -> Bool
`matchesPosting`) forall a b. (a -> b) -> a -> b
$ Journal -> [Posting]
journalPostings Journal
j

printDayWith :: (t -> t) -> (DateSpan, t) -> t
printDayWith t -> t
f (DateSpan (Just Day
b) Maybe Day
_, t
ps) = forall r. PrintfType r => [Char] -> r
printf [Char]
"%s %s\n" (forall a. Show a => a -> [Char]
show Day
b) (t -> t
f t
ps)
printDayWith t -> t
_ (DateSpan, t)
_ = forall a. HasCallStack => [Char] -> a
error [Char]
"Expected start date for DateSpan"  -- PARTIAL:

countBar :: t a -> [Char]
countBar t a
ps = forall a. Int -> a -> [a]
replicate (forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
ps) Char
barchar