diff --git a/README.md b/README.md index 59882a1..7d193ae 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,12 @@ On a mac: stack test ``` +## Run a single test file + +``` +stack runghc -- -isrc -itest test/Cis194/Hw/SomeSpec.hs +``` + ## Using QuickCheck in ghci :m +Test.QuickCheck diff --git a/src/Cis194/Hw/LogAnalysis.hs b/src/Cis194/Hw/LogAnalysis.hs index 3adfa15..c3512df 100644 --- a/src/Cis194/Hw/LogAnalysis.hs +++ b/src/Cis194/Hw/LogAnalysis.hs @@ -6,22 +6,46 @@ module Cis194.Hw.LogAnalysis where import Cis194.Hw.Log parseMessage :: String -> LogMessage -parseMessage s = Unknown s +parseMessage s = + case words s of + "E":i:t:ws -> buildMessage (Error (read i)) t ws + "I":t:ws -> buildMessage Info t ws + "W":t:ws -> buildMessage Warning t ws + _ -> Unknown s + +buildMessage :: MessageType -> String -> [String] -> LogMessage +buildMessage m t ws = LogMessage m (read t) (unwords ws) parse :: String -> [LogMessage] -parse _ = [] +parse = map parseMessage . lines insert :: LogMessage -> MessageTree -> MessageTree +insert Unknown{} t = t +insert l Leaf = Node Leaf l Leaf +insert n@(LogMessage _ nt _) (Node l m@(LogMessage _ mt _) r) + | nt < mt = Node (insert n l) m r + | nt > mt = Node l m (insert n r) insert _ t = t build :: [LogMessage] -> MessageTree -build _ = Leaf +build = foldl (flip insert) Leaf inOrder :: MessageTree -> [LogMessage] -inOrder _ = [] +inOrder t = go t [] + where + go (Node l m r) = go l . (m:) . go r + go Leaf = id -- whatWentWrong takes an unsorted list of LogMessages, and returns a list of the -- messages corresponding to any errors with a severity of 50 or greater, -- sorted by timestamp. whatWentWrong :: [LogMessage] -> [String] -whatWentWrong _ = [] +whatWentWrong = map getString . filter isSevere . inOrder . build + +isSevere :: LogMessage -> Bool +isSevere (LogMessage (Error s) _ _) = s >= 50 +isSevere _ = False + +getString :: LogMessage -> String +getString (LogMessage _ _ s) = s +getString (Unknown s) = s