~percona-toolkit-dev/percona-toolkit/fix-empty-table-bug-987393

« back to all changes in this revision

Viewing changes to lib/bash/report_formatting.sh

  • Committer: Daniel Nichter
  • Date: 2012-04-03 16:14:55 UTC
  • mfrom: (217.6.22 2.0.3)
  • Revision ID: daniel@percona.com-20120403161455-ntv33vju9o6njtqv
Merge summary-tools-2.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This program is copyright 2011 Percona Inc.
 
2
# Feedback and improvements are welcome.
 
3
#
 
4
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 
5
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 
6
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
7
#
 
8
# This program is free software; you can redistribute it and/or modify it under
 
9
# the terms of the GNU General Public License as published by the Free Software
 
10
# Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
 
11
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
 
12
# licenses.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along with
 
15
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
16
# Place, Suite 330, Boston, MA  02111-1307  USA.
 
17
# ###########################################################################
 
18
# report_formatting package
 
19
# ###########################################################################
 
20
 
 
21
# Package: report_formatting
 
22
# Common report formatting functions for the summary tools.
 
23
 
 
24
set -u
 
25
 
 
26
POSIXLY_CORRECT=1
 
27
export POSIXLY_CORRECT
 
28
 
 
29
fuzzy_formula='
 
30
   rounded = 0;
 
31
   if (fuzzy_var <= 10 ) {
 
32
      rounded   = 1;
 
33
   }
 
34
   factor = 1;
 
35
   while ( rounded == 0 ) {
 
36
      if ( fuzzy_var <= 50 * factor ) {
 
37
         fuzzy_var = sprintf("%.0f", fuzzy_var / (5 * factor)) * 5 * factor;
 
38
         rounded   = 1;
 
39
      }
 
40
      else if ( fuzzy_var <= 100  * factor) {
 
41
         fuzzy_var = sprintf("%.0f", fuzzy_var / (10 * factor)) * 10 * factor;
 
42
         rounded   = 1;
 
43
      }
 
44
      else if ( fuzzy_var <= 250  * factor) {
 
45
         fuzzy_var = sprintf("%.0f", fuzzy_var / (25 * factor)) * 25 * factor;
 
46
         rounded   = 1;
 
47
      }
 
48
      factor = factor * 10;
 
49
   }'
 
50
 
 
51
# Does fuzzy rounding: rounds to nearest interval, but the interval gets larger
 
52
# as the number gets larger.  This is to make things easier to diff.
 
53
fuzz () {
 
54
   awk -v fuzzy_var="$1" "BEGIN { ${fuzzy_formula} print fuzzy_var;}"
 
55
}
 
56
 
 
57
# Fuzzy computes the percent that $1 is of $2
 
58
fuzzy_pct () {
 
59
   local pct="$(awk -v one="$1" -v two="$2" 'BEGIN{ if (two > 0) { printf "%d", one/two*100; } else {print 0} }')";
 
60
   echo "$(fuzz "${pct}")%"
 
61
}
 
62
 
 
63
# Prints a section header. All spaces in the string passed in are replaced
 
64
# with #'s and all underscores with spaces.
 
65
section () {
 
66
   local str="$1"
 
67
   awk -v var="${str} _" 'BEGIN {
 
68
      line = sprintf("# %-60s", var);
 
69
      i = index(line, "_");
 
70
      x = substr(line, i);
 
71
      gsub(/[_ \t]/, "#", x);
 
72
      printf("%s%s\n", substr(line, 1, i-1), x);
 
73
   }'
 
74
}
 
75
 
 
76
NAME_VAL_LEN=12
 
77
name_val () {
 
78
   # We use $NAME_VAL_LEN here because the two summary tools, as well as
 
79
   # the tests, use diffent widths.
 
80
   printf "%+*s | %s\n" "${NAME_VAL_LEN}" "$1" "$2"
 
81
}
 
82
 
 
83
# Sub: shorten
 
84
#   Shorten a value in bytes to another representation.
 
85
 
86
shorten() {
 
87
   local num="$1"
 
88
   local prec="${2:-2}"
 
89
   local div="${3:-1024}"
 
90
 
 
91
   echo "$num" | awk -v prec="$prec" -v div="$div" '
 
92
   {
 
93
      size = 4;
 
94
      val  = $1;
 
95
 
 
96
      unit = val >= 1099511627776 ? "T" : val >= 1073741824 ? "G" : val >= 1048576 ? "M" : val >= 1024 ? "k" : "";
 
97
 
 
98
      while ( int(val) && !(val % 1024) ) {
 
99
         val /= 1024;
 
100
      }
 
101
 
 
102
      while ( val > 1000 ) {
 
103
         val /= div;
 
104
      }
 
105
 
 
106
      printf "%.*f%s", prec, val, unit;
 
107
   }
 
108
   '
 
109
}
 
110
 
 
111
group_concat () {
 
112
   sed -e '{H; $!d;}' -e 'x' -e 's/\n[[:space:]]*\([[:digit:]]*\)[[:space:]]*/, \1x/g' -e 's/[[:space:]][[:space:]]*/ /g' -e 's/, //' "${1}"
 
113
}
 
114
 
 
115
# ###########################################################################
 
116
# End report_formatting package
 
117
# ###########################################################################