Revision 090e5de8 src/Ganeti/Storage/Diskstats/Parser.hs

b/src/Ganeti/Storage/Diskstats/Parser.hs
27 27
-}
28 28
module Ganeti.Storage.Diskstats.Parser (diskstatsParser) where
29 29

  
30
import Control.Applicative ((<*>), (*>), (<*), (<$>))
30
import Control.Applicative ((<*>), (<*), (<$>))
31 31
import qualified Data.Attoparsec.Text as A
32 32
import qualified Data.Attoparsec.Combinator as AC
33 33
import Data.Attoparsec.Text (Parser)
34
import Data.Text (unpack)
35 34

  
35
import Ganeti.Parsers
36 36
import Ganeti.Storage.Diskstats.Types
37 37

  
38
-- * Utility functions
39

  
40
-- | Our own space-skipping function, because A.skipSpace also skips
41
-- newline characters. It skips ZERO or more spaces, so it does not
42
-- fail if there are no spaces.
43
skipSpaces :: Parser ()
44
skipSpaces = A.skipWhile A.isHorizontalSpace
45

  
46
-- | A parser recognizing a number preceeded by spaces.
47
numberP :: Parser Int
48
numberP = skipSpaces *> A.decimal
49

  
50
-- | A parser recognizing a word preceded by spaces, and closed by a space.
51
stringP :: Parser String
52
stringP = skipSpaces *> fmap unpack (A.takeWhile $ not . A.isHorizontalSpace)
53

  
54 38
-- * Parser implementation
55 39

  
56 40
-- | The parser for one line of the diskstatus file.
57 41
oneDiskstatsParser :: Parser Diskstats
58 42
oneDiskstatsParser =
59
  Diskstats <$> numberP <*> numberP <*> stringP <*> numberP <*> numberP
60
    <*> numberP <*> numberP <*> numberP <*> numberP <*> numberP <*> numberP
61
    <*> numberP <*> numberP <*> numberP <* A.endOfLine
43
  let majorP = numberP
44
      minorP = numberP
45
      nameP = stringP
46
      readsNumP = numberP
47
      mergedReadsP = numberP
48
      secReadP = numberP
49
      timeReadP = numberP
50
      writesP = numberP
51
      mergedWritesP = numberP
52
      secWrittenP = numberP
53
      timeWriteP = numberP
54
      iosP = numberP
55
      timeIOP = numberP
56
      wIOmillisP = numberP
57
  in
58
    Diskstats <$> majorP <*> minorP <*> nameP <*> readsNumP <*> mergedReadsP
59
      <*> secReadP <*> timeReadP <*> writesP <*> mergedWritesP <*> secWrittenP
60
      <*> timeWriteP <*> iosP <*> timeIOP <*> wIOmillisP <* A.endOfLine
62 61

  
63 62
-- | The parser for a whole diskstatus file.
64 63
diskstatsParser :: Parser [Diskstats]

Also available in: Unified diff