|
| 1 | +{-# LANGUAGE OverloadedRecordDot #-} |
| 2 | + |
1 | 3 | module Main where
|
2 | 4 |
|
3 | 5 | import Hydra.Cardano.Api
|
4 |
| -import Hydra.Prelude |
| 6 | +import Hydra.Prelude hiding (encodeUtf8) |
5 | 7 |
|
| 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 (..)) |
6 | 14 | 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) |
8 | 21 |
|
9 | 22 | main :: IO ()
|
10 | 23 | 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 |
12 | 35 | 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 |
0 commit comments