Fix hspace's KM metrics
[ganeti-local] / Ganeti / HTools / Utils.hs
index 1c51f38..e7f63b3 100644 (file)
@@ -31,20 +31,24 @@ module Ganeti.HTools.Utils
     , readEitherString
     , loadJSArray
     , fromObj
+    , tryFromObj
+    , fromJVal
     , asJSObject
     , asObjectList
     , fromJResult
     , tryRead
     , formatTable
+    , annotateResult
     ) where
 
 import Data.List
-import Control.Monad
 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.
@@ -73,23 +77,18 @@ fst3 (a, _, _) = a
 
 -- * Mathematical functions
 
--- 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)
+-- Simple and slow statistical functions, please replace with better
+-- versions
 
--- | Standard deviation.
-stdDev :: Floating a => [a] -> a
-stdDev lst =
-    let mv = meanValue lst
+-- | 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 / fromIntegral (length lst))
-    in bv
-
--- | Coefficient of variation.
-varianceCoeff :: Floating a => [a] -> a
-varianceCoeff lst = stdDev lst / fromIntegral (length lst)
+        bv = sqrt (av / ll) -- stddev
+        cv = bv / ll        -- covariance
+    in cv
 
 -- * JSON-related functions
 
@@ -113,12 +112,30 @@ loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
 loadJSArray = fromJResult . J.decodeStrict
 
 -- | Reads a the value of a key in a JSON object.
-fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
+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
 
+-- | 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