{-| Utility functions -}
+{-
+
+Copyright (C) 2009 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
, sepSplit
- , swapPairs
+ , fst3
, varianceCoeff
- , readData
, commaJoin
, readEitherString
, loadJSArray
, fromObj
- , getStringElement
- , getIntElement
- , getBoolElement
- , getListElement
- , getObjectElement
+ , tryFromObj
+ , fromJVal
, asJSObject
, asObjectList
- , Result(Ok, Bad)
, fromJResult
- , (|+)
+ , tryRead
+ , formatTable
+ , annotateResult
) where
-import Data.Either
import Data.List
-import Control.Monad
-import System
-import System.IO
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
-
-{-
-
-This is similar to the JSON library Result type - *very* similar, but
-we want to use it in multiple places, so we abstract it into a
-mini-library here
-
--}
-
-data Result a
- = Bad String
- | Ok a
- deriving (Show)
-
-instance Monad Result where
- (>>=) (Bad x) _ = Bad x
- (>>=) (Ok x) fn = fn x
- return = Ok
- fail = Bad
-
-fromJResult :: Monad m => J.Result a -> m a
-fromJResult (J.Error x) = fail x
-fromJResult (J.Ok x) = return x
+-- * Miscelaneous
-- | Comma-join a string list.
commaJoin :: [String] -> String
sepSplit sep s
| x == "" && xs == [] = []
| xs == [] = [x]
- | ys == [] = x:"":[]
- | otherwise = x:(sepSplit sep ys)
+ | 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))
-
--- 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
-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 an Ok result or print the error and exit
-readData :: Result a -> IO a
-readData nd =
- (case nd of
- Bad x -> do
- putStrLn x
- exitWith $ ExitFailure 1
- Ok x -> return x)
+-- | Simple version of 'fst' for a triple
+fst3 :: (a, b, c) -> a
+fst3 (a, _, _) = a
+
+-- * Mathematical functions
+
+-- Simple and slow statistical functions, please replace with better
+-- versions
+-- | The covariance of the list
+varianceCoeff :: [Double] -> Double
+varianceCoeff lst =
+ let ll = fromIntegral (length lst)::Double -- length of list
+ mv = sum lst / ll -- mean value
+ av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst
+ bv = sqrt (av / ll) -- stddev
+ cv = bv / ll -- covariance
+ in cv
+
+-- * JSON-related functions
+
+-- | Converts a JSON Result into a monadic value.
+fromJResult :: Monad m => J.Result a -> m a
+fromJResult (J.Error x) = fail 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
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 -> m [J.JSObject J.JSValue]
-loadJSArray s = fromJResult $ J.decodeStrict s
+loadJSArray = fromJResult . J.decodeStrict
-fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
+-- | Reads a 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 (J.fromJSObject o) of
+ case lookup k o of
Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
Just val -> fromJResult $ J.readJSON val
-getStringElement :: (Monad m) => String -> J.JSObject J.JSValue -> m String
-getStringElement = fromObj
-
-getIntElement :: (Monad m) => String -> J.JSObject J.JSValue -> m Int
-getIntElement = fromObj
-
-getBoolElement :: (Monad m) => String -> J.JSObject J.JSValue -> m Bool
-getBoolElement = fromObj
-
-getListElement :: (Monad m) => String -> J.JSObject J.JSValue -> m [J.JSValue]
-getListElement = fromObj
-
-getObjectElement :: (Monad m) => String -> J.JSObject J.JSValue
- -> m (J.JSObject J.JSValue)
-getObjectElement = fromObj
-
+-- | 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 ++ " key '" ++ k ++ "'") (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 = sequence . map asJSObject
-
--- | Function to concat two strings with a separator under a monad
-(|+) :: (Monad m) => m String -> m String -> m String
-(|+) = liftM2 (\x y -> x ++ "|" ++ y)
+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