Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Utils.hs @ cefd4a4a

History | View | Annotate | Download (9.3 kB)

1 525bfb36 Iustin Pop
{-| Utility functions. -}
2 e4f08c46 Iustin Pop
3 e2fa2baf Iustin Pop
{-
4 e2fa2baf Iustin Pop
5 5850e990 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
6 e2fa2baf Iustin Pop
7 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
8 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
9 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
10 e2fa2baf Iustin Pop
(at your option) any later version.
11 e2fa2baf Iustin Pop
12 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
13 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
14 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 e2fa2baf Iustin Pop
General Public License for more details.
16 e2fa2baf Iustin Pop
17 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
18 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
19 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 e2fa2baf Iustin Pop
02110-1301, USA.
21 e2fa2baf Iustin Pop
22 e2fa2baf Iustin Pop
-}
23 e2fa2baf Iustin Pop
24 26d62e4c Iustin Pop
module Ganeti.Utils
25 ebf38064 Iustin Pop
  ( debug
26 ebf38064 Iustin Pop
  , debugFn
27 ebf38064 Iustin Pop
  , debugXy
28 ebf38064 Iustin Pop
  , sepSplit
29 ebf38064 Iustin Pop
  , stdDev
30 ebf38064 Iustin Pop
  , if'
31 ebf38064 Iustin Pop
  , select
32 ebf38064 Iustin Pop
  , applyIf
33 ebf38064 Iustin Pop
  , commaJoin
34 79eef90b Agata Murawska
  , ensureQuoted
35 ebf38064 Iustin Pop
  , tryRead
36 ebf38064 Iustin Pop
  , formatTable
37 c3024b7e René Nussbaumer
  , printTable
38 ebf38064 Iustin Pop
  , parseUnit
39 19e310cc René Nussbaumer
  , plural
40 04edfc99 Iustin Pop
  , niceSort
41 04edfc99 Iustin Pop
  , niceSortKey
42 88a10df5 Iustin Pop
  , exitIfBad
43 88a10df5 Iustin Pop
  , exitErr
44 88a10df5 Iustin Pop
  , exitWhen
45 88a10df5 Iustin Pop
  , exitUnless
46 ebf38064 Iustin Pop
  ) where
47 e4f08c46 Iustin Pop
48 04edfc99 Iustin Pop
import Data.Char (toUpper, isAlphaNum, isDigit)
49 04edfc99 Iustin Pop
import Data.Function (on)
50 29ac5975 Iustin Pop
import Data.List
51 e4f08c46 Iustin Pop
52 e4f08c46 Iustin Pop
import Debug.Trace
53 e4f08c46 Iustin Pop
54 88a10df5 Iustin Pop
import Ganeti.BasicTypes
55 88a10df5 Iustin Pop
import System.IO
56 88a10df5 Iustin Pop
import System.Exit
57 88a10df5 Iustin Pop
58 9188aeef Iustin Pop
-- * Debug functions
59 9188aeef Iustin Pop
60 e4f08c46 Iustin Pop
-- | To be used only for debugging, breaks referential integrity.
61 e4f08c46 Iustin Pop
debug :: Show a => a -> a
62 e4f08c46 Iustin Pop
debug x = trace (show x) x
63 e4f08c46 Iustin Pop
64 525bfb36 Iustin Pop
-- | Displays a modified form of the second parameter before returning
65 525bfb36 Iustin Pop
-- it.
66 adc5c176 Iustin Pop
debugFn :: Show b => (a -> b) -> a -> a
67 adc5c176 Iustin Pop
debugFn fn x = debug (fn x) `seq` x
68 adc5c176 Iustin Pop
69 525bfb36 Iustin Pop
-- | Show the first parameter before returning the second one.
70 adc5c176 Iustin Pop
debugXy :: Show a => a -> b -> b
71 05ff7a00 Agata Murawska
debugXy = seq . debug
72 adc5c176 Iustin Pop
73 525bfb36 Iustin Pop
-- * Miscellaneous
74 1b7cf8ca Iustin Pop
75 61bbbed7 Agata Murawska
-- | Apply the function if condition holds, otherwise use default value.
76 61bbbed7 Agata Murawska
applyIf :: Bool -> (a -> a) -> a -> a
77 61bbbed7 Agata Murawska
applyIf b f x = if b then f x else x
78 61bbbed7 Agata Murawska
79 e4f08c46 Iustin Pop
-- | Comma-join a string list.
80 e4f08c46 Iustin Pop
commaJoin :: [String] -> String
81 e4f08c46 Iustin Pop
commaJoin = intercalate ","
82 e4f08c46 Iustin Pop
83 748d5d50 Iustin Pop
-- | Split a list on a separator and return an array.
84 748d5d50 Iustin Pop
sepSplit :: Eq a => a -> [a] -> [[a]]
85 e4f08c46 Iustin Pop
sepSplit sep s
86 ebf38064 Iustin Pop
  | null s    = []
87 ebf38064 Iustin Pop
  | null xs   = [x]
88 ebf38064 Iustin Pop
  | null ys   = [x,[]]
89 ebf38064 Iustin Pop
  | otherwise = x:sepSplit sep ys
90 ebf38064 Iustin Pop
  where (x, xs) = break (== sep) s
91 ebf38064 Iustin Pop
        ys = drop 1 xs
92 e4f08c46 Iustin Pop
93 19e310cc René Nussbaumer
-- | Simple pluralize helper
94 19e310cc René Nussbaumer
plural :: Int -> String -> String -> String
95 19e310cc René Nussbaumer
plural 1 s _ = s
96 19e310cc René Nussbaumer
plural _ _ p = p
97 19e310cc René Nussbaumer
98 79eef90b Agata Murawska
-- | Ensure a value is quoted if needed.
99 79eef90b Agata Murawska
ensureQuoted :: String -> String
100 79eef90b Agata Murawska
ensureQuoted v = if not (all (\c -> isAlphaNum c || c == '.') v)
101 79eef90b Agata Murawska
                 then '\'':v ++ "'"
102 79eef90b Agata Murawska
                 else v
103 79eef90b Agata Murawska
104 9188aeef Iustin Pop
-- * Mathematical functions
105 9188aeef Iustin Pop
106 185297fa Iustin Pop
-- Simple and slow statistical functions, please replace with better
107 185297fa Iustin Pop
-- versions
108 e4f08c46 Iustin Pop
109 525bfb36 Iustin Pop
-- | Standard deviation function.
110 4715711d Iustin Pop
stdDev :: [Double] -> Double
111 4715711d Iustin Pop
stdDev lst =
112 7570569e Iustin Pop
  -- first, calculate the list length and sum lst in a single step,
113 7570569e Iustin Pop
  -- for performance reasons
114 7570569e Iustin Pop
  let (ll', sx) = foldl' (\(rl, rs) e ->
115 7570569e Iustin Pop
                           let rl' = rl + 1
116 7570569e Iustin Pop
                               rs' = rs + e
117 7570569e Iustin Pop
                           in rl' `seq` rs' `seq` (rl', rs')) (0::Int, 0) lst
118 7570569e Iustin Pop
      ll = fromIntegral ll'::Double
119 7570569e Iustin Pop
      mv = sx / ll
120 7570569e Iustin Pop
      av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst
121 4715711d Iustin Pop
  in sqrt (av / ll) -- stddev
122 dd4c56ed Iustin Pop
123 bfe6c954 Guido Trotter
-- *  Logical functions
124 bfe6c954 Guido Trotter
125 bfe6c954 Guido Trotter
-- Avoid syntactic sugar and enhance readability. These functions are proposed
126 bfe6c954 Guido Trotter
-- by some for inclusion in the Prelude, and at the moment they are present
127 bfe6c954 Guido Trotter
-- (with various definitions) in the utility-ht package. Some rationale and
128 bfe6c954 Guido Trotter
-- discussion is available at <http://www.haskell.org/haskellwiki/If-then-else>
129 bfe6c954 Guido Trotter
130 bfe6c954 Guido Trotter
-- | \"if\" as a function, rather than as syntactic sugar.
131 bfe6c954 Guido Trotter
if' :: Bool -- ^ condition
132 bfe6c954 Guido Trotter
    -> a    -- ^ \"then\" result
133 bfe6c954 Guido Trotter
    -> a    -- ^ \"else\" result
134 bfe6c954 Guido Trotter
    -> a    -- ^ \"then\" or "else" result depending on the condition
135 bfe6c954 Guido Trotter
if' True x _ = x
136 bfe6c954 Guido Trotter
if' _    _ y = y
137 bfe6c954 Guido Trotter
138 5b763470 Iustin Pop
-- * Parsing utility functions
139 5b763470 Iustin Pop
140 525bfb36 Iustin Pop
-- | Parse results from readsPrec.
141 5b763470 Iustin Pop
parseChoices :: (Monad m, Read a) => String -> String -> [(a, String)] -> m a
142 5b763470 Iustin Pop
parseChoices _ _ ((v, ""):[]) = return v
143 5b763470 Iustin Pop
parseChoices name s ((_, e):[]) =
144 5b763470 Iustin Pop
    fail $ name ++ ": leftover characters when parsing '"
145 5b763470 Iustin Pop
           ++ s ++ "': '" ++ e ++ "'"
146 5b763470 Iustin Pop
parseChoices name s _ = fail $ name ++ ": cannot parse string '" ++ s ++ "'"
147 5b763470 Iustin Pop
148 5b763470 Iustin Pop
-- | Safe 'read' function returning data encapsulated in a Result.
149 5b763470 Iustin Pop
tryRead :: (Monad m, Read a) => String -> String -> m a
150 5b763470 Iustin Pop
tryRead name s = parseChoices name s $ reads s
151 c5f7412e Iustin Pop
152 525bfb36 Iustin Pop
-- | Format a table of strings to maintain consistent length.
153 c5f7412e Iustin Pop
formatTable :: [[String]] -> [Bool] -> [[String]]
154 c5f7412e Iustin Pop
formatTable vals numpos =
155 c5f7412e Iustin Pop
    let vtrans = transpose vals  -- transpose, so that we work on rows
156 c5f7412e Iustin Pop
                                 -- rather than columns
157 c5f7412e Iustin Pop
        mlens = map (maximum . map length) vtrans
158 c5f7412e Iustin Pop
        expnd = map (\(flds, isnum, ml) ->
159 c5f7412e Iustin Pop
                         map (\val ->
160 c5f7412e Iustin Pop
                                  let delta = ml - length val
161 c5f7412e Iustin Pop
                                      filler = replicate delta ' '
162 c5f7412e Iustin Pop
                                  in if delta > 0
163 c5f7412e Iustin Pop
                                     then if isnum
164 c5f7412e Iustin Pop
                                          then filler ++ val
165 c5f7412e Iustin Pop
                                          else val ++ filler
166 c5f7412e Iustin Pop
                                     else val
167 c5f7412e Iustin Pop
                             ) flds
168 c5f7412e Iustin Pop
                    ) (zip3 vtrans numpos mlens)
169 c5f7412e Iustin Pop
   in transpose expnd
170 9b9da389 Iustin Pop
171 c3024b7e René Nussbaumer
-- | Constructs a printable table from given header and rows
172 c3024b7e René Nussbaumer
printTable :: String -> [String] -> [[String]] -> [Bool] -> String
173 c3024b7e René Nussbaumer
printTable lp header rows isnum =
174 2cdaf225 Iustin Pop
  unlines . map ((++) lp . (:) ' ' . unwords) $
175 c3024b7e René Nussbaumer
  formatTable (header:rows) isnum
176 c3024b7e René Nussbaumer
177 1cdcf8f3 Iustin Pop
-- | Converts a unit (e.g. m or GB) into a scaling factor.
178 1cdcf8f3 Iustin Pop
parseUnitValue :: (Monad m) => String -> m Rational
179 1cdcf8f3 Iustin Pop
parseUnitValue unit
180 1cdcf8f3 Iustin Pop
  -- binary conversions first
181 1cdcf8f3 Iustin Pop
  | null unit                     = return 1
182 1cdcf8f3 Iustin Pop
  | unit == "m" || upper == "MIB" = return 1
183 1cdcf8f3 Iustin Pop
  | unit == "g" || upper == "GIB" = return kbBinary
184 1cdcf8f3 Iustin Pop
  | unit == "t" || upper == "TIB" = return $ kbBinary * kbBinary
185 1cdcf8f3 Iustin Pop
  -- SI conversions
186 1cdcf8f3 Iustin Pop
  | unit == "M" || upper == "MB"  = return mbFactor
187 1cdcf8f3 Iustin Pop
  | unit == "G" || upper == "GB"  = return $ mbFactor * kbDecimal
188 1cdcf8f3 Iustin Pop
  | unit == "T" || upper == "TB"  = return $ mbFactor * kbDecimal * kbDecimal
189 1cdcf8f3 Iustin Pop
  | otherwise = fail $ "Unknown unit '" ++ unit ++ "'"
190 1cdcf8f3 Iustin Pop
  where upper = map toUpper unit
191 5850e990 Iustin Pop
        kbBinary = 1024 :: Rational
192 5850e990 Iustin Pop
        kbDecimal = 1000 :: Rational
193 1cdcf8f3 Iustin Pop
        decToBin = kbDecimal / kbBinary -- factor for 1K conversion
194 1cdcf8f3 Iustin Pop
        mbFactor = decToBin * decToBin -- twice the factor for just 1K
195 1cdcf8f3 Iustin Pop
196 1cb92fac Iustin Pop
-- | Tries to extract number and scale from the given string.
197 1cb92fac Iustin Pop
--
198 1cb92fac Iustin Pop
-- Input must be in the format NUMBER+ SPACE* [UNIT]. If no unit is
199 1cb92fac Iustin Pop
-- specified, it defaults to MiB. Return value is always an integral
200 1cb92fac Iustin Pop
-- value in MiB.
201 1cb92fac Iustin Pop
parseUnit :: (Monad m, Integral a, Read a) => String -> m a
202 1cb92fac Iustin Pop
parseUnit str =
203 ebf38064 Iustin Pop
  -- TODO: enhance this by splitting the unit parsing code out and
204 ebf38064 Iustin Pop
  -- accepting floating-point numbers
205 1cdcf8f3 Iustin Pop
  case (reads str::[(Int, String)]) of
206 ebf38064 Iustin Pop
    [(v, suffix)] ->
207 ebf38064 Iustin Pop
      let unit = dropWhile (== ' ') suffix
208 1cdcf8f3 Iustin Pop
      in do
209 1cdcf8f3 Iustin Pop
        scaling <- parseUnitValue unit
210 1cdcf8f3 Iustin Pop
        return $ truncate (fromIntegral v * scaling)
211 ebf38064 Iustin Pop
    _ -> fail $ "Can't parse string '" ++ str ++ "'"
212 88a10df5 Iustin Pop
213 88a10df5 Iustin Pop
-- | Unwraps a 'Result', exiting the program if it is a 'Bad' value,
214 88a10df5 Iustin Pop
-- otherwise returning the actual contained value.
215 88a10df5 Iustin Pop
exitIfBad :: String -> Result a -> IO a
216 707cd3d7 Helga Velroyen
exitIfBad msg (Bad s) = exitErr (msg ++ ": " ++ s)
217 88a10df5 Iustin Pop
exitIfBad _ (Ok v) = return v
218 88a10df5 Iustin Pop
219 88a10df5 Iustin Pop
-- | Exits immediately with an error message.
220 88a10df5 Iustin Pop
exitErr :: String -> IO a
221 88a10df5 Iustin Pop
exitErr errmsg = do
222 707cd3d7 Helga Velroyen
  hPutStrLn stderr $ "Error: " ++ errmsg
223 88a10df5 Iustin Pop
  exitWith (ExitFailure 1)
224 88a10df5 Iustin Pop
225 88a10df5 Iustin Pop
-- | Exits with an error message if the given boolean condition if true.
226 88a10df5 Iustin Pop
exitWhen :: Bool -> String -> IO ()
227 88a10df5 Iustin Pop
exitWhen True msg = exitErr msg
228 88a10df5 Iustin Pop
exitWhen False _  = return ()
229 88a10df5 Iustin Pop
230 88a10df5 Iustin Pop
-- | Exits with an error message /unless/ the given boolean condition
231 88a10df5 Iustin Pop
-- if true, the opposite of 'exitWhen'.
232 88a10df5 Iustin Pop
exitUnless :: Bool -> String -> IO ()
233 88a10df5 Iustin Pop
exitUnless cond = exitWhen (not cond)
234 04edfc99 Iustin Pop
235 04edfc99 Iustin Pop
-- | Helper for 'niceSort'. Computes the key element for a given string.
236 04edfc99 Iustin Pop
extractKey :: [Either Integer String]  -- ^ Current (partial) key, reversed
237 04edfc99 Iustin Pop
           -> String                   -- ^ Remaining string
238 04edfc99 Iustin Pop
           -> ([Either Integer String], String)
239 04edfc99 Iustin Pop
extractKey ek [] = (reverse ek, [])
240 04edfc99 Iustin Pop
extractKey ek xs@(x:_) =
241 04edfc99 Iustin Pop
  let (span_fn, conv_fn) = if isDigit x
242 04edfc99 Iustin Pop
                             then (isDigit, Left . read)
243 04edfc99 Iustin Pop
                             else (not . isDigit, Right)
244 04edfc99 Iustin Pop
      (k, rest) = span span_fn xs
245 04edfc99 Iustin Pop
  in extractKey (conv_fn k:ek) rest
246 04edfc99 Iustin Pop
247 04edfc99 Iustin Pop
{-| Sort a list of strings based on digit and non-digit groupings.
248 04edfc99 Iustin Pop
249 04edfc99 Iustin Pop
Given a list of names @['a1', 'a10', 'a11', 'a2']@ this function
250 04edfc99 Iustin Pop
will sort the list in the logical order @['a1', 'a2', 'a10', 'a11']@.
251 04edfc99 Iustin Pop
252 04edfc99 Iustin Pop
The sort algorithm breaks each name in groups of either only-digits or
253 04edfc99 Iustin Pop
no-digits, and sorts based on each group.
254 04edfc99 Iustin Pop
255 04edfc99 Iustin Pop
Internally, this is not implemented via regexes (like the Python
256 04edfc99 Iustin Pop
version), but via actual splitting of the string in sequences of
257 04edfc99 Iustin Pop
either digits or everything else, and converting the digit sequences
258 04edfc99 Iustin Pop
in /Left Integer/ and the non-digit ones in /Right String/, at which
259 04edfc99 Iustin Pop
point sorting becomes trivial due to the built-in 'Either' ordering;
260 04edfc99 Iustin Pop
we only need one extra step of dropping the key at the end.
261 04edfc99 Iustin Pop
262 04edfc99 Iustin Pop
-}
263 04edfc99 Iustin Pop
niceSort :: [String] -> [String]
264 04edfc99 Iustin Pop
niceSort = map snd . sort . map (\s -> (fst $ extractKey [] s, s))
265 04edfc99 Iustin Pop
266 04edfc99 Iustin Pop
-- | Key-version of 'niceSort'. We use 'sortBy' and @compare `on` fst@
267 04edfc99 Iustin Pop
-- since we don't want to add an ordering constraint on the /a/ type,
268 04edfc99 Iustin Pop
-- hence the need to only compare the first element of the /(key, a)/
269 04edfc99 Iustin Pop
-- tuple.
270 04edfc99 Iustin Pop
niceSortKey :: (a -> String) -> [a] -> [a]
271 04edfc99 Iustin Pop
niceSortKey keyfn =
272 04edfc99 Iustin Pop
  map snd . sortBy (compare `on` fst) .
273 04edfc99 Iustin Pop
  map (\s -> (fst . extractKey [] $ keyfn s, s))