module Ganeti.HTools.Utils
(
debug
, debugFn
, debugXy
, sepSplit
, stdDev
, if'
, select
, commaJoin
, readEitherString
, JSRecord
, loadJSArray
, fromObj
, fromObjWithDefault
, maybeFromObj
, tryFromObj
, fromJVal
, asJSObject
, asObjectList
, fromJResult
, tryRead
, formatTable
, annotateResult
, defaultGroupID
, parseUnit
) where
import Control.Monad (liftM)
import Data.Char (toUpper)
import Data.List
import Data.Maybe (fromMaybe)
import qualified Text.JSON as J
import Text.Printf (printf)
import Debug.Trace
import Ganeti.HTools.Types
debug :: Show a => a -> a
debug x = trace (show x) x
debugFn :: Show b => (a -> b) -> a -> a
debugFn fn x = debug (fn x) `seq` x
debugXy :: Show a => a -> b -> b
debugXy a b = debug a `seq` b
commaJoin :: [String] -> String
commaJoin = intercalate ","
sepSplit :: Eq a => a -> [a] -> [[a]]
sepSplit sep s
| null s = []
| null xs = [x]
| null ys = [x,[]]
| otherwise = x:sepSplit sep ys
where (x, xs) = break (== sep) s
ys = drop 1 xs
stdDev :: [Double] -> Double
stdDev lst =
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)
if' :: Bool
-> a
-> a
-> a
if' True x _ = x
if' _ _ y = y
select :: a
-> [(Bool, a)]
-> a
select def = maybe def snd . find fst
type JSRecord = [(String, J.JSValue)]
fromJResult :: Monad m => String -> J.Result a -> m a
fromJResult s (J.Error x) = fail (s ++ ": " ++ x)
fromJResult _ (J.Ok x) = return x
readEitherString :: (Monad m) => J.JSValue -> m String
readEitherString v =
case v of
J.JSString s -> return $ J.fromJSString s
_ -> fail "Wrong JSON type"
loadJSArray :: (Monad m)
=> String
-> String
-> m [J.JSObject J.JSValue]
loadJSArray s = fromJResult s . J.decodeStrict
fromObj :: (J.JSON a, Monad m) => JSRecord -> String -> m a
fromObj o k =
case lookup k o of
Nothing -> fail $ printf "key '%s' not found, object contains only %s"
k (show (map fst o))
Just val -> fromKeyValue k val
maybeFromObj :: (J.JSON a, Monad m) =>
JSRecord -> String -> m (Maybe a)
maybeFromObj o k =
case lookup k o of
Nothing -> return Nothing
Just val -> liftM Just (fromKeyValue k val)
fromObjWithDefault :: (J.JSON a, Monad m) =>
JSRecord -> String -> a -> m a
fromObjWithDefault o k d = liftM (fromMaybe d) $ maybeFromObj o k
fromKeyValue :: (J.JSON a, Monad m)
=> String
-> J.JSValue
-> m a
fromKeyValue k val =
fromJResult (printf "key '%s', value '%s'" k (show val)) (J.readJSON val)
annotateResult :: String -> Result a -> Result a
annotateResult owner (Bad s) = Bad $ owner ++ ": " ++ s
annotateResult _ v = v
tryFromObj :: (J.JSON a) =>
String
-> JSRecord
-> String
-> Result a
tryFromObj t o = annotateResult t . fromObj o
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
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
asJSObject (J.JSObject a) = return a
asJSObject _ = fail "not an object"
asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
asObjectList = mapM asJSObject
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 ++ "'"
tryRead :: (Monad m, Read a) => String -> String -> m a
tryRead name s = parseChoices name s $ reads s
formatTable :: [[String]] -> [Bool] -> [[String]]
formatTable vals numpos =
let vtrans = transpose vals
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
defaultGroupID :: GroupID
defaultGroupID = "00000000-0000-0000-0000-000000000000"
parseUnit :: (Monad m, Integral a, Read a) => String -> m a
parseUnit str =
case reads str of
[(v, suffix)] ->
let unit = dropWhile (== ' ') suffix
upper = map toUpper unit
siConvert x = x * 1000000 `div` 1048576
in case () of
_ | null unit -> return v
| unit == "m" || upper == "MIB" -> return v
| unit == "M" || upper == "MB" -> return $ siConvert v
| unit == "g" || upper == "GIB" -> return $ v * 1024
| unit == "G" || upper == "GB" -> return $ siConvert
(v * 1000)
| unit == "t" || upper == "TIB" -> return $ v * 1048576
| unit == "T" || upper == "TB" -> return $
siConvert (v * 1000000)
| otherwise -> fail $ "Unknown unit '" ++ unit ++ "'"
_ -> fail $ "Can't parse string '" ++ str ++ "'"