Skip to content

Commit

Permalink
log sql with the loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
silky committed Jun 8, 2021
1 parent 37009aa commit ccaecaa
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
10 changes: 5 additions & 5 deletions plutus-pab/app/Cli.hs
Expand Up @@ -192,14 +192,14 @@ runConfigCommand ConfigCommandArgs{ccaTrace, ccaPABConfig=Config{dbConfig}} (Ins
do
connection <- App.dbConnect (LM.convertLog LM.PABMsg ccaTrace) dbConfig
fmap (either (error . show) id)
$ Beam.runBeamStoreAction connection
$ Beam.runBeamStoreAction connection (LM.convertLog (LM.PABMsg . LM.SLoggerBridge) ccaTrace)
$ Contract.addDefinition @ContractExe contractExe

-- Get the state of a contract
runConfigCommand ConfigCommandArgs{ccaTrace, ccaPABConfig=Config{dbConfig}} (ContractState contractInstanceId) = do
connection <- App.dbConnect (LM.convertLog LM.PABMsg ccaTrace) dbConfig
fmap (either (error . show) id)
$ Beam.runBeamStoreAction connection
$ Beam.runBeamStoreAction connection (LM.convertLog (LM.PABMsg . LM.SLoggerBridge) ccaTrace)
$ interpret (LM.handleLogMsgTrace ccaTrace)
$ do
s <- Contract.getState @ContractExe contractInstanceId
Expand All @@ -211,7 +211,7 @@ runConfigCommand ConfigCommandArgs{ccaTrace, ccaPABConfig=Config{dbConfig}} (Con
runConfigCommand ConfigCommandArgs{ccaTrace, ccaPABConfig=Config{dbConfig}} ReportInstalledContracts = do
connection <- App.dbConnect (LM.convertLog LM.PABMsg ccaTrace) dbConfig
fmap (either (error . show) id)
$ Beam.runBeamStoreAction connection
$ Beam.runBeamStoreAction connection (LM.convertLog (LM.PABMsg . LM.SLoggerBridge) ccaTrace)
$ interpret (LM.handleLogMsgTrace ccaTrace)
$ do
installedContracts <- Contract.getDefinitions @ContractExe
Expand All @@ -224,7 +224,7 @@ runConfigCommand ConfigCommandArgs{ccaTrace, ccaPABConfig=Config{dbConfig}} Repo
runConfigCommand ConfigCommandArgs{ccaTrace, ccaPABConfig=Config{dbConfig}} ReportActiveContracts = do
connection <- App.dbConnect (LM.convertLog LM.PABMsg ccaTrace) dbConfig
fmap (either (error . show) id)
$ Beam.runBeamStoreAction connection
$ Beam.runBeamStoreAction connection (LM.convertLog (LM.PABMsg . LM.SLoggerBridge) ccaTrace)
$ interpret (LM.handleLogMsgTrace ccaTrace)
$ do
logInfo @(LM.AppMsg ContractExe) LM.ActiveContractsMsg
Expand All @@ -237,7 +237,7 @@ runConfigCommand ConfigCommandArgs{ccaTrace, ccaPABConfig=Config{dbConfig}} Repo
runConfigCommand ConfigCommandArgs{ccaTrace, ccaPABConfig=Config{dbConfig}} (ReportContractHistory contractInstanceId) = do
connection <- App.dbConnect (LM.convertLog LM.PABMsg ccaTrace) dbConfig
fmap (either (error . show) id)
$ Beam.runBeamStoreAction connection
$ Beam.runBeamStoreAction connection (LM.convertLog (LM.PABMsg . LM.SLoggerBridge) ccaTrace)
$ interpret (LM.handleLogMsgTrace ccaTrace)
$ do
logInfo @(LM.AppMsg ContractExe) LM.ContractHistoryMsg
Expand Down
3 changes: 3 additions & 0 deletions plutus-pab/put-data-in-pab.sh
Expand Up @@ -8,6 +8,9 @@ rm -rf pab-core.db*

cabal run exe:plutus-pab -- --config plutus-pab.yaml migrate pab-core.db

# Ensure marlowe apps are up-to-date
cd ../marlowe && cabal build && cd -

contracts=(marlowe-app marlowe-companion-app marlowe-follow-app)

for c in "${contracts[@]}"; do
Expand Down
4 changes: 2 additions & 2 deletions plutus-pab/src/Plutus/PAB/App.hs
Expand Up @@ -108,13 +108,13 @@ appEffectHandlers config trace =
, handleContractStoreEffect =
interpret (Core.handleUserEnvReader @ContractExe @AppEnv)
. interpret (Core.handleMappedReader @AppEnv dbConnection)
. interpret handleDbStore
. interpret (handleDbStore (convertLog SLoggerBridge trace))
. reinterpret3 BeamEff.handleContractStore

, handleContractDefinitionStoreEffect =
interpret (Core.handleUserEnvReader @ContractExe @AppEnv)
. interpret (Core.handleMappedReader @AppEnv dbConnection)
. interpret handleDbStore
. interpret (handleDbStore (convertLog SLoggerBridge trace))
. reinterpret3 BeamEff.handleContractDefinitionStore

, handleServicesEffects = \wallet ->
Expand Down
7 changes: 5 additions & 2 deletions plutus-pab/src/Plutus/PAB/Db/Beam.hs
Expand Up @@ -11,6 +11,7 @@ Interface to beam ecosystem used by the PAB to store contracts.
module Plutus.PAB.Db.Beam
where

import Cardano.BM.Trace (Trace)
import Control.Monad.Freer (Eff, interpret, runM, subsume)
import Control.Monad.Freer.Delay (DelayEffect, handleDelayEffect)
import Control.Monad.Freer.Error (runError)
Expand All @@ -22,20 +23,22 @@ import Plutus.PAB.Db.Beam.ContractStore (handleContractStore
import Plutus.PAB.Effects.Contract (ContractDefinitionStore, ContractStore)
import Plutus.PAB.Effects.Contract.ContractExe (ContractExe)
import Plutus.PAB.Effects.DbStore
import Plutus.PAB.Monitoring.MonadLoggerBridge (MonadLoggerMsg)
import Plutus.PAB.Types (PABError)

-- | Run the ContractStore and ContractDefinitionStore effects on the
-- SQLite database.
runBeamStoreAction ::
forall a.
Connection
-> Trace IO MonadLoggerMsg
-> Eff '[ContractDefinitionStore ContractExe, ContractStore ContractExe, DelayEffect, IO] a
-> IO (Either PABError a)
runBeamStoreAction connection =
runBeamStoreAction connection trace =
runM
. runError
. runReader connection
. interpret handleDbStore
. interpret (handleDbStore trace)
. subsume @IO
. handleDelayEffect
. interpret handleContractStore
Expand Down
29 changes: 18 additions & 11 deletions plutus-pab/src/Plutus/PAB/Effects/DbStore.hs
Expand Up @@ -17,16 +17,20 @@

module Plutus.PAB.Effects.DbStore where

import Control.Monad.Freer (Eff, LastMember, Member, type (~>))
import Control.Monad.Freer.Reader (Reader, ask)
import Control.Monad.Freer.TH (makeEffect)
import Data.Text (Text)
import Cardano.BM.Trace (Trace)
import Control.Monad.Freer (Eff, LastMember, Member, type (~>))
import Control.Monad.Freer.Reader (Reader, ask)
import Control.Monad.Freer.TH (makeEffect)
import qualified Control.Monad.Logger as MonadLogger
import Data.Text (Text)
import qualified Data.Text as Text
import Database.Beam
import Database.Beam.Backend.SQL
import Database.Beam.Migrate
import Database.Beam.Schema.Tables
import Database.Beam.Sqlite
import Database.SQLite.Simple (Connection)
import Database.SQLite.Simple (Connection)
import Plutus.PAB.Monitoring.MonadLoggerBridge (MonadLoggerMsg, TraceLoggerT (..))

data ContractT f
= Contract
Expand Down Expand Up @@ -140,30 +144,33 @@ handleDbStore ::
( Member (Reader Connection) effs
, LastMember IO effs
)
=> DbStoreEffect
=> Trace IO MonadLoggerMsg
-> DbStoreEffect
~> Eff effs
handleDbStore eff = do
handleDbStore trace eff = do
connection <- ask @Connection
let traceSql = flip runTraceLoggerT trace . MonadLogger.logDebugN . Text.pack

case eff of
AddRow table record ->
liftIO
$ runBeamSqliteDebug putStrLn connection
$ runBeamSqliteDebug traceSql connection
$ runInsert
$ insert table (insertValues [record])

SelectList q ->
liftIO
$ runBeamSqliteDebug putStrLn connection
$ runBeamSqliteDebug traceSql connection
$ runSelectReturningList q

SelectOne q ->
liftIO
$ runBeamSqliteDebug putStrLn connection
$ runBeamSqliteDebug traceSql connection
$ runSelectReturningOne q

UpdateRow q ->
liftIO
$ runBeamSqliteDebug putStrLn connection
$ runBeamSqliteDebug traceSql connection
$ runUpdate q

makeEffect ''DbStoreEffect

0 comments on commit ccaecaa

Please sign in to comment.