user web: allow sorting of columns of host data for a given user. I

have done this in a general way, which should permit any page to
be easily modified to permit sorting by any column.

svn path=/trunk/boinc/; revision=9516
This commit is contained in:
Bruce Allen 2006-02-20 19:11:51 +00:00
parent 49f1a1be8f
commit 24a80c66a1
3 changed files with 101 additions and 36 deletions

View File

@ -152,29 +152,6 @@ function show_host($host, $private, $ipprivate) {
}
// The following is used to show a user's hosts
//
function user_host_table_start($private) {
start_table();
echo "<tr>";
echo "<th>Computer ID<br><font size=-2>Click for more info</font></th>\n";
if ($private) {
echo "<th>Name</th>\n
";
} else {
echo "<th>Rank</th>";
}
echo "
<th>Recent average credit</th>
<th>Total credit</th>
<th>CPU type</th>
<th>Operating system</th>
";
$config = get_config();
if (parse_bool($config, "show_results")) echo "<th>Results</th>";
echo "<th>Last contact</th></tr>";
}
// the following is used for list of top hosts
//
function top_host_table_start($sort_by) {

View File

@ -536,4 +536,40 @@ function is_ascii($str) {
}
return true;
}
// This function takes as input a GET variable name X and value Y.
// It returns the corresponding text GET variable string, appended with
// any existing GET variable names and values, with &X=Y.
//
// This is useful for constructing urls for sorting a table by different
// columns.
//
function make_GET_list($variable_name, $variable_value) {
$retval="";
$sepchar='?';
$modified=false;
foreach ($_GET as $key => $value) {
$retval .= "$sepchar"."$key=";
$sepchar='&';
if ($key==$variable_name) {
$modified=true;
if ($value!=$variable_value) {
$retval .= "$variable_value";
} else {
$retval .= "$variable_value"."_reversed";
}
}
else {
$retval .= "$value";
}
}
if (!$modified) $retval .= "$sepchar$variable_name=$variable_value";
return $retval;
}
function link_with_GET_variables($text, $baseurl, $variable_name, $variable_value) {
$list=make_GET_list($variable_name, $variable_value);
return "<a href=\"$baseurl$list\">$text</a>";
}
?>

View File

@ -7,6 +7,38 @@ require_once("../inc/util.inc");
require_once("../inc/host.inc");
require_once("../inc/cache.inc");
function more_or_less($show_all) {
if ($show_all) {
echo "<p>Show: All hosts | ".link_with_GET_variables("Only hosts active in past 30 days<br>", "hosts_user.php", 'show_all', '0');
} else {
echo "<p>Show: ".link_with_GET_variables("All hosts", "hosts_user.php", 'show_all', '1')." | Only hosts active in past 30 days<br>";;
}
}
// The following is used to show a user's hosts
//
function user_host_table_start($private) {
start_table();
echo "<tr>";
echo "<th>".link_with_GET_variables("Computer ID", "hosts_user.php", 'sort', 'id')."<br><font size=-2>Click for more info</font></th>\n";
if ($private) {
echo "<th>".link_with_GET_variables("Name", "hosts_user.php", 'sort', 'name')."</th>\n
";
} else {
echo "<th>Rank</th>";
}
echo "
<th>".link_with_GET_variables("Recent average credit", "hosts_user.php", 'sort', 'expavg_credit')."</th>
<th>".link_with_GET_variables("Total credit", "hosts_user.php", 'sort', 'total_credit')."</th>
<th>".link_with_GET_variables("CPU type", "hosts_user.php", 'sort', 'cpu')."</th>
<th>".link_with_GET_variables("Operating system", "hosts_user.php", 'sort', 'os')."</th>
";
$config = get_config();
if (parse_bool($config, "show_results")) echo "<th>Results</th>";
echo "<th>".link_with_GET_variables("Last contact", "hosts_user.php", 'sort', 'rpc_time')."</th>";
}
db_init();
$userid = get_int("userid", true);
$show_all = get_int("show_all", true);
@ -21,11 +53,18 @@ if ($userid) {
if (!$user) {
error_page("No such user");
}
$cache_args = "userid=$userid&show_all=$show_all";
$caching=true;
$list=make_GET_list("", "");
if (!strncmp($list, "?", 1)) {
$cache_args=substr($list, 1);
} else {
// should never happen
$cache_args="userid=$userid&show_all=$show_all";
}
start_cache(USER_PAGE_TTL, $cache_args);
if ($user->show_hosts) {
page_head("Computers belonging to $user->name");
more_or_less($show_all);
user_host_table_start(false);
} else {
page_head("Computers hidden");
@ -40,36 +79,49 @@ if ($userid) {
$caching=false;
$userid = $user->id;
page_head("Your computers");
more_or_less($show_all);
user_host_table_start(true);
$private = true;
}
$i = 1;
$sort_clause = "rpc_time desc";
$sort = get_str("sort", true);
if ($sort == "total_credit") $sort_clause = "total_credit desc";
if ($sort == "total_credit_reversed") $sort_clause = "total_credit";
if ($sort == "expavg_credit") $sort_clause = "expavg_credit desc";
if ($sort == "expavg_credit_reversed") $sort_clause = "expavg_credit";
if ($sort == "name") $sort_clause = "domain_name";
if ($sort == "name_reversed") $sort_clause = "domain_name desc";
if ($sort == "id") $sort_clause = "id";
if ($sort == "id_reversed") $sort_clause = "id desc";
if ($sort == "expavg_credit") $sort_clause = "expavg_credit desc";
if ($sort == "expavg_credit_reversed") $sort_clause = "expavg_credit ";
if ($sort == "cpu") $sort_clause = "p_model";
if ($sort == "cpu_reversed") $sort_clause = "p_model desc";
if ($sort == "os") $sort_clause = "os_name, os_version";
if ($sort == "os_reversed") $sort_clause = "os_name desc, os_version";
if ($sort == "rpc_time") $sort_clause = "rpc_time desc";
if ($sort == "rpc_time_reversed") $sort_clause = "rpc_time";
$now = time();
$more_hosts = false;
$old_hosts=0;
$i = 1;
$result = mysql_query("select * from host where userid=$userid order by $sort_clause");
while ($host = mysql_fetch_object($result)) {
if (!$show_all && (($now - $host->rpc_time) > 30*86400)) {
$more_hosts = true;
continue;
$is_old=false;
if (($now - $host->rpc_time) > 30*86400) {
$is_old=true;
$old_hosts++;
}
if (!$show_all && $is_old) continue;
show_host_row($host, $i, $private, false);
$i++;
}
mysql_free_result($result);
echo "</table>\n";
if ($more_hosts) {
echo "<p>
Hosts older than 30 days not shown.
<a href=hosts_user.php?userid=$userid&show_all=1>Show all hosts</a>.
";
}
if ($old_hosts>0) more_or_less($show_all);
if ($caching) {
page_tail(true);
end_cache(USER_PAGE_TTL, $cache_args);