Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Utils.hs @ d85a0a0f

History | View | Annotate | Download (3 kB)

1
{-| Utility functions -}
2

    
3
module Ganeti.HTools.Utils
4
    (
5
      debug
6
    , sepSplit
7
    , varianceCoeff
8
    , commaJoin
9
    , readEitherString
10
    , loadJSArray
11
    , fromObj
12
    , asJSObject
13
    , asObjectList
14
    , fromJResult
15
    ) where
16

    
17
import Data.List
18
import Control.Monad
19
import System
20
import System.IO
21
import qualified Text.JSON as J
22
import Text.Printf (printf)
23

    
24
import Ganeti.HTools.Types
25

    
26
import Debug.Trace
27

    
28
-- * Debug functions
29

    
30
-- | To be used only for debugging, breaks referential integrity.
31
debug :: Show a => a -> a
32
debug x = trace (show x) x
33

    
34
-- * Miscelaneous
35

    
36
-- | Comma-join a string list.
37
commaJoin :: [String] -> String
38
commaJoin = intercalate ","
39

    
40
-- | Split a string on a separator and return an array.
41
sepSplit :: Char -> String -> [String]
42
sepSplit sep s
43
    | x == "" && xs == [] = []
44
    | xs == []            = [x]
45
    | ys == []            = x:"":[]
46
    | otherwise           = x:(sepSplit sep ys)
47
    where (x, xs) = break (== sep) s
48
          ys = drop 1 xs
49

    
50
-- | Partial application of sepSplit to @'.'@
51
commaSplit :: String -> [String]
52
commaSplit = sepSplit ','
53

    
54
-- * Mathematical functions
55

    
56
-- Simple and slow statistical functions, please replace with better versions
57

    
58
-- | Mean value of a list.
59
meanValue :: Floating a => [a] -> a
60
meanValue lst = (sum lst) / (fromIntegral $ length lst)
61

    
62
-- | Standard deviation.
63
stdDev :: Floating a => [a] -> a
64
stdDev lst =
65
    let mv = meanValue lst
66
        square = (^ (2::Int)) -- silences "defaulting the constraint..."
67
        av = sum $ map square $ map (\e -> e - mv) lst
68
        bv = sqrt (av / (fromIntegral $ length lst))
69
    in bv
70

    
71
-- | Coefficient of variation.
72
varianceCoeff :: Floating a => [a] -> a
73
varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst)
74

    
75
-- * JSON-related functions
76

    
77
-- | Converts a JSON Result into a monadic value.
78
fromJResult :: Monad m => J.Result a -> m a
79
fromJResult (J.Error x) = fail x
80
fromJResult (J.Ok x) = return x
81

    
82
-- | Tries to read a string from a JSON value.
83
--
84
-- In case the value was not a string, we fail the read (in the
85
-- context of the current monad.
86
readEitherString :: (Monad m) => J.JSValue -> m String
87
readEitherString v =
88
    case v of
89
      J.JSString s -> return $ J.fromJSString s
90
      _ -> fail "Wrong JSON type"
91

    
92
-- | Converts a JSON message into an array of JSON objects.
93
loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
94
loadJSArray s = fromJResult $ J.decodeStrict s
95

    
96
-- | Reads a the value of a key in a JSON object.
97
fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
98
fromObj k o =
99
    case lookup k (J.fromJSObject o) of
100
      Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
101
      Just val -> fromJResult $ J.readJSON val
102

    
103
-- | Converts a JSON value into a JSON object.
104
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
105
asJSObject (J.JSObject a) = return a
106
asJSObject _ = fail "not an object"
107

    
108
-- | Coneverts a list of JSON values into a list of JSON objects.
109
asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
110
asObjectList = sequence . map asJSObject