2009-04-03 14:58:05 +04:00
|
|
|
{-|
|
|
|
|
|
2012-05-14 22:52:22 +04:00
|
|
|
A 'Posting' represents a change (by some 'MixedAmount') of the balance in
|
|
|
|
some 'Account'. Each 'Transaction' contains two or more postings which
|
|
|
|
should add up to 0. Postings reference their parent transaction, so we can
|
|
|
|
look up the date or description there.
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-03-25 01:51:56 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
|
2012-05-07 18:36:40 +04:00
|
|
|
module Hledger.Data.Posting (
|
|
|
|
-- * Posting
|
|
|
|
nullposting,
|
2012-12-06 04:03:07 +04:00
|
|
|
posting,
|
|
|
|
post,
|
2019-02-22 03:20:04 +03:00
|
|
|
vpost,
|
|
|
|
post',
|
|
|
|
vpost',
|
2018-10-12 06:37:20 +03:00
|
|
|
nullsourcepos,
|
|
|
|
nullassertion,
|
|
|
|
assertion,
|
2019-02-22 03:20:04 +03:00
|
|
|
balassert,
|
|
|
|
balassertTot,
|
|
|
|
balassertParInc,
|
|
|
|
balassertTotInc,
|
2012-05-07 18:36:40 +04:00
|
|
|
-- * operations
|
2017-01-13 18:25:44 +03:00
|
|
|
originalPosting,
|
2015-05-16 21:51:35 +03:00
|
|
|
postingStatus,
|
2012-05-07 18:36:40 +04:00
|
|
|
isReal,
|
|
|
|
isVirtual,
|
|
|
|
isBalancedVirtual,
|
|
|
|
isEmptyPosting,
|
2019-02-18 23:11:07 +03:00
|
|
|
hasBalanceAssignment,
|
2012-05-07 18:36:40 +04:00
|
|
|
hasAmount,
|
2012-05-28 04:27:55 +04:00
|
|
|
postingAllTags,
|
|
|
|
transactionAllTags,
|
2012-12-22 04:24:38 +04:00
|
|
|
relatedPostings,
|
2016-12-10 18:04:48 +03:00
|
|
|
removePrices,
|
2012-05-07 18:36:40 +04:00
|
|
|
-- * date operations
|
|
|
|
postingDate,
|
2012-12-06 08:43:41 +04:00
|
|
|
postingDate2,
|
2012-05-07 18:36:40 +04:00
|
|
|
isPostingInDateSpan,
|
2014-04-14 01:57:40 +04:00
|
|
|
isPostingInDateSpan',
|
2012-05-07 18:36:40 +04:00
|
|
|
postingsDateSpan,
|
2014-04-14 02:14:07 +04:00
|
|
|
postingsDateSpan',
|
2012-05-14 22:52:22 +04:00
|
|
|
-- * account name operations
|
2012-05-07 18:36:40 +04:00
|
|
|
accountNamesFromPostings,
|
|
|
|
accountNamePostingType,
|
|
|
|
accountNameWithoutPostingType,
|
|
|
|
accountNameWithPostingType,
|
|
|
|
joinAccountNames,
|
|
|
|
concatAccountNames,
|
|
|
|
accountNameApplyAliases,
|
2015-09-27 04:54:46 +03:00
|
|
|
accountNameApplyAliasesMemo,
|
2019-07-17 20:38:14 +03:00
|
|
|
-- * comment/tag operations
|
|
|
|
commentJoin,
|
|
|
|
commentAddTag,
|
|
|
|
commentAddTagNextLine,
|
2012-05-07 18:36:40 +04:00
|
|
|
-- * arithmetic
|
|
|
|
sumPostings,
|
|
|
|
-- * rendering
|
|
|
|
showPosting,
|
|
|
|
-- * misc.
|
2012-05-14 22:52:22 +04:00
|
|
|
showComment,
|
2019-05-23 22:16:20 +03:00
|
|
|
postingTransformAmount,
|
2019-05-28 21:42:32 +03:00
|
|
|
postingApplyValuation,
|
2019-05-23 22:16:20 +03:00
|
|
|
postingToCost,
|
2018-09-06 23:08:26 +03:00
|
|
|
tests_Posting
|
2012-05-07 18:36:40 +04:00
|
|
|
)
|
2009-04-03 14:58:05 +04:00
|
|
|
where
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.List
|
2019-05-23 22:16:20 +03:00
|
|
|
import qualified Data.Map as M
|
2012-12-06 04:03:07 +04:00
|
|
|
import Data.Maybe
|
2015-09-27 04:45:44 +03:00
|
|
|
import Data.MemoUgly (memo)
|
2018-03-25 01:51:56 +03:00
|
|
|
#if !(MIN_VERSION_base(4,11,0))
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
import Data.Monoid
|
2018-03-25 01:51:56 +03:00
|
|
|
#endif
|
lib: textification: comments and tags
No change.
hledger -f data/100x100x10.journal stats
<<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.020 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
<<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.018 elapsed), 0.009 GC (0.013 elapsed) :ghc>>
hledger -f data/1000x1000x10.journal stats
<<ghc: 349576344 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.124 MUT (0.130 elapsed), 0.047 GC (0.055 elapsed) :ghc>>
<<ghc: 349576280 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.126 MUT (0.132 elapsed), 0.049 GC (0.058 elapsed) :ghc>>
hledger -f data/10000x1000x10.journal stats
<<ghc: 3424030664 bytes, 6658 GCs, 11403359/41071624 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.000 elapsed), 1.207 MUT (1.228 elapsed), 0.473 GC (0.528 elapsed) :ghc>>
<<ghc: 3424030760 bytes, 6658 GCs, 11403874/41077288 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.002 elapsed), 1.234 MUT (1.256 elapsed), 0.470 GC (0.520 elapsed) :ghc>>
hledger -f data/100000x1000x10.journal stats
<<ghc: 34306547448 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.003 elapsed), 12.615 MUT (12.813 elapsed), 4.656 GC (5.291 elapsed) :ghc>>
<<ghc: 34306547320 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.009 elapsed), 12.802 MUT (13.065 elapsed), 4.774 GC (5.441 elapsed) :ghc>>
2016-05-25 03:09:20 +03:00
|
|
|
import Data.Text (Text)
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
import qualified Data.Text as T
|
2011-05-28 08:11:44 +04:00
|
|
|
import Data.Time.Calendar
|
2011-08-03 03:29:13 +04:00
|
|
|
import Safe
|
2011-05-28 08:11:44 +04:00
|
|
|
|
2019-07-15 13:28:52 +03:00
|
|
|
import Hledger.Utils
|
2010-05-20 03:08:53 +04:00
|
|
|
import Hledger.Data.Types
|
|
|
|
import Hledger.Data.Amount
|
|
|
|
import Hledger.Data.AccountName
|
2010-07-11 22:56:36 +04:00
|
|
|
import Hledger.Data.Dates (nulldate, spanContainsDate)
|
2019-06-15 02:17:06 +03:00
|
|
|
import Hledger.Data.Valuation
|
2009-04-03 14:58:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-06 04:03:07 +04:00
|
|
|
nullposting, posting :: Posting
|
|
|
|
nullposting = Posting
|
2012-12-06 04:28:23 +04:00
|
|
|
{pdate=Nothing
|
2012-12-06 06:41:37 +04:00
|
|
|
,pdate2=Nothing
|
2017-06-16 02:25:37 +03:00
|
|
|
,pstatus=Unmarked
|
2012-12-06 04:03:07 +04:00
|
|
|
,paccount=""
|
|
|
|
,pamount=nullmixedamt
|
|
|
|
,pcomment=""
|
|
|
|
,ptype=RegularPosting
|
|
|
|
,ptags=[]
|
2013-05-29 03:18:15 +04:00
|
|
|
,pbalanceassertion=Nothing
|
2012-12-06 04:03:07 +04:00
|
|
|
,ptransaction=Nothing
|
2019-02-21 07:07:40 +03:00
|
|
|
,poriginal=Nothing
|
2012-12-06 04:03:07 +04:00
|
|
|
}
|
|
|
|
posting = nullposting
|
|
|
|
|
2019-02-22 03:20:04 +03:00
|
|
|
-- constructors
|
|
|
|
|
|
|
|
-- | Make a posting to an account.
|
2012-12-06 04:03:07 +04:00
|
|
|
post :: AccountName -> Amount -> Posting
|
2019-02-22 03:20:04 +03:00
|
|
|
post acc amt = posting {paccount=acc, pamount=Mixed [amt]}
|
|
|
|
|
|
|
|
-- | Make a virtual (unbalanced) posting to an account.
|
|
|
|
vpost :: AccountName -> Amount -> Posting
|
|
|
|
vpost acc amt = (post acc amt){ptype=VirtualPosting}
|
|
|
|
|
|
|
|
-- | Make a posting to an account, maybe with a balance assertion.
|
|
|
|
post' :: AccountName -> Amount -> Maybe BalanceAssertion -> Posting
|
|
|
|
post' acc amt ass = posting {paccount=acc, pamount=Mixed [amt], pbalanceassertion=ass}
|
|
|
|
|
|
|
|
-- | Make a virtual (unbalanced) posting to an account, maybe with a balance assertion.
|
|
|
|
vpost' :: AccountName -> Amount -> Maybe BalanceAssertion -> Posting
|
|
|
|
vpost' acc amt ass = (post' acc amt ass){ptype=VirtualPosting, pbalanceassertion=ass}
|
2009-04-03 14:58:05 +04:00
|
|
|
|
2018-10-12 06:37:20 +03:00
|
|
|
nullsourcepos :: GenericSourcePos
|
|
|
|
nullsourcepos = JournalSourcePos "" (1,1)
|
|
|
|
|
|
|
|
nullassertion, assertion :: BalanceAssertion
|
|
|
|
nullassertion = BalanceAssertion
|
|
|
|
{baamount=nullamt
|
2019-02-15 21:34:40 +03:00
|
|
|
,batotal=False
|
2019-02-18 06:50:22 +03:00
|
|
|
,bainclusive=False
|
2018-10-12 06:37:20 +03:00
|
|
|
,baposition=nullsourcepos
|
|
|
|
}
|
|
|
|
assertion = nullassertion
|
|
|
|
|
2019-02-22 03:20:04 +03:00
|
|
|
-- | Make a partial, exclusive balance assertion.
|
|
|
|
balassert :: Amount -> Maybe BalanceAssertion
|
|
|
|
balassert amt = Just $ nullassertion{baamount=amt}
|
|
|
|
|
|
|
|
-- | Make a total, exclusive balance assertion.
|
|
|
|
balassertTot :: Amount -> Maybe BalanceAssertion
|
|
|
|
balassertTot amt = Just $ nullassertion{baamount=amt, batotal=True}
|
|
|
|
|
|
|
|
-- | Make a partial, inclusive balance assertion.
|
|
|
|
balassertParInc :: Amount -> Maybe BalanceAssertion
|
|
|
|
balassertParInc amt = Just $ nullassertion{baamount=amt, bainclusive=True}
|
|
|
|
|
|
|
|
-- | Make a total, inclusive balance assertion.
|
|
|
|
balassertTotInc :: Amount -> Maybe BalanceAssertion
|
|
|
|
balassertTotInc amt = Just $ nullassertion{baamount=amt, batotal=True, bainclusive=True}
|
|
|
|
|
2017-05-30 23:39:53 +03:00
|
|
|
-- Get the original posting, if any.
|
2017-01-13 18:25:44 +03:00
|
|
|
originalPosting :: Posting -> Posting
|
2019-02-21 07:07:40 +03:00
|
|
|
originalPosting p = fromMaybe p $ poriginal p
|
2017-01-13 18:25:44 +03:00
|
|
|
|
2014-05-07 08:35:38 +04:00
|
|
|
-- XXX once rendered user output, but just for debugging now; clean up
|
2009-04-03 14:58:05 +04:00
|
|
|
showPosting :: Posting -> String
|
2012-05-14 22:52:22 +04:00
|
|
|
showPosting p@Posting{paccount=a,pamount=amt,ptype=t} =
|
2014-05-07 08:35:38 +04:00
|
|
|
unlines $ [concatTopPadded [show (postingDate p) ++ " ", showaccountname a ++ " ", showamount amt, showComment (pcomment p)]]
|
2009-04-03 14:58:05 +04:00
|
|
|
where
|
2009-11-25 09:13:35 +03:00
|
|
|
ledger3ishlayout = False
|
|
|
|
acctnamewidth = if ledger3ishlayout then 25 else 22
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
showaccountname = fitString (Just acctnamewidth) Nothing False False . bracket . T.unpack . elideAccountName width
|
2009-12-19 06:44:52 +03:00
|
|
|
(bracket,width) = case t of
|
2009-11-25 09:13:35 +03:00
|
|
|
BalancedVirtualPosting -> (\s -> "["++s++"]", acctnamewidth-2)
|
|
|
|
VirtualPosting -> (\s -> "("++s++")", acctnamewidth-2)
|
|
|
|
_ -> (id,acctnamewidth)
|
2015-10-10 21:53:28 +03:00
|
|
|
showamount = padLeftWide 12 . showMixedAmount
|
2012-05-14 22:52:22 +04:00
|
|
|
|
|
|
|
|
lib: textification: comments and tags
No change.
hledger -f data/100x100x10.journal stats
<<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.016 MUT (0.020 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
<<ghc: 42859576 bytes, 84 GCs, 193781/269984 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.018 elapsed), 0.009 GC (0.013 elapsed) :ghc>>
hledger -f data/1000x1000x10.journal stats
<<ghc: 349576344 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.124 MUT (0.130 elapsed), 0.047 GC (0.055 elapsed) :ghc>>
<<ghc: 349576280 bytes, 681 GCs, 1407388/4091680 avg/max bytes residency (7 samples), 11M in use, 0.000 INIT (0.000 elapsed), 0.126 MUT (0.132 elapsed), 0.049 GC (0.058 elapsed) :ghc>>
hledger -f data/10000x1000x10.journal stats
<<ghc: 3424030664 bytes, 6658 GCs, 11403359/41071624 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.000 elapsed), 1.207 MUT (1.228 elapsed), 0.473 GC (0.528 elapsed) :ghc>>
<<ghc: 3424030760 bytes, 6658 GCs, 11403874/41077288 avg/max bytes residency (11 samples), 111M in use, 0.000 INIT (0.002 elapsed), 1.234 MUT (1.256 elapsed), 0.470 GC (0.520 elapsed) :ghc>>
hledger -f data/100000x1000x10.journal stats
<<ghc: 34306547448 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.003 elapsed), 12.615 MUT (12.813 elapsed), 4.656 GC (5.291 elapsed) :ghc>>
<<ghc: 34306547320 bytes, 66727 GCs, 76805504/414629288 avg/max bytes residency (14 samples), 1009M in use, 0.000 INIT (0.009 elapsed), 12.802 MUT (13.065 elapsed), 4.774 GC (5.441 elapsed) :ghc>>
2016-05-25 03:09:20 +03:00
|
|
|
showComment :: Text -> String
|
|
|
|
showComment t = if T.null t then "" else " ;" ++ T.unpack t
|
2012-05-14 22:52:22 +04:00
|
|
|
|
2009-04-03 14:58:05 +04:00
|
|
|
isReal :: Posting -> Bool
|
|
|
|
isReal p = ptype p == RegularPosting
|
|
|
|
|
2009-05-17 03:12:42 +04:00
|
|
|
isVirtual :: Posting -> Bool
|
|
|
|
isVirtual p = ptype p == VirtualPosting
|
|
|
|
|
|
|
|
isBalancedVirtual :: Posting -> Bool
|
|
|
|
isBalancedVirtual p = ptype p == BalancedVirtualPosting
|
|
|
|
|
2009-04-03 14:58:05 +04:00
|
|
|
hasAmount :: Posting -> Bool
|
2012-05-27 22:14:20 +04:00
|
|
|
hasAmount = (/= missingmixedamt) . pamount
|
2009-05-25 21:28:41 +04:00
|
|
|
|
2019-02-18 23:11:07 +03:00
|
|
|
hasBalanceAssignment :: Posting -> Bool
|
|
|
|
hasBalanceAssignment p = not (hasAmount p) && isJust (pbalanceassertion p)
|
2016-12-10 18:04:48 +03:00
|
|
|
|
2017-12-29 23:16:46 +03:00
|
|
|
-- | Sorted unique account names referenced by these postings.
|
2009-12-19 08:57:54 +03:00
|
|
|
accountNamesFromPostings :: [Posting] -> [AccountName]
|
2017-12-29 23:16:46 +03:00
|
|
|
accountNamesFromPostings = nub . sort . map paccount
|
2009-12-19 08:57:54 +03:00
|
|
|
|
|
|
|
sumPostings :: [Posting] -> MixedAmount
|
2017-01-13 03:24:53 +03:00
|
|
|
sumPostings = sumStrict . map pamount
|
2009-12-19 08:57:54 +03:00
|
|
|
|
2016-12-10 18:04:48 +03:00
|
|
|
-- | Remove all prices of a posting
|
|
|
|
removePrices :: Posting -> Posting
|
|
|
|
removePrices p = p{ pamount = Mixed $ remove <$> amounts (pamount p) }
|
2019-06-08 00:23:19 +03:00
|
|
|
where remove a = a { aprice = Nothing }
|
2016-12-10 18:04:48 +03:00
|
|
|
|
2012-12-06 06:41:37 +04:00
|
|
|
-- | Get a posting's (primary) date - it's own primary date if specified,
|
|
|
|
-- otherwise the parent transaction's primary date, or the null date if
|
|
|
|
-- there is no parent transaction.
|
2009-12-19 08:57:54 +03:00
|
|
|
postingDate :: Posting -> Day
|
2012-12-06 04:28:23 +04:00
|
|
|
postingDate p = fromMaybe txndate $ pdate p
|
2014-09-11 00:07:53 +04:00
|
|
|
where
|
2012-12-06 04:28:23 +04:00
|
|
|
txndate = maybe nulldate tdate $ ptransaction p
|
2009-12-19 08:57:54 +03:00
|
|
|
|
2012-12-06 08:43:41 +04:00
|
|
|
-- | Get a posting's secondary (secondary) date, which is the first of:
|
2012-12-06 06:41:37 +04:00
|
|
|
-- posting's secondary date, transaction's secondary date, posting's
|
|
|
|
-- primary date, transaction's primary date, or the null date if there is
|
|
|
|
-- no parent transaction.
|
2012-12-06 08:43:41 +04:00
|
|
|
postingDate2 :: Posting -> Day
|
|
|
|
postingDate2 p = headDef nulldate $ catMaybes dates
|
2012-12-06 06:41:37 +04:00
|
|
|
where dates = [pdate2 p
|
2012-12-06 08:43:41 +04:00
|
|
|
,maybe Nothing tdate2 $ ptransaction p
|
2012-12-06 06:41:37 +04:00
|
|
|
,pdate p
|
2019-02-14 16:14:52 +03:00
|
|
|
,fmap tdate (ptransaction p)
|
2012-12-06 06:41:37 +04:00
|
|
|
]
|
2012-12-06 05:10:15 +04:00
|
|
|
|
2017-06-16 02:25:37 +03:00
|
|
|
-- | Get a posting's status. This is cleared or pending if those are
|
|
|
|
-- explicitly set on the posting, otherwise the status of its parent
|
|
|
|
-- transaction, or unmarked if there is no parent transaction. (Note
|
2019-07-15 13:28:52 +03:00
|
|
|
-- the ambiguity, unmarked can mean "posting and transaction are both
|
2017-06-16 02:25:37 +03:00
|
|
|
-- unmarked" or "posting is unmarked and don't know about the transaction".
|
2017-06-16 02:54:34 +03:00
|
|
|
postingStatus :: Posting -> Status
|
2015-05-16 21:51:35 +03:00
|
|
|
postingStatus Posting{pstatus=s, ptransaction=mt}
|
2017-06-16 02:25:37 +03:00
|
|
|
| s == Unmarked = case mt of Just t -> tstatus t
|
|
|
|
Nothing -> Unmarked
|
2015-05-16 21:51:35 +03:00
|
|
|
| otherwise = s
|
|
|
|
|
2012-05-28 04:27:55 +04:00
|
|
|
-- | Tags for this posting including any inherited from its parent transaction.
|
|
|
|
postingAllTags :: Posting -> [Tag]
|
2014-03-06 02:43:58 +04:00
|
|
|
postingAllTags p = ptags p ++ maybe [] ttags (ptransaction p)
|
2012-05-28 04:27:55 +04:00
|
|
|
|
2014-03-02 05:42:13 +04:00
|
|
|
-- | Tags for this transaction including any from its postings.
|
2012-05-28 04:27:55 +04:00
|
|
|
transactionAllTags :: Transaction -> [Tag]
|
2014-03-02 05:42:13 +04:00
|
|
|
transactionAllTags t = ttags t ++ concatMap ptags (tpostings t)
|
2012-05-28 04:27:55 +04:00
|
|
|
|
2012-12-22 04:24:38 +04:00
|
|
|
-- Get the other postings from this posting's transaction.
|
|
|
|
relatedPostings :: Posting -> [Posting]
|
|
|
|
relatedPostings p@Posting{ptransaction=Just t} = filter (/= p) $ tpostings t
|
|
|
|
relatedPostings _ = []
|
|
|
|
|
2009-12-19 08:57:54 +03:00
|
|
|
-- | Does this posting fall within the given date span ?
|
|
|
|
isPostingInDateSpan :: DateSpan -> Posting -> Bool
|
2010-07-11 22:56:36 +04:00
|
|
|
isPostingInDateSpan s = spanContainsDate s . postingDate
|
2009-12-21 08:23:07 +03:00
|
|
|
|
2014-04-14 01:57:40 +04:00
|
|
|
-- --date2-sensitive version, separate for now to avoid disturbing multiBalanceReport.
|
|
|
|
isPostingInDateSpan' :: WhichDate -> DateSpan -> Posting -> Bool
|
|
|
|
isPostingInDateSpan' PrimaryDate s = spanContainsDate s . postingDate
|
|
|
|
isPostingInDateSpan' SecondaryDate s = spanContainsDate s . postingDate2
|
|
|
|
|
2009-12-21 08:23:07 +03:00
|
|
|
isEmptyPosting :: Posting -> Bool
|
|
|
|
isEmptyPosting = isZeroMixedAmount . pamount
|
|
|
|
|
2012-05-14 22:52:22 +04:00
|
|
|
-- | Get the minimal date span which contains all the postings, or the
|
|
|
|
-- null date span if there are none.
|
2009-12-21 09:03:34 +03:00
|
|
|
postingsDateSpan :: [Posting] -> DateSpan
|
|
|
|
postingsDateSpan [] = DateSpan Nothing Nothing
|
|
|
|
postingsDateSpan ps = DateSpan (Just $ postingDate $ head ps') (Just $ addDays 1 $ postingDate $ last ps')
|
2019-02-14 16:14:52 +03:00
|
|
|
where ps' = sortOn postingDate ps
|
2009-12-21 09:03:34 +03:00
|
|
|
|
2014-04-14 02:14:07 +04:00
|
|
|
-- --date2-sensitive version, as above.
|
|
|
|
postingsDateSpan' :: WhichDate -> [Posting] -> DateSpan
|
2014-04-14 19:31:11 +04:00
|
|
|
postingsDateSpan' _ [] = DateSpan Nothing Nothing
|
2014-04-14 02:14:07 +04:00
|
|
|
postingsDateSpan' wd ps = DateSpan (Just $ postingdate $ head ps') (Just $ addDays 1 $ postingdate $ last ps')
|
|
|
|
where
|
2019-02-14 16:14:52 +03:00
|
|
|
ps' = sortOn postingdate ps
|
2014-04-14 02:14:07 +04:00
|
|
|
postingdate = if wd == PrimaryDate then postingDate else postingDate2
|
|
|
|
|
2011-08-05 04:05:39 +04:00
|
|
|
-- AccountName stuff that depends on PostingType
|
2011-08-03 03:29:13 +04:00
|
|
|
|
|
|
|
accountNamePostingType :: AccountName -> PostingType
|
|
|
|
accountNamePostingType a
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
| T.null a = RegularPosting
|
|
|
|
| T.head a == '[' && T.last a == ']' = BalancedVirtualPosting
|
|
|
|
| T.head a == '(' && T.last a == ')' = VirtualPosting
|
2011-08-03 03:29:13 +04:00
|
|
|
| otherwise = RegularPosting
|
|
|
|
|
|
|
|
accountNameWithoutPostingType :: AccountName -> AccountName
|
|
|
|
accountNameWithoutPostingType a = case accountNamePostingType a of
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
BalancedVirtualPosting -> T.init $ T.tail a
|
|
|
|
VirtualPosting -> T.init $ T.tail a
|
2011-08-03 03:29:13 +04:00
|
|
|
RegularPosting -> a
|
|
|
|
|
|
|
|
accountNameWithPostingType :: PostingType -> AccountName -> AccountName
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
accountNameWithPostingType BalancedVirtualPosting a = "["<>accountNameWithoutPostingType a<>"]"
|
|
|
|
accountNameWithPostingType VirtualPosting a = "("<>accountNameWithoutPostingType a<>")"
|
2011-08-03 03:29:13 +04:00
|
|
|
accountNameWithPostingType RegularPosting a = accountNameWithoutPostingType a
|
|
|
|
|
|
|
|
-- | Prefix one account name to another, preserving posting type
|
|
|
|
-- indicators like concatAccountNames.
|
|
|
|
joinAccountNames :: AccountName -> AccountName -> AccountName
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
joinAccountNames a b = concatAccountNames $ filter (not . T.null) [a,b]
|
2011-08-03 03:29:13 +04:00
|
|
|
|
|
|
|
-- | Join account names into one. If any of them has () or [] posting type
|
|
|
|
-- indicators, these (the first type encountered) will also be applied to
|
|
|
|
-- the resulting account name.
|
|
|
|
concatAccountNames :: [AccountName] -> AccountName
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
concatAccountNames as = accountNameWithPostingType t $ T.intercalate ":" $ map accountNameWithoutPostingType as
|
2011-08-03 03:29:13 +04:00
|
|
|
where t = headDef RegularPosting $ filter (/= RegularPosting) $ map accountNamePostingType as
|
|
|
|
|
2015-05-14 22:50:32 +03:00
|
|
|
-- | Rewrite an account name using all matching aliases from the given list, in sequence.
|
|
|
|
-- Each alias sees the result of applying the previous aliases.
|
2014-10-25 02:05:10 +04:00
|
|
|
accountNameApplyAliases :: [AccountAlias] -> AccountName -> AccountName
|
|
|
|
accountNameApplyAliases aliases a = accountNameWithPostingType atype aname'
|
|
|
|
where
|
|
|
|
(aname,atype) = (accountNameWithoutPostingType a, accountNamePostingType a)
|
2015-05-14 22:50:32 +03:00
|
|
|
aname' = foldl
|
2015-05-28 20:39:12 +03:00
|
|
|
(\acct alias -> dbg6 "result" $ aliasReplace (dbg6 "alias" alias) (dbg6 "account" acct))
|
2015-05-14 22:50:32 +03:00
|
|
|
aname
|
|
|
|
aliases
|
|
|
|
|
2015-09-27 04:54:46 +03:00
|
|
|
-- | Memoising version of accountNameApplyAliases, maybe overkill.
|
|
|
|
accountNameApplyAliasesMemo :: [AccountAlias] -> AccountName -> AccountName
|
|
|
|
accountNameApplyAliasesMemo aliases = memo (accountNameApplyAliases aliases)
|
2019-08-19 04:16:39 +03:00
|
|
|
-- XXX re-test this memoisation
|
2015-09-27 04:54:46 +03:00
|
|
|
|
2015-05-14 22:50:32 +03:00
|
|
|
-- aliasMatches :: AccountAlias -> AccountName -> Bool
|
|
|
|
-- aliasMatches (BasicAlias old _) a = old `isAccountNamePrefixOf` a
|
|
|
|
-- aliasMatches (RegexAlias re _) a = regexMatchesCI re a
|
|
|
|
|
|
|
|
aliasReplace :: AccountAlias -> AccountName -> AccountName
|
2015-05-28 20:39:12 +03:00
|
|
|
aliasReplace (BasicAlias old new) a
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
| old `isAccountNamePrefixOf` a || old == a = new <> T.drop (T.length old) a
|
2015-05-28 20:39:12 +03:00
|
|
|
| otherwise = a
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
aliasReplace (RegexAlias re repl) a = T.pack $ regexReplaceCIMemo re repl $ T.unpack a -- XXX
|
2014-10-29 18:20:27 +03:00
|
|
|
|
2019-09-05 23:41:36 +03:00
|
|
|
-- | Apply a specified valuation to this posting's amount, using the
|
|
|
|
-- provided price oracle, commodity styles, reference dates, and
|
|
|
|
-- whether this is for a multiperiod report or not. See
|
|
|
|
-- amountApplyValuation.
|
|
|
|
postingApplyValuation :: PriceOracle -> M.Map CommoditySymbol AmountStyle -> Day -> Maybe Day -> Day -> Bool -> Posting -> ValuationType -> Posting
|
|
|
|
postingApplyValuation priceoracle styles periodlast mreportlast today ismultiperiod p v =
|
2019-05-28 21:42:32 +03:00
|
|
|
case v of
|
2019-06-02 01:28:10 +03:00
|
|
|
AtCost Nothing -> postingToCost styles p
|
2019-09-05 23:41:36 +03:00
|
|
|
AtCost mc -> postingValueAtDate priceoracle styles mc periodlast $ postingToCost styles p
|
|
|
|
AtEnd mc -> postingValueAtDate priceoracle styles mc periodlast p
|
|
|
|
AtNow mc -> postingValueAtDate priceoracle styles mc today p
|
|
|
|
AtDefault mc | ismultiperiod -> postingValueAtDate priceoracle styles mc periodlast p
|
|
|
|
AtDefault mc -> postingValueAtDate priceoracle styles mc (fromMaybe today mreportlast) p
|
|
|
|
AtDate d mc -> postingValueAtDate priceoracle styles mc d p
|
2019-05-28 21:42:32 +03:00
|
|
|
|
|
|
|
-- | Convert this posting's amount to cost, and apply the appropriate amount styles.
|
|
|
|
postingToCost :: M.Map CommoditySymbol AmountStyle -> Posting -> Posting
|
|
|
|
postingToCost styles p@Posting{pamount=a} = p{pamount=mixedAmountToCost styles a}
|
2019-05-23 22:16:20 +03:00
|
|
|
|
2019-06-02 01:28:10 +03:00
|
|
|
-- | Convert this posting's amount to market value in the given commodity,
|
|
|
|
-- or the default valuation commodity, at the given valuation date,
|
2019-08-19 04:16:39 +03:00
|
|
|
-- using the given market price oracle.
|
2019-06-02 01:28:10 +03:00
|
|
|
-- When market prices available on that date are not sufficient to
|
|
|
|
-- calculate the value, amounts are left unchanged.
|
2019-08-19 04:16:39 +03:00
|
|
|
postingValueAtDate :: PriceOracle -> M.Map CommoditySymbol AmountStyle -> Maybe CommoditySymbol -> Day -> Posting -> Posting
|
|
|
|
postingValueAtDate priceoracle styles mc d p = postingTransformAmount (mixedAmountValueAtDate priceoracle styles mc d) p
|
2019-05-23 22:16:20 +03:00
|
|
|
|
2019-05-28 21:42:32 +03:00
|
|
|
-- | Apply a transform function to this posting's amount.
|
|
|
|
postingTransformAmount :: (MixedAmount -> MixedAmount) -> Posting -> Posting
|
|
|
|
postingTransformAmount f p@Posting{pamount=a} = p{pamount=f a}
|
2019-05-23 22:16:20 +03:00
|
|
|
|
2019-07-17 20:38:14 +03:00
|
|
|
-- | Join two parts of a comment, eg a tag and another tag, or a tag
|
|
|
|
-- and a non-tag, on a single line. Interpolates a comma and space
|
|
|
|
-- unless one of the parts is empty.
|
|
|
|
commentJoin :: Text -> Text -> Text
|
|
|
|
commentJoin c1 c2
|
|
|
|
| T.null c1 = c2
|
|
|
|
| T.null c2 = c1
|
|
|
|
| otherwise = c1 <> ", " <> c2
|
|
|
|
|
|
|
|
-- | Add a tag to a comment, comma-separated from any prior content.
|
|
|
|
commentAddTag :: Text -> Tag -> Text
|
|
|
|
commentAddTag c (t,v)
|
|
|
|
| T.null c' = tag
|
|
|
|
| otherwise = c' `commentJoin` tag
|
|
|
|
where
|
|
|
|
c' = textchomp c
|
|
|
|
tag = t <> ":" <> v
|
|
|
|
|
|
|
|
-- | Add a tag on its own line to a comment, preserving any prior content.
|
|
|
|
commentAddTagNextLine :: Text -> Tag -> Text
|
|
|
|
commentAddTagNextLine cmt (t,v) =
|
|
|
|
cmt <> if "\n" `T.isSuffixOf` cmt then "" else "\n" <> t <> ":" <> v
|
|
|
|
|
2011-08-05 04:05:39 +04:00
|
|
|
|
2018-09-04 20:26:22 +03:00
|
|
|
-- tests
|
2011-08-03 03:29:13 +04:00
|
|
|
|
2018-09-06 23:08:26 +03:00
|
|
|
tests_Posting = tests "Posting" [
|
2018-09-04 20:26:22 +03:00
|
|
|
|
|
|
|
tests "accountNamePostingType" [
|
2011-08-03 03:29:13 +04:00
|
|
|
accountNamePostingType "a" `is` RegularPosting
|
2018-09-04 20:26:22 +03:00
|
|
|
,accountNamePostingType "(a)" `is` VirtualPosting
|
|
|
|
,accountNamePostingType "[a]" `is` BalancedVirtualPosting
|
|
|
|
]
|
2011-08-03 03:29:13 +04:00
|
|
|
|
2018-09-04 20:26:22 +03:00
|
|
|
,tests "accountNameWithoutPostingType" [
|
2011-08-03 03:29:13 +04:00
|
|
|
accountNameWithoutPostingType "(a)" `is` "a"
|
2018-09-04 20:26:22 +03:00
|
|
|
]
|
2011-08-03 03:29:13 +04:00
|
|
|
|
2018-09-04 20:26:22 +03:00
|
|
|
,tests "accountNameWithPostingType" [
|
2011-08-03 03:29:13 +04:00
|
|
|
accountNameWithPostingType VirtualPosting "[a]" `is` "(a)"
|
2018-09-04 20:26:22 +03:00
|
|
|
]
|
2011-08-03 03:29:13 +04:00
|
|
|
|
2018-09-04 20:26:22 +03:00
|
|
|
,tests "joinAccountNames" [
|
2011-08-03 03:29:13 +04:00
|
|
|
"a" `joinAccountNames` "b:c" `is` "a:b:c"
|
2018-09-04 20:26:22 +03:00
|
|
|
,"a" `joinAccountNames` "(b:c)" `is` "(a:b:c)"
|
|
|
|
,"[a]" `joinAccountNames` "(b:c)" `is` "[a:b:c]"
|
|
|
|
,"" `joinAccountNames` "a" `is` "a"
|
|
|
|
]
|
2011-08-03 03:29:13 +04:00
|
|
|
|
2018-09-04 20:26:22 +03:00
|
|
|
,tests "concatAccountNames" [
|
2011-08-03 03:29:13 +04:00
|
|
|
concatAccountNames [] `is` ""
|
2018-09-04 20:26:22 +03:00
|
|
|
,concatAccountNames ["a","(b)","[c:d]"] `is` "(a:b:c:d)"
|
|
|
|
]
|
2011-08-03 03:29:13 +04:00
|
|
|
|
2019-07-17 20:38:14 +03:00
|
|
|
,tests "commentAddTag" [
|
|
|
|
commentAddTag "" ("a","") `is` "a:"
|
|
|
|
,commentAddTag "[1/2]" ("a","") `is` "[1/2], a:"
|
|
|
|
]
|
|
|
|
|
|
|
|
,tests "commentAddTagNextLine" [
|
|
|
|
commentAddTagNextLine "" ("a","") `is` "\na:"
|
|
|
|
,commentAddTagNextLine "[1/2]" ("a","") `is` "[1/2]\na:"
|
|
|
|
]
|
2010-12-27 23:26:22 +03:00
|
|
|
]
|
2014-09-11 00:07:53 +04:00
|
|
|
|