File: dbinfo.php

package info (click to toggle)
boinc 7.14.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,132 kB
  • sloc: cpp: 163,589; php: 113,173; ansic: 49,284; pascal: 35,620; xml: 17,864; java: 13,521; python: 6,551; sh: 4,082; perl: 1,843; makefile: 1,796; objc: 1,543; sql: 959; csh: 126; lisp: 47
file content (234 lines) | stat: -rw-r--r-- 7,177 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php

require_once("../inc/util_ops.inc");
require_once("../inc/db_ops.inc");

class DB_REC {

    var $name;
    var $data_size;
    var $index_size;
    var $total_size;
    var $rows;
    var $size_per_row;

    function __construct($n, $d, $i, $t, $r, $s) {
        $this->name = $n;
        $this->data_size = $d;
		$this->index_size = $i;
		$this->total_size = $t;
		$this->rows = $r;
		$this->size_per_row = $s;
    }
    function __destruct() {
    }
}


$db_name = parse_config(get_config(), "<db_name>");

db_init();

admin_page_head("BOINC Database Info");

// if you have other db's just add more get_db_info lines
$db_rec = get_db_info($db_name);

// show_db_info($db_name, $db_rec); 
sort_db_info($db_name, $db_rec);

admin_page_tail();


// returns formatted data size
function size_format($size){
	$retval = 0;

	$KB = 1024;
    $MB = 1024*1024;
    $GB = 1024*1024*1024;
	$TB = 1024*1024*1024*1024;
	
    if ($size < $KB) {
	    $retval = $size;
    } elseif (($size > $KB) && ($size < $MB)) {
		$retval = sprintf("%.0fK", ($size / $KB));    
    } elseif ( ($size >= $MB) && ($size < $GB)) {
		$retval = sprintf("%.2fMB", ($size / $MB));
    } elseif ( ($size >= $GB) && ($size < $TB)) {
		$retval = sprintf("%.2fGB", ($size / $GB));
	} elseif ( $size >= $TB ) {
		$retval = sprintf("%.2fTB", ($size / $TB));
	}
	return $retval;
}


// returns the DB data structure as DB_REC
function get_db_info($db_name) 
{
	// Carl grabbed this from the mysql.com boards http://dev.mysql.com/doc/refman/5.0/en/show-table-status.html  
    $result = _mysql_query("SHOW TABLE STATUS FROM $db_name");

    // SQL output
    // mysql> show table status from [table_name];
    // | Name | Engine | Version | Row_format | Rows    
	// | Avg_row_length | Data_length | Max_data_length  
	// | Index_length | Data_free  | Auto_increment | Create_time         
	// | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
    //

    $gdata  = 0;
    $gindex = 0;
    $gtotal = 0;
    $grows  = 0;

	$i = 0;
	$db_rec = array();
	while($myarr = _mysql_fetch_assoc($result)) {

		// sum grand totals
		$total  =  $myarr["Data_length"] + $myarr["Index_length"];
		$gindex += $myarr["Index_length"];
        $gdata  += $myarr["Data_length"];
		$grows  += $myarr["Rows"]; 
		$gtotal += $total;
 
	    $db_rec[$i] = new DB_REC ($myarr["Name"], $myarr["Data_length"], $myarr["Index_length"], $total, $myarr["Rows"], $myarr["Avg_row_length"] );	
		$i++;
	}

    $db_rec[$i] = new DB_REC ("Total", $gdata, $gindex, $gtotal, $grows, "" );	

	return $db_rec;
}


// shows the plain db structure
function show_db_info($db_name, $db_rec) 
{ 

	echo "<table cols=6>";
	echo "<tr>";
	echo "<th colspan=6> Database $db_name </th>";
	echo "</tr>";

	echo "<tr>";
	echo "<th>Table</th>";
	echo "<th>Data Size</th>";
	echo "<th>Index Size</th>";
	echo "<th>Total Size</th>";
	echo "<th>Total Rows</th>";
	echo "<th>Avg. Size per Row</th>";
	echo "</tr>";

	for ($i = 0; $i < sizeof($db_rec)-1; $i++){ 
		echo "<tr>";
		echo "<td align=left valign=top class=fieldname>" . $db_rec[$i]->name . "</td>";
		echo "<td align=left valign=top class=fieldname>" . size_format($db_rec[$i]->data_size)  . "</td>";
		echo "<td align=left valign=top class=fieldname>" . size_format($db_rec[$i]->index_size) . "</td>";
		echo "<td align=left valign=top class=fieldname>" . size_format($db_rec[$i]->total_size) . "</td>";
        echo "<td align=left valign=top class=fieldname>" . number_format($db_rec[$i]->rows)     . "</td>";
        echo "<td align=left valign=top class=fieldname>" . size_format($db_rec[$i]->size_per_row) . "</td>";
		echo "</tr>";
	}

	// Last record is just a summary
	$i = sizeof($db_rec)-1;
	echo "<tr>";
    echo "<th align=left>" . $db_rec[$i]->name . "</th>";
    echo "<th align=left>" . size_format($db_rec[$i]->data_size)  . "</th>";
    echo "<th align=left>" . size_format($db_rec[$i]->index_size) . "</th>";
    echo "<th align=left>" . size_format($db_rec[$i]->total_size) . "</th>";
    echo "<th align=left>" . number_format($db_rec[$i]->rows)     . "</th>";
    echo "<th align=left></th>";
	echo "</tr>";

    echo "</table>";
}



// NB: same as show_db_info but with sortable cloumns
function sort_db_info($db_name, $db_rec) 
{ 
	// sort 
	$file_list = array();
    $file_sort = array();

	$sort =  get_str("sort", true);
	$r = get_str("r", true);

	// check if its empty
	if(empty($sort)) $sort = "name";
	// check for allowed keys 
	if ((strcmp($sort, "name")!=0) && 
		(strcmp($sort, "data_size")!=0) && 
		(strcmp($sort, "index_size")!=0) && 
		(strcmp($sort, "total_size")!=0)  && 
		(strcmp($sort, "rows")!=0) && 
		(strcmp($sort, "size_per_row")!=0)) 
		$sort = "name";
	if(empty($r)) $r=0;

	for ($i = 0; $i < sizeof($db_rec)-1; $i++){
		$file_details["name"]         = $db_rec[$i]->name;
		$file_details["data_size"]    = $db_rec[$i]->data_size;
		$file_details["index_size"]   = $db_rec[$i]->index_size;
		$file_details["total_size"]   = $db_rec[$i]->total_size;
	    $file_details["rows"]         = $db_rec[$i]->rows;
		$file_details["size_per_row"] = $db_rec[$i]->size_per_row;

		$file_list[$i] = $file_details;
		$key = strtolower($file_details[$sort]);
		$file_sort[$i] = $key;
	}
	
	if($r)arsort($file_sort);
    else  asort($file_sort);
	// -- end sort 

	echo "<table cols=6>";
	echo "<tr>";
	echo "<th colspan=6> Database $db_name </th>";
	echo "</tr>";

	echo "<tr>";
	echo "<th><a href='dbinfo.php?sort=name&r=" . (!$r) . "'>Table </a></th>";
	echo "<th><a href='dbinfo.php?sort=data_size&r=" . (!$r) . "'>Data Size</a></th>";
	echo "<th><a href='dbinfo.php?sort=index_size&r=" . (!$r) . "'>Index Size</a></th>";
	echo "<th><a href='dbinfo.php?sort=total_size&r=" . (!$r) . "'>Total Size</a></th>";
	echo "<th><a href='dbinfo.php?sort=rows&r=" . (!$r) . "'>Total Rows</a></th>";
	echo "<th><a href='dbinfo.php?sort=size_per_row&r=" . (!$r) . "'>Avg. Size per Row</a></th>";
	echo "</tr>";

	$i = 0;
	while ( list($key, $value) = each($file_sort) ) {
		$value = $file_list[$key];
		echo "<tr>"; 
		echo "<td align=left valign=top class=fieldname>" . $value["name"] . "</td>";
		echo "<td align=left valign=top class=fieldname>" . size_format($value["data_size"])  . "</td>";
		echo "<td align=left valign=top class=fieldname>" . size_format($value["index_size"]) . "</td>";
		echo "<td align=left valign=top class=fieldname>" . size_format($value["total_size"]) . "</td>";
        echo "<td align=left valign=top class=fieldname>" . number_format($value["rows"])     . "</td>";
        echo "<td align=left valign=top class=fieldname>" . size_format($value["size_per_row"]) . "</td>";
		echo "</tr>";
		$i++;
	}

	// Last record is just a summary
	$i = sizeof($db_rec)-1;
	echo "<tr>";
    echo "<th align=left>" . $db_rec[$i]->name . "</th>";
    echo "<th align=left>" . size_format($db_rec[$i]->data_size)  . "</th>";
    echo "<th align=left>" . size_format($db_rec[$i]->index_size) . "</th>";
    echo "<th align=left>" . size_format($db_rec[$i]->total_size) . "</th>";
    echo "<th align=left>" . number_format($db_rec[$i]->rows)     . "</th>";
    echo "<th align=left></th>";
	echo "</tr>";

    echo "</table>";
}


?>