pwncash/lib/Internal/Statement.hs

225 lines
7.5 KiB
Haskell
Raw Normal View History

{-# LANGUAGE FlexibleContexts #-}
2022-12-11 17:51:11 -05:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
2023-01-05 22:23:22 -05:00
module Internal.Statement
( readImport
)
where
import Data.Csv
import Internal.Database.Model
import Internal.Types
import Internal.Utils
import RIO
import qualified RIO.ByteString.Lazy as BL
import RIO.FilePath
import qualified RIO.List as L
import qualified RIO.Map as M
import qualified RIO.Text as T
import RIO.Time
import qualified RIO.Vector as V
2022-12-11 18:34:05 -05:00
2022-12-11 17:51:11 -05:00
-- TODO this probably won't scale well (pipes?)
2023-01-07 23:42:04 -05:00
readImport :: MonadUnliftIO m => Import -> MappingT m StatementRes
readImport Import {..} = do
rs <- L.sort . concat <$> mapM (readImport_ impSkipLines impDelim impTxOpts) impPaths
2023-01-07 23:42:04 -05:00
-- TODO show more useful information here (eg which file emitted this
-- error and possibly where)
return $ matchRecords impMatches rs
2023-01-05 22:23:22 -05:00
readImport_
:: MonadUnliftIO m
=> Natural
-> Word
-> TxOpts
-> FilePath
-> MappingT m [TxRecord]
2022-12-11 17:51:11 -05:00
readImport_ n delim tns p = do
2023-01-05 22:23:22 -05:00
dir <- asks kmConfigDir
bs <- liftIO $ BL.readFile $ dir </> p
case decodeByNameWithP (parseTxRecord tns) opts $ skip bs of
Left m -> liftIO $ putStrLn m >> return []
Right (_, v) -> return $ catMaybes $ V.toList v
2022-12-11 17:51:11 -05:00
where
2023-01-05 22:23:22 -05:00
opts = defaultDecodeOptions {decDelimiter = fromIntegral delim}
2022-12-11 17:51:11 -05:00
skip = BL.intercalate "\n" . L.drop (fromIntegral n) . BL.split 10
-- TODO handle this better, this maybe thing is a hack to skip lines with
-- blank dates but will likely want to make this more flexible
parseTxRecord :: TxOpts -> NamedRecord -> Parser (Maybe TxRecord)
2023-01-05 22:23:22 -05:00
parseTxRecord TxOpts {..} r = do
d <- r .: T.encodeUtf8 toDate
if d == ""
then return Nothing
else do
a <- parseRational toAmountFmt =<< r .: T.encodeUtf8 toAmount
e <- r .: T.encodeUtf8 toDesc
os <- M.fromList <$> mapM (\n -> (n,) <$> r .: T.encodeUtf8 n) toOther
d' <- parseTimeM True defaultTimeLocale (T.unpack toDateFmt) d
return $ Just $ TxRecord d' a e os
2022-12-11 17:51:11 -05:00
2023-01-07 23:42:04 -05:00
matchRecords :: [Match] -> [TxRecord] -> StatementRes
matchRecords ms rs = case matchAll (matchPriorities ms) rs of
Left e -> StatementFail $ StatementErrors [] [] [e]
Right (matched, unmatched, notfound) ->
-- TODO record number of times each match hits for debugging
let (errors, matched_) = partitionEithers $ balanceTx <$> matched
in case (matched_, unmatched, notfound, errors) of
(xs, [], [], []) -> StatementPass xs
(_, us, ns, es) -> StatementFail $ StatementErrors us ns es
2022-12-11 17:51:11 -05:00
matchPriorities :: [Match] -> [MatchGroup]
matchPriorities =
2023-01-05 22:23:22 -05:00
fmap matchToGroup
. L.groupBy (\a b -> mPriority a == mPriority b)
. L.sortOn (Down . mPriority)
2022-12-11 17:51:11 -05:00
matchToGroup :: [Match] -> MatchGroup
matchToGroup ms =
2023-01-05 22:23:22 -05:00
uncurry MatchGroup $
first (L.sortOn mDate) $
L.partition (isJust . mDate) ms
2022-12-11 17:51:11 -05:00
-- TDOO could use a better struct to flatten the maybe date subtype
data MatchGroup = MatchGroup
2023-01-05 22:23:22 -05:00
{ mgDate :: [Match]
, mgNoDate :: [Match]
}
deriving (Show)
2022-12-11 17:51:11 -05:00
data Zipped a = Zipped ![a] ![a]
data Unzipped a = Unzipped ![a] ![a] ![a]
initZipper :: [a] -> Zipped a
initZipper = Zipped []
resetZipper :: Zipped a -> Zipped a
resetZipper = initZipper . recoverZipper
recoverZipper :: Zipped a -> [a]
recoverZipper (Zipped as bs) = reverse as ++ bs
zipperSlice :: (a -> b -> Ordering) -> b -> Zipped a -> Either (Zipped a) (Unzipped a)
zipperSlice f x = go
where
go z@(Zipped _ []) = Left z
go z@(Zipped bs (a : as)) = case f a x of
2023-01-05 22:23:22 -05:00
GT -> go $ Zipped (a : bs) as
EQ -> Right $ goEq (Unzipped bs [a] as)
LT -> Left z
goEq z@(Unzipped _ _ []) = z
goEq z@(Unzipped bs cs (a : as)) = case f a x of
2023-01-05 22:23:22 -05:00
GT -> goEq $ Unzipped (a : bs) cs as
EQ -> goEq $ Unzipped bs (a : cs) as
LT -> z
2022-12-11 17:51:11 -05:00
zipperMatch :: Unzipped Match -> TxRecord -> PureErr (Zipped Match, MatchRes RawTx)
2022-12-11 17:51:11 -05:00
zipperMatch (Unzipped bs cs as) x = go [] cs
where
go _ [] = Right (Zipped bs $ cs ++ as, MatchFail)
go prev (m : ms) = do
res <- matches m x
case res of
MatchFail -> go (m : prev) ms
skipOrPass ->
let ps = reverse prev
ms' = maybe ms (: ms) (matchDec m)
in Right (Zipped bs $ ps ++ ms' ++ as, skipOrPass)
zipperMatch' :: Zipped Match -> TxRecord -> PureErr (Zipped Match, MatchRes RawTx)
2022-12-11 17:51:11 -05:00
zipperMatch' z x = go z
where
go (Zipped bs (a : as)) = do
res <- matches a x
case res of
MatchFail -> go (Zipped (a : bs) as)
skipOrPass -> Right (Zipped (maybe bs (: bs) $ matchDec a) as, skipOrPass)
go z' = Right (z', MatchFail)
2022-12-11 17:51:11 -05:00
matchDec :: Match -> Maybe Match
2023-01-05 22:23:22 -05:00
matchDec m@Match {mTimes = t} =
if t' == Just 0 then Nothing else Just $ m {mTimes = t'}
2022-12-11 17:51:11 -05:00
where
t' = fmap pred t
2023-01-07 23:42:04 -05:00
matchAll :: [MatchGroup] -> [TxRecord] -> PureErr ([RawTx], [TxRecord], [Match])
2022-12-11 17:51:11 -05:00
matchAll = go ([], [])
where
go (matched, unused) gs rs = case (gs, rs) of
(_, []) -> return (matched, [], unused)
([], _) -> return (matched, rs, unused)
(g : gs', _) -> do
(ts, unmatched, us) <- matchGroup g rs
go (ts ++ matched, us ++ unused) gs' unmatched
2023-01-07 23:42:04 -05:00
matchGroup :: MatchGroup -> [TxRecord] -> PureErr ([RawTx], [TxRecord], [Match])
matchGroup MatchGroup {mgDate = ds, mgNoDate = ns} rs = do
(md, rest, ud) <- matchDates ds rs
(mn, unmatched, un) <- matchNonDates ns rest
return (md ++ mn, unmatched, filter ((/= Nothing) . mTimes) $ ud ++ un)
2023-01-07 23:42:04 -05:00
matchDates :: [Match] -> [TxRecord] -> PureErr ([RawTx], [TxRecord], [Match])
2022-12-11 17:51:11 -05:00
matchDates ms = go ([], [], initZipper ms)
where
2023-01-07 23:42:04 -05:00
go (matched, unmatched, z) [] =
Right
( catMaybes matched
, reverse unmatched
, recoverZipper z
)
go (matched, unmatched, z) (r : rs) = case zipperSlice findDate r z of
Left zipped -> go (matched, r : unmatched, zipped) rs
Right unzipped -> do
(z', res) <- zipperMatch unzipped r
let (m, u) = case res of
MatchPass p -> (Just p : matched, unmatched)
MatchSkip -> (Nothing : matched, unmatched)
MatchFail -> (matched, r : unmatched)
go (m, u, z') rs
2022-12-11 17:51:11 -05:00
findDate m r = maybe EQ (`compareDate` trDate r) $ mDate m
2023-01-07 23:42:04 -05:00
matchNonDates :: [Match] -> [TxRecord] -> PureErr ([RawTx], [TxRecord], [Match])
2022-12-11 17:51:11 -05:00
matchNonDates ms = go ([], [], initZipper ms)
where
2023-01-07 23:42:04 -05:00
go (matched, unmatched, z) [] =
Right
( catMaybes matched
, reverse unmatched
, recoverZipper z
)
go (matched, unmatched, z) (r : rs) = do
(z', res) <- zipperMatch' z r
let (m, u) = case res of
MatchPass p -> (Just p : matched, unmatched)
MatchSkip -> (Nothing : matched, unmatched)
MatchFail -> (matched, r : unmatched)
2023-01-05 22:23:22 -05:00
in go (m, u, resetZipper z') rs
2022-12-11 17:51:11 -05:00
2023-01-07 23:42:04 -05:00
balanceTx :: RawTx -> PureErr BalTx
2023-01-05 22:23:22 -05:00
balanceTx t@Tx {txSplits = ss} = do
bs <- balanceSplits ss
return $ t {txSplits = bs}
2022-12-11 17:51:11 -05:00
2023-01-07 23:42:04 -05:00
balanceSplits :: [RawSplit] -> PureErr [BalSplit]
balanceSplits ss =
2023-01-05 22:23:22 -05:00
fmap concat
<$> mapM (uncurry bal)
$ groupByKey
$ fmap (\s -> (sCurrency s, s)) ss
2022-12-11 17:51:11 -05:00
where
2023-01-05 22:23:22 -05:00
hasValue s@(Split {sValue = Just v}) = Right s {sValue = v}
hasValue s = Left s
2022-12-11 17:51:11 -05:00
bal cur rss
2023-01-05 22:23:22 -05:00
| length rss < 2 = Left $ T.append "Need at least two splits to balance: " cur
| otherwise = case partitionEithers $ fmap hasValue rss of
([noVal], val) -> Right $ noVal {sValue = foldr (\s x -> x - sValue s) 0 val} : val
([], val) -> Right val
_ -> Left $ T.append "Exactly one split must be blank: " cur
2022-12-11 17:51:11 -05:00
groupByKey :: Ord k => [(k, v)] -> [(k, [v])]
groupByKey = M.toList . M.fromListWith (++) . fmap (second (: []))