From 8474c811c5b4fecfd5022c85d7257f0fe60d08e0 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Sun, 9 Mar 2025 18:58:51 +1100 Subject: [PATCH] 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. --- hledger/Hledger/Cli/Commands/Run.hs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/hledger/Hledger/Cli/Commands/Run.hs b/hledger/Hledger/Cli/Commands/Run.hs index f36d342b7..4fe780a13 100644 --- a/hledger/Hledger/Cli/Commands/Run.hs +++ b/hledger/Hledger/Cli/Commands/Run.hs @@ -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