root / htools / Ganeti / HTools / Utils.hs @ bfe6c954
History | View | Annotate | Download (8.3 kB)
1 | 525bfb36 | Iustin Pop | {-| Utility functions. -} |
---|---|---|---|
2 | e4f08c46 | Iustin Pop | |
3 | e2fa2baf | Iustin Pop | {- |
4 | e2fa2baf | Iustin Pop | |
5 | e8230242 | Iustin Pop | Copyright (C) 2009, 2010, 2011 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 | adc5c176 | Iustin Pop | , debugFn |
28 | adc5c176 | Iustin Pop | , debugXy |
29 | 209b3711 | Iustin Pop | , sepSplit |
30 | 4715711d | Iustin Pop | , stdDev |
31 | bfe6c954 | Guido Trotter | , if' |
32 | bfe6c954 | Guido Trotter | , select |
33 | 3d7cd10b | Iustin Pop | , commaJoin |
34 | 9ba5c28f | Iustin Pop | , readEitherString |
35 | 28f19313 | Iustin Pop | , JSRecord |
36 | 9ba5c28f | Iustin Pop | , loadJSArray |
37 | 9ba5c28f | Iustin Pop | , fromObj |
38 | a810ad21 | Iustin Pop | , fromObjWithDefault |
39 | f36a8028 | Iustin Pop | , maybeFromObj |
40 | 117dc2d8 | Iustin Pop | , tryFromObj |
41 | 117dc2d8 | Iustin Pop | , fromJVal |
42 | 3f6af65c | Iustin Pop | , asJSObject |
43 | 3f6af65c | Iustin Pop | , asObjectList |
44 | 942403e6 | Iustin Pop | , fromJResult |
45 | 5b763470 | Iustin Pop | , tryRead |
46 | c5f7412e | Iustin Pop | , formatTable |
47 | 117dc2d8 | Iustin Pop | , annotateResult |
48 | c4d98278 | Iustin Pop | , defaultGroupID |
49 | 209b3711 | Iustin Pop | ) where |
50 | e4f08c46 | Iustin Pop | |
51 | f36a8028 | Iustin Pop | import Control.Monad (liftM) |
52 | 29ac5975 | Iustin Pop | import Data.List |
53 | a810ad21 | Iustin Pop | import Data.Maybe (fromMaybe) |
54 | 942403e6 | Iustin Pop | import qualified Text.JSON as J |
55 | 9ba5c28f | Iustin Pop | import Text.Printf (printf) |
56 | e4f08c46 | Iustin Pop | |
57 | e4f08c46 | Iustin Pop | import Debug.Trace |
58 | e4f08c46 | Iustin Pop | |
59 | 117dc2d8 | Iustin Pop | import Ganeti.HTools.Types |
60 | 117dc2d8 | Iustin Pop | |
61 | 9188aeef | Iustin Pop | -- * Debug functions |
62 | 9188aeef | Iustin Pop | |
63 | e4f08c46 | Iustin Pop | -- | To be used only for debugging, breaks referential integrity. |
64 | e4f08c46 | Iustin Pop | debug :: Show a => a -> a |
65 | e4f08c46 | Iustin Pop | debug x = trace (show x) x |
66 | e4f08c46 | Iustin Pop | |
67 | 525bfb36 | Iustin Pop | -- | Displays a modified form of the second parameter before returning |
68 | 525bfb36 | Iustin Pop | -- it. |
69 | adc5c176 | Iustin Pop | debugFn :: Show b => (a -> b) -> a -> a |
70 | adc5c176 | Iustin Pop | debugFn fn x = debug (fn x) `seq` x |
71 | adc5c176 | Iustin Pop | |
72 | 525bfb36 | Iustin Pop | -- | Show the first parameter before returning the second one. |
73 | adc5c176 | Iustin Pop | debugXy :: Show a => a -> b -> b |
74 | adc5c176 | Iustin Pop | debugXy a b = debug a `seq` b |
75 | adc5c176 | Iustin Pop | |
76 | 525bfb36 | Iustin Pop | -- * Miscellaneous |
77 | 1b7cf8ca | Iustin Pop | |
78 | e4f08c46 | Iustin Pop | -- | Comma-join a string list. |
79 | e4f08c46 | Iustin Pop | commaJoin :: [String] -> String |
80 | e4f08c46 | Iustin Pop | commaJoin = intercalate "," |
81 | e4f08c46 | Iustin Pop | |
82 | 748d5d50 | Iustin Pop | -- | Split a list on a separator and return an array. |
83 | 748d5d50 | Iustin Pop | sepSplit :: Eq a => a -> [a] -> [[a]] |
84 | e4f08c46 | Iustin Pop | sepSplit sep s |
85 | 748d5d50 | Iustin Pop | | null s = [] |
86 | 748d5d50 | Iustin Pop | | null xs = [x] |
87 | 748d5d50 | Iustin Pop | | null ys = [x,[]] |
88 | 748d5d50 | Iustin Pop | | otherwise = x:sepSplit sep ys |
89 | e4f08c46 | Iustin Pop | where (x, xs) = break (== sep) s |
90 | e4f08c46 | Iustin Pop | ys = drop 1 xs |
91 | e4f08c46 | Iustin Pop | |
92 | 9188aeef | Iustin Pop | -- * Mathematical functions |
93 | 9188aeef | Iustin Pop | |
94 | 185297fa | Iustin Pop | -- Simple and slow statistical functions, please replace with better |
95 | 185297fa | Iustin Pop | -- versions |
96 | e4f08c46 | Iustin Pop | |
97 | 525bfb36 | Iustin Pop | -- | Standard deviation function. |
98 | 4715711d | Iustin Pop | stdDev :: [Double] -> Double |
99 | 4715711d | Iustin Pop | stdDev lst = |
100 | 7570569e | Iustin Pop | -- first, calculate the list length and sum lst in a single step, |
101 | 7570569e | Iustin Pop | -- for performance reasons |
102 | 7570569e | Iustin Pop | let (ll', sx) = foldl' (\(rl, rs) e -> |
103 | 7570569e | Iustin Pop | let rl' = rl + 1 |
104 | 7570569e | Iustin Pop | rs' = rs + e |
105 | 7570569e | Iustin Pop | in rl' `seq` rs' `seq` (rl', rs')) (0::Int, 0) lst |
106 | 7570569e | Iustin Pop | ll = fromIntegral ll'::Double |
107 | 7570569e | Iustin Pop | mv = sx / ll |
108 | 7570569e | Iustin Pop | av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst |
109 | 4715711d | Iustin Pop | in sqrt (av / ll) -- stddev |
110 | dd4c56ed | Iustin Pop | |
111 | bfe6c954 | Guido Trotter | -- * Logical functions |
112 | bfe6c954 | Guido Trotter | |
113 | bfe6c954 | Guido Trotter | -- Avoid syntactic sugar and enhance readability. These functions are proposed |
114 | bfe6c954 | Guido Trotter | -- by some for inclusion in the Prelude, and at the moment they are present |
115 | bfe6c954 | Guido Trotter | -- (with various definitions) in the utility-ht package. Some rationale and |
116 | bfe6c954 | Guido Trotter | -- discussion is available at <http://www.haskell.org/haskellwiki/If-then-else> |
117 | bfe6c954 | Guido Trotter | |
118 | bfe6c954 | Guido Trotter | -- | \"if\" as a function, rather than as syntactic sugar. |
119 | bfe6c954 | Guido Trotter | if' :: Bool -- ^ condition |
120 | bfe6c954 | Guido Trotter | -> a -- ^ \"then\" result |
121 | bfe6c954 | Guido Trotter | -> a -- ^ \"else\" result |
122 | bfe6c954 | Guido Trotter | -> a -- ^ \"then\" or "else" result depending on the condition |
123 | bfe6c954 | Guido Trotter | if' True x _ = x |
124 | bfe6c954 | Guido Trotter | if' _ _ y = y |
125 | bfe6c954 | Guido Trotter | |
126 | bfe6c954 | Guido Trotter | -- | Return the first result with a True condition, or the default otherwise. |
127 | bfe6c954 | Guido Trotter | select :: a -- ^ default result |
128 | bfe6c954 | Guido Trotter | -> [(Bool, a)] -- ^ list of \"condition, result\" |
129 | bfe6c954 | Guido Trotter | -> a -- ^ first result which has a True condition, or default |
130 | bfe6c954 | Guido Trotter | select def = maybe def snd . find fst |
131 | bfe6c954 | Guido Trotter | |
132 | 9188aeef | Iustin Pop | -- * JSON-related functions |
133 | 9188aeef | Iustin Pop | |
134 | 525bfb36 | Iustin Pop | -- | A type alias for the list-based representation of J.JSObject. |
135 | 28f19313 | Iustin Pop | type JSRecord = [(String, J.JSValue)] |
136 | 28f19313 | Iustin Pop | |
137 | 9188aeef | Iustin Pop | -- | Converts a JSON Result into a monadic value. |
138 | c96d44df | Iustin Pop | fromJResult :: Monad m => String -> J.Result a -> m a |
139 | c96d44df | Iustin Pop | fromJResult s (J.Error x) = fail (s ++ ": " ++ x) |
140 | c96d44df | Iustin Pop | fromJResult _ (J.Ok x) = return x |
141 | 50d26669 | Iustin Pop | |
142 | 9188aeef | Iustin Pop | -- | Tries to read a string from a JSON value. |
143 | 9188aeef | Iustin Pop | -- |
144 | 9188aeef | Iustin Pop | -- In case the value was not a string, we fail the read (in the |
145 | 9188aeef | Iustin Pop | -- context of the current monad. |
146 | 5aa48dbe | Iustin Pop | readEitherString :: (Monad m) => J.JSValue -> m String |
147 | 9ba5c28f | Iustin Pop | readEitherString v = |
148 | 9ba5c28f | Iustin Pop | case v of |
149 | 5aa48dbe | Iustin Pop | J.JSString s -> return $ J.fromJSString s |
150 | 5aa48dbe | Iustin Pop | _ -> fail "Wrong JSON type" |
151 | 9ba5c28f | Iustin Pop | |
152 | 9188aeef | Iustin Pop | -- | Converts a JSON message into an array of JSON objects. |
153 | c8b662f1 | Iustin Pop | loadJSArray :: (Monad m) |
154 | c8b662f1 | Iustin Pop | => String -- ^ Operation description (for error reporting) |
155 | c8b662f1 | Iustin Pop | -> String -- ^ Input message |
156 | c8b662f1 | Iustin Pop | -> m [J.JSObject J.JSValue] |
157 | c96d44df | Iustin Pop | loadJSArray s = fromJResult s . J.decodeStrict |
158 | 9ba5c28f | Iustin Pop | |
159 | f36a8028 | Iustin Pop | -- | Reads the value of a key in a JSON object. |
160 | 28f19313 | Iustin Pop | fromObj :: (J.JSON a, Monad m) => JSRecord -> String -> m a |
161 | e8230242 | Iustin Pop | fromObj o k = |
162 | 262f3e6c | Iustin Pop | case lookup k o of |
163 | 2befdc14 | Iustin Pop | Nothing -> fail $ printf "key '%s' not found, object contains only %s" |
164 | 2befdc14 | Iustin Pop | k (show (map fst o)) |
165 | f36a8028 | Iustin Pop | Just val -> fromKeyValue k val |
166 | f36a8028 | Iustin Pop | |
167 | f36a8028 | Iustin Pop | -- | Reads the value of an optional key in a JSON object. |
168 | e8230242 | Iustin Pop | maybeFromObj :: (J.JSON a, Monad m) => |
169 | 28f19313 | Iustin Pop | JSRecord -> String -> m (Maybe a) |
170 | e8230242 | Iustin Pop | maybeFromObj o k = |
171 | f36a8028 | Iustin Pop | case lookup k o of |
172 | f36a8028 | Iustin Pop | Nothing -> return Nothing |
173 | f36a8028 | Iustin Pop | Just val -> liftM Just (fromKeyValue k val) |
174 | f36a8028 | Iustin Pop | |
175 | a810ad21 | Iustin Pop | -- | Reads the value of a key in a JSON object with a default if missing. |
176 | a810ad21 | Iustin Pop | fromObjWithDefault :: (J.JSON a, Monad m) => |
177 | 28f19313 | Iustin Pop | JSRecord -> String -> a -> m a |
178 | a810ad21 | Iustin Pop | fromObjWithDefault o k d = liftM (fromMaybe d) $ maybeFromObj o k |
179 | a810ad21 | Iustin Pop | |
180 | 525bfb36 | Iustin Pop | -- | Reads a JValue, that originated from an object key. |
181 | f36a8028 | Iustin Pop | fromKeyValue :: (J.JSON a, Monad m) |
182 | f36a8028 | Iustin Pop | => String -- ^ The key name |
183 | f36a8028 | Iustin Pop | -> J.JSValue -- ^ The value to read |
184 | f36a8028 | Iustin Pop | -> m a |
185 | f36a8028 | Iustin Pop | fromKeyValue k val = |
186 | f36a8028 | Iustin Pop | fromJResult (printf "key '%s', value '%s'" k (show val)) (J.readJSON val) |
187 | 9ba5c28f | Iustin Pop | |
188 | 525bfb36 | Iustin Pop | -- | Annotate a Result with an ownership information. |
189 | 117dc2d8 | Iustin Pop | annotateResult :: String -> Result a -> Result a |
190 | 117dc2d8 | Iustin Pop | annotateResult owner (Bad s) = Bad $ owner ++ ": " ++ s |
191 | 117dc2d8 | Iustin Pop | annotateResult _ v = v |
192 | 117dc2d8 | Iustin Pop | |
193 | 117dc2d8 | Iustin Pop | -- | Try to extract a key from a object with better error reporting |
194 | 525bfb36 | Iustin Pop | -- than fromObj. |
195 | 117dc2d8 | Iustin Pop | tryFromObj :: (J.JSON a) => |
196 | 28f19313 | Iustin Pop | String -- ^ Textual "owner" in error messages |
197 | 28f19313 | Iustin Pop | -> JSRecord -- ^ The object array |
198 | 28f19313 | Iustin Pop | -> String -- ^ The desired key from the object |
199 | a083e855 | Iustin Pop | -> Result a |
200 | e8230242 | Iustin Pop | tryFromObj t o = annotateResult t . fromObj o |
201 | 117dc2d8 | Iustin Pop | |
202 | 117dc2d8 | Iustin Pop | -- | Small wrapper over readJSON. |
203 | 117dc2d8 | Iustin Pop | fromJVal :: (Monad m, J.JSON a) => J.JSValue -> m a |
204 | 117dc2d8 | Iustin Pop | fromJVal v = |
205 | 117dc2d8 | Iustin Pop | case J.readJSON v of |
206 | 39420403 | Iustin Pop | J.Error s -> fail ("Cannot convert value '" ++ show v ++ |
207 | 39420403 | Iustin Pop | "', error: " ++ s) |
208 | 117dc2d8 | Iustin Pop | J.Ok x -> return x |
209 | 117dc2d8 | Iustin Pop | |
210 | 9188aeef | Iustin Pop | -- | Converts a JSON value into a JSON object. |
211 | 5aa48dbe | Iustin Pop | asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue) |
212 | 5aa48dbe | Iustin Pop | asJSObject (J.JSObject a) = return a |
213 | 5aa48dbe | Iustin Pop | asJSObject _ = fail "not an object" |
214 | 942403e6 | Iustin Pop | |
215 | 9188aeef | Iustin Pop | -- | Coneverts a list of JSON values into a list of JSON objects. |
216 | 5aa48dbe | Iustin Pop | asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue] |
217 | 9f6dcdea | Iustin Pop | asObjectList = mapM asJSObject |
218 | 5b763470 | Iustin Pop | |
219 | 5b763470 | Iustin Pop | -- * Parsing utility functions |
220 | 5b763470 | Iustin Pop | |
221 | 525bfb36 | Iustin Pop | -- | Parse results from readsPrec. |
222 | 5b763470 | Iustin Pop | parseChoices :: (Monad m, Read a) => String -> String -> [(a, String)] -> m a |
223 | 5b763470 | Iustin Pop | parseChoices _ _ ((v, ""):[]) = return v |
224 | 5b763470 | Iustin Pop | parseChoices name s ((_, e):[]) = |
225 | 5b763470 | Iustin Pop | fail $ name ++ ": leftover characters when parsing '" |
226 | 5b763470 | Iustin Pop | ++ s ++ "': '" ++ e ++ "'" |
227 | 5b763470 | Iustin Pop | parseChoices name s _ = fail $ name ++ ": cannot parse string '" ++ s ++ "'" |
228 | 5b763470 | Iustin Pop | |
229 | 5b763470 | Iustin Pop | -- | Safe 'read' function returning data encapsulated in a Result. |
230 | 5b763470 | Iustin Pop | tryRead :: (Monad m, Read a) => String -> String -> m a |
231 | 5b763470 | Iustin Pop | tryRead name s = parseChoices name s $ reads s |
232 | c5f7412e | Iustin Pop | |
233 | 525bfb36 | Iustin Pop | -- | Format a table of strings to maintain consistent length. |
234 | c5f7412e | Iustin Pop | formatTable :: [[String]] -> [Bool] -> [[String]] |
235 | c5f7412e | Iustin Pop | formatTable vals numpos = |
236 | c5f7412e | Iustin Pop | let vtrans = transpose vals -- transpose, so that we work on rows |
237 | c5f7412e | Iustin Pop | -- rather than columns |
238 | c5f7412e | Iustin Pop | mlens = map (maximum . map length) vtrans |
239 | c5f7412e | Iustin Pop | expnd = map (\(flds, isnum, ml) -> |
240 | c5f7412e | Iustin Pop | map (\val -> |
241 | c5f7412e | Iustin Pop | let delta = ml - length val |
242 | c5f7412e | Iustin Pop | filler = replicate delta ' ' |
243 | c5f7412e | Iustin Pop | in if delta > 0 |
244 | c5f7412e | Iustin Pop | then if isnum |
245 | c5f7412e | Iustin Pop | then filler ++ val |
246 | c5f7412e | Iustin Pop | else val ++ filler |
247 | c5f7412e | Iustin Pop | else val |
248 | c5f7412e | Iustin Pop | ) flds |
249 | c5f7412e | Iustin Pop | ) (zip3 vtrans numpos mlens) |
250 | c5f7412e | Iustin Pop | in transpose expnd |
251 | 9b9da389 | Iustin Pop | |
252 | 525bfb36 | Iustin Pop | -- | Default group UUID (just a string, not a real UUID). |
253 | c4d98278 | Iustin Pop | defaultGroupID :: GroupID |
254 | c4d98278 | Iustin Pop | defaultGroupID = "00000000-0000-0000-0000-000000000000" |