Skip to content

Commit e6a0ac3

Browse files
committed
Use conduit
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent 608275d commit e6a0ac3

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

hydra-node/exe/visualize-logs/Main.hs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,47 @@
11
{-# LANGUAGE OverloadedRecordDot #-}
2+
{-# OPTIONS_GHC -Wno-deprecations #-}
23

34
module Main where
45

56
import Hydra.Cardano.Api
67
import Hydra.Prelude hiding (encodeUtf8)
78

9+
import Conduit
810
import Control.Lens ((^?))
911
import Data.Aeson (eitherDecode')
1012
import Data.Aeson.Lens (key, _String)
1113
import Data.ByteString.Lazy.Char8 qualified as C8
1214
import Data.Text.Encoding (encodeUtf8)
1315
import Hydra.Logging (Envelope (..))
14-
import Hydra.Logging.Messages (HydraLog)
15-
import Hydra.Node (HydraNodeLog)
16+
import Hydra.Logging.Messages (HydraLog (..))
1617

17-
data Decoded tx
18-
= DecodedHydraLog (HydraLog tx)
19-
| DecodedHydraNodeLog (HydraNodeLog tx)
20-
deriving (Show)
18+
newtype Decoded tx
19+
= DecodedHydraLog Text
20+
deriving newtype (Show)
2121

2222
main :: IO ()
2323
main = do
24-
logLines <- readFileLBS "../devnet/alice-logs.txt"
25-
let allLogLines = zip [1 :: Int ..] (C8.lines logLines)
26-
decodedLines <- forM allLogLines $ \(ix, l) ->
27-
case l ^? key "message" . _String of
28-
Nothing -> error "Failed to find key 'message' which was expected"
29-
Just line ->
30-
let envelope = fromStrict $ encodeUtf8 line
31-
in case decodeAs envelope (undefined :: Envelope (HydraLog Tx)) of
32-
Left e -> error $ "Line " <> show ix <> ": error - " <> show e <> " line: " <> show line
33-
Right decoded -> pure $ DecodedHydraLog $ decoded.message
34-
print decodedLines
35-
pure ()
24+
decodedLines <-
25+
runConduitRes $
26+
sourceFileBS "../devnet/alice-logs.txt"
27+
.| linesUnboundedAsciiC
28+
.| mapMC
29+
( \l ->
30+
case l ^? key "message" . _String of
31+
Nothing -> error "Failed to find key 'message' which was expected"
32+
Just line ->
33+
let envelope = fromStrict $ encodeUtf8 line
34+
in case decodeAs envelope (undefined :: Envelope (HydraLog Tx)) of
35+
Left e -> error $ show e <> line
36+
Right decoded ->
37+
case decoded.message of
38+
NodeOptions opt -> pure $ DecodedHydraLog $ "NODE STARTING: " <> show opt
39+
Node msg -> pure $ DecodedHydraLog $ "NODE LOG: " <> show msg
40+
_ -> pure $ DecodedHydraLog "_____"
41+
)
42+
.| sinkList
43+
forM_ decodedLines $ \l ->
44+
putTextLn (show l)
3645

3746
decodeAs :: forall a. FromJSON a => C8.ByteString -> a -> Either String a
3847
decodeAs l _ =

hydra-node/hydra-node.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ executable visualize-logs
254254
build-depends:
255255
, aeson
256256
, base
257+
, conduit
257258
, bytestring
258259
, containers
259260
, hydra-cardano-api

0 commit comments

Comments
 (0)