Utils.hs 3.58 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
2
{-| Utility functions -}

Iustin Pop's avatar
Iustin Pop committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{-

Copyright (C) 2009 Google Inc.

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

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

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

-}

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

39
import Data.List
40
41
import Control.Monad
import qualified Text.JSON as J
42
import Text.Printf (printf)
Iustin Pop's avatar
Iustin Pop committed
43
44
45

import Debug.Trace

Iustin Pop's avatar
Iustin Pop committed
46
47
-- * Debug functions

Iustin Pop's avatar
Iustin Pop committed
48
49
50
51
-- | To be used only for debugging, breaks referential integrity.
debug :: Show a => a -> a
debug x = trace (show x) x

Iustin Pop's avatar
Iustin Pop committed
52
-- * Miscelaneous
Iustin Pop's avatar
Iustin Pop committed
53

Iustin Pop's avatar
Iustin Pop committed
54
55
56
57
58
59
60
61
62
-- | Comma-join a string list.
commaJoin :: [String] -> String
commaJoin = intercalate ","

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

68
69
70
71
-- | Simple version of 'fst' for a triple
fst3 :: (a, b, c) -> a
fst3 (a, _, _) = a

Iustin Pop's avatar
Iustin Pop committed
72
73
-- * Mathematical functions

Iustin Pop's avatar
Iustin Pop committed
74
75
76
77
-- Simple and slow statistical functions, please replace with better versions

-- | Mean value of a list.
meanValue :: Floating a => [a] -> a
Iustin Pop's avatar
Iustin Pop committed
78
meanValue lst = sum lst / fromIntegral (length lst)
Iustin Pop's avatar
Iustin Pop committed
79
80
81
82
83

-- | Standard deviation.
stdDev :: Floating a => [a] -> a
stdDev lst =
    let mv = meanValue lst
Iustin Pop's avatar
Iustin Pop committed
84
        av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst
Iustin Pop's avatar
Iustin Pop committed
85
        bv = sqrt (av / fromIntegral (length lst))
Iustin Pop's avatar
Iustin Pop committed
86
87
88
89
    in bv

-- | Coefficient of variation.
varianceCoeff :: Floating a => [a] -> a
Iustin Pop's avatar
Iustin Pop committed
90
varianceCoeff lst = stdDev lst / fromIntegral (length lst)
Iustin Pop's avatar
Iustin Pop committed
91

Iustin Pop's avatar
Iustin Pop committed
92
93
94
95
96
97
98
99
100
101
102
-- * JSON-related functions

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

-- | Tries to read a string from a JSON value.
--
-- In case the value was not a string, we fail the read (in the
-- context of the current monad.
103
readEitherString :: (Monad m) => J.JSValue -> m String
104
105
readEitherString v =
    case v of
106
107
      J.JSString s -> return $ J.fromJSString s
      _ -> fail "Wrong JSON type"
108

Iustin Pop's avatar
Iustin Pop committed
109
-- | Converts a JSON message into an array of JSON objects.
110
loadJSArray :: (Monad m) => String -> m [J.JSObject J.JSValue]
Iustin Pop's avatar
Iustin Pop committed
111
loadJSArray = fromJResult . J.decodeStrict
112

Iustin Pop's avatar
Iustin Pop committed
113
-- | Reads a the value of a key in a JSON object.
114
fromObj :: (J.JSON a, Monad m) => String -> J.JSObject J.JSValue -> m a
115
fromObj k o =
116
    case lookup k (J.fromJSObject o) of
Iustin Pop's avatar
Iustin Pop committed
117
      Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
118
      Just val -> fromJResult $ J.readJSON val
119

Iustin Pop's avatar
Iustin Pop committed
120
-- | Converts a JSON value into a JSON object.
121
122
123
asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue)
asJSObject (J.JSObject a) = return a
asJSObject _ = fail "not an object"
124

Iustin Pop's avatar
Iustin Pop committed
125
-- | Coneverts a list of JSON values into a list of JSON objects.
126
asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue]
Iustin Pop's avatar
Iustin Pop committed
127
asObjectList = mapM asJSObject