{-| Utility functions -}
+{-
+
+Copyright (C) 2009, 2010 Google Inc.
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA.
+
+-}
+
module Ganeti.HTools.Utils
(
debug
- , isLeft
- , fromLeft
- , fromRight
+ , debugFn
+ , debugXy
, sepSplit
- , swapPairs
- , varianceCoeff
- , readData
+ , stdDev
, commaJoin
- , combineEithers
- , ensureEitherList
- , eitherListHead
, readEitherString
- , parseEitherList
, loadJSArray
, fromObj
- , getStringElement
- , getIntElement
- , getListElement
- , concatEitherElems
- , applyEither1
- , applyEither2
+ , maybeFromObj
+ , tryFromObj
+ , fromJVal
+ , asJSObject
+ , asObjectList
+ , fromJResult
+ , tryRead
+ , formatTable
+ , annotateResult
+ , defaultGroupID
) where
-import Data.Either
+import Control.Monad (liftM)
import Data.List
-import Monad
-import System
-import System.IO
-import Text.JSON
+import qualified Text.JSON as J
import Text.Printf (printf)
import Debug.Trace
+import Ganeti.HTools.Types
+
+-- * Debug functions
+
-- | To be used only for debugging, breaks referential integrity.
debug :: Show a => a -> a
debug x = trace (show x) x
--- | Check if the given argument is Left something
-isLeft :: Either a b -> Bool
-isLeft val =
- case val of
- Left _ -> True
- _ -> False
+-- | Displays a modified form of the second parameter before returning it
+debugFn :: Show b => (a -> b) -> a -> a
+debugFn fn x = debug (fn x) `seq` x
-fromLeft :: Either a b -> a
-fromLeft = either (\x -> x) (\_ -> undefined)
+-- | Show the first parameter before returning the second one
+debugXy :: Show a => a -> b -> b
+debugXy a b = debug a `seq` b
-fromRight :: Either a b -> b
-fromRight = either (\_ -> undefined) id
+-- * Miscelaneous
-- | Comma-join a string list.
commaJoin :: [String] -> String
commaJoin = intercalate ","
--- | Split a string on a separator and return an array.
-sepSplit :: Char -> String -> [String]
+-- | Split a list on a separator and return an array.
+sepSplit :: Eq a => a -> [a] -> [[a]]
sepSplit sep s
- | x == "" && xs == [] = []
- | xs == [] = [x]
- | ys == [] = x:"":[]
- | otherwise = x:(sepSplit sep ys)
+ | null s = []
+ | null xs = [x]
+ | null ys = [x,[]]
+ | otherwise = x:sepSplit sep ys
where (x, xs) = break (== sep) s
ys = drop 1 xs
--- | Partial application of sepSplit to @'.'@
-commaSplit :: String -> [String]
-commaSplit = sepSplit ','
-
--- | Swap a list of @(a, b)@ into @(b, a)@
-swapPairs :: [(a, b)] -> [(b, a)]
-swapPairs = map (\ (a, b) -> (b, a))
+-- * Mathematical functions
--- Simple and slow statistical functions, please replace with better versions
+-- Simple and slow statistical functions, please replace with better
+-- versions
--- | Mean value of a list.
-meanValue :: Floating a => [a] -> a
-meanValue lst = (sum lst) / (fromIntegral $ length lst)
-
--- | Standard deviation.
-stdDev :: Floating a => [a] -> a
+-- | Standard deviation function
+stdDev :: [Double] -> Double
stdDev lst =
- let mv = meanValue lst
- square = (^ (2::Int)) -- silences "defaulting the constraint..."
- av = sum $ map square $ map (\e -> e - mv) lst
- bv = sqrt (av / (fromIntegral $ length lst))
- in bv
-
--- | Coefficient of variation.
-varianceCoeff :: Floating a => [a] -> a
-varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst)
-
--- | Get a Right result or print the error and exit
-readData :: (String -> IO (Either String String)) -> String -> IO String
-readData fn host = do
- nd <- fn host
- when (isLeft nd) $
- do
- putStrLn $ fromLeft nd
- exitWith $ ExitFailure 1
- return $ fromRight nd
-
-{-- Our cheap monad-like stuff.
-
-Thi is needed since Either e a is already a monad instance somewhere
-in the standard libraries (Control.Monad.Error) and we don't need that
-entire thing.
-
--}
-combineEithers :: (Either String a)
- -> (a -> Either String b)
- -> (Either String b)
-combineEithers (Left s) _ = Left s
-combineEithers (Right s) f = f s
-
-ensureEitherList :: [Either String a] -> Either String [a]
-ensureEitherList lst =
- foldr (\elem accu ->
- case (elem, accu) of
- (Left x, _) -> Left x
- (_, Left x) -> Left x -- should never happen
- (Right e, Right a) -> Right (e:a)
- )
- (Right []) lst
-
-eitherListHead :: Either String [a] -> Either String a
-eitherListHead lst =
- case lst of
- Left x -> Left x
- Right (x:_) -> Right x
- Right [] -> Left "List empty"
-
-readEitherString :: JSValue -> Either String String
+ -- first, calculate the list length and sum lst in a single step,
+ -- for performance reasons
+ let (ll', sx) = foldl' (\(rl, rs) e ->
+ let rl' = rl + 1
+ rs' = rs + e
+ in rl' `seq` rs' `seq` (rl', rs')) (0::Int, 0) lst
+ ll = fromIntegral ll'::Double
+ mv = sx / ll
+ av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst
+ in sqrt (av / ll) -- stddev
+
+-- * JSON-related functions
+
+-- | Converts a JSON Result into a monadic value.
+fromJResult :: Monad m => String -> J.Result a -> m a
+fromJResult s (J.Error x) = fail (s ++ ": " ++ x)
+fromJResult _ (J.Ok x) = return x
+
+-- | Tries to read a string from a JSON value.
+--
+-- In case the value was not a string, we fail the read (in the
+-- context of the current monad.
+readEitherString :: (Monad m) => J.JSValue -> m String
readEitherString v =
case v of
- JSString s -> Right $ fromJSString s
- _ -> Left "Wrong JSON type"
-
-parseEitherList :: (JSObject JSValue -> Either String String)
- -> [JSObject JSValue]
- -> Either String String
-parseEitherList fn idata =
- let ml = ensureEitherList $ map fn idata
- in ml `combineEithers` (Right . unlines)
-
-loadJSArray :: String -> Either String [JSObject JSValue]
-loadJSArray s = resultToEither $ decodeStrict s
-
-fromObj :: JSON a => String -> JSObject JSValue -> Either String a
+ J.JSString s -> return $ J.fromJSString s
+ _ -> fail "Wrong JSON type"
+
+-- | Converts a JSON message into an array of JSON objects.
+loadJSArray :: (Monad m)
+ => String -- ^ Operation description (for error reporting)
+ -> String -- ^ Input message
+ -> m [J.JSObject J.JSValue]
+loadJSArray s = fromJResult s . J.decodeStrict
+
+-- | Reads the value of a key in a JSON object.
+fromObj :: (J.JSON a, Monad m) => String -> [(String, J.JSValue)] -> m a
fromObj k o =
- case lookup k (fromJSObject o) of
- Nothing -> Left $ printf "key '%s' not found" k
- Just val -> resultToEither $ readJSON val
-
-getStringElement :: String -> JSObject JSValue -> Either String String
-getStringElement = fromObj
-
-getIntElement :: String -> JSObject JSValue -> Either String Int
-getIntElement = fromObj
-
-getListElement :: String -> JSObject JSValue
- -> Either String [JSValue]
-getListElement = fromObj
-
-concatEitherElems :: Either String String
- -> Either String String
- -> Either String String
-concatEitherElems = applyEither2 (\x y -> x ++ "|" ++ y)
-
-applyEither1 :: (a -> b) -> Either String a -> Either String b
-applyEither1 fn a =
- case a of
- Left x -> Left x
- Right y -> Right $ fn y
-
-applyEither2 :: (a -> b -> c)
- -> Either String a
- -> Either String b
- -> Either String c
-applyEither2 fn a b =
- case (a, b) of
- (Right x, Right y) -> Right $ fn x y
- (Left x, _) -> Left x
- (_, Left y) -> Left y
+ case lookup k o of
+ Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
+ Just val -> fromKeyValue k val
+
+-- | Reads the value of an optional key in a JSON object.
+maybeFromObj :: (J.JSON a, Monad m) => String -> [(String, J.JSValue)]
+ -> m (Maybe a)
+maybeFromObj k o =
+ case lookup k o of
+ Nothing -> return Nothing
+ Just val -> liftM Just (fromKeyValue k val)
+
+-- | Reads a JValue, that originated from an object key
+fromKeyValue :: (J.JSON a, Monad m)
+ => String -- ^ The key name
+ -> J.JSValue -- ^ The value to read
+ -> m a
+fromKeyValue k val =
+ fromJResult (printf "key '%s', value '%s'" k (show val)) (J.readJSON val)
+
+-- | Annotate a Result with an ownership information
+annotateResult :: String -> Result a -> Result a
+annotateResult owner (Bad s) = Bad $ owner ++ ": " ++ s
+annotateResult _ v = v
+
+-- | Try to extract a key from a object with better error reporting
+-- than fromObj
+tryFromObj :: (J.JSON a) =>
+ String -> [(String, J.JSValue)] -> String -> Result a
+tryFromObj t o k = annotateResult t (fromObj k o)
+
+-- | Small wrapper over readJSON.
+fromJVal :: (Monad m, J.JSON a) => J.JSValue -> m a
+fromJVal v =
+ case J.readJSON v of
+ J.Error s -> fail ("Cannot convert value " ++ show v ++ ", error: " ++ s)
+ J.Ok x -> return x
+
+-- | Converts a JSON value into a JSON object.
+asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
+asJSObject (J.JSObject a) = return a
+asJSObject _ = fail "not an object"
+
+-- | Coneverts a list of JSON values into a list of JSON objects.
+asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
+asObjectList = mapM asJSObject
+
+-- * Parsing utility functions
+
+-- | Parse results from readsPrec
+parseChoices :: (Monad m, Read a) => String -> String -> [(a, String)] -> m a
+parseChoices _ _ ((v, ""):[]) = return v
+parseChoices name s ((_, e):[]) =
+ fail $ name ++ ": leftover characters when parsing '"
+ ++ s ++ "': '" ++ e ++ "'"
+parseChoices name s _ = fail $ name ++ ": cannot parse string '" ++ s ++ "'"
+
+-- | Safe 'read' function returning data encapsulated in a Result.
+tryRead :: (Monad m, Read a) => String -> String -> m a
+tryRead name s = parseChoices name s $ reads s
+
+-- | Format a table of strings to maintain consistent length
+formatTable :: [[String]] -> [Bool] -> [[String]]
+formatTable vals numpos =
+ let vtrans = transpose vals -- transpose, so that we work on rows
+ -- rather than columns
+ mlens = map (maximum . map length) vtrans
+ expnd = map (\(flds, isnum, ml) ->
+ map (\val ->
+ let delta = ml - length val
+ filler = replicate delta ' '
+ in if delta > 0
+ then if isnum
+ then filler ++ val
+ else val ++ filler
+ else val
+ ) flds
+ ) (zip3 vtrans numpos mlens)
+ in transpose expnd
+
+-- | Default group UUID (just a string, not a real UUID)
+defaultGroupID :: GroupID
+defaultGroupID = "00000000-0000-0000-0000-000000000000"