1 {-# OPTIONS_GHC -Wall #-}
2 module LogAnalysis where
6 parseMessage :: String -> LogMessage
7 parseMessage s = let ws = words s in
10 parseMessage' ("I":ts:ws') = LogMessage Info (read ts) (unwords ws')
11 parseMessage' ("W":ts:ws') = LogMessage Warning (read ts) (unwords ws')
12 parseMessage' ("E":level:ts:ws') = LogMessage (Error (read level)) (read ts) (unwords ws')
13 parseMessage' x = Unknown (unwords x)
15 parse :: String -> [LogMessage]
16 parse s = map parseMessage (lines s)
19 insert :: LogMessage -> MessageTree -> MessageTree
20 insert (LogMessage t ts s) (Leaf) = Node Leaf (LogMessage t ts s) Leaf
21 insert (LogMessage t1 ts1 s1) (Node lt (LogMessage t2 ts2 s2) rt) | ts1 <= ts2 = Node (insert (LogMessage t1 ts1 s1) lt) (LogMessage t2 ts2 s2) rt
22 | otherwise = Node lt (LogMessage t2 ts2 s2) (insert (LogMessage t1 ts1 s1) rt)
26 build :: [LogMessage] -> MessageTree
27 build = foldr (\msg t -> insert msg t) Leaf
30 inOrder :: MessageTree -> [LogMessage]
32 inOrder (Node lt rootmsg rt) = inOrder lt ++ [rootmsg] ++ inOrder rt
35 whatWentWrong :: [LogMessage] -> [String]
36 whatWentWrong ls = let sortedLogMsgs = inOrder (build ls)
38 filterErrorMessages (>= 50) sortedLogMsgs
40 filterErrorMessages _ [] = [""]
41 filterErrorMessages f ((LogMessage (Error level) _ s):ms) | f level = s: filterErrorMessages f ms
42 | otherwise = filterErrorMessages f ms
43 filterErrorMessages f (_:ms) = filterErrorMessages f ms