Commits

Stefan Saasen  committed a2d2dd8

Move the log file sort function into common, add a test and sort the log file without a date correctly to the end

  • Participants
  • Parent commits 132080b

Comments (0)

Files changed (3)

File logparser/src/Main.hs

 import Stash.Log.Parser
 import Stash.Log.Analyser
 import Stash.Log.GitOpsAnalyser
+import Stash.Log.Common (sortLogFiles)
 import Data.Default
-import Data.List (isSuffixOf,sortBy)
-import Data.Monoid (mappend)
-import Data.String.Utils (split)
+import Data.List (isSuffixOf)
 import UI.Command
 import Prelude hiding (takeWhile)
 import Text.Printf (printf)
 import Control.Monad (liftM)
 import Control.Monad.Trans (liftIO)
-import System.Path.NameManip
 
 -- =================================================================================
 
                                     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)

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

 module Stash.Log.Common
 ( logDateEqHour
 , logDateEqMin
+, sortLogFiles
 ) where
 
 import Stash.Log.Parser (LogDate(..))
+import Data.List (sortBy)
+import Data.Monoid (mappend)
+import Data.String.Utils (split)
+import System.Path.NameManip
 
 -- | Do the given dates are within the same hour?
 logDateEqHour :: LogDate -> LogDate -> Bool
 logDateEqMin a b = logDateEqHour a b &&
                 getMinute a == getMinute b
 
+-- | 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)
+                                     _                 -> ("9999", 0)

File logparser/tests/Properties.hs

 {-# LANGUAGE OverloadedStrings #-}
 
--- | Tests for the 'Data.Hashable' module.  We test functions by
--- comparing the C and Haskell implementations.
-
 module Main (main) where
 
 import qualified Data.ByteString.Lazy.Char8 as L
 import Stash.Log.Analyser
 import Stash.Log.Parser
 import Stash.Log.GitOpsAnalyser
+import Stash.Log.Common (sortLogFiles)
 import Data.Maybe
 import Test.QuickCheck hiding ((.&.))
 import Test.Framework (Test, defaultMain, testGroup)
     (protocolCount dataLogLines)
 
 ------------------------------------------------------------------------
+test_sortFilesAsc = H.assertEqual
+    "Should sort the given files correctly"
+    ["atlassian-stash-access-2012-11-29.1.log.bz2",
+     "atlassian-stash-access-2012-11-29.2.log.bz2",
+     "atlassian-stash-access-2012-11-29.4.log.bz2",
+     "atlassian-stash-access-2012-11-29.10.log.bz2",
+     "atlassian-stash-access.log"]
+     (sortLogFiles input)
+    where input = ["atlassian-stash-access-2012-11-29.4.log.bz2",
+                     "atlassian-stash-access-2012-11-29.1.log.bz2",
+                     "atlassian-stash-access-2012-11-29.10.log.bz2",
+                     "atlassian-stash-access.log",
+                     "atlassian-stash-access-2012-11-29.2.log.bz2"]
+
+------------------------------------------------------------------------
 -- Test harness
 
 main :: IO ()
         ,testCase "analyser/dataConcurrentConn logDateEqMin" test_plotDataConcurrentConn
         ,testCase "analyser/dataConcurrentConn logDateEqHour" test_plotDataConcurrentConnHour
       ],
+      testGroup "Common"
+      [
+        testCase "common/sortLogFiles" test_sortFilesAsc
+      ],
       testGroup "parser"
       [ testCase "parser/parse empty String" test_logLineParserEmpty
         ,testCase "parser/parse single line" test_logLineParseSingleLine