Commits

Stefan Saasen committed 73c38d6

Fix duration parsing

  • Participants
  • Parent commits 0539d6b

Comments (0)

Files changed (3)

File logparser/src/Stash/Log/GitOpsAnalyser.hs

         where clientIp line = head $ split "," (S.unpack $ getRemoteAdress line)
               ops           = [isClone, isFetch, isShallowClone, isPush, isRefAdvertisement]
               f line        = isOutgoingLogLine line && p line && isGitOperation line
-              m line        =  let  duration    = getRequestDuration line
+              m line        =  let  duration    = fromMaybe 0 $ getRequestDuration line
                                     zero        = replicate 5 0
                                     inc op      = if op line then (+duration) else id
                                     missOps     = map (inc . uncachedOperation) ops

File logparser/src/Stash/Log/Parser.hs

 import qualified Data.Text as T
 import Data.Attoparsec.Char8 hiding (char, space, take)
 import Prelude hiding (takeWhile)
-import Data.ByteString.Char8 (readInteger)
+import Data.ByteString.Char8 (readInteger, readInt)
 import Data.String.Utils (split)
 import Data.Maybe (mapMaybe)
 import Text.Printf (printf)
     ,getAction              :: Action
     ,getDetails             :: S.ByteString
     ,getLabels              :: [String]
-    ,getRequestDuration     :: Int
+    ,getRequestDuration     :: Maybe Int
     ,getSessionId           :: S.ByteString
 } deriving (Show, Eq)
 
 
 logEntry :: Parser S.ByteString
 logEntry = do
-   entry <- takeTill (== '|')
-   pipe
-   space
+   entry <- parseEntry
    return $ S.init entry
 
+parseDuration :: Parser (Maybe Int)
+parseDuration = do
+    entry <- parseEntry
+    return $ fmap fst (readInt entry)
+
+parseEntry :: Parser S.ByteString
+parseEntry = do
+    entry <- takeTill (== '|')
+    pipe
+    space
+    return entry
+
 -- | Parse the request Id. A request id consist of a a char indicating an
 -- request 'i' or response 'o', followed by the minute of the day, a request
 -- counter and the number of concurrent requests, separated by an 'x'. E.g. i8x1401519x6
     action <- parseAction
     details <- logEntry
     labels_ <- logEntry
-    rawDuration <- logEntry
+    duration <- parseDuration
     sessionId <- logEntry
     let labels = map trim $ split "," (S.unpack labels_)
         username = if rawUsername == "-" then Nothing else Just rawUsername
-        duration = read $ S.unpack rawDuration
     return $ LogLine remoteAddress protocol requestId username date
                     action details labels duration sessionId
 

File logparser/tests/Properties.hs

 parsedLogLine3 = parseLogLine inputLine
     where inputLine = "172.16.3.7 | ssh | o949x7523178x1 | atlaseye_user | 2012-10-23 15:49:46,461 | git-upload-pack '/CONF/teamcal.git' | - | fetch | 117 | de84i5 | "
 
+parsedLogLine4 = parseLogLine inputLine
+    where inputLine = "63.246.22.41,172.16.1.187 | https | i0x4771443x6 | - | 2012-12-10 00:00:00,199 | \"GET /git/STASH/stash.git/info/refs HTTP/1.1\" | \"\" \"git/1.7.4.1\" | - | - | - | "
+
 test_parseLogEntryDate = H.assertEqual
     "Should parse the date correctly"
     (LogDate 2012 8 22 18 32 08 505)
     ["shallow clone"]
     (getLabels $ fromJust parsedLogLine)
 
+test_logLineParseDuration = H.assertEqual
+    "Should parse the duration correctly"
+    (Just 117)
+    (getRequestDuration $ fromJust parsedLogLine3)
+
+test_logLineParseDurationNothing = H.assertEqual
+    "Should parse the duration correctly"
+    Nothing
+    (getRequestDuration $ fromJust parsedLogLine4)
+
 test_logLineParseRequestId = H.assertEqual
     "Should parse the request id"
     'o'
         ,testCase "parser/parse protocol (https)" test_logLineParseProtocol
         ,testCase "parser/parse protocol (ssh)" test_logLineParseProtocolSsh
         ,testCase "parser/parse request id" test_logLineParseRequestId
+        ,testCase "parser/parse duration" test_logLineParseDuration
+        ,testCase "parser/parse duration - Nothing" test_logLineParseDurationNothing
         ,testCase "parser/parse action" test_logLineParseAction
         ,testCase "parser/parse details" test_logLineParseDetails
         ,testCase "parser/parse labels" test_logLineParseLabels