Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Utils.hs @ 78694255

History | View | Annotate | Download (3.8 kB)

1
{-| Utility functions -}
2

    
3
{-
4

    
5
Copyright (C) 2009 Google Inc.
6

    
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11

    
12
This program is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
General Public License for more details.
16

    
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
02110-1301, USA.
21

    
22
-}
23

    
24
module Ganeti.HTools.Utils
25
    (
26
      debug
27
    , sepSplit
28
    , fst3
29
    , varianceCoeff
30
    , commaJoin
31
    , readEitherString
32
    , loadJSArray
33
    , fromObj
34
    , asJSObject
35
    , asObjectList
36
    , fromJResult
37
    ) where
38

    
39
import Data.List
40
import Control.Monad
41
import System
42
import System.IO
43
import qualified Text.JSON as J
44
import Text.Printf (printf)
45

    
46
import Ganeti.HTools.Types
47

    
48
import Debug.Trace
49

    
50
-- * Debug functions
51

    
52
-- | To be used only for debugging, breaks referential integrity.
53
debug :: Show a => a -> a
54
debug x = trace (show x) x
55

    
56
-- * Miscelaneous
57

    
58
-- | Comma-join a string list.
59
commaJoin :: [String] -> String
60
commaJoin = intercalate ","
61

    
62
-- | Split a string on a separator and return an array.
63
sepSplit :: Char -> String -> [String]
64
sepSplit sep s
65
    | x == "" && xs == [] = []
66
    | xs == []            = [x]
67
    | ys == []            = x:"":[]
68
    | otherwise           = x:(sepSplit sep ys)
69
    where (x, xs) = break (== sep) s
70
          ys = drop 1 xs
71

    
72
-- | Partial application of sepSplit to @'.'@
73
commaSplit :: String -> [String]
74
commaSplit = sepSplit ','
75

    
76
-- | Simple version of 'fst' for a triple
77
fst3 :: (a, b, c) -> a
78
fst3 (a, _, _) = a
79

    
80
-- * Mathematical functions
81

    
82
-- Simple and slow statistical functions, please replace with better versions
83

    
84
-- | Mean value of a list.
85
meanValue :: Floating a => [a] -> a
86
meanValue lst = (sum lst) / (fromIntegral $ length lst)
87

    
88
-- | Squaring function
89
square :: (Num a) => a -> a
90
square = (^ 2)
91

    
92
-- | Standard deviation.
93
stdDev :: Floating a => [a] -> a
94
stdDev lst =
95
    let mv = meanValue lst
96
        av = sum $ map square $ map (\e -> e - mv) lst
97
        bv = sqrt (av / (fromIntegral $ length lst))
98
    in bv
99

    
100
-- | Coefficient of variation.
101
varianceCoeff :: Floating a => [a] -> a
102
varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst)
103

    
104
-- * JSON-related functions
105

    
106
-- | Converts a JSON Result into a monadic value.
107
fromJResult :: Monad m => J.Result a -> m a
108
fromJResult (J.Error x) = fail x
109
fromJResult (J.Ok x) = return x
110

    
111
-- | Tries to read a string from a JSON value.
112
--
113
-- In case the value was not a string, we fail the read (in the
114
-- context of the current monad.
115
readEitherString :: (Monad m) => J.JSValue -> m String
116
readEitherString v =
117
    case v of
118
      J.JSString s -> return $ J.fromJSString s
119
      _ -> fail "Wrong JSON type"
120

    
121
-- | Converts a JSON message into an array of JSON objects.
122
loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
123
loadJSArray s = fromJResult $ J.decodeStrict s
124

    
125
-- | Reads a the value of a key in a JSON object.
126
fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
127
fromObj k o =
128
    case lookup k (J.fromJSObject o) of
129
      Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
130
      Just val -> fromJResult $ J.readJSON val
131

    
132
-- | Converts a JSON value into a JSON object.
133
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
134
asJSObject (J.JSObject a) = return a
135
asJSObject _ = fail "not an object"
136

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