Add copyright/license information
[ganeti-local] / Ganeti / HTools / Utils.hs
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     , varianceCoeff
29     , commaJoin
30     , readEitherString
31     , loadJSArray
32     , fromObj
33     , asJSObject
34     , asObjectList
35     , fromJResult
36     ) where
37
38 import Data.List
39 import Control.Monad
40 import System
41 import System.IO
42 import qualified Text.JSON as J
43 import Text.Printf (printf)
44
45 import Ganeti.HTools.Types
46
47 import Debug.Trace
48
49 -- * Debug functions
50
51 -- | To be used only for debugging, breaks referential integrity.
52 debug :: Show a => a -> a
53 debug x = trace (show x) x
54
55 -- * Miscelaneous
56
57 -- | Comma-join a string list.
58 commaJoin :: [String] -> String
59 commaJoin = intercalate ","
60
61 -- | Split a string on a separator and return an array.
62 sepSplit :: Char -> String -> [String]
63 sepSplit sep s
64     | x == "" && xs == [] = []
65     | xs == []            = [x]
66     | ys == []            = x:"":[]
67     | otherwise           = x:(sepSplit sep ys)
68     where (x, xs) = break (== sep) s
69           ys = drop 1 xs
70
71 -- | Partial application of sepSplit to @'.'@
72 commaSplit :: String -> [String]
73 commaSplit = sepSplit ','
74
75 -- * Mathematical functions
76
77 -- Simple and slow statistical functions, please replace with better versions
78
79 -- | Mean value of a list.
80 meanValue :: Floating a => [a] -> a
81 meanValue lst = (sum lst) / (fromIntegral $ length lst)
82
83 -- | Standard deviation.
84 stdDev :: Floating a => [a] -> a
85 stdDev lst =
86     let mv = meanValue lst
87         square = (^ (2::Int)) -- silences "defaulting the constraint..."
88         av = sum $ map square $ map (\e -> e - mv) lst
89         bv = sqrt (av / (fromIntegral $ length lst))
90     in bv
91
92 -- | Coefficient of variation.
93 varianceCoeff :: Floating a => [a] -> a
94 varianceCoeff lst = (stdDev lst) / (fromIntegral $ length lst)
95
96 -- * JSON-related functions
97
98 -- | Converts a JSON Result into a monadic value.
99 fromJResult :: Monad m => J.Result a -> m a
100 fromJResult (J.Error x) = fail x
101 fromJResult (J.Ok x) = return x
102
103 -- | Tries to read a string from a JSON value.
104 --
105 -- In case the value was not a string, we fail the read (in the
106 -- context of the current monad.
107 readEitherString :: (Monad m) => J.JSValue -> m String
108 readEitherString v =
109     case v of
110       J.JSString s -> return $ J.fromJSString s
111       _ -> fail "Wrong JSON type"
112
113 -- | Converts a JSON message into an array of JSON objects.
114 loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
115 loadJSArray s = fromJResult $ J.decodeStrict s
116
117 -- | Reads a the value of a key in a JSON object.
118 fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
119 fromObj k o =
120     case lookup k (J.fromJSObject o) of
121       Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
122       Just val -> fromJResult $ J.readJSON val
123
124 -- | Converts a JSON value into a JSON object.
125 asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
126 asJSObject (J.JSObject a) = return a
127 asJSObject _ = fail "not an object"
128
129 -- | Coneverts a list of JSON values into a list of JSON objects.
130 asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
131 asObjectList = sequence . map asJSObject