Skip to content

Commit f25c7d2

Browse files
author
HirotoShioi
committed
Fix styling
1 parent 7df57af commit f25c7d2

File tree

10 files changed

+297
-248
lines changed

10 files changed

+297
-248
lines changed

src/CLI.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,41 @@ data CLI
1616
| ProcessTickets -- ^ Procss all the tickets in the Zendesk
1717
| RawRequest String -- ^ Raw request to the given url
1818
| ShowStatistics -- ^ Show statistics
19-
deriving (Show)
19+
deriving Show
2020

2121
-- | Parser for ProcessTicket
2222
cmdProcessTicket :: Parser CLI
2323
cmdProcessTicket = ProcessTicket <$> argument auto
24-
( metavar "TICKET_ID"
25-
<> help "Ticket id to analyze")
24+
(metavar "TICKET_ID"
25+
<> help "Ticket id to analyze")
2626

2727
-- | Parser for RawRequest
2828
cmdRawRequest :: Parser CLI
2929
cmdRawRequest = RawRequest <$> strOption
30-
( metavar "URL"
31-
<> help "Url to request")
30+
(metavar "URL"
31+
<> help "Url to request")
3232

3333
-- | Parser for CLI commands
3434
cli :: Parser CLI
3535
cli = hsubparser $ mconcat
36-
[
37-
command "collect-emails" (info (pure CollectEmails)
38-
(progDesc "Collect emails requested by single user"))
39-
, command "process-tickets" (info (pure ProcessTickets)
40-
(progDesc "Process all the tickets i.e add comments, tags."))
41-
, command "process-ticket" (info cmdProcessTicket
42-
(progDesc "Process Zendesk ticket of an given ticket id"))
43-
, command "raw-request" (info cmdRawRequest
44-
(progDesc "Raw request to the given url"))
45-
, command "show-stats" (info (pure ShowStatistics)
46-
(progDesc "Print list of ticket Ids that agent has been assigned"))
47-
]
36+
[
37+
command "collect-emails" (info (pure CollectEmails)
38+
(progDesc "Collect emails requested by single user"))
39+
, command "process-tickets" (info (pure ProcessTickets)
40+
(progDesc "Process all the tickets i.e add comments, tags."))
41+
, command "process-ticket" (info cmdProcessTicket
42+
(progDesc "Process Zendesk ticket of an given ticket id"))
43+
, command "raw-request" (info cmdRawRequest
44+
(progDesc "Raw request to the given url"))
45+
, command "show-stats" (info (pure ShowStatistics)
46+
(progDesc "Print list of ticket Ids that agent has been assigned"))
47+
]
4848

4949
getCliArgs :: IO CLI
5050
getCliArgs = execParser opts
5151
where
5252
opts = info (cli <**> helper <**> versionHelper)
53-
( fullDesc
53+
(fullDesc
5454
<> header "Log classifier"
5555
<> progDesc "Client for peforming analysis on Zendesk"
5656
)

src/Classify.hs

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
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

915
import Universum
1016

@@ -19,8 +25,7 @@ import Regex (MatchWithCaptures, Regex, RegexCompileError, RegexString
1925

2026
type 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
4448
compileBehavior 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

5660
runBehavior :: HasCallStack => Behavior
5761
-> Map FilePath LByteString
5862
-> IO (Map FilePath [(ErrorName, ErrorCode)])
5963
runBehavior (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

8486
readZip :: HasCallStack => LByteString -> Either String (Map FilePath LByteString)
8587
readZip rawzip = case Zip.toArchiveOrFail rawzip of
@@ -104,40 +106,41 @@ getMatches :: LByteString -> [MatchWithCaptures] -> [LByteString]
104106
getMatches fullBS = map (getOneMatch fullBS)
105107

106108
getOneMatch :: 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

109112
classifyZip :: HasCallStack
110113
=> LByteString
111114
-> IO (Either String (Map FilePath [(ErrorName, ErrorCode)]))
112115
classifyZip 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

142145
isStaleLockFile :: Map FilePath [(ErrorName, ErrorCode)] -> Maybe LText
143146
isStaleLockFile = Map.foldl' checkFile Nothing
@@ -155,5 +158,5 @@ postProcessError :: Map FilePath [(ErrorName, ErrorCode)]
155158
postProcessError 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

src/Lib.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Lib
2-
( someFunc
3-
) where
2+
( someFunc
3+
) where
44

55
import Universum
66
-- TODO(ks): Here we want to import the functionality we require in cardano-report-server.

src/LogAnalysis/Classifier.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
{-# LANGUAGE RecordWildCards #-}
44

55
module LogAnalysis.Classifier
6-
( extractIssuesFromLogs
7-
, extractErrorCodes
6+
( extractErrorCodes
7+
, extractIssuesFromLogs
88
, prettyFormatAnalysis
99
) where
1010

@@ -47,8 +47,8 @@ filterAnalysis :: Analysis -> Either Text Analysis
4747
filterAnalysis as = do
4848
let filteredAnalysis = Map.filter (/=[]) as
4949
if null filteredAnalysis
50-
then Left "Cannot find any known issues"
51-
else return $ Map.map (take numberOfErrorText) filteredAnalysis
50+
then Left "Cannot find any known issues"
51+
else return $ Map.map (take numberOfErrorText) filteredAnalysis
5252

5353
extractErrorCodes :: Analysis -> [ Text ]
5454
extractErrorCodes as = map (\(Knowledge{..}, _) -> renderErrorCode kErrorCode) $ Map.toList as
@@ -58,11 +58,11 @@ prettyFormatAnalysis :: Analysis -> LText
5858
prettyFormatAnalysis as =
5959
let aList = Map.toList as
6060
in foldr (\(Knowledge{..}, txts) acc ->
61-
"\n" <> show kErrorCode
62-
<> "\n" <> kProblem
63-
<> "\n **" <> kSolution
64-
<> "** \n"
65-
<> foldr1 (\txt ts -> "\n" <> txt <> "\n" <> ts) txts -- List errors
66-
<> "\n" <> acc
67-
<> "\n\n"
68-
) "" aList
61+
"\n" <> show kErrorCode
62+
<> "\n" <> kProblem
63+
<> "\n **" <> kSolution
64+
<> "** \n"
65+
<> foldr1 (\txt ts -> "\n" <> txt <> "\n" <> ts) txts -- List errors
66+
<> "\n" <> acc
67+
<> "\n\n"
68+
) "" aList

src/LogAnalysis/KnowledgeCSVParser.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ insideQuotes =
1717
LT.append <$> (LT.fromStrict <$> ALT.takeWhile (/= '"'))
1818
<*> (LT.concat <$> many (LT.cons <$> dquotes <*> insideQuotes))
1919
<?> "inside of double quotes"
20-
where
21-
dquotes =
22-
string "\"\"" >> return '"'
23-
<?> "paired double quotes"
20+
where
21+
dquotes = string "\"\"" >> return '"'
22+
<?> "paired double quotes"
2423

2524
-- | Parse quoted field
2625
quotedField :: Parser LText

0 commit comments

Comments
 (0)