Commits

Stefan Saasen  committed ab1e537

Add support for reading bzip2 compressed log files

  • Participants
  • Parent commits 522fa5d

Comments (0)

Files changed (3)

 
 [https://bitbucket.org/ssaasen/stash-log-parser/downloads](https://bitbucket.org/ssaasen/stash-log-parser/downloads)
 
-The `logparser` binary supports multiple commands and currently expects
-a single logfile as an argument.
+The `logparser` binary supports multiple commands and accepts one or more
+logfiles as arguments (either in uncompressed or compressed (bzip2) form).
 
 E.g.
 
-    $> logparser gitOperations FILEPATH
-
-Note: To combine multiple log files into a single file use:
-
-    $> cat atlassian-stash-access-2012-0*.log > aggregated-log-file
+    $> logparser gitOperations ./path/to/atlassian-stash-access-2012-09-12*.log
 
 Executing `logparser` will show the help with a list of supported commands.
 
 per hour, the output will look like this:
 
 
-    [922] λ > logparser gitOperations aggregated.log 
+    [922] λ > logparser gitOperations atlassian-stash-access-2012-*.log.bz2
     2012-08-22 18|2|0|13|0|733|0|0|0|0|0|2|0|13|0|733
     2012-08-22 19|3|24|74|0|1660|0|0|0|0|0|3|24|74|0|1660
     2012-08-22 20|2|33|119|0|1369|0|0|0|0|0|2|33|119|0|1369

File logparser/logparser.cabal

                             data-default,
                             mtl >= 2.0.0.0 && < 3,
                             text,
+                            bzlib,
                             unordered-containers >= 0.2
     ghc-options:
                             -Wall

File logparser/src/Main.hs

+{-# LANGUAGE OverloadedStrings #-}
 module Main where
 
 import qualified Data.ByteString.Lazy.Char8 as L
+import qualified Codec.Compression.BZip as BZip
 import Stash.Log.Parser
 import Stash.Log.Analyser
 import Stash.Log.GitOpsAnalyser
 import Data.Default
+import Data.List (isSuffixOf)
 import UI.Command
 import Prelude hiding (takeWhile)
 import Text.Printf (printf)
 printCountLines :: (Show a) => (L.ByteString -> a) -> [FilePath] -> IO ()
 printCountLines f path = print . f =<< readFiles path
 
-toLines :: [FilePath] -> IO [L.ByteString]
-toLines = liftM L.lines . readFiles
-
 formatLogDate :: LogDate -> String
 formatLogDate date = printf "%04d-%02d-%02d %02d:%02d" (getYear date) (getMonth date)
                             (getDay date) (getHour date) (getMinute date)
 
-readFiles :: [FilePath] -> IO L.ByteString
-readFiles = fmap L.concat . mapM L.readFile
+-- =================================================================================
 
+toLines :: [FilePath] -> IO [L.ByteString]
+toLines = liftM L.lines . readFiles
+
+readFiles :: [FilePath] -> IO L.ByteString
+readFiles = fmap L.concat . mapM readCompressedOrUncompressed
 
+readCompressedOrUncompressed :: FilePath -> IO L.ByteString
+readCompressedOrUncompressed path = if ".bz2" `isSuffixOf` path
+                                    then liftM BZip.decompress $ L.readFile path
+                                    else L.readFile path