module Ganeti.DataCollectors.Types
( addStatus
, DCCategory(..)
, DCKind(..)
, DCReport(..)
, DCStatus(..)
, DCStatusCode(..)
, DCVersion(..)
, buildReport
) where
import Data.Char
import Text.JSON
import Ganeti.Constants as C
import Ganeti.THH
import Ganeti.Utils (getCurrentTime)
data DCCategory = DCInstance | DCStorage | DCDaemon | DCHypervisor
deriving (Show, Eq)
instance JSON DCCategory where
showJSON = showJSON . map toLower . drop 2 . show
readJSON =
error "JSON read instance not implemented for type DCCategory"
data DCStatusCode = DCSCOk
| DCSCTempBad
| DCSCUnknown
| DCSCBad
deriving (Show, Eq, Ord)
instance JSON DCStatusCode where
showJSON DCSCOk = showJSON (0 :: Int)
showJSON DCSCTempBad = showJSON (1 :: Int)
showJSON DCSCUnknown = showJSON (2 :: Int)
showJSON DCSCBad = showJSON (4 :: Int)
readJSON = error "JSON read instance not implemented for type DCStatusCode"
$(buildObject "DCStatus" "dcStatus"
[ simpleField "code" [t| DCStatusCode |]
, simpleField "message" [t| String |]
])
data DCKind = DCKPerf
| DCKStatus
deriving (Show, Eq)
instance JSON DCKind where
showJSON DCKPerf = showJSON (0 :: Int)
showJSON DCKStatus = showJSON (1 :: Int)
readJSON = error "JSON read instance not implemented for type DCKind"
data DCVersion = DCVerBuiltin | DCVersion String deriving (Show, Eq)
instance JSON DCVersion where
showJSON DCVerBuiltin = showJSON C.builtinDataCollectorVersion
showJSON (DCVersion v) = showJSON v
readJSON = error "JSON read instance not implemented for type DCVersion"
$(buildObject "DCReport" "dcReport"
[ simpleField "name" [t| String |]
, simpleField "version" [t| DCVersion |]
, simpleField "format_version" [t| Int |]
, simpleField "timestamp" [t| Integer |]
, optionalNullSerField $
simpleField "category" [t| DCCategory |]
, simpleField "kind" [t| DCKind |]
, simpleField "data" [t| JSValue |]
])
addStatus :: DCStatus -> JSValue -> JSValue
addStatus dcStatus (JSObject obj) =
makeObj $ ("status", showJSON dcStatus) : fromJSObject obj
addStatus dcStatus value = makeObj
[ ("status", showJSON dcStatus)
, ("data", value)
]
buildReport :: String -> DCVersion -> Int -> Maybe DCCategory -> DCKind
-> JSValue -> IO DCReport
buildReport name version format_version category kind jsonData = do
now <- getCurrentTime
let timestamp = now * 1000000000 :: Integer
return $
DCReport name version format_version timestamp category kind
jsonData