Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Utils.hs @ 2e5eb96a

History | View | Annotate | Download (7.4 kB)

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