imp run: More successful cache hits with forecast

If forecast start and end dates are explicitly requested with
--forecast=, those parts of the reportspan_ can be replaced with the
forecast dates, meaning more successful cache hits.
This commit is contained in:
Stephen Morgan 2025-03-09 18:58:51 +11:00 committed by Simon Michael
parent 133560aa93
commit 8474c811c5

View File

@ -43,7 +43,6 @@ import Hledger.Cli.DocFiles (runTldrForPage, runInfoForTopic, runManForTopic)
import Hledger.Cli.Utils (journalTransform)
import Text.Printf (printf)
import System.Process (system)
import Data.Maybe (isJust)
-- | Command line options for this command.
runmode = hledgerCommandMode
@ -245,10 +244,19 @@ withJournalCached defaultJournalOverride cliopts cmd = do
journal <- runExceptT $ if snd (splitReaderPrefix fp) == "-" then readStdin else readJournalFile iopts fp
either error' (\j -> return (Map.insert (ioptsWithoutReportSpan,fp) j cache, j)) journal
where
-- InputOptions contain reportspan_ that is used to calculare forecast period,
-- InputOptions contain reportspan_ that is used to calculate forecast period,
-- that is used by journalFinalise to insert forecast transactions.
-- For the purposes of caching, we want to ignore this when --forecast is not given.
ioptsWithoutReportSpan = if isJust (forecast_ iopts) then iopts else iopts { reportspan_ = emptydatespan }
-- For the purposes of caching, we want to ignore it whenever
-- --forecast is not used, or when explicit dates are requested.
ioptsWithoutReportSpan = iopts{ reportspan_ = forecastreportspan }
where
forecastreportspan = case forecast_ iopts of
Nothing -> emptydatespan
-- This could be better if we had access to the journal (as we
-- could use 'forecastPeriod') or to the journal end date (as
-- forecast transactions are never generated before journal end
-- unless specifically requested).
Just forecastspan -> forecastspan `spanDefaultsFrom` reportspan_ iopts
-- Read stdin, or if we read it alread, use a cache
-- readStdin :: InputOpts -> ExceptT String IO Journal
readStdin = do