1 {-| Utility functions -}
3 module Ganeti.HTools.Utils
31 import qualified Text.JSON as J
32 import Text.Printf (printf)
36 -- | To be used only for debugging, breaks referential integrity.
37 debug :: Show a => a -> a
38 debug x = trace (show x) x
43 This is similar to the JSON library Result type - *very* similar, but
44 we want to use it in multiple places, so we abstract it into a
54 instance Monad Result where
55 (>>=) (Bad x) _ = Bad x
56 (>>=) (Ok x) fn = fn x
60 fromJResult :: Monad m => J.Result a -> m a
61 fromJResult (J.Error x) = fail x
62 fromJResult (J.Ok x) = return x
64 -- | Comma-join a string list.
65 commaJoin :: [String] -> String
66 commaJoin = intercalate ","
68 -- | Split a string on a separator and return an array.
69 sepSplit :: Char -> String -> [String]
71 | x == "" && xs == [] = []
74 | otherwise = x:(sepSplit sep ys)
75 where (x, xs) = break (== sep) s
78 -- | Partial application of sepSplit to @'.'@
79 commaSplit :: String -> [String]
80 commaSplit = sepSplit ','
82 -- | Swap a list of @(a, b)@ into @(b, a)@
83 swapPairs :: [(a, b)] -> [(b, a)]
84 swapPairs = map (\ (a, b) -> (b, a))
86 -- Simple and slow statistical functions, please replace with better versions
88 -- | Mean value of a list.
89 meanValue :: Floating a => [a] -> a
90 meanValue lst = (sum lst) / (fromIntegral $ length lst)
92 -- | Standard deviation.
93 stdDev :: Floating a => [a] -> a
95 let mv = meanValue lst
96 square = (^ (2::Int)) -- silences "defaulting the constraint..."
97 av = sum $ map square $ map (\e -> e - mv) lst
98 bv = sqrt (av / (fromIntegral $ length lst))
101 -- | Coefficient of variation.
102 varianceCoeff :: Floating a => [a] -> a
103 varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst)
105 -- | Get an Ok result or print the error and exit
106 readData :: Result a -> IO a
111 exitWith $ ExitFailure 1
114 readEitherString :: (Monad m) => J.JSValue -> m String
117 J.JSString s -> return $ J.fromJSString s
118 _ -> fail "Wrong JSON type"
120 loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
121 loadJSArray s = fromJResult $ J.decodeStrict s
123 fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
125 case lookup k (J.fromJSObject o) of
126 Nothing -> fail $ printf "key '%s' not found" k
127 Just val -> fromJResult $ J.readJSON val
129 getStringElement :: (Monad m) => String -> J.JSObject J.JSValue -> m String
130 getStringElement = fromObj
132 getIntElement :: (Monad m) => String -> J.JSObject J.JSValue -> m Int
133 getIntElement = fromObj
135 getBoolElement :: (Monad m) => String -> J.JSObject J.JSValue -> m Bool
136 getBoolElement = fromObj
138 getListElement :: (Monad m) => String -> J.JSObject J.JSValue -> m [J.JSValue]
139 getListElement = fromObj
141 getObjectElement :: (Monad m) => String -> J.JSObject J.JSValue
142 -> m (J.JSObject J.JSValue)
143 getObjectElement = fromObj
145 asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
146 asJSObject (J.JSObject a) = return a
147 asJSObject _ = fail "not an object"
149 asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
150 asObjectList = sequence . map asJSObject
152 -- | Function to concat two strings with a separator under a monad
153 (|+) :: (Monad m) => m String -> m String -> m String
154 (|+) = liftM2 (\x y -> x ++ "|" ++ y)