From 13d3acabe126e10db395228bfcb9ad4c99187a63 Mon Sep 17 00:00:00 2001
From: Michele Tartara <mtartara@google.com>
Date: Tue, 8 Jan 2013 14:12:21 +0000
Subject: [PATCH] Add reporting infrastructure for data collectors

This commit adds the part of the JSON report generation code that will be
common to all the data collectors, according to the format specified in the
design document.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 Makefile.am                        |  1 +
 lib/constants.py                   |  3 ++
 src/Ganeti/DataCollectors/Types.hs | 58 ++++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+)
 create mode 100644 src/Ganeti/DataCollectors/Types.hs

diff --git a/Makefile.am b/Makefile.am
index 4150e8d9c..3eba4ea63 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -487,6 +487,7 @@ HS_LIB_SRCS = \
 	src/Ganeti/DataCollectors/CLI.hs \
 	src/Ganeti/DataCollectors/Drbd.hs \
 	src/Ganeti/DataCollectors/Program.hs \
+	src/Ganeti/DataCollectors/Types.hs \
 	src/Ganeti/Errors.hs \
 	src/Ganeti/HTools/Backend/IAlloc.hs \
 	src/Ganeti/HTools/Backend/Luxi.hs \
diff --git a/lib/constants.py b/lib/constants.py
index 20c6495b0..d62834bc3 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -2315,5 +2315,8 @@ AUTO_REPAIR_ALL_RESULTS = frozenset([
     AUTO_REPAIR_ENOPERM,
 ])
 
+# The version identifier for builtin data collectors
+BUILTIN_DATA_COLLECTOR_VERSION = "B"
+
 # Do not re-export imported modules
 del re, _vcsversion, _autoconf, socket, pathutils, compat
diff --git a/src/Ganeti/DataCollectors/Types.hs b/src/Ganeti/DataCollectors/Types.hs
new file mode 100644
index 000000000..c423a47e6
--- /dev/null
+++ b/src/Ganeti/DataCollectors/Types.hs
@@ -0,0 +1,58 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+{-| Implementation of the Ganeti data collector types.
+
+-}
+
+{-
+
+Copyright (C) 2012 Google Inc.
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA.
+
+-}
+
+module Ganeti.DataCollectors.Types
+  ( DCReport(..)
+  , buildReport
+  ) where
+
+import Data.Maybe
+import Text.JSON
+
+import Ganeti.Constants as C
+import Ganeti.THH
+import Ganeti.Utils (getCurrentTime)
+
+-- | This is the format of the report produced by each data collector.
+$(buildObject "DCReport" "dcReport"
+  [ simpleField "name"           [t| String |]
+  , simpleField "version"        [t| String |]
+  , simpleField "format_version" [t| Int |]
+  , simpleField "timestamp"      [t| Integer |]
+  , simpleField "data"           [t| JSValue |]
+  ])
+
+-- | Utility function for building a report automatically adding the current
+-- timestamp (rounded up to seconds).
+-- If the version is not specified, it will be set to the value indicating
+-- a builtin collector.
+buildReport :: String -> Maybe String -> Int -> JSValue -> IO DCReport
+buildReport name version format_version jsonData = do
+  now <- getCurrentTime
+  let timestamp = now * 1000000000 :: Integer
+      ver = fromMaybe C.builtinDataCollectorVersion version
+  return $ DCReport name ver format_version timestamp jsonData
-- 
GitLab