44{-# LANGUAGE RankNTypes #-}
55
66-- Currenty not used..
7- module Classify (classifyZip , ErrorCode (.. ), ErrorName , postProcessError , ConfirmedError (.. )) where
7+ module Classify
8+ ( ConfirmedError (.. )
9+ , ErrorCode (.. )
10+ , ErrorName
11+ , classifyZip
12+ , postProcessError
13+ ) where
814
915import Universum
1016
@@ -19,8 +25,7 @@ import Regex (MatchWithCaptures, Regex, RegexCompileError, RegexString
1925
2026type ErrorName = Text
2127
22- data ErrorHandler
23- = ErrorHandler
28+ data ErrorHandler = ErrorHandler
2429 { errorHandlerFileRegex :: ! Regex
2530 , errorHandlerRegex :: ! Regex
2631 , errorHandlerMessage :: LByteString -> [MatchWithCaptures ] -> Maybe ErrorCode
@@ -33,53 +38,50 @@ data ErrorCode =
3338 | NetworkError [ LByteString ]
3439 deriving Show
3540
36- newtype ConfirmedError =
37- ConfirmedStaleLockFile LText
41+ newtype ConfirmedError = ConfirmedStaleLockFile LText
3842
39- newtype Behavior
40- = Behavior (Map ErrorName ErrorHandler )
43+ newtype Behavior = Behavior (Map ErrorName ErrorHandler )
4144
42- compileBehavior :: [(ErrorName , (RegexString , RegexString , LByteString -> [MatchWithCaptures ] -> Maybe ErrorCode ))]
45+ compileBehavior :: [(ErrorName , (RegexString , RegexString ,
46+ LByteString -> [MatchWithCaptures ] -> Maybe ErrorCode ))]
4347 -> Either RegexCompileError Behavior
4448compileBehavior input = do
45- pairs <- forM input $ \ (errName, (frx, rx, toErrorCode)) -> do
46- frxCompiled <- compileRegex frx
47- rxCompiled <- compileRegex rx
48- let errHandler = ErrorHandler
49+ pairs <- forM input $ \ (errName, (frx, rx, toErrorCode)) -> do
50+ frxCompiled <- compileRegex frx
51+ rxCompiled <- compileRegex rx
52+ let errHandler = ErrorHandler
4953 { errorHandlerFileRegex = frxCompiled
5054 , errorHandlerRegex = rxCompiled
5155 , errorHandlerMessage = toErrorCode
5256 }
53- pure (errName, errHandler)
54- pure (Behavior (Map. fromList pairs))
57+ pure (errName, errHandler)
58+ pure (Behavior (Map. fromList pairs))
5559
5660runBehavior :: HasCallStack => Behavior
5761 -> Map FilePath LByteString
5862 -> IO (Map FilePath [(ErrorName , ErrorCode )])
5963runBehavior (Behavior behavior) files = do
60- let
61- checkFile :: FilePath -> LByteString -> IO (Maybe [(ErrorName , ErrorCode )])
62- checkFile fp contents = do
63- hits <- Map. traverseMaybeWithKey (checkBehaviorOnFile fp contents) behavior
64- if length hits > 0 then
65- pure $ Just $ Map. toList hits
66- else
67- pure Nothing
68- checkBehaviorOnFile :: FilePath
69- -> LByteString
70- -> ErrorName
71- -> ErrorHandler
72- -> IO (Maybe ErrorCode )
73- checkBehaviorOnFile fp contents _ errhandler =
74- if matchTest (errorHandlerFileRegex errhandler) (LBSC. pack fp) then do
75- let
76- matches = matchAll (errorHandlerRegex errhandler) contents
77- if (length matches) == 0 then
78- pure Nothing
79- else
80- pure $ errorHandlerMessage errhandler contents matches
81- else pure Nothing
82- Map. traverseMaybeWithKey checkFile files
64+ let
65+ checkFile :: FilePath -> LByteString -> IO (Maybe [(ErrorName , ErrorCode )])
66+ checkFile fp contents = do
67+ hits <- Map. traverseMaybeWithKey (checkBehaviorOnFile fp contents) behavior
68+ if length hits > 0
69+ then pure $ Just $ Map. toList hits
70+ else pure Nothing
71+ checkBehaviorOnFile :: FilePath
72+ -> LByteString
73+ -> ErrorName
74+ -> ErrorHandler
75+ -> IO (Maybe ErrorCode )
76+ checkBehaviorOnFile fp contents _ errhandler =
77+ if matchTest (errorHandlerFileRegex errhandler) (LBSC. pack fp) then do
78+ let matches = matchAll (errorHandlerRegex errhandler) contents
79+ if (length matches) == 0 then
80+ pure Nothing
81+ else
82+ pure $ errorHandlerMessage errhandler contents matches
83+ else pure Nothing
84+ Map. traverseMaybeWithKey checkFile files
8385
8486readZip :: HasCallStack => LByteString -> Either String (Map FilePath LByteString )
8587readZip rawzip = case Zip. toArchiveOrFail rawzip of
@@ -104,40 +106,41 @@ getMatches :: LByteString -> [MatchWithCaptures] -> [LByteString]
104106getMatches fullBS = map (getOneMatch fullBS)
105107
106108getOneMatch :: LByteString -> MatchWithCaptures -> LByteString
107- getOneMatch bs ((start, len), _) = LBSC. take (fromIntegral len) $ LBSC. drop (fromIntegral start) bs
109+ getOneMatch bs ((start, len), _) =
110+ LBSC. take (fromIntegral len) $ LBSC. drop (fromIntegral start) bs
108111
109112classifyZip :: HasCallStack
110113 => LByteString
111114 -> IO (Either String (Map FilePath [(ErrorName , ErrorCode )]))
112115classifyZip rawzip = do
113116 -- zip <- readZip <$> LreadFile "logs.zip"
114- let
115- zipmap' = readZip rawzip
116- let
117- behaviorList :: [ ( ErrorName , ( RegexString , RegexString , LByteString -> [ MatchWithCaptures ] -> Maybe ErrorCode ) ) ]
118- behaviorList
119- = [ ( " conn-refused "
120- , ( " Daedalus.log "
121- , " \" message \" : \" connect ECONNREFUSED [^ \" ]* \" "
122- , countRefused ) )
123- , ( " stale-lock-file "
124- , ( " node.pub$ "
125- , " [^ \n ]*Wallet[^ \n ]*/open.lock: Locked by [0-9]+: resource busy "
126- , \ contents matches -> Just $ StateLockFile $ decodeUtf8 $ last $ N. fromList $ getMatches contents matches ) )
127- , ( " file-not-found"
128- , ( " node.pub$"
129- , " : [^ ]*: openBinaryFile: does not exist \\ (No such file or directory\\ )"
130- , \ contents matches -> Just $ FileNotFound $ getMatches contents matches ) )
131- , ( " TransportError"
132- , ( " node"
133- , " TransportError[^\n ]*"
134- , \ contents matches -> Just $ NetworkError $ getMatches contents matches ) )
135- ]
117+ let zipmap' = readZip rawzip
118+ behaviorList :: [( ErrorName , ( RegexString , RegexString ,
119+ LByteString -> [ MatchWithCaptures ] -> Maybe ErrorCode ))]
120+ behaviorList
121+ = [ ( " conn-refused "
122+ , ( " Daedalus.log "
123+ , " \" message \" : \" connect ECONNREFUSED [^ \" ]* \" "
124+ , countRefused ) )
125+ , ( " stale-lock-file "
126+ , ( " node.pub$ "
127+ , " [^ \n ]*Wallet[^ \n ]*/open.lock: Locked by [0-9]+: resource busy "
128+ , \ contents matches -> Just $ StateLockFile $ decodeUtf8
129+ $ last $ N. fromList $ getMatches contents matches ) )
130+ , ( " file-not-found"
131+ , ( " node.pub$"
132+ , " : [^ ]*: openBinaryFile: does not exist \\ (No such file or directory\\ )"
133+ , \ contents matches -> Just $ FileNotFound $ getMatches contents matches ) )
134+ , ( " TransportError"
135+ , ( " node"
136+ , " TransportError[^\n ]*"
137+ , \ contents matches -> Just $ NetworkError $ getMatches contents matches ) )
138+ ]
136139 behavior <- either (fail . toString) pure
137140 $ compileBehavior behaviorList
138141 case zipmap' of
139- Left err -> pure $ Left err
140- Right zipmap -> Right <$> classifyLogs behavior zipmap
142+ Left err -> pure $ Left err
143+ Right zipmap -> Right <$> classifyLogs behavior zipmap
141144
142145isStaleLockFile :: Map FilePath [(ErrorName , ErrorCode )] -> Maybe LText
143146isStaleLockFile = Map. foldl' checkFile Nothing
@@ -155,5 +158,5 @@ postProcessError :: Map FilePath [(ErrorName, ErrorCode)]
155158postProcessError input = finalAnswer
156159 where
157160 finalAnswer = case isStaleLockFile input of
158- Just msg -> Right $ ConfirmedStaleLockFile msg
159- Nothing -> Left input
161+ Just msg -> Right $ ConfirmedStaleLockFile msg
162+ Nothing -> Left input
0 commit comments