cvs: pearweb /public_html package-info.php package-stats-graph.php package-stats.php

From: Date: Sun, 02 Jun 2002 19:46:42 +0000
Subject: cvs: pearweb /public_html package-info.php package-stats-graph.php package-stats.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4378@lists.php.net to get a copy of this message
richard Sun Jun 2 15:46:42 2002 EDT Modified files: /pearweb/public_html package-info.php package-stats-graph.php package-stats.php Log: * Removed need for authentication on package stats * Added link to stats page to info page * Added options to manipulate graph on stats page * Added graph caching (5 mins)

Index: pearweb/public_html/package-info.php diff -u pearweb/public_html/package-info.php:1.16 pearweb/public_html/package-info.php:1.17 --- pearweb/public_html/package-info.php:1.16 Tue May 28 11:30:31 2002 +++ pearweb/public_html/package-info.php Sun Jun 2 15:46:42 2002 @@ -191,10 +191,16 @@ $get_link = make_link("/get/$name", 'Download Latest'); $changelog_link = make_link("package-changelog.php?pacid=$pacid", 'ChangeLog'); + +// Category ID for stats link +$cid = $dbh->getOne('SELECT category FROM packages WHERE id = ' . $pacid); ?> - <td width="33%" align="center">[ <?php print $get_link; ?> ]</td> - <td width="33%" align="center"><?php print $cvs_link;?></td> - <td width="33%" align="center">[ <?php print $changelog_link;?> ]</td> + <td width="50%" align="center">[ <?php print $get_link; ?> ]</td> + <td width="50%" align="center">[ <?php print $changelog_link;?> ]</td> +</tr> +<tr> + <td width="50%" align="center"><nobr><?php print $cvs_link;?></nobr></td> + <td width="50%" align="center"><nobr>[ <a href="package-stats.php?pid=<?=$pacid?>&rid=&cid=<?=$cid?>">View package statistics</a> ]</nobr></td> </tr> </table> Index: pearweb/public_html/package-stats-graph.php diff -u pearweb/public_html/package-stats-graph.php:1.3 pearweb/public_html/package-stats-graph.php:1.4 --- pearweb/public_html/package-stats-graph.php:1.3 Thu May 30 06:55:56 2002 +++ pearweb/public_html/package-stats-graph.php Sun Jun 2 15:46:42 2002 @@ -2,8 +2,8 @@ /** * Filename.......: package-stats-graph.php * Project........: Pearweb -* Last Modified..: $Date: 2002/05/30 10:55:56 $ -* CVS Revision...: $Revision: 1.3 $ +* Last Modified..: $Date: 2002/06/02 19:46:42 $ +* CVS Revision...: $Revision: 1.4 $ * * Use JPGraph library to display graphical * stats of downloads. @@ -19,67 +19,112 @@ include ("../include/jpgraph/jpgraph_bar.php"); /** -* Determine the stats based on the supplied -* package id (pid) and release id (rid). -* Release id may be empty implying all releases. +* Cache time in secs */ - for ($i=date('n'), $year=date('Y'); count(@$data_x) < 12; $i--) { + $cache_time = 300; + +/** +* This is the x axis labels. May change when +* selectable dates is added. +*/ + for ($i=date('n'), $year=date('Y'); count(@$x_axis) < 12; $i--) { if ($i == 0) { $i = 12; $year--; } - $data_x[$i] = date("M", mktime(0,0,0,$i,1,$year)); - $data_y[$i] = 0; + $x_axis[$i] = date("M", mktime(0,0,0,$i,1,$year)); } - $sql = sprintf("SELECT UNIX_TIMESTAMP(d.dl_when) AS date, COUNT(*) AS downloads - FROM packages p, downloads d - WHERE d.package = p.id - AND p.id = %s - %s - GROUP BY MONTH(d.dl_when) - ORDER BY YEAR(d.dl_when) DESC, MONTH(d.dl_when) DESC", - $_GET['pid'], - $release_clause = !empty($_GET['rid']) ? 'AND d.release = ' . $_GET['rid'] : ''); - - if ($result = $dbh->query($sql)) { - while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) { - $data_y[date('n', $row['date'])] = $row['downloads']; - } - } - - $data_x = array_reverse(array_values($data_x)); - $data_y = array_reverse(array_values($data_y)); +/** +* Determine the stats based on the supplied +* package id (pid) and release id (rid). +* If release id is empty a group bar chart is +* drawn with each release having a different +* color. +*/ + if (!empty($_GET['releases'])) { + $releases = explode(',', $_GET['releases']); + } + + foreach ($releases as $release) { + $y_axis = array(); + list($rid, $colour) = explode('_', $release); + $colour = '#' . $colour; + foreach (array_keys($x_axis) as $key) { + $y_axis[$key] = 0; + } + + $sql = sprintf("SELECT UNIX_TIMESTAMP(d.dl_when) AS date, COUNT(*) AS downloads + FROM packages p, downloads d + WHERE d.package = p.id + AND p.id = %s + %s + GROUP BY MONTH(d.dl_when) + ORDER BY YEAR(d.dl_when) DESC, MONTH(d.dl_when) DESC", + $_GET['pid'], + $release_clause = $rid > 0 ? 'AND d.release = ' . $rid : ''); + + if ($result = $dbh->query($sql)) { + while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) { + $y_axis[date('n', $row['date'])] = $row['downloads']; + } + } + + // Create the bar pot + $bplots[$rid] = new BarPlot(array_reverse(array_values($y_axis))); + $bplots[$rid]->SetWidth(0.6); + $bplots[$rid]->SetFillGradient("white", $colour, GRAD_HOR); + //$bplot->setFillColor("#339900"); + $bplots[$rid]->SetColor("black"); + } + + $x_axis = array_reverse(array_values($x_axis)); + $bplots = array_values($bplots); + + /** + * Get package name + */ + $package_name = $dbh->getOne('SELECT name FROM packages WHERE id = ' . $_GET['pid']); + $package_rel = !empty($_GET['rid']) ? $dbh->getOne('SELECT version FROM releases WHERE id = ' . $_GET['rid']) : ''; /** * Go through setting up the graph */ - // Main graph object - $graph = new Graph(543,200,"jabba", 5); + if (!DEVBOX) { + // Send some caching headers to prevent unnecessary requests + header('Last-Modified: ' . date('r', md5($_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING']))); + header('Expires: ' . date('r', time() + $cache_time)); + header('Cache-Control: public, max-age=' . $cache_time); + header('Pragma: cache'); + + // Main graph object + $graph = new Graph(543, 200, md5($_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING']), $cache_time); + } else { + // Main graph object + $graph = new Graph(543, 200); + } $graph->img->SetMargin(40,20,30,30); $graph->SetScale("textlin"); $graph->SetMarginColor("#cccccc"); // Set up the title for the graph - $graph->title->Set("Download statistics for " . $dbh->getOne('SELECT name FROM packages WHERE id = ' . $_GET['pid'])); + $graph->title->Set(sprintf("Download statistics for %s %s", $package_name, $package_rel)); $graph->title->SetColor("black"); // Show 0 label on Y-axis (default is not to show) $graph->yscale->ticks->SupressZeroLabel(false); // Setup X-axis labels - $graph->xaxis->SetTickLabels($data_x); - - // Create the bar pot - $bplot = new BarPlot($data_y); - $bplot->SetWidth(0.6); - $bplot->SetFillGradient("white","#339900",GRAD_HOR); - //$bplot->setFillColor("#339900"); - - // Set color for the frame of each bar - $bplot->SetColor("black"); - $graph->Add($bplot); + $graph->xaxis->SetTickLabels($x_axis); + // Add the grouped or single bar chartplot + if (count($bplots) > 1) { + $gbplot = new GroupBarPlot($bplots); + $graph->Add($gbplot); + } else { + $graph->Add($bplots[0]); + } + // Finally send the graph to the browser $graph->Stroke(); ?> Index: pearweb/public_html/package-stats.php diff -u pearweb/public_html/package-stats.php:1.8 pearweb/public_html/package-stats.php:1.9 --- pearweb/public_html/package-stats.php:1.8 Tue May 28 15:08:40 2002 +++ pearweb/public_html/package-stats.php Sun Jun 2 15:46:42 2002 @@ -15,10 +15,9 @@ +----------------------------------------------------------------------+ | Authors: Martin Jansen <mj@php.net> | +----------------------------------------------------------------------+ - $Id: package-stats.php,v 1.8 2002/05/28 19:08:40 imajes Exp $ + $Id: package-stats.php,v 1.9 2002/06/02 19:46:42 richard Exp $ */ -auth_require(); require_once "HTML/Form.php"; response_header("Package statistics"); @@ -176,7 +175,116 @@ /** * Print the graph */ - printf('<br /><img src="package-stats-graph.php?pid=%s&rid=%s" width="543" height="200" alt="">', $_GET['pid'], $_GET['rid']); + printf('<br /><img src="package-stats-graph.php?pid=%s&releases=%s_339900" name="stats_graph" width="543" height="200" alt="">', $_GET['pid'], (int)$_GET['rid']); + + /** + * Print the graph control stuff + */ + $releases = $dbh->getAll('SELECT id, version FROM releases WHERE package = ' . $_GET['pid'], DB_FETCHMODE_ASSOC); + ?> +<br /><br /> +<script language="JavaScript" type="text/javascript"> +<!-- + function clearGraphList() + { + graphForm = document.forms['graph_control']; + for (i=0; i<graphForm.graph_list.options.length; i++) { + graphForm.graph_list.options[i] = null; + } + } + + function addGraphItem() + { + graphForm = document.forms['graph_control']; + selectedRelease = graphForm.releases.options[graphForm.releases.selectedIndex]; + selectedColour = graphForm.colours.options[graphForm.colours.selectedIndex]; + + if (selectedRelease.value != "" && selectedColour.value != "") { + newText = 'Release ' + selectedRelease.text + ' in ' + selectedColour.text; + newValue = selectedRelease.value + '_' + selectedColour.value; + graphForm.graph_list.options[graphForm.graph_list.options.length] = new Option(newText, newValue); + + } else { + alert('Please select a release and a colour!'); + } + } + + function removeGraphItem() + { + graphForm = document.forms['graph_control']; + graphList = graphForm.graph_list; + + if (graphList.selectedIndex != null) { + graphList.options[graphList.selectedIndex] = null; + } + } + + function updateGraph() + { + graphForm = document.forms['graph_control']; + releases_qs = ''; + + if (graphForm.graph_list.options.length) { + for (i=0; i<graphForm.graph_list.options.length; i++) { + if (i == 0) { + releases_qs += graphForm.graph_list.options[i].value; + } else { + releases_qs += ',' + graphForm.graph_list.options[i].value; + } + } + graphForm.update.value = 'Updating...'; + document.images['stats_graph'].src = 'package-stats-graph.php?pid=<?=$_GET['pid']?>&releases=' + releases_qs; + graphForm.update.value = 'Update graph'; + + } else { + alert('Please select one or more releases to show!'); + } + } +//--> +</script> +<form name="graph_control"> <!-- No action as it should never be submitted --> +<table border="0"> + <tr> + <td colspan="2"> + Show graph of:<br /> + <select style="width: 543px" name="graph_list" size="5"> + </select> + </td> + </tr> + <tr> + <td valign="top"> + Release: + <select align="absmiddle" name="releases"> + <option value="">Select...</option> + <option value="0">All</option> + <?foreach($releases as $r):?> + <option value="<?=$r['id']?>"><?=$r['version']?></option> + <?endforeach?> + </select> + Colour: + <select align="absmiddle" name="colours"> + <option>Select...</option> + <option value="339900">Green</option> + <option value="dd0000">Red</option> + <option value="003399">Blue</option> + <option value="000000">Black</option> + <option value="999900">Yellow</option> + </select> + </td> + <td align="right"> + <input type="submit" style="width: 100px" name="add" value="Add" onclick="addGraphItem(); return false;"> + <input type="submit" style="width: 100px" name="remove" value="Remove" onclick="removeGraphItem(); return false" /> + </td> + </tr> + <tr> + <td align="center" colspan="2"> + <input type="submit" name="update" value="Update graph" onclick="updateGraph(); return false" /> + </td> + </tr> +</table> +</form> +<br /> + <?php } } else {
« previous php.pear.cvs (#4378) next »