Revision e5fe8978 src/Ganeti/DataCollectors/Types.hs

b/src/Ganeti/DataCollectors/Types.hs
41 41
  ) where
42 42

  
43 43
import Data.Char
44
import Data.Ratio
44 45
import qualified Data.Map as Map
45 46
import qualified Data.Sequence as Seq
46 47
import Text.JSON
......
51 52

  
52 53
-- | The possible classes a data collector can belong to.
53 54
data DCCategory = DCInstance | DCStorage | DCDaemon | DCHypervisor
54
  deriving (Show, Eq)
55
  deriving (Show, Eq, Read)
55 56

  
56 57
-- | Get the category name and return it as a string.
57 58
getCategoryName :: DCCategory -> String
58 59
getCategoryName dcc = map toLower . drop 2 . show $ dcc
59 60

  
61
categoryNames :: Map.Map String DCCategory
62
categoryNames =
63
  let l = [DCInstance, DCStorage, DCDaemon, DCHypervisor]
64
  in Map.fromList $ zip (map getCategoryName l) l
65

  
60 66
-- | The JSON instance for DCCategory.
61 67
instance JSON DCCategory where
62 68
  showJSON = showJSON . getCategoryName
63
  readJSON =
64
    error "JSON read instance not implemented for type DCCategory"
69
  readJSON (JSString s) =
70
    let s' = fromJSString s
71
    in case Map.lookup s' categoryNames of
72
         Just category -> Ok category
73
         Nothing -> fail $ "Invalid category name " ++ s' ++ " for type\
74
                           \ DCCategory"
75
  readJSON v = fail $ "Invalid JSON value " ++ show v ++ " for type DCCategory"
65 76

  
66 77
-- | The possible status codes of a data collector.
67 78
data DCStatusCode = DCSCOk      -- ^ Everything is OK
......
93 104
instance JSON DCKind where
94 105
  showJSON DCKPerf   = showJSON (0 :: Int)
95 106
  showJSON DCKStatus = showJSON (1 :: Int)
96
  readJSON = error "JSON read instance not implemented for type DCKind"
107
  readJSON (JSRational _ x) =
108
    if denominator x /= 1
109
    then fail $ "Invalid JSON value " ++ show x ++ " for type DCKind"
110
    else
111
      let x' = (fromIntegral . numerator $ x) :: Int
112
      in if x' == 0 then Ok DCKPerf
113
         else if x' == 1 then Ok DCKStatus
114
         else fail $ "Invalid JSON value " ++ show x' ++ " for type DCKind"
115
  readJSON v = fail $ "Invalid JSON value " ++ show v ++ " for type DCKind"
97 116

  
98 117
-- | Type representing the version number of a data collector.
99 118
data DCVersion = DCVerBuiltin | DCVersion String deriving (Show, Eq)
......
102 121
instance JSON DCVersion where
103 122
  showJSON DCVerBuiltin = showJSON C.builtinDataCollectorVersion
104 123
  showJSON (DCVersion v) = showJSON v
105
  readJSON = error "JSON read instance not implemented for type DCVersion"
124
  readJSON (JSString s) =
125
    if fromJSString s == C.builtinDataCollectorVersion
126
    then Ok DCVerBuiltin else Ok . DCVersion $ fromJSString s
127
  readJSON v = fail $ "Invalid JSON value " ++ show v ++ " for type DCVersion"
106 128

  
107 129
-- | Type for the value field of the above map.
108 130
data CollectorData = CPULoadData (Seq.Seq (Integer, [Int]))

Also available in: Unified diff