Better logs

This commit is contained in:
Akshay Mankar 2024-04-22 23:21:44 +02:00
parent f76db8b36a
commit db38a5ffd5
Signed by: axeman
GPG key ID: CA08F3AB62369B89
3 changed files with 40 additions and 27 deletions

View file

@ -11,6 +11,7 @@ import Control.Exception
import Control.Lens (view, (&), (.~)) import Control.Lens (view, (&), (.~))
import Control.Monad (when) import Control.Monad (when)
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Control.Monad.Trans.Class (lift)
import Data.Aeson hiding (Options) import Data.Aeson hiding (Options)
import Data.Aeson qualified as Aeson import Data.Aeson qualified as Aeson
import Data.ByteString.Lazy qualified as LBS import Data.ByteString.Lazy qualified as LBS
@ -50,7 +51,7 @@ main = withOpenSSL $ do
liftIO $ liftIO $
startBot (traceBotDefault $ botApp env) clientEnv >>= \case startBot (traceBotDefault $ botApp env) clientEnv >>= \case
Left err -> do Left err -> do
info env "main" $ "Bot failed with: " <> fromString (show err) runKatipContextT env.logEnv () "main" $ info $ "Bot failed with: " <> fromString (show err)
exitFailure exitFailure
_ -> pure () _ -> pure ()
@ -221,7 +222,7 @@ action update _ = do
handler :: Env -> Action -> Model -> Eff Action Model handler :: Env -> Action -> Model -> Eff Action Model
handler env (StartChat chat) model = handler env (StartChat chat) model =
model <# do model <# do
info env "telegram.handler" $ "Chat started! " <> fromString (ppAsJSON chat) runKatipContextT env.logEnv () "telegram.handler" $ info $ "Chat started! " <> fromString (ppAsJSON chat)
scrapeJob :: Env -> BotJob Model Action scrapeJob :: Env -> BotJob Model Action
scrapeJob env = scrapeJob env =
@ -232,27 +233,37 @@ scrapeJob env =
scrapeJobTask env m NoWBS scrapeJobTask env m NoWBS
} }
wbsText :: WBS -> Text
wbsText WBS = "WBS"
wbsText NoWBS = "NoWBS"
scrapeJobTask :: Env -> Model -> WBS -> Eff Action Model scrapeJobTask :: Env -> Model -> WBS -> Eff Action Model
scrapeJobTask env m wbs = scrapeJobTask env m wbs =
m <# do m
info env "scrapeJobTask" "Starting scrape job" <# runKatipContextT
env.logEnv
(sl "wbs" (wbsText wbs))
"scrapeJobTask"
( do
info "Starting scrape job"
res <- liftIO $ queryIBW env.wreqOpts wbs res <- liftIO $ queryIBW env.wreqOpts wbs
let offers = fromJust $ scrapeStringLike res.searchresults scrapeOffers let offers = fromJust $ scrapeStringLike res.searchresults scrapeOffers
info env "scrapeJobTask" "Fetched offers" info "Fetched offers"
mapM_ mapM_
( \offer -> do ( \offer -> do
isNewOffer <- liftIO $ saveOffer env.dbConn wbs offer isNewOffer <- liftIO $ saveOffer env.dbConn wbs offer
when isNewOffer $ do when isNewOffer $ do
info env "scrapeJobTask" "Found a new offer" info "Found a new offer"
notify env wbs offer katipAddNamespace "notify" $ notify wbs offer
) )
offers offers
)
info :: (MonadIO m) => Env -> Namespace -> LogStr -> m () info :: (Katip m) => LogStr -> m ()
info env ns l = liftIO $ runKatipT env.logEnv $ logMsg ns InfoS l info = logMsg mempty InfoS
notify :: Env -> WBS -> Offer -> BotM () notify :: WBS -> Offer -> KatipContextT BotM ()
notify env wbs offer = do notify wbs offer = do
let offerTitle = "<b><u>" <> offer.title <> "</u></b>" let offerTitle = "<b><u>" <> offer.title <> "</u></b>"
offerAddress = "<b>Address:</b> " <> fromMaybe "N/A" offer.address offerAddress = "<b>Address:</b> " <> fromMaybe "N/A" offer.address
offerRooms = "<b>Rooms:</b> " <> maybe "N/A" (Text.pack . show) offer.rooms offerRooms = "<b>Rooms:</b> " <> maybe "N/A" (Text.pack . show) offer.rooms
@ -267,13 +278,13 @@ notify env wbs offer = do
msgReq = case wbs of msgReq = case wbs of
WBS -> sendMsgReq3 WBS -> sendMsgReq3
NoWBS -> sendMsgReq4 NoWBS -> sendMsgReq4
sendMessageWithLogs env offer msgReq sendMessageWithLogs offer msgReq
sendMessageWithLogs :: Env -> Offer -> SendMessageRequest -> BotM () sendMessageWithLogs :: Offer -> SendMessageRequest -> KatipContextT BotM ()
sendMessageWithLogs env offer sendMsgReq = do sendMessageWithLogs offer sendMsgReq = do
res <- runTG sendMsgReq res <- lift $ runTG sendMsgReq
if res.responseOk if res.responseOk
then info env "notify" "Notified successfully" then info "Notified successfully"
else do else do
info env "notify" $ "Failed to notify the offer: " <> fromString (show offer) info $ "Failed to notify the offer: " <> fromString (show offer)
info env "notify" $ "Response: " <> fromString (Text.unpack $ Text.decodeUtf8 (LBS.toStrict $ Aeson.encode res)) info $ "Response: " <> fromString (Text.unpack $ Text.decodeUtf8 (LBS.toStrict $ Aeson.encode res))

View file

@ -29,6 +29,7 @@ executable berlin-scraper
, http-client-openssl , http-client-openssl
, bytestring , bytestring
, katip , katip
, transformers
hs-source-dirs: app hs-source-dirs: app
default-language: GHC2021 default-language: GHC2021
ghc-options: -threaded -with-rtsopts=-N ghc-options: -threaded -with-rtsopts=-N

View file

@ -1,6 +1,7 @@
{ mkDerivation, aeson, base, bytestring, http-client-openssl, katip { mkDerivation, aeson, base, bytestring, http-client-openssl, katip
, lens, lib, optparse-generic, scalpel, servant-client , lens, lib, optparse-generic, scalpel, servant-client
, sqlite-simple, telegram-bot-api, telegram-bot-simple, text, wreq , sqlite-simple, telegram-bot-api, telegram-bot-simple, text
, transformers, wreq
}: }:
mkDerivation { mkDerivation {
pname = "berlin-scraper"; pname = "berlin-scraper";
@ -11,7 +12,7 @@ mkDerivation {
executableHaskellDepends = [ executableHaskellDepends = [
aeson base bytestring http-client-openssl katip lens aeson base bytestring http-client-openssl katip lens
optparse-generic scalpel servant-client sqlite-simple optparse-generic scalpel servant-client sqlite-simple
telegram-bot-api telegram-bot-simple text wreq telegram-bot-api telegram-bot-simple text transformers wreq
]; ];
license = lib.licenses.agpl3Plus; license = lib.licenses.agpl3Plus;
mainProgram = "berlin-scraper"; mainProgram = "berlin-scraper";