Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / DataCollectors / Types.hs @ a895fa19

History | View | Annotate | Download (2.2 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Implementation of the Ganeti data collector types.
4

    
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2012, 2013 Google Inc.
10

    
11
This program is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published by
13
the Free Software Foundation; either version 2 of the License, or
14
(at your option) any later version.
15

    
16
This program is distributed in the hope that it will be useful, but
17
WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
General Public License for more details.
20

    
21
You should have received a copy of the GNU General Public License
22
along with this program; if not, write to the Free Software
23
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
02110-1301, USA.
25

    
26
-}
27

    
28
module Ganeti.DataCollectors.Types
29
  ( DCReport(..)
30
  , DCVersion(..)
31
  , buildReport
32
  ) where
33

    
34
import Text.JSON
35

    
36
import Ganeti.Constants as C
37
import Ganeti.THH
38
import Ganeti.Utils (getCurrentTime)
39

    
40
-- | Type representing the version number of a data collector.
41
data DCVersion = DCVerBuiltin | DCVersion String deriving (Show, Eq)
42

    
43
-- | The JSON instance for DCVersion.
44
instance JSON DCVersion where
45
  showJSON DCVerBuiltin = showJSON C.builtinDataCollectorVersion
46
  showJSON (DCVersion v) = showJSON v
47
  readJSON = error "JSON read instance not implemented for type DCVersion"
48

    
49
-- | This is the format of the report produced by each data collector.
50
$(buildObject "DCReport" "dcReport"
51
  [ simpleField "name"           [t| String |]
52
  , simpleField "version"        [t| DCVersion |]
53
  , simpleField "format_version" [t| Int |]
54
  , simpleField "timestamp"      [t| Integer |]
55
  , simpleField "data"           [t| JSValue |]
56
  ])
57

    
58
-- | Utility function for building a report automatically adding the current
59
-- timestamp (rounded up to seconds).
60
-- If the version is not specified, it will be set to the value indicating
61
-- a builtin collector.
62
buildReport :: String -> DCVersion -> Int -> JSValue -> IO DCReport
63
buildReport name version format_version jsonData = do
64
  now <- getCurrentTime
65
  let timestamp = now * 1000000000 :: Integer
66
  return $ DCReport name version format_version timestamp jsonData