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