Make IAlloc.loadData return maps
[ganeti-local] / Ganeti / HTools / Utils.hs
1 {-| Utility functions -}
2
3 module Ganeti.HTools.Utils
4     (
5       debug
6     , sepSplit
7     , varianceCoeff
8     , readData
9     , commaJoin
10     , readEitherString
11     , loadJSArray
12     , fromObj
13     , asJSObject
14     , asObjectList
15     , fromJResult
16     ) where
17
18 import Data.List
19 import Control.Monad
20 import System
21 import System.IO
22 import qualified Text.JSON as J
23 import Text.Printf (printf)
24
25 import Ganeti.HTools.Types
26
27 import Debug.Trace
28
29 -- | To be used only for debugging, breaks referential integrity.
30 debug :: Show a => a -> a
31 debug x = trace (show x) x
32
33
34 fromJResult :: Monad m => J.Result a -> m a
35 fromJResult (J.Error x) = fail x
36 fromJResult (J.Ok x) = return x
37
38 -- | Comma-join a string list.
39 commaJoin :: [String] -> String
40 commaJoin = intercalate ","
41
42 -- | Split a string on a separator and return an array.
43 sepSplit :: Char -> String -> [String]
44 sepSplit sep s
45     | x == "" && xs == [] = []
46     | xs == []            = [x]
47     | ys == []            = x:"":[]
48     | otherwise           = x:(sepSplit sep ys)
49     where (x, xs) = break (== sep) s
50           ys = drop 1 xs
51
52 -- | Partial application of sepSplit to @'.'@
53 commaSplit :: String -> [String]
54 commaSplit = sepSplit ','
55
56 -- Simple and slow statistical functions, please replace with better versions
57
58 -- | Mean value of a list.
59 meanValue :: Floating a => [a] -> a
60 meanValue lst = (sum lst) / (fromIntegral $ length lst)
61
62 -- | Standard deviation.
63 stdDev :: Floating a => [a] -> a
64 stdDev lst =
65     let mv = meanValue lst
66         square = (^ (2::Int)) -- silences "defaulting the constraint..."
67         av = sum $ map square $ map (\e -> e - mv) lst
68         bv = sqrt (av / (fromIntegral $ length lst))
69     in bv
70
71 -- | Coefficient of variation.
72 varianceCoeff :: Floating a => [a] -> a
73 varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst)
74
75 -- | Get an Ok result or print the error and exit
76 readData :: Result a -> IO a
77 readData nd =
78     (case nd of
79        Bad x -> do
80          putStrLn x
81          exitWith $ ExitFailure 1
82        Ok x -> return x)
83
84 readEitherString :: (Monad m) => J.JSValue -> m String
85 readEitherString v =
86     case v of
87       J.JSString s -> return $ J.fromJSString s
88       _ -> fail "Wrong JSON type"
89
90 loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
91 loadJSArray s = fromJResult $ J.decodeStrict s
92
93 fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
94 fromObj k o =
95     case lookup k (J.fromJSObject o) of
96       Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
97       Just val -> fromJResult $ J.readJSON val
98
99 asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
100 asJSObject (J.JSObject a) = return a
101 asJSObject _ = fail "not an object"
102
103 asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
104 asObjectList = sequence . map asJSObject