dev:TimeclockReader, Timeclock: refactor/reindent [#2417]

This commit is contained in:
Simon Michael 2025-09-01 06:22:46 +01:00
parent a2710a5c2b
commit 7ac0fa1aaa
2 changed files with 163 additions and 158 deletions

View File

@ -10,8 +10,8 @@ converted to 'Transactions' and queried like a ledger.
{-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE StandaloneDeriving #-}
module Hledger.Data.Timeclock ( module Hledger.Data.Timeclock (
timeclockEntriesToTransactions timeclockToTransactions
,timeclockEntriesToTransactionsSingle ,timeclockToTransactionsOld
,tests_Timeclock ,tests_Timeclock
) )
where where
@ -37,110 +37,81 @@ import Hledger.Data.Posting
-- compact output -- compact output
instance Show TimeclockEntry where instance Show TimeclockEntry where
show t = printf "%s %s %s %s" (show $ tlcode t) (show $ tldatetime t) (tlaccount t) (tldescription t) show t = printf "%s %s %s %s" (show $ tlcode t) (show $ tldatetime t) (tlaccount t) (tldescription t)
instance Show TimeclockCode where instance Show TimeclockCode where
show SetBalance = "b" show SetBalance = "b"
show SetRequiredHours = "h" show SetRequiredHours = "h"
show In = "i" show In = "i"
show Out = "o" show Out = "o"
show FinalOut = "O" show FinalOut = "O"
instance Read TimeclockCode where instance Read TimeclockCode where
readsPrec _ ('b' : xs) = [(SetBalance, xs)] readsPrec _ ('b':xs) = [(SetBalance, xs)]
readsPrec _ ('h' : xs) = [(SetRequiredHours, xs)] readsPrec _ ('h':xs) = [(SetRequiredHours, xs)]
readsPrec _ ('i' : xs) = [(In, xs)] readsPrec _ ('i':xs) = [(In, xs)]
readsPrec _ ('o' : xs) = [(Out, xs)] readsPrec _ ('o':xs) = [(Out, xs)]
readsPrec _ ('O' : xs) = [(FinalOut, xs)] readsPrec _ ('O':xs) = [(FinalOut, xs)]
readsPrec _ _ = [] readsPrec _ _ = []
data Session = Session data Session = Session {
{ in' :: TimeclockEntry, in' :: TimeclockEntry,
out :: TimeclockEntry out :: TimeclockEntry
} deriving Show } deriving Show
data Sessions = Sessions data Sessions = Sessions {
{ completed :: [Session], completed :: [Session],
active :: [TimeclockEntry] active :: [TimeclockEntry]
} deriving Show } deriving Show
-- | Find the relevant clockin in the actives list that should be paired with this clockout. -- | Convert timeclock entries to journal transactions.
-- If there is a session that has the same account name, then use that. -- This is the old version from hledger <1.43, now enabled by --old-timeclock.
-- Otherwise, if there is an active anonymous session, use that. -- It requires strictly alternating clock-in and clock-entries.
-- Otherwise, raise an error. -- It was documented as allowing only one clocked-in session at a time,
findInForOut :: TimeclockEntry -> ([TimeclockEntry], [TimeclockEntry]) -> (TimeclockEntry, [TimeclockEntry]) -- but in fact it allows concurrent sessions, even with the same account name.
findInForOut _ (matchingout : othermatches, rest) = (matchingout, othermatches <> rest) --
findInForOut o ([], activeins) = -- Entries must be a strict alternation of in and out, beginning with in.
if emptyname then (first, rest) else error' errmsg -- When there is no clockout, one is added with the provided current time.
where -- Sessions crossing midnight are split into days to give accurate per-day totals.
l = show $ unPos $ sourceLine $ tlsourcepos o -- If entries are not in the expected in/out order, an error is raised.
c = unPos $ sourceColumn $ tlsourcepos o --
emptyname = tlaccount o == "" timeclockToTransactionsOld :: LocalTime -> [TimeclockEntry] -> [Transaction]
(first, rest) = case uncons activeins of timeclockToTransactionsOld _ [] = []
Just (hd, tl) -> (hd, tl) timeclockToTransactionsOld now [i]
Nothing -> error' errmsg | tlcode i /= In = errorExpectedCodeButGot In i
errmsg = | odate > idate = entryFromTimeclockInOut i o' : timeclockToTransactionsOld now [i',o]
printf | otherwise = [entryFromTimeclockInOut i o]
"%s:\n%s\n%s\n\nCould not find previous clockin to match this clockout."
(sourcePosPretty $ tlsourcepos o)
(l ++ " | " ++ show o)
(replicate (length l) ' ' ++ " |" ++ replicate c ' ' ++ "^")
-- | Assuming that entries have been sorted, we go through each time log entry.
-- We collect all of the "i" in the list "actives," and each time we encounter
-- an "o," we look for the corresponding "i" in actives.
-- If we cannot find it, then it is an error (since the list is sorted).
-- If the "o" is recorded on a different day than the "i" then we close the
-- active entry at the end of its day, replace it in the active list
-- with a start at midnight on the next day, and try again.
-- This raises an error if any outs cannot be paired with an in.
pairClockEntries :: [TimeclockEntry] -> [TimeclockEntry] -> [Session] -> Sessions
pairClockEntries [] actives sessions = Sessions {completed = sessions, active = actives}
pairClockEntries (entry : rest) actives sessions
| tlcode entry == In = pairClockEntries rest inentries sessions
| tlcode entry == Out = pairClockEntries rest' actives' sessions'
| otherwise = pairClockEntries rest actives sessions
where where
(inentry, newactive) = findInForOut entry (partition (\e -> tlaccount e == tlaccount entry) actives) o = TimeclockEntry (tlsourcepos i) Out end "" "" "" []
(itime, otime) = (tldatetime inentry, tldatetime entry) end = if itime > now then itime else now
(idate, odate) = (localDay itime, localDay otime) (itime,otime) = (tldatetime i,tldatetime o)
omidnight = entry {tldatetime = itime {localDay = idate, localTimeOfDay = TimeOfDay 23 59 59}} (idate,odate) = (localDay itime,localDay otime)
imidnight = inentry {tldatetime = itime {localDay = addDays 1 idate, localTimeOfDay = midnight}} o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}}
(sessions', actives', rest') i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
| odate > idate = (Session {in' = inentry, out = omidnight} : sessions, imidnight : newactive, entry : rest) timeclockToTransactionsOld now (i:o:rest)
| otherwise = (Session {in' = inentry, out = entry} : sessions, newactive, rest) | tlcode i /= In = errorExpectedCodeButGot In i
inentries = case filter ((== tlaccount entry) . tlaccount) actives of | tlcode o /= Out = errorExpectedCodeButGot Out o
[] -> entry : actives | odate > idate = entryFromTimeclockInOut i o' : timeclockToTransactionsOld now (i':o:rest)
activesinthisacct -> error' $ T.unpack $ makeTimeClockErrorExcerpt entry $ T.unlines $ [ | otherwise = entryFromTimeclockInOut i o : timeclockToTransactionsOld now rest
""
,"overlaps with session beginning at:"
,""
]
<> map (flip makeTimeClockErrorExcerpt "") activesinthisacct
<> [
"Overlapping sessions with the same account name are not supported."
]
-- XXX better to show full session(s)
-- <> map T.show (filter ((`elem` activesinthisacct).in') sessions)
makeTimeClockErrorExcerpt :: TimeclockEntry -> T.Text -> T.Text
makeTimeClockErrorExcerpt e@TimeclockEntry{tlsourcepos=pos} msg = T.unlines [
T.pack (sourcePosPretty pos) <> ":"
,l <> " | " <> T.show e
-- ,T.replicate (T.length l) " " <> " |" -- <> T.replicate c " " <> "^")
] <> msg
where where
l = T.show $ unPos $ sourceLine $ tlsourcepos e (itime,otime) = (tldatetime i,tldatetime o)
-- c = unPos $ sourceColumn $ tlsourcepos e (idate,odate) = (localDay itime,localDay otime)
o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}}
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}}
{- HLINT ignore timeclockToTransactionsOld -}
-- | Convert time log entries to journal transactions, allowing multiple clocked-in sessions at once. -- | Convert timeclock entries to journal transactions.
-- This is the new, default behaviour. -- This is the new, default version added in hledger 1.43 and improved in 1.50.
-- It allows concurrent clocked-in sessions (though not with the same account name),
-- and clock-in/clock-out entries in any order.
--
-- Entries are processed in time order, then (for entries with the same time) in parse order. -- Entries are processed in time order, then (for entries with the same time) in parse order.
-- When there is no clockout, one is added with the provided current time. -- When there is no clockout, one is added with the provided current time.
-- Sessions crossing midnight are split into days to give accurate per-day totals. -- Sessions crossing midnight are split into days to give accurate per-day totals.
-- If any entries cannot be paired as expected, an error is raised. -- If any entries cannot be paired as expected, an error is raised.
timeclockEntriesToTransactions :: LocalTime -> [TimeclockEntry] -> [Transaction] --
timeclockEntriesToTransactions now entries = transactions timeclockToTransactions :: LocalTime -> [TimeclockEntry] -> [Transaction]
timeclockToTransactions now entries = transactions
where where
sessions = dbg6 "sessions" $ pairClockEntries (sortTimeClockEntries entries) [] [] sessions = dbg6 "sessions" $ pairClockEntries (sortTimeClockEntries entries) [] []
transactionsFromSession s = entryFromTimeclockInOut (in' s) (out s) transactionsFromSession s = entryFromTimeclockInOut (in' s) (out s)
@ -151,46 +122,77 @@ timeclockEntriesToTransactions now entries = transactions
stillopen = dbg6 "stillopen" $ pairClockEntries ((active sessions) <> outs) [] [] stillopen = dbg6 "stillopen" $ pairClockEntries ((active sessions) <> outs) [] []
transactions = map transactionsFromSession $ sortBy (\s1 s2 -> compare (in' s1) (in' s2)) (completed sessions ++ completed stillopen) transactions = map transactionsFromSession $ sortBy (\s1 s2 -> compare (in' s1) (in' s2)) (completed sessions ++ completed stillopen)
-- | Sort timeclock entries first by date and time (with time zone ignored as usual), then by file position. -- | Sort timeclock entries first by date and time (with time zone ignored as usual), then by file position.
-- Ie, sort by time, but preserve the parse order of entries with the same time. -- Ie, sort by time, but preserve the parse order of entries with the same time.
sortTimeClockEntries :: [TimeclockEntry] -> [TimeclockEntry] sortTimeClockEntries :: [TimeclockEntry] -> [TimeclockEntry]
sortTimeClockEntries = sortBy (\e1 e2 -> compare (tldatetime e1, tlsourcepos e1) (tldatetime e2, tlsourcepos e2)) sortTimeClockEntries = sortBy (\e1 e2 -> compare (tldatetime e1, tlsourcepos e1) (tldatetime e2, tlsourcepos e2))
-- | Convert time log entries to journal transactions, allowing only one clocked-in session at a time. -- | Assuming that entries have been sorted, we go through each time log entry.
-- Entries must be a strict alternation of in and out, beginning with in. -- We collect all of the "i" in the list "actives," and each time we encounter
-- When there is no clockout, one is added with the provided current time. -- an "o," we look for the corresponding "i" in actives.
-- Sessions crossing midnight are split into days to give accurate per-day totals. -- If we cannot find it, then it is an error (since the list is sorted).
-- If entries are not in the expected in/out order, an error is raised. -- If the "o" is recorded on a different day than the "i" then we close the
-- This is the old, legacy behaviour, enabled by --old-timeclock. -- active entry at the end of its day, replace it in the active list
timeclockEntriesToTransactionsSingle :: LocalTime -> [TimeclockEntry] -> [Transaction] -- with a start at midnight on the next day, and try again.
-- This raises an error if any outs cannot be paired with an in.
timeclockEntriesToTransactionsSingle _ [] = [] pairClockEntries :: [TimeclockEntry] -> [TimeclockEntry] -> [Session] -> Sessions
pairClockEntries [] actives sessions1 = Sessions {completed = sessions1, active = actives}
timeclockEntriesToTransactionsSingle now [i] pairClockEntries (entry:es) actives sessions1
| tlcode i /= In = errorExpectedCodeButGot In i | tlcode entry == In = pairClockEntries es inentries sessions1
| odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactionsSingle now [i',o] | tlcode entry == Out = pairClockEntries es' actives' sessions2
| otherwise = [entryFromTimeclockInOut i o] | otherwise = pairClockEntries es actives sessions1
where where
o = TimeclockEntry (tlsourcepos i) Out end "" "" "" [] (inentry, newactive) = findInForOut entry (partition (\e -> tlaccount e == tlaccount entry) actives)
end = if itime > now then itime else now (itime, otime) = (tldatetime inentry, tldatetime entry)
(itime,otime) = (tldatetime i,tldatetime o) (idate, odate) = (localDay itime, localDay otime)
(idate,odate) = (localDay itime,localDay otime) omidnight = entry {tldatetime = itime {localDay = idate, localTimeOfDay = TimeOfDay 23 59 59}}
o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}} imidnight = inentry {tldatetime = itime {localDay = addDays 1 idate, localTimeOfDay = midnight}}
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}} (sessions2, actives', es')
| odate > idate = (Session {in' = inentry, out = omidnight} : sessions1, imidnight:newactive, entry:es)
timeclockEntriesToTransactionsSingle now (i:o:rest) | otherwise = (Session {in' = inentry, out = entry} : sessions1, newactive, es)
| tlcode i /= In = errorExpectedCodeButGot In i inentries = case filter ((== tlaccount entry) . tlaccount) actives of
| tlcode o /= Out = errorExpectedCodeButGot Out o [] -> entry:actives
| odate > idate = entryFromTimeclockInOut i o' : timeclockEntriesToTransactionsSingle now (i':o:rest) activesinthisacct -> error' $ T.unpack $ makeTimeClockErrorExcerpt entry $ T.unlines $ [
| otherwise = entryFromTimeclockInOut i o : timeclockEntriesToTransactionsSingle now rest ""
where ,"overlaps with session beginning at:"
(itime,otime) = (tldatetime i,tldatetime o) ,""
(idate,odate) = (localDay itime,localDay otime) ]
o' = o{tldatetime=itime{localDay=idate, localTimeOfDay=TimeOfDay 23 59 59}} <> map (flip makeTimeClockErrorExcerpt "") activesinthisacct
i' = i{tldatetime=itime{localDay=addDays 1 idate, localTimeOfDay=midnight}} <> [ "Overlapping sessions with the same account name are not supported." ]
-- XXX better to show full session(s)
{- HLINT ignore timeclockEntriesToTransactionsSingle -} -- <> map T.show (filter ((`elem` activesinthisacct).in') sessions)
where
makeTimeClockErrorExcerpt :: TimeclockEntry -> T.Text -> T.Text
makeTimeClockErrorExcerpt e@TimeclockEntry{tlsourcepos=pos} msg = T.unlines [
T.pack (sourcePosPretty pos) <> ":"
,l <> " | " <> T.show e
-- ,T.replicate (T.length l) " " <> " |" -- <> T.replicate c " " <> "^")
] <> msg
where
l = T.show $ unPos $ sourceLine $ tlsourcepos e
-- c = unPos $ sourceColumn $ tlsourcepos e
-- | Find the relevant clockin in the actives list that should be paired with this clockout.
-- If there is a session that has the same account name, then use that.
-- Otherwise, if there is an active anonymous session, use that.
-- Otherwise, raise an error.
findInForOut :: TimeclockEntry -> ([TimeclockEntry], [TimeclockEntry]) -> (TimeclockEntry, [TimeclockEntry])
findInForOut _ (matchingout:othermatches, rest) = (matchingout, othermatches <> rest)
findInForOut o ([], activeins) =
if emptyname then (first, rest) else error' errmsg
where
l = show $ unPos $ sourceLine $ tlsourcepos o
c = unPos $ sourceColumn $ tlsourcepos o
emptyname = tlaccount o == ""
(first, rest) = case uncons activeins of
Just (hd, tl) -> (hd, tl)
Nothing -> error' errmsg
errmsg =
printf
"%s:\n%s\n%s\n\nCould not find previous clockin to match this clockout."
(sourcePosPretty $ tlsourcepos o)
(l ++ " | " ++ show o)
(replicate (length l) ' ' ++ " |" ++ replicate c ' ' ++ "^")
errorExpectedCodeButGot :: TimeclockCode -> TimeclockEntry -> a errorExpectedCodeButGot :: TimeclockCode -> TimeclockEntry -> a
errorExpectedCodeButGot expected actual = error' $ printf errorExpectedCodeButGot expected actual = error' $ printf
@ -265,7 +267,7 @@ entryFromTimeclockInOut i o
-- tests -- tests
tests_Timeclock = testGroup "Timeclock" [ tests_Timeclock = testGroup "Timeclock" [
testCaseSteps "timeclockEntriesToTransactions tests" $ \step -> do testCaseSteps "timeclockToTransactions tests" $ \step -> do
step "gathering data" step "gathering data"
today <- getCurrentDay today <- getCurrentDay
now' <- getCurrentTime now' <- getCurrentTime
@ -278,7 +280,7 @@ tests_Timeclock = testGroup "Timeclock" [
mktime d = LocalTime d . fromMaybe midnight . mktime d = LocalTime d . fromMaybe midnight .
parseTimeM True defaultTimeLocale "%H:%M:%S" parseTimeM True defaultTimeLocale "%H:%M:%S"
showtime = formatTime defaultTimeLocale "%H:%M" showtime = formatTime defaultTimeLocale "%H:%M"
txndescs = map (T.unpack . tdescription) . timeclockEntriesToTransactions now txndescs = map (T.unpack . tdescription) . timeclockToTransactions now
future = utcToLocalTime tz $ addUTCTime 100 now' future = utcToLocalTime tz $ addUTCTime 100 now'
futurestr = showtime future futurestr = showtime future
step "started yesterday, split session at midnight" step "started yesterday, split session at midnight"

View File

@ -182,32 +182,35 @@ parse iopts fp t = initialiseAndParseJournal (timeclockfilep iopts) iopts fp t
-- timeclockfilep args -- timeclockfilep args
timeclockfilep :: MonadIO m => InputOpts -> JournalParser m ParsedJournal timeclockfilep :: MonadIO m => InputOpts -> JournalParser m ParsedJournal
timeclockfilep iopts = do many timeclockitemp timeclockfilep iopts = do
eof many timeclockitemp
j@Journal{jparsetimeclockentries=es} <- get eof
-- Convert timeclock entries in this journal to transactions, closing any unfinished sessions. j@Journal{jparsetimeclockentries=es} <- get
-- Doing this here rather than in journalFinalise means timeclock sessions can't span file boundaries, -- Convert timeclock entries in this journal to transactions, closing any unfinished sessions.
-- but it simplifies code above. -- Doing this here rather than in journalFinalise means timeclock sessions can't span file boundaries,
now <- liftIO getCurrentLocalTime -- but it simplifies code above.
-- journalFinalise expects the transactions in reverse order, so reverse the output in either case now <- liftIO getCurrentLocalTime
let j' = if _oldtimeclock iopts then -- journalFinalise expects the transactions in reverse order, so reverse the output in either case
-- timeclockEntriesToTransactionsSingle expects the entries to be in normal order, let
-- but they have been parsed in reverse order, so reverse them before calling j' = if _oldtimeclock iopts
j{jtxns = reverse $ timeclockEntriesToTransactionsSingle now $ reverse es, jparsetimeclockentries = []} then
else -- timeclockToTransactionsOld expects the entries to be in normal order,
-- We don't need to reverse these transactions -- but they have been parsed in reverse order, so reverse them before calling
-- since they are sorted inside of timeclockEntriesToTransactions j{jtxns = reverse $ timeclockToTransactionsOld now $ reverse es, jparsetimeclockentries = []}
j{jtxns = reverse $ timeclockEntriesToTransactions now es, jparsetimeclockentries = []} else
return j' -- We don't need to reverse these transactions
where -- since they are sorted inside of timeclockToTransactions
-- As all ledger line types can be distinguished by the first j{jtxns = reverse $ timeclockToTransactions now es, jparsetimeclockentries = []}
-- character, excepting transactions versus empty (blank or return j'
-- comment-only) lines, can use choice w/o try where
timeclockitemp = choice [ -- As all ledger line types can be distinguished by the first
void (lift emptyorcommentlinep) -- character, excepting transactions versus empty (blank or
, entryp >>= \e -> modify' (\j -> j{jparsetimeclockentries = e : jparsetimeclockentries j}) -- comment-only) lines, can use choice w/o try
] <?> "timeclock entry, comment line, or empty line" timeclockitemp = choice [
where entryp = if _oldtimeclock iopts then oldtimeclockentryp else timeclockentryp void (lift emptyorcommentlinep)
,entryp >>= \e -> modify' (\j -> j{jparsetimeclockentries = e : jparsetimeclockentries j})
] <?> "timeclock entry, comment line, or empty line"
where entryp = if _oldtimeclock iopts then oldtimeclockentryp else timeclockentryp
-- | Parse a timeclock entry (loose pre-1.50 format). -- | Parse a timeclock entry (loose pre-1.50 format).
oldtimeclockentryp :: JournalParser m TimeclockEntry oldtimeclockentryp :: JournalParser m TimeclockEntry