Revision 525bfb36 htools/Ganeti/HTools/Utils.hs
b/htools/Ganeti/HTools/Utils.hs | ||
---|---|---|
1 |
{-| Utility functions -} |
|
1 |
{-| Utility functions. -}
|
|
2 | 2 |
|
3 | 3 |
{- |
4 | 4 |
|
... | ... | |
62 | 62 |
debug :: Show a => a -> a |
63 | 63 |
debug x = trace (show x) x |
64 | 64 |
|
65 |
-- | Displays a modified form of the second parameter before returning it |
|
65 |
-- | Displays a modified form of the second parameter before returning |
|
66 |
-- it. |
|
66 | 67 |
debugFn :: Show b => (a -> b) -> a -> a |
67 | 68 |
debugFn fn x = debug (fn x) `seq` x |
68 | 69 |
|
69 |
-- | Show the first parameter before returning the second one |
|
70 |
-- | Show the first parameter before returning the second one.
|
|
70 | 71 |
debugXy :: Show a => a -> b -> b |
71 | 72 |
debugXy a b = debug a `seq` b |
72 | 73 |
|
73 |
-- * Miscelaneous |
|
74 |
-- * Miscellaneous
|
|
74 | 75 |
|
75 | 76 |
-- | Comma-join a string list. |
76 | 77 |
commaJoin :: [String] -> String |
... | ... | |
91 | 92 |
-- Simple and slow statistical functions, please replace with better |
92 | 93 |
-- versions |
93 | 94 |
|
94 |
-- | Standard deviation function |
|
95 |
-- | Standard deviation function.
|
|
95 | 96 |
stdDev :: [Double] -> Double |
96 | 97 |
stdDev lst = |
97 | 98 |
-- first, calculate the list length and sum lst in a single step, |
... | ... | |
107 | 108 |
|
108 | 109 |
-- * JSON-related functions |
109 | 110 |
|
110 |
-- | A type alias for the list-based representation of J.JSObject |
|
111 |
-- | A type alias for the list-based representation of J.JSObject.
|
|
111 | 112 |
type JSRecord = [(String, J.JSValue)] |
112 | 113 |
|
113 | 114 |
-- | Converts a JSON Result into a monadic value. |
... | ... | |
153 | 154 |
JSRecord -> String -> a -> m a |
154 | 155 |
fromObjWithDefault o k d = liftM (fromMaybe d) $ maybeFromObj o k |
155 | 156 |
|
156 |
-- | Reads a JValue, that originated from an object key |
|
157 |
-- | Reads a JValue, that originated from an object key.
|
|
157 | 158 |
fromKeyValue :: (J.JSON a, Monad m) |
158 | 159 |
=> String -- ^ The key name |
159 | 160 |
-> J.JSValue -- ^ The value to read |
... | ... | |
161 | 162 |
fromKeyValue k val = |
162 | 163 |
fromJResult (printf "key '%s', value '%s'" k (show val)) (J.readJSON val) |
163 | 164 |
|
164 |
-- | Annotate a Result with an ownership information |
|
165 |
-- | Annotate a Result with an ownership information.
|
|
165 | 166 |
annotateResult :: String -> Result a -> Result a |
166 | 167 |
annotateResult owner (Bad s) = Bad $ owner ++ ": " ++ s |
167 | 168 |
annotateResult _ v = v |
168 | 169 |
|
169 | 170 |
-- | Try to extract a key from a object with better error reporting |
170 |
-- than fromObj |
|
171 |
-- than fromObj.
|
|
171 | 172 |
tryFromObj :: (J.JSON a) => |
172 | 173 |
String -- ^ Textual "owner" in error messages |
173 | 174 |
-> JSRecord -- ^ The object array |
... | ... | |
194 | 195 |
|
195 | 196 |
-- * Parsing utility functions |
196 | 197 |
|
197 |
-- | Parse results from readsPrec |
|
198 |
-- | Parse results from readsPrec.
|
|
198 | 199 |
parseChoices :: (Monad m, Read a) => String -> String -> [(a, String)] -> m a |
199 | 200 |
parseChoices _ _ ((v, ""):[]) = return v |
200 | 201 |
parseChoices name s ((_, e):[]) = |
... | ... | |
206 | 207 |
tryRead :: (Monad m, Read a) => String -> String -> m a |
207 | 208 |
tryRead name s = parseChoices name s $ reads s |
208 | 209 |
|
209 |
-- | Format a table of strings to maintain consistent length |
|
210 |
-- | Format a table of strings to maintain consistent length.
|
|
210 | 211 |
formatTable :: [[String]] -> [Bool] -> [[String]] |
211 | 212 |
formatTable vals numpos = |
212 | 213 |
let vtrans = transpose vals -- transpose, so that we work on rows |
... | ... | |
225 | 226 |
) (zip3 vtrans numpos mlens) |
226 | 227 |
in transpose expnd |
227 | 228 |
|
228 |
-- | Default group UUID (just a string, not a real UUID) |
|
229 |
-- | Default group UUID (just a string, not a real UUID).
|
|
229 | 230 |
defaultGroupID :: GroupID |
230 | 231 |
defaultGroupID = "00000000-0000-0000-0000-000000000000" |
Also available in: Unified diff