lib: Make queryDepth return Maybe Int.

This commit is contained in:
Stephen Morgan 2020-07-16 19:30:18 +10:00 committed by Simon Michael
parent dc076b0d5b
commit 51ea6d9f25
5 changed files with 21 additions and 23 deletions

View File

@ -64,7 +64,7 @@ import Data.Monoid ((<>))
#endif
import qualified Data.Text as T
import Data.Time.Calendar
import Safe (readDef, maximumByDef, maximumDef, minimumDef)
import Safe (readDef, maximumByMay, maximumMay, minimumMay)
import Text.Megaparsec
import Text.Megaparsec.Char
@ -488,33 +488,33 @@ queryDateSpan' _ = nulldatespan
-- | What is the earliest of these dates, where Nothing is earliest ?
earliestMaybeDate :: [Maybe Day] -> Maybe Day
earliestMaybeDate = minimumDef Nothing
earliestMaybeDate = fromMaybe Nothing . minimumMay
-- | What is the latest of these dates, where Nothing is earliest ?
latestMaybeDate :: [Maybe Day] -> Maybe Day
latestMaybeDate = maximumDef Nothing
latestMaybeDate = fromMaybe Nothing . maximumMay
-- | What is the earliest of these dates, where Nothing is the latest ?
earliestMaybeDate' :: [Maybe Day] -> Maybe Day
earliestMaybeDate' = minimumDef Nothing . filter isJust
earliestMaybeDate' = fromMaybe Nothing . minimumMay . filter isJust
-- | What is the latest of these dates, where Nothing is the latest ?
latestMaybeDate' :: [Maybe Day] -> Maybe Day
latestMaybeDate' = maximumByDef Nothing compareNothingMax
latestMaybeDate' = fromMaybe Nothing . maximumByMay compareNothingMax
where
compareNothingMax Nothing Nothing = EQ
compareNothingMax (Just _) Nothing = LT
compareNothingMax Nothing (Just _) = GT
compareNothingMax (Just a) (Just b) = compare a b
-- | The depth limit this query specifies, or a large number if none.
queryDepth :: Query -> Int
queryDepth = minimumDef maxBound . queryDepth'
-- | The depth limit this query specifies, if it has one
queryDepth :: Query -> Maybe Int
queryDepth = minimumMay . queryDepth'
where
queryDepth' (Depth d) = [d]
queryDepth' (Or qs) = concatMap queryDepth' qs
queryDepth' (And qs) = concatMap queryDepth' qs
queryDepth' _ = []
queryDepth' (Or qs) = concatMap queryDepth' qs
queryDepth' (And qs) = concatMap queryDepth' qs
queryDepth' _ = []
-- | The account we are currently focussed on, if any, and whether subaccounts are included.
-- Just looks at the first query option.

View File

@ -289,7 +289,7 @@ acctChangesFromPostings ropts q ps = HM.fromList [(aname a, a) | a <- as]
as = filterAccounts . drop 1 $ accountsFromPostings ps
filterAccounts = case accountlistmode_ ropts of
ALTree -> filter ((depthq `matchesAccount`) . aname) -- exclude deeper balances
ALFlat -> clipAccountsAndAggregate (queryDepth depthq) . -- aggregate deeper balances at the depth limit.
ALFlat -> maybe id clipAccountsAndAggregate (queryDepth depthq) . -- aggregate deeper balances at the depth limit.
filter ((0<) . anumpostings)
depthq = dbg "depthq" $ filterQuery queryIsDepth q
@ -299,8 +299,8 @@ calculateAccountChanges :: ReportOpts -> Query -> [DateSpan]
-> Map DateSpan [Posting]
-> HashMap ClippedAccountName (Map DateSpan Account)
calculateAccountChanges ropts q colspans colps
| queryDepth q == 0 = acctchanges <> elided
| otherwise = acctchanges
| queryDepth q == Just 0 = acctchanges <> elided
| otherwise = acctchanges
where
-- Transpose to get each account's balance changes across all columns.
acctchanges = transposeMap colacctchanges
@ -461,7 +461,7 @@ displayedAccounts ropts q valuedaccts
tallies = subaccountTallies . HM.keys $ HM.filterWithKey isInteresting valuedaccts
isZeroRow balance = all (mixedAmountLooksZero . balance)
depth = queryDepth q
depth = fromMaybe maxBound $ queryDepth q
-- | Sort the rows by amount or by account declaration order.
sortRows :: ReportOpts -> Journal -> [MultiBalanceReportRow] -> [MultiBalanceReportRow]

View File

@ -72,7 +72,7 @@ postingsReport ropts@ReportOpts{..} q j =
where
reportspan = adjustReportDates ropts q j
whichdate = whichDateFromOpts ropts
depth = queryDepth q
depth = fromMaybe maxBound $ queryDepth q
styles = journalCommodityStyles j
priceoracle = journalPriceOracle infer_value_ j
multiperiod = interval_ /= NoInterval

View File

@ -92,7 +92,7 @@ runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=_iopts,reportopts_=rop
reportopts_= ropts{
-- incorporate any depth: query args into depth_,
-- any date: query args into period_
depth_ =depthfromoptsandargs,
depth_ =queryDepth q,
period_=periodfromoptsandargs,
query_ =unwords -- as in ReportOptions, with same limitations
$ collectopts filteredQueryArg (rawopts_ copts),
@ -107,8 +107,6 @@ runBrickUi uopts@UIOpts{cliopts_=copts@CliOpts{inputopts_=_iopts,reportopts_=rop
}
where
q = queryFromOpts d ropts
depthfromoptsandargs = case queryDepth q of 99999 -> Nothing
d -> Just d
datespanfromargs = queryDateSpan (date2_ ropts) $ fst $ parseQuery d (T.pack $ query_ ropts)
periodfromoptsandargs =
dateSpanAsPeriod $ spansIntersect [periodAsDateSpan $ period_ ropts, datespanfromargs]

View File

@ -74,10 +74,10 @@ accounts CliOpts{rawopts_=rawopts, reportopts_=ropts} j = do
-- 3. if there's a depth limit, depth-clip and remove any no longer useful items
clippedaccts =
dbg1 "clippedaccts" $
filter (matchesAccount acctq) $ -- clipping can leave accounts that no longer match the query, remove such
nub $ -- clipping can leave duplicates (adjacent, hopefully)
filter (not . T.null) $ -- depth:0 can leave nulls
map (clipAccountName depth) $ -- clip at depth if specified
filter (matchesAccount acctq) $ -- clipping can leave accounts that no longer match the query, remove such
nub $ -- clipping can leave duplicates (adjacent, hopefully)
filter (not . T.null) $ -- depth:0 can leave nulls
maybe id (map . clipAccountName) depth $ -- clip at depth if specified
sortedaccts
-- 4. print what remains as a list or tree, maybe applying --drop in the former case