Add support for building without curl
[ganeti-local] / Ganeti / HTools / Utils.hs
1 {-| Utility functions -}
2
3 {-
4
5 Copyright (C) 2009 Google Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.
21
22 -}
23
24 module Ganeti.HTools.Utils
25     (
26       debug
27     , sepSplit
28     , fst3
29     , varianceCoeff
30     , commaJoin
31     , readEitherString
32     , loadJSArray
33     , fromObj
34     , asJSObject
35     , asObjectList
36     , fromJResult
37     ) where
38
39 import Data.List
40 import Control.Monad
41 import qualified Text.JSON as J
42 import Text.Printf (printf)
43
44 import Debug.Trace
45
46 -- * Debug functions
47
48 -- | To be used only for debugging, breaks referential integrity.
49 debug :: Show a => a -> a
50 debug x = trace (show x) x
51
52 -- * Miscelaneous
53
54 -- | Comma-join a string list.
55 commaJoin :: [String] -> String
56 commaJoin = intercalate ","
57
58 -- | Split a string on a separator and return an array.
59 sepSplit :: Char -> String -> [String]
60 sepSplit sep s
61     | x == "" && xs == [] = []
62     | xs == []            = [x]
63     | ys == []            = [x,""]
64     | otherwise           = x:sepSplit sep ys
65     where (x, xs) = break (== sep) s
66           ys = drop 1 xs
67
68 -- | Simple version of 'fst' for a triple
69 fst3 :: (a, b, c) -> a
70 fst3 (a, _, _) = a
71
72 -- * Mathematical functions
73
74 -- Simple and slow statistical functions, please replace with better versions
75
76 -- | Mean value of a list.
77 meanValue :: Floating a => [a] -> a
78 meanValue lst = sum lst / fromIntegral (length lst)
79
80 -- | Standard deviation.
81 stdDev :: Floating a => [a] -> a
82 stdDev lst =
83     let mv = meanValue lst
84         av = foldl' (\accu elem -> let d = elem - mv in accu + d * d) 0.0 lst
85         bv = sqrt (av / fromIntegral (length lst))
86     in bv
87
88 -- | Coefficient of variation.
89 varianceCoeff :: Floating a => [a] -> a
90 varianceCoeff lst = stdDev lst / fromIntegral (length lst)
91
92 -- * JSON-related functions
93
94 -- | Converts a JSON Result into a monadic value.
95 fromJResult :: Monad m => J.Result a -> m a
96 fromJResult (J.Error x) = fail x
97 fromJResult (J.Ok x) = return x
98
99 -- | Tries to read a string from a JSON value.
100 --
101 -- In case the value was not a string, we fail the read (in the
102 -- context of the current monad.
103 readEitherString :: (Monad m) => J.JSValue -> m String
104 readEitherString v =
105     case v of
106       J.JSString s -> return $ J.fromJSString s
107       _ -> fail "Wrong JSON type"
108
109 -- | Converts a JSON message into an array of JSON objects.
110 loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
111 loadJSArray = fromJResult . J.decodeStrict
112
113 -- | Reads a the value of a key in a JSON object.
114 fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
115 fromObj k o =
116     case lookup k (J.fromJSObject o) of
117       Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
118       Just val -> fromJResult $ J.readJSON val
119
120 -- | Converts a JSON value into a JSON object.
121 asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
122 asJSObject (J.JSObject a) = return a
123 asJSObject _ = fail "not an object"
124
125 -- | Coneverts a list of JSON values into a list of JSON objects.
126 asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
127 asObjectList = mapM asJSObject