root / htools / Ganeti / HTools / Utils.hs @ 34ace266
History | View | Annotate | Download (6.2 kB)
1 |
{-| Utility functions. -} |
---|---|
2 |
|
3 |
{- |
4 |
|
5 |
Copyright (C) 2009, 2010, 2011 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 |
( debug |
26 |
, debugFn |
27 |
, debugXy |
28 |
, sepSplit |
29 |
, stdDev |
30 |
, if' |
31 |
, select |
32 |
, applyIf |
33 |
, commaJoin |
34 |
, tryRead |
35 |
, formatTable |
36 |
, parseUnit |
37 |
, plural |
38 |
) where |
39 |
|
40 |
import Data.Char (toUpper) |
41 |
import Data.List |
42 |
|
43 |
import Debug.Trace |
44 |
|
45 |
-- * Debug functions |
46 |
|
47 |
-- | To be used only for debugging, breaks referential integrity. |
48 |
debug :: Show a => a -> a |
49 |
debug x = trace (show x) x |
50 |
|
51 |
-- | Displays a modified form of the second parameter before returning |
52 |
-- it. |
53 |
debugFn :: Show b => (a -> b) -> a -> a |
54 |
debugFn fn x = debug (fn x) `seq` x |
55 |
|
56 |
-- | Show the first parameter before returning the second one. |
57 |
debugXy :: Show a => a -> b -> b |
58 |
debugXy = seq . debug |
59 |
|
60 |
-- * Miscellaneous |
61 |
|
62 |
-- | Apply the function if condition holds, otherwise use default value. |
63 |
applyIf :: Bool -> (a -> a) -> a -> a |
64 |
applyIf b f x = if b then f x else x |
65 |
|
66 |
-- | Comma-join a string list. |
67 |
commaJoin :: [String] -> String |
68 |
commaJoin = intercalate "," |
69 |
|
70 |
-- | Split a list on a separator and return an array. |
71 |
sepSplit :: Eq a => a -> [a] -> [[a]] |
72 |
sepSplit sep s |
73 |
| null s = [] |
74 |
| null xs = [x] |
75 |
| null ys = [x,[]] |
76 |
| otherwise = x:sepSplit sep ys |
77 |
where (x, xs) = break (== sep) s |
78 |
ys = drop 1 xs |
79 |
|
80 |
-- | Simple pluralize helper |
81 |
plural :: Int -> String -> String -> String |
82 |
plural 1 s _ = s |
83 |
plural _ _ p = p |
84 |
|
85 |
-- * Mathematical functions |
86 |
|
87 |
-- Simple and slow statistical functions, please replace with better |
88 |
-- versions |
89 |
|
90 |
-- | Standard deviation function. |
91 |
stdDev :: [Double] -> Double |
92 |
stdDev lst = |
93 |
-- first, calculate the list length and sum lst in a single step, |
94 |
-- for performance reasons |
95 |
let (ll', sx) = foldl' (\(rl, rs) e -> |
96 |
let rl' = rl + 1 |
97 |
rs' = rs + e |
98 |
in rl' `seq` rs' `seq` (rl', rs')) (0::Int, 0) lst |
99 |
ll = fromIntegral ll'::Double |
100 |
mv = sx / ll |
101 |
av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst |
102 |
in sqrt (av / ll) -- stddev |
103 |
|
104 |
-- * Logical functions |
105 |
|
106 |
-- Avoid syntactic sugar and enhance readability. These functions are proposed |
107 |
-- by some for inclusion in the Prelude, and at the moment they are present |
108 |
-- (with various definitions) in the utility-ht package. Some rationale and |
109 |
-- discussion is available at <http://www.haskell.org/haskellwiki/If-then-else> |
110 |
|
111 |
-- | \"if\" as a function, rather than as syntactic sugar. |
112 |
if' :: Bool -- ^ condition |
113 |
-> a -- ^ \"then\" result |
114 |
-> a -- ^ \"else\" result |
115 |
-> a -- ^ \"then\" or "else" result depending on the condition |
116 |
if' True x _ = x |
117 |
if' _ _ y = y |
118 |
|
119 |
-- | Return the first result with a True condition, or the default otherwise. |
120 |
select :: a -- ^ default result |
121 |
-> [(Bool, a)] -- ^ list of \"condition, result\" |
122 |
-> a -- ^ first result which has a True condition, or default |
123 |
select def = maybe def snd . find fst |
124 |
|
125 |
|
126 |
-- * Parsing utility functions |
127 |
|
128 |
-- | Parse results from readsPrec. |
129 |
parseChoices :: (Monad m, Read a) => String -> String -> [(a, String)] -> m a |
130 |
parseChoices _ _ ((v, ""):[]) = return v |
131 |
parseChoices name s ((_, e):[]) = |
132 |
fail $ name ++ ": leftover characters when parsing '" |
133 |
++ s ++ "': '" ++ e ++ "'" |
134 |
parseChoices name s _ = fail $ name ++ ": cannot parse string '" ++ s ++ "'" |
135 |
|
136 |
-- | Safe 'read' function returning data encapsulated in a Result. |
137 |
tryRead :: (Monad m, Read a) => String -> String -> m a |
138 |
tryRead name s = parseChoices name s $ reads s |
139 |
|
140 |
-- | Format a table of strings to maintain consistent length. |
141 |
formatTable :: [[String]] -> [Bool] -> [[String]] |
142 |
formatTable vals numpos = |
143 |
let vtrans = transpose vals -- transpose, so that we work on rows |
144 |
-- rather than columns |
145 |
mlens = map (maximum . map length) vtrans |
146 |
expnd = map (\(flds, isnum, ml) -> |
147 |
map (\val -> |
148 |
let delta = ml - length val |
149 |
filler = replicate delta ' ' |
150 |
in if delta > 0 |
151 |
then if isnum |
152 |
then filler ++ val |
153 |
else val ++ filler |
154 |
else val |
155 |
) flds |
156 |
) (zip3 vtrans numpos mlens) |
157 |
in transpose expnd |
158 |
|
159 |
-- | Tries to extract number and scale from the given string. |
160 |
-- |
161 |
-- Input must be in the format NUMBER+ SPACE* [UNIT]. If no unit is |
162 |
-- specified, it defaults to MiB. Return value is always an integral |
163 |
-- value in MiB. |
164 |
parseUnit :: (Monad m, Integral a, Read a) => String -> m a |
165 |
parseUnit str = |
166 |
-- TODO: enhance this by splitting the unit parsing code out and |
167 |
-- accepting floating-point numbers |
168 |
case reads str of |
169 |
[(v, suffix)] -> |
170 |
let unit = dropWhile (== ' ') suffix |
171 |
upper = map toUpper unit |
172 |
siConvert x = x * 1000000 `div` 1048576 |
173 |
in case () of |
174 |
_ | null unit -> return v |
175 |
| unit == "m" || upper == "MIB" -> return v |
176 |
| unit == "M" || upper == "MB" -> return $ siConvert v |
177 |
| unit == "g" || upper == "GIB" -> return $ v * 1024 |
178 |
| unit == "G" || upper == "GB" -> return $ siConvert |
179 |
(v * 1000) |
180 |
| unit == "t" || upper == "TIB" -> return $ v * 1048576 |
181 |
| unit == "T" || upper == "TB" -> return $ |
182 |
siConvert (v * 1000000) |
183 |
| otherwise -> fail $ "Unknown unit '" ++ unit ++ "'" |
184 |
_ -> fail $ "Can't parse string '" ++ str ++ "'" |