1 {-| Utility functions -}
3 module Ganeti.HTools.Utils
22 import qualified Text.JSON as J
23 import Text.Printf (printf)
25 import Ganeti.HTools.Types
31 -- | To be used only for debugging, breaks referential integrity.
32 debug :: Show a => a -> a
33 debug x = trace (show x) x
37 -- | Comma-join a string list.
38 commaJoin :: [String] -> String
39 commaJoin = intercalate ","
41 -- | Split a string on a separator and return an array.
42 sepSplit :: Char -> String -> [String]
44 | x == "" && xs == [] = []
47 | otherwise = x:(sepSplit sep ys)
48 where (x, xs) = break (== sep) s
51 -- | Partial application of sepSplit to @'.'@
52 commaSplit :: String -> [String]
53 commaSplit = sepSplit ','
55 -- * Mathematical functions
57 -- Simple and slow statistical functions, please replace with better versions
59 -- | Mean value of a list.
60 meanValue :: Floating a => [a] -> a
61 meanValue lst = (sum lst) / (fromIntegral $ length lst)
63 -- | Standard deviation.
64 stdDev :: Floating a => [a] -> a
66 let mv = meanValue lst
67 square = (^ (2::Int)) -- silences "defaulting the constraint..."
68 av = sum $ map square $ map (\e -> e - mv) lst
69 bv = sqrt (av / (fromIntegral $ length lst))
72 -- | Coefficient of variation.
73 varianceCoeff :: Floating a => [a] -> a
74 varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst)
76 -- | Get an Ok result or print the error and exit.
77 readData :: Result a -> IO a
82 exitWith $ ExitFailure 1
85 -- * JSON-related functions
87 -- | Converts a JSON Result into a monadic value.
88 fromJResult :: Monad m => J.Result a -> m a
89 fromJResult (J.Error x) = fail x
90 fromJResult (J.Ok x) = return x
92 -- | Tries to read a string from a JSON value.
94 -- In case the value was not a string, we fail the read (in the
95 -- context of the current monad.
96 readEitherString :: (Monad m) => J.JSValue -> m String
99 J.JSString s -> return $ J.fromJSString s
100 _ -> fail "Wrong JSON type"
102 -- | Converts a JSON message into an array of JSON objects.
103 loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
104 loadJSArray s = fromJResult $ J.decodeStrict s
106 -- | Reads a the value of a key in a JSON object.
107 fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
109 case lookup k (J.fromJSObject o) of
110 Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
111 Just val -> fromJResult $ J.readJSON val
113 -- | Converts a JSON value into a JSON object.
114 asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
115 asJSObject (J.JSObject a) = return a
116 asJSObject _ = fail "not an object"
118 -- | Coneverts a list of JSON values into a list of JSON objects.
119 asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
120 asObjectList = sequence . map asJSObject