{-# LANGUAGE TemplateHaskell #-}

{-|

Show a bar chart of transaction counts per week, or other report interval.
With --postings, show posting counts by account instead;
this is a shortcut for @balance --count --chart -W@.

-}

module Hledger.Cli.Commands.Activity
where

import Data.Maybe (fromMaybe)
import System.Console.CmdArgs.Explicit (flagNone)

import Hledger
import Hledger.Cli.Chart (chartWidthFlag)
import Hledger.Cli.CliOptions
import Hledger.Cli.Commands.Balance (balance)

activitymode :: Mode RawOpts
activitymode = CommandHelpStr
-> [Flag RawOpts]
-> [(CommandHelpStr, [Flag RawOpts])]
-> [Flag RawOpts]
-> ([Arg RawOpts], Maybe (Arg RawOpts))
-> Mode RawOpts
hledgerCommandMode
  $(embedFileRelative "Hledger/Cli/Commands/Activity.txt")
  [ [CommandHelpStr] -> Flag RawOpts
outputFormatFlag [CommandHelpStr
"txt",CommandHelpStr
"svg",CommandHelpStr
"html",CommandHelpStr
"csv",CommandHelpStr
"tsv",CommandHelpStr
"json"]
  , Flag RawOpts
outputFileFlag
  , Flag RawOpts
outputViewFlag
  , Flag RawOpts
chartWidthFlag
  , [CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"txns"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"txns") CommandHelpStr
"count transactions, showing just the total (default)"
  , [CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"postings"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"postings") CommandHelpStr
"count postings, showing each account's count"
  , [CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"sort-amount",CommandHelpStr
"S"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"sort-amount") CommandHelpStr
"with --postings, sort accounts by posting count, largest first"
  , [CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"total",CommandHelpStr
"T"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"row-total") CommandHelpStr
"show the total count, below the chart"
  , [CommandHelpStr]
-> (RawOpts -> RawOpts) -> CommandHelpStr -> Flag RawOpts
forall a. [CommandHelpStr] -> (a -> a) -> CommandHelpStr -> Flag a
flagNone [CommandHelpStr
"average",CommandHelpStr
"A"] (CommandHelpStr -> RawOpts -> RawOpts
setboolopt CommandHelpStr
"average") CommandHelpStr
"show the average count per period, below the chart (and as a line in SVG charts)"
  ]
  [(CommandHelpStr, [Flag RawOpts])]
cligeneralflagsgroups1
  [Flag RawOpts]
hiddenflags
  ([], Arg RawOpts -> Maybe (Arg RawOpts)
forall a. a -> Maybe a
Just (Arg RawOpts -> Maybe (Arg RawOpts))
-> Arg RawOpts -> Maybe (Arg RawOpts)
forall a b. (a -> b) -> a -> b
$ CommandHelpStr -> Arg RawOpts
argsFlag CommandHelpStr
"[QUERY]")

-- | Show a bar chart of the number of transactions per report interval (weekly by default),
-- as a single total (like depth 0), since a transaction's postings may be in several accounts.
-- Or with --postings, the number of postings, by account.
-- This runs the balance command with --count (or its transaction-counting equivalent) and --chart.
-- If both --txns and --postings are given, the last one wins.
activity :: CliOpts -> Journal -> IO ()
activity :: CliOpts -> Journal -> IO ()
activity opts :: CliOpts
opts@CliOpts{RawOpts
rawopts_ :: RawOpts
rawopts_ :: CliOpts -> RawOpts
rawopts_, reportspec_ :: CliOpts -> ReportSpec
reportspec_=rspec :: ReportSpec
rspec@ReportSpec{_rsReportOpts :: ReportSpec -> ReportOpts
_rsReportOpts=ReportOpts
ropts}} Journal
j = do
  rspec' <- (CommandHelpStr -> IO ReportSpec)
-> (ReportSpec -> IO ReportSpec)
-> Either CommandHelpStr ReportSpec
-> IO ReportSpec
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either CommandHelpStr -> IO ReportSpec
forall a. HasCallStack => CommandHelpStr -> IO a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
CommandHelpStr -> m a
fail ReportSpec -> IO ReportSpec
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either CommandHelpStr ReportSpec -> IO ReportSpec)
-> Either CommandHelpStr ReportSpec -> IO ReportSpec
forall a b. (a -> b) -> a -> b
$ ReportOpts -> ReportSpec -> Either CommandHelpStr ReportSpec
updateReportSpec ReportOpts
ropts' ReportSpec
rspec
  balance opts
    { rawopts_    = setopt "chart" "auto" rawopts_
    , reportspec_ = rspec'
    } j
  where
    txns :: Bool
txns = Bool -> Maybe Bool -> Bool
forall a. a -> Maybe a -> a
fromMaybe Bool
True (Maybe Bool -> Bool) -> Maybe Bool -> Bool
forall a b. (a -> b) -> a -> b
$ (CommandHelpStr -> Maybe Bool) -> RawOpts -> Maybe Bool
forall a. (CommandHelpStr -> Maybe a) -> RawOpts -> Maybe a
choiceopt CommandHelpStr -> Maybe Bool
countopt RawOpts
rawopts_
      where
        countopt :: CommandHelpStr -> Maybe Bool
countopt CommandHelpStr
"txns"     = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
        countopt CommandHelpStr
"postings" = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
        countopt CommandHelpStr
_          = Maybe Bool
forall a. Maybe a
Nothing
    ropts' :: ReportOpts
ropts' = ReportOpts
ropts
      { balancecalc_ = if txns then CalcTransactionsCount else CalcPostingsCount
      , interval_    = if interval_ ropts == NoInterval then Weeks 1 else interval_ ropts
      , depth_       = if txns then DepthSpec (Just 0) [] else depth_ ropts
      }