| 1 |
{-| |
| 2 |
|
| 3 |
Print some statistics for the journal. |
| 4 |
|
| 5 |
-} |
| 6 |
|
| 7 |
{-# LANGUAGE OverloadedStrings #-} |
| 8 |
{-# LANGUAGE TemplateHaskell #-} |
| 9 |
{-# LANGUAGE NamedFieldPuns #-} |
| 10 |
{-# LANGUAGE RecordWildCards #-} |
| 11 |
|
| 12 |
module Hledger.Cli.Commands.Stats ( |
| 13 |
statsmode |
| 14 |
,stats |
| 15 |
) |
| 16 |
where |
| 17 |
|
| 18 |
import Control.Monad (when) |
| 19 |
import Data.Default (def) |
| 20 |
import Data.List (intercalate, nub, sortOn) |
| 21 |
import Data.List.Extra (nubSort) |
| 22 |
import Data.Map qualified as Map |
| 23 |
import Data.Maybe (fromMaybe) |
| 24 |
import Data.HashSet (size, fromList) |
| 25 |
import Data.Text qualified as T |
| 26 |
import Data.Text.Lazy qualified as TL |
| 27 |
import Data.Time.Calendar (Day, addDays, diffDays) |
| 28 |
import Data.Time.Clock.POSIX (getPOSIXTime) |
| 29 |
import GHC.Stats |
| 30 |
import GitHash (tGitInfoCwdTry) |
| 31 |
import System.Console.CmdArgs.Explicit hiding (Group) |
| 32 |
import System.FilePath (takeFileName) |
| 33 |
import System.Mem (performMajorGC) |
| 34 |
import Text.Printf (printf) |
| 35 |
import Text.Tabular.AsciiWide |
| 36 |
|
| 37 |
import Hledger |
| 38 |
import Hledger.Cli.CliOptions |
| 39 |
import Hledger.Cli.Utils (writeOutput) |
| 40 |
import Hledger.Cli.Version (packageversion, versionStringWith) |
| 41 |
|
| 42 |
|
| 43 |
statsmode = hledgerCommandMode |
| 44 |
$(embedFileRelative "Hledger/Cli/Commands/Stats.txt") |
| 45 |
[ flagNone ["1"] (setboolopt "") "show a single line of output" |
| 46 |
-- Cli.hs converts -1 to --depth=1, no point giving it another name here |
| 47 |
, flagNone ["verbose","v"] (setboolopt "verbose") "show more detailed output" |
| 48 |
,flagReq ["output-file","o"] (\s opts -> Right $ setopt "output-file" s opts) "FILE" "write output to FILE." |
| 49 |
] |
| 50 |
cligeneralflagsgroups1 |
| 51 |
hiddenflags |
| 52 |
([], Just $ argsFlag "[QUERY]") |
| 53 |
|
| 54 |
-- like Register.summarisePostings |
| 55 |
-- | Print various statistics for the journal. |
| 56 |
stats :: CliOpts -> Journal -> IO () |
| 57 |
stats opts@CliOpts{rawopts_=rawopts, reportspec_=rspec, progstarttime_} j = do |
| 58 |
t <- getPOSIXTime |
| 59 |
-- the first lines - general journal stats for one or more periods |
| 60 |
let |
| 61 |
today = _rsDay rspec |
| 62 |
oneline = intopt "depth" rawopts == 1 |
| 63 |
verbose = boolopt "verbose" rawopts |
| 64 |
q = _rsQuery rspec |
| 65 |
l = ledgerFromJournal q j |
| 66 |
intervalspans = snd $ reportSpanBothDates j rspec |
| 67 |
ismultiperiod = length intervalspans > 1 |
| 68 |
(txts, tnums) = unzip . map (showLedgerStats verbose l today) $ maybeDayPartitionToDateSpans intervalspans |
| 69 |
out1 = (if ismultiperiod then id else init) $ unlines txts |
| 70 |
|
| 71 |
-- the last line - overall performance stats, with memory info if available, |
| 72 |
-- in human-friendly or machine-friendly format |
| 73 |
-- normal: |
| 74 |
-- Runtime stats : 0.14 s elapsed, 7606 txns/s |
| 75 |
-- Runtime stats : 0.14 s elapsed, 7606 txns/s, 6 MB live, 18 MB alloc |
| 76 |
-- oneline: |
| 77 |
-- SHORTVERSION(<SPC><TAB>VALUE[<SPC>DESC])+ |
| 78 |
-- 1.50.99<SPC><TAB>hledger 1.50.99-g0835a2485-20251119, mac-aarch64<SPC><TAB>2025.journal<SPC><TAB>1.99 s elapsed<SPC><TAB>524 txns/s |
| 79 |
-- 1.50.99<SPC><TAB>hledger 1.50.99-g0835a2485-20251119, mac-aarch64<SPC><TAB>2025.journal<SPC><TAB>1.99 s elapsed<SPC><TAB>524 txns/s<SPC><TAB>788 MB live<SPC><TAB>2172 MB alloc |
| 80 |
-- |
| 81 |
rtsstats <- getRTSStatsEnabled |
| 82 |
(maxlivemb, maxinusemb) <- if rtsstats |
| 83 |
then do |
| 84 |
-- do one last garbage collection; probably little effect, hopefully little wasted time |
| 85 |
performMajorGC |
| 86 |
RTSStats{..} <- getRTSStats |
| 87 |
return (toMegabytes max_live_bytes, toMegabytes max_mem_in_use_bytes) |
| 88 |
else |
| 89 |
return (0,0) |
| 90 |
let |
| 91 |
(label, sep) |
| 92 |
| oneline = (lstrip $ versionStringWith $$tGitInfoCwdTry False "" packageversion <> "\t", "\t") |
| 93 |
| otherwise = ("Runtime stats : ", ", ") |
| 94 |
dt = t - progstarttime_ |
| 95 |
tnum = sum tnums |
| 96 |
ss = |
| 97 |
[ takeFileName $ journalFilePath j | oneline ] |
| 98 |
<> [ |
| 99 |
printf "%.2f s elapsed" (realToFrac dt :: Float) |
| 100 |
,printf "%.0f txns/s" (fromIntegral tnum / realToFrac dt :: Float) |
| 101 |
] |
| 102 |
<> if rtsstats then [ |
| 103 |
printf "%0.0f MB live" maxlivemb |
| 104 |
,printf "%0.0f MB alloc" maxinusemb |
| 105 |
-- printf "%0.0f MB avg live" (toMegabytes $ fromIntegral cumulative_live_bytes / fromIntegral major_gcs) |
| 106 |
] |
| 107 |
else [ |
| 108 |
"(add +RTS -T -RTS for more)" |
| 109 |
] |
| 110 |
out2 = label <> intercalate sep ss <> "\n" |
| 111 |
|
| 112 |
when (not oneline) $ writeOutput opts out1 |
| 113 |
when (oneline && debugLevel>0) $ do |
| 114 |
let tabstops = intercalate (replicate 7 ' ') (replicate 21 ".") <> "\n" |
| 115 |
writeOutput opts tabstops |
| 116 |
writeOutput opts $ (if ismultiperiod then "\n" else "") <> out2 |
| 117 |
|
| 118 |
toMegabytes n = realToFrac n / 1000000 ::Float -- SI preferred definition, 10^6 |
| 119 |
-- toMebibytes n = realToFrac n / 1048576 ::Float -- traditional computing definition, 2^20 |
| 120 |
|
| 121 |
-- | Generate multiline stats output, possibly verbose, |
| 122 |
-- for the given ledger and date period and current date. |
| 123 |
-- Also return the number of transactions in the period. |
| 124 |
showLedgerStats :: Bool -> Ledger -> Day -> DateSpan -> (String, Int) |
| 125 |
showLedgerStats verbose l today spn = |
| 126 |
(unlines $ map (TL.unpack . renderRow def{tableBorders=False, borderSpaces=False} . showRow) stts |
| 127 |
,tnum) |
| 128 |
where |
| 129 |
showRow (label, val) = Group NoLine $ map (Header . textCell TopLeft) |
| 130 |
[fitText (Just w) (Just w) False True label `T.append` ": ", T.pack val] |
| 131 |
w = 20 -- keep synced with labels above |
| 132 |
-- w = maximum $ map (T.length . fst) stts |
| 133 |
(stts, tnum) = ([ |
| 134 |
("Main file", path' :: String) -- ++ " (from " ++ source ++ ")") |
| 135 |
,("Included files", if verbose then unlines includedpaths else show (length includedpaths)) |
| 136 |
,("Txns span", printf "%s to %s (%d days)" (showstart spn) (showend spn) days) |
| 137 |
,("Last txn", maybe "none" show lastdate ++ showelapsed lastelapsed) |
| 138 |
,("Txns", printf "%d (%0.1f per day)" tnum txnrate) |
| 139 |
,("Txns last 30 days", printf "%d (%0.1f per day)" tnum30 txnrate30) |
| 140 |
,("Txns last 7 days", printf "%d (%0.1f per day)" tnum7 txnrate7) |
| 141 |
,("Payees/descriptions", show $ size $ fromList $ map (tdescription) ts) |
| 142 |
,("Accounts", printf "%d (depth %d)" acctnum acctdepth) |
| 143 |
,("Commodities", printf "%s%s" (show $ length cs) (if verbose then " (" <> T.intercalate ", " cs <> ")" else "")) |
| 144 |
,("Market prices", printf "%s%s" (show $ length mktprices) (if verbose then " (" <> T.intercalate ", " mktpricecommodities <> ")" else "")) |
| 145 |
-- Txns this month : %(monthtxns)s (last month in the same period: %(lastmonthtxns)s) |
| 146 |
-- Unmarked txns : %(unmarked)s |
| 147 |
-- Days since reconciliation : %(reconcileelapsed)s |
| 148 |
-- Days since last txn : %(recentelapsed)s |
| 149 |
] |
| 150 |
,tnum1) |
| 151 |
where |
| 152 |
j = ljournal l |
| 153 |
path' = if verbose then path else ".../" <> takeFileName path where path = journalFilePath j |
| 154 |
includedpaths = drop 1 $ journalFilePaths j |
| 155 |
ts = sortOn tdate $ filter (spanContainsDate spn . tdate) $ jtxns j |
| 156 |
as = nub $ map paccount $ concatMap tpostings ts |
| 157 |
cs = either error' Map.keys $ commodityStylesFromAmounts $ concatMap (amountsRaw . pamount) $ concatMap tpostings ts -- PARTIAL: |
| 158 |
lastdate | null ts = Nothing |
| 159 |
| otherwise = Just $ tdate $ last ts |
| 160 |
lastelapsed = fmap (diffDays today) lastdate |
| 161 |
showelapsed Nothing = "" |
| 162 |
showelapsed (Just dys) = printf " (%d %s)" dys' direction |
| 163 |
where dys' = abs dys |
| 164 |
direction | dys >= 0 = "days ago" :: String |
| 165 |
| otherwise = "days from now" |
| 166 |
tnum1 = length ts -- Integer would be better |
| 167 |
showstart (DateSpan (Just efd) _) = show $ fromEFDay efd |
| 168 |
showstart _ = "" |
| 169 |
showend (DateSpan _ (Just efd)) = show $ fromEFDay efd |
| 170 |
showend _ = "" |
| 171 |
days = fromMaybe 0 $ daysInSpan spn |
| 172 |
txnrate | days==0 = 0 |
| 173 |
| otherwise = fromIntegral tnum1 / fromIntegral days :: Double |
| 174 |
tnum30 = length $ filter withinlast30 ts |
| 175 |
withinlast30 t = d >= addDays (-30) today && (d<=today) where d = tdate t |
| 176 |
txnrate30 = fromIntegral tnum30 / 30 :: Double |
| 177 |
tnum7 = length $ filter withinlast7 ts |
| 178 |
withinlast7 t = d >= addDays (-7) today && (d<=today) where d = tdate t |
| 179 |
txnrate7 = fromIntegral tnum7 / 7 :: Double |
| 180 |
acctnum = length as |
| 181 |
acctdepth | null as = 0 |
| 182 |
| otherwise = maximum $ map accountNameLevel as |
| 183 |
mktprices = jpricedirectives j |
| 184 |
mktpricecommodities = nubSort $ map pdcommodity mktprices |