From 70c91a2a2595949330c02f2820fe77b9838a7a60 Mon Sep 17 00:00:00 2001 From: Dmitry Zuikov Date: Tue, 10 Sep 2024 05:32:34 +0300 Subject: [PATCH] wip --- fixme-new/lib/Fixme/Run.hs | 16 +++++++++++++ fixme-new/lib/Fixme/Run/Internal.hs | 8 +++++++ fixme-new/lib/Fixme/State.hs | 37 ++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/fixme-new/lib/Fixme/Run.hs b/fixme-new/lib/Fixme/Run.hs index 496924e5..df11dc28 100644 --- a/fixme-new/lib/Fixme/Run.hs +++ b/fixme-new/lib/Fixme/Run.hs @@ -327,6 +327,17 @@ runTop forms = do -- magic <- lift $ asks fixmeEnvScanMagic >>= readTVarIO -- liftIO $ print $ pretty magic + + entry $ bindMatch "report" $ nil_ $ lift . \case + ( SymbolVal "template" : StringLike t : p ) -> do + runReport (Just t) p + + ( SymbolVal "--template" : StringLike t : p ) -> do + runReport (Just t) p + + p -> do + runReport Nothing p + entry $ bindMatch "fixme:key:show" $ nil_ \case [ FixmeHashLike w ] -> lift $ void $ runMaybeT do key <- lift (selectFixmeKey w) >>= toMPlus @@ -571,6 +582,11 @@ runTop forms = do entry $ bindMatch "log:trace:off" $ nil_ $ const do lift $ setLoggingOff @TRACE + entry $ bindMatch "log:debug:on" $ nil_ $ const do + lift $ setLogging @DEBUG $ toStderr . logPrefix "" + + entry $ bindMatch "log:debug:off" $ nil_ $ const do + lift $ setLoggingOff @DEBUG entry $ bindMatch "debug:peer:check" $ nil_ $ const do peer <- lift $ getClientAPI @PeerAPI @UNIX diff --git a/fixme-new/lib/Fixme/Run/Internal.hs b/fixme-new/lib/Fixme/Run/Internal.hs index bf3aa9b7..f533a6dc 100644 --- a/fixme-new/lib/Fixme/Run/Internal.hs +++ b/fixme-new/lib/Fixme/Run/Internal.hs @@ -195,3 +195,11 @@ scanFiles = do pure True +runReport :: (FixmePerks m, HasPredicate q) => Maybe FilePath -> q -> FixmeM m () +runReport tpl q = do + debug $ "runReport" <+> pretty tpl + fxs <- listFixme q + for_ fxs $ \fme -> do + liftIO $ print $ pretty fme + + diff --git a/fixme-new/lib/Fixme/State.hs b/fixme-new/lib/Fixme/State.hs index 95b980ad..2c68e2d6 100644 --- a/fixme-new/lib/Fixme/State.hs +++ b/fixme-new/lib/Fixme/State.hs @@ -4,6 +4,7 @@ module Fixme.State ( evolve , withState , cleanupDatabase + , listFixme , insertFixme , modifyFixme , insertScanned @@ -196,16 +197,12 @@ genPredQ tbl what = go what All -> ("true", mempty) FixmeHashExactly x -> - ([qc|(s2.fixme = ?)|], [Bound x]) - - AttrLike "fixme-hash" val -> do - let binds = [Bound (val <> "%")] - ([qc|(s2.fixme like ?)|], binds) + ([qc|(o.o = ?)|], [Bound x]) AttrLike name val -> do let x = val <> "%" let binds = [Bound x] - ([qc|(json_extract({tbl}, '$."{name}"') like ?)|], binds) + ([qc|(json_extract({tbl}.blob, '$."{name}"') like ?)|], binds) Not a -> do let (sql, bound) = go a @@ -267,6 +264,34 @@ selectFixmeKey s = do <&> fmap fromOnly <&> headMay + +listFixme :: (FixmePerks m, MonadReader FixmeEnv m, HasPredicate q) + => q + -> m [Fixme] +listFixme expr = do + + let (w,bound) = genPredQ "s1" (predicate expr) + let end = case bound of + [] -> " or true" :: String + _ -> " or false" + + let sql = [qc| + with s1 as ( + select (cast (json_group_object(o.k, o.v) as blob)) as blob from object o + group by o.o + ) + select blob from s1 + where + {w} + {end} + |] + + debug $ pretty sql + + withState $ select @(Only LBS.ByteString) sql bound + <&> fmap (Aeson.decode @Fixme . fromOnly) + <&> catMaybes + getFixme :: (FixmePerks m, MonadReader FixmeEnv m) => FixmeKey -> m (Maybe Fixme) getFixme key = do