Created
April 13, 2012 23:08
-
-
Save JakubOboza/2380711 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Text.ParserCombinators.Parsec | |
data RailsLogSummaryLine = RailsLogSummaryLine { | |
requestStatus :: String, | |
requestTotalTime :: String, | |
requestDbTime :: String, | |
requestViewTime :: String | |
} deriving (Show, Ord, Eq) | |
data RailsLogParsingResult = Fail String | |
| Summary RailsLogSummaryLine | |
plainValue :: Parser String | |
plainValue = many1 (noneOf " ()") | |
timeValue :: Parser String | |
timeValue = many1 (noneOf "ms") | |
bracketedValue = do | |
char '(' | |
plainValue | |
space | |
viewTime <- timeValue | |
plainValue | |
space | |
char '|' | |
space | |
plainValue | |
space | |
dbTime <- timeValue | |
plainValue | |
char ')' | |
return (viewTime, dbTime) | |
summaryLogLine :: Parser RailsLogSummaryLine | |
summaryLogLine = do | |
plainValue | |
space | |
status <- plainValue | |
space | |
plainValue | |
space | |
plainValue | |
space | |
totalTime <- timeValue | |
plainValue | |
space | |
viewAndRenderTime <- bracketedValue | |
return $ RailsLogSummaryLine status totalTime (fst viewAndRenderTime) (snd viewAndRenderTime) | |
parseSummaryLine inputLine = case parse summaryLogLine "(summary)" inputLine of | |
Left err -> Fail $ show err | |
Right res -> Summary res | |
clearFailedParses [] result = result | |
clearFailedParses (h:hs) result = | |
case h of | |
Summary smr -> clearFailedParses hs (show smr : result) | |
Fail err -> clearFailedParses hs result | |
clearFailedParses' [] result = result | |
clearFailedParses' (h:hs) result = | |
case h of | |
Summary smr -> clearFailedParses' hs (smr : result) | |
Fail err -> clearFailedParses' hs result | |
logEntryTimes e = | |
case e of | |
RailsLogSummaryLine _ total view db -> [ (read total :: Float), (read view :: Float), (read db :: Float)] | |
avgTimes [] (total:view:db) = [show total, show view, show db] | |
avgTimes (h:hs) result = | |
let total = ((result !! 0) + ((logEntryTimes h) !! 0)) / 2.0 in | |
let view = ((result !! 1) + ((logEntryTimes h) !! 1)) / 2.0 in | |
let db = ((result !! 2) + ((logEntryTimes h) !! 2)) / 2.0 in | |
avgTimes hs [ total, view, db ] | |
main = do | |
file <- readFile "production.log" | |
let logLines = lines file in | |
let parseResult = map parseSummaryLine logLines in | |
let results = clearFailedParses parseResult [] in | |
let results' = clearFailedParses' parseResult [] in | |
let vals = avgTimes results' [1.0, 1.0, 1.0] in | |
mapM putStrLn vals |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment