Skip to content

Commit 608275d

Browse files
committed
Parse lines to Envelope type
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent 5fb1076 commit 608275d

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
1+
{-# LANGUAGE OverloadedRecordDot #-}
2+
13
module Main where
24

35
import Hydra.Cardano.Api
4-
import Hydra.Prelude
6+
import Hydra.Prelude hiding (encodeUtf8)
57

8+
import Control.Lens ((^?))
9+
import Data.Aeson (eitherDecode')
10+
import Data.Aeson.Lens (key, _String)
11+
import Data.ByteString.Lazy.Char8 qualified as C8
12+
import Data.Text.Encoding (encodeUtf8)
13+
import Hydra.Logging (Envelope (..))
614
import Hydra.Logging.Messages (HydraLog)
7-
import Hydra.Utils (readJsonFileThrow)
15+
import Hydra.Node (HydraNodeLog)
16+
17+
data Decoded tx
18+
= DecodedHydraLog (HydraLog tx)
19+
| DecodedHydraNodeLog (HydraNodeLog tx)
20+
deriving (Show)
821

922
main :: IO ()
1023
main = do
11-
results <- readJsonFileThrow parseJSON "devnet/alice-logs.txt" :: IO (Either String (HydraLog Tx))
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
1235
pure ()
36+
37+
decodeAs :: forall a. FromJSON a => C8.ByteString -> a -> Either String a
38+
decodeAs l _ =
39+
case eitherDecode' l :: Either String a of
40+
Left e -> Left e
41+
Right decoded -> pure decoded

hydra-node/hydra-node.cabal

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,20 @@ executable visualize-logs
252252
hs-source-dirs: exe/visualize-logs
253253
main-is: Main.hs
254254
build-depends:
255+
, aeson
255256
, base
256257
, bytestring
258+
, containers
257259
, hydra-cardano-api
258260
, hydra-node
259261
, hydra-prelude
262+
, lens
263+
, lens-aeson
264+
, text
260265
, unix
261266

262267
ghc-options: -threaded -rtsopts -with-rtsopts=-N4
268+
263269
benchmark tx-cost
264270
import: project-config
265271
hs-source-dirs: bench/tx-cost

hydra-node/src/Hydra/Logging.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ instance ToJSON a => ToJSON (Envelope a) where
7676
, "message" .= message
7777
]
7878

79+
instance FromJSON a => FromJSON (Envelope a) where
80+
parseJSON = Aeson.withObject "Envelope" $ \o -> do
81+
timestamp <- o Aeson..: "timestamp"
82+
threadId <- o Aeson..: "threadId"
83+
namespace <- o Aeson..: "namespace"
84+
message <- o Aeson..: "message"
85+
pure Envelope{timestamp, threadId, namespace, message}
86+
7987
defaultQueueSize :: Natural
8088
defaultQueueSize = 500
8189

0 commit comments

Comments
 (0)