1. Stefan Saasen
  2. stash-log-parser

Commits

Stefan Saasen  committed d6f62ab

Add support for extracting repository information out of teh Stash support zip file

  • Participants
  • Parent commits c6947ae
  • Branches master

Comments (0)

Files changed (5)

File generate-support-data-graphs.sh

View file
+#!/bin/bash
+
+set -e
+set -u
+
+rm -f *.png
+
+# Ensure that 'logparser' is in the PATH (e.g. run rebuild.sh or 'cabal copy')
+
+if [ "$#" -lt "1" ]; then
+    echo "Usage:"
+    echo ""
+    echo "  $0 path/to/the/Stash_support_2013-06.xxx.zip"
+    echo ""
+    exit -1
+fi
+
+SUPPORT_ZIP="$1"
+
+export GNUPLOT_LIB="gnuplot:."
+
+time stash-support repositoryInformation ${SUPPORT_ZIP} > repository-information.dat
+gnuplot < gnuplot/support-zip/repository-stats.plot
+

File gnuplot/support-zip/repository-stats.plot

View file
+set datafile separator "|"
+set terminal png size 1400,1000
+
+set xlabel "Repository"
+
+set grid
+set auto x
+set xtic out nomirror rotate by -45 font ",8"
+
+
+set output "repository-information.png"
+set ylabel "Repository - Size on Disk"
+set title "Repository disk size"
+
+plot "repository-information.dat" every ::::29 using ($2/1024):xticlabels(1) with lines title "Repository Size"
+

File logparser/logparser.cabal

View file
                             bzlib,
                             QuickCheck >= 2.4.0.1
 
+
 executable logparser
     main-is:                Main.hs
     hs-source-dirs:         src
-    other-modules:          Stash.Log.Analyser, Stash.Log.Parser, Stash.Log.GitOpsAnalyser, Stash.Log.Common, Stash.Log.Output, Stash.Log.Input
     build-depends:          base < 5 && >= 3,
                             bytestring >= 0.9,
                             MissingH,
     if os(linux)
         ghc-options:
                             -static -optc-static -optl-static -optl-pthread
+
+executable stash-support
+    main-is:                MainZip.hs
+    hs-source-dirs:         src
+    build-depends:          base < 5 && >= 3,
+                            bytestring >= 0.9,
+                            MissingH,
+                            attoparsec >= 0.10,
+                            mtl >= 2.0.0.0 && < 3,
+                            text,
+                            bzlib,
+                            containers,
+                            cmdargs >= 0.10,
+                            unordered-containers >= 0.2,
+                            time,
+                            zip-conduit,
+                            HaXml,
+                            directory,
+                            conduit,
+                            aeson
+    ghc-options:
+                            -Wall
+                            --Werror
+                            -fno-warn-unused-do-bind
+                            -rtsopts
+                            -O2
+                            -- -prof -auto-all -caf-all -fforce-recomp
+    if os(linux)
+        ghc-options:
+                            -static -optc-static -optl-static -optl-pthread
+

File logparser/src/MainZip.hs

View file
+{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-}
+module Main where
+
+import System.Environment (getArgs, withArgs)
+import Stash.Support.ZipFile
+import System.Console.CmdArgs
+import Prelude hiding (takeWhile)
+
+-- =================================================================================
+
+appName :: String
+appName = "stash-support"
+
+appVersion :: String
+appVersion = "1.0"
+
+appShortDesc :: String
+appShortDesc = "Analyse the Atlassian Stash Support Zip"
+
+data SupportZip = RepositoryInformation            {file :: FilePath}
+             deriving (Data,Typeable,Show,Eq)
+
+
+repositoryInformation :: SupportZip
+repositoryInformation = RepositoryInformation {file = def &= args}
+                &= name "repositoryInformation" &= help "Extract repository information\
+                \ from the given support zip file"
+
+mode :: Mode (CmdArgs SupportZip)
+mode = cmdArgsMode $ modes [repositoryInformation]
+        &= help appShortDesc
+        &= program appName &= summary (appName ++ " " ++ appVersion)
+        &= verbosity
+
+
+run :: SupportZip -> IO ()
+run (RepositoryInformation file') = extractRepositoryInformation file'
+
+main :: IO ()
+main = do
+    options <- getArgs
+    -- We need arguments so if there are no arguments given, invoke the help command
+    config <- (if null options then withArgs ["--help"] else id) $ cmdArgsRun mode
+    run config
+

File logparser/src/Stash/Support/ZipFile.hs

View file
+{-# LANGUAGE PackageImports, OverloadedStrings #-}
+
+module Stash.Support.ZipFile (
+    extractRepositoryInformation
+) where
+
+import qualified     Data.Conduit.Binary         as CB
+import "zip-conduit" Codec.Archive.Zip
+import               Control.Monad (join, when)
+import               Data.Maybe (mapMaybe)
+import               Text.XML.HaXml  hiding (when)
+import               Text.XML.HaXml.Posn
+import               Text.XML.HaXml.Util
+import               Text.XML.HaXml.Xtract.Parse
+import               Text.Printf
+import               Data.Attoparsec.Text   hiding (take)
+import               Control.Applicative
+import qualified     Data.Text as T
+import               Data.List (sortBy)
+import               System.Directory (doesFileExist)
+
+findNodeList :: String -> Content Posn -> [Content Posn]
+findNodeList = xtract id
+
+toContent :: Element Posn -> Content Posn
+toContent el = CElem el noPos
+
+parseXmlString :: String -> Element Posn
+parseXmlString ct =
+    let Document _ _ root _ = xmlParse "" ct
+    in root
+
+data Size = Byte {
+    bytes :: Int
+} deriving (Eq, Show)
+
+instance Ord Size where
+    compare (Byte a) (Byte b) = compare a b
+
+parseSize :: T.Text -> Maybe Size
+parseSize = maybeResult . parse par
+    where par :: Parser Size
+          par = do
+            b <- decimal
+            d <- asciiCI "KB" <|> asciiCI "MB"
+            return $ Byte (m d b)
+            where m "KB" = (* 1024)       -- assuming kibi
+                  m "MB" = (* 1024^2)     -- assuming mibi
+                  m "GB" = (* 1024^3)     -- assuming gibi
+                  m _    = id
+
+
+extractRepositoryInformation :: FilePath -> IO ()
+extractRepositoryInformation archivePath = do
+    fileExists <- doesFileExist archivePath
+    when fileExists $ do
+        fileName <- extract archivePath
+        parseXml fileName
+        return ()
+
+extract :: FilePath -> IO FilePath
+extract archivePath = do
+    let fileName = "application.xml"
+    withArchive archivePath $ sourceEntry "application-properties/application.xml" $ CB.sinkFile fileName
+    return fileName
+
+parseXml :: FilePath -> IO ()
+parseXml fileName = do
+    content <- readFile fileName
+    let xml  = toContent $ parseXmlString content
+        -- stashInformation         = findNodeList "//stash-information/(build-version + build-number)" xml
+        -- machineInformation       = findNodeList "//java-runtime-environment/(max-heap)" xml
+        repositories             = findNodeList "//repository" xml
+        mapElement (Elem (N qname') _ xs) = Just (qname', join $ fmap verbatim xs)
+        mapElement _             = Nothing
+        extractNameAndSize       = mapMaybe (mapElement . contentElem) . cat [childrenBy (tag "name"), childrenBy (tag "size")]
+        toTuple (a:b:_)          = Just (snd a, maybe 0 bytes $ parseSize $ T.pack $ snd b, snd b)
+        toTuple _                = Nothing
+        printNameAndSize (name,bytes',size) = printf "%s | %d | %s \n" name bytes' size
+
+    mapM_ printNameAndSize $ sortBy (\(_,s,_) (_,s2,_) -> compare s2 s) $ mapMaybe (toTuple . extractNameAndSize) repositories
+
+
+