Commits

Stefan Saasen committed bdbc21c

Sort the logfiles before parsing them

The current log file naming format does not sort properly when using shell
globs. If there are more than 10 log files per day the numbering scheme used
(without zero padding) causes the log file with the sequence number 10 to be
between numbers 1 and 2.

E.g.

atlassian-stash-access-2012-11-29.0.log
atlassian-stash-access-2012-11-29.1.log
atlassian-stash-access-2012-11-29.10.log
atlassian-stash-access-2012-11-29.2.log

This change will properly sort the log files, taking the date/sequence number
based naming scheme into account.

Comments (0)

Files changed (1)

logparser/src/Main.hs

 import Stash.Log.Analyser
 import Stash.Log.GitOpsAnalyser
 import Data.Default
-import Data.List (isSuffixOf)
+import Data.List (isSuffixOf,sortBy)
+import Data.Monoid (mappend)
+import Data.String.Utils (split)
 import UI.Command
 import Prelude hiding (takeWhile)
 import Text.Printf (printf)
 import Control.Monad (liftM)
 import Control.Monad.Trans (liftIO)
+import System.Path.NameManip
 
 -- =================================================================================
 
 toLines = liftM L.lines . readFiles
 
 readFiles :: [FilePath] -> IO L.ByteString
-readFiles = fmap L.concat . mapM readCompressedOrUncompressed
+readFiles = fmap L.concat . mapM readCompressedOrUncompressed . sortLogFiles
 
 readCompressedOrUncompressed :: FilePath -> IO L.ByteString
 readCompressedOrUncompressed path = if ".bz2" `isSuffixOf` path
                                     then liftM BZip.decompress $ L.readFile path
                                     else L.readFile path
+
+-- | Sort the logfiles by date and log file sequence number
+-- The logfile naming scheme is: "atlassian-stash-access-2012-11-29.0.log(.bz2)"
+sortLogFiles :: [FilePath] -> [FilePath]
+sortLogFiles = sortBy logFilePred
+    where extractFile = last . slice_path
+          sortPred (date1, num1) (date2, num2) = compare date1 date2 `mappend` compare num1 num2
+          logFilePred logFileName1 logFileName2 = sortPred (extractSortPairs logFileName1) (extractSortPairs logFileName2)
+          extractSortPairs path = let elems = drop 3 $ split "-" $ extractFile path
+                                  in case elems of
+                                     (year:month:(rest:_)) -> case split "." rest of
+                                                             (day:num:_) -> (year ++ "-" ++ month ++ "-" ++ day, read num :: Int)
+                                                             _           -> ("", 0)
+                                     _                 -> ("", 0)