AJAX Load stats
mikeytown2 - June 27, 2009 - 02:26
| Project: | Boost |
| Version: | 6.x-1.x-dev |
| Component: | Core compatibility |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Description
AJAX print stats in a block.
easy but slow way
stats.php
<?php
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$block = module_invoke('statistics', 'block', 'view', 0);
print $block['content'];
?>put in a block
<div id="stats">
</div>
<script>
$('#stats').load('/stats.php');
</script>Harder to do, but faster
Coming soon.

#1
Save stats block in a cron run; cron would need to be run often to make this effective.
put this at the top of boost_stats.php; before everything else.
<?php
if ($_GET['stats']) {
// Connect to DB.
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
$stats_block = db_fetch_array(db_query_range("SELECT value FROM {variable} WHERE name = '%s'", 'boost_statistics_html', 0, 1));
$stats_block = unserialize($stats_block['value']);
echo $stats_block;
exit;
}
?>
Have cron run this
<?php$block = module_invoke('statistics', 'block', 'view', 0);
variable_set('boost_statistics_html' ,$block['content']);
?>
Put this in a block
<div id="stats"></div>
<script type="text/javascript">
$('#stats').load('/boost_stats.php?stats=1');
</script>
Be sure to set the correct path, if your in a subdir.
#2
If I do not copy boost_stats.php to my document root, I get a disturbing yellow bar on my status page. If I do copy it to my document root, I get a php call with every load of my static file, which somewhat mitigates the scalability of using a static file cache. Should there be a setting to turn off the warning if I don't want the stats, or maybe move the warning to red text on the performance page?
#3
Disable the stats module, and the yellow bar goes away.
#4
Couple of changes to do:
Check that the statistics module is enabled, not just it's values stored in the variables table.
SELECT 'status' FROM 'system' WHERE 'name' = 'statistics'EDIT: http://api.drupal.org/api/function/module_exists
Break boost_stats.php into functions and allow for a single ajax transaction when updating stats and getting the stats html.
#5
Use $delta value for boost_block(); add #1 to boost_cron().
http://api.drupal.org/api/function/hook_block
Use jquery if/else in drupal_add_js() with
$preprocess = FALSEfor page specific javascript variables.if ( $("#boost-stats").length > 0 ) {$('#boost-stats').load('/boost_stats.php?stats=1&...');
}
else {
load('/boost_stats.php?...');
}
Get rid of boost_footer().
#6
First try at rewrite... it still needs work.
#7
I'm happy with this patch. A new block controls stats.
#8
Need to change code on status page.
#9
make the html the same, thus controlling the display is done entirely in boost_stats.php
#10
in JS if boost_stats.php doesn't exits, fall back to a boost/ajax menu hook that does what I need.
#11
committed patch, still need to do other changes
#12
Step 1: JS that sends info & updates multiple div's.
<script type="text/javascript">$.getJSON("test.php", {nocache: "1", js: "1", nid: "1", q: "front", title: "test", referer: document.referrer}, function(response) {
$.each(response, function(id, html) {
$('#' + id).html(html);
});
});
</script>
Step 2: boost_stats.php does a full drupal bootstrap or not, depending on the settings.
Boot up DB & variable_get('throttle_level', 0) among others. If 1 then use cached html for output, if 0 then do a full bootstrap and return current html for the block(s).
Step 3: Fall back code
Create a menu hook called boost_stats.php that returns the current html for the block(s).
Setp 4: Smarter status page.
#13
Steps 1 & 2 done
#14
committed
#15
Step 2.5: Hide Block
Hide the block if show stats is set to false, via reply from AJAX. JSON returns ID & NULL.
Step 3: Fall back code
Create a menu hook called boost_stats.php that returns the current html for the block(s).
Setp 4: Smarter status page.
#16
use jQuery to hide block.
<script>$.getJSON(Drupal.settings.basePath + "$filename", {nocache: "1", js: "1", nid: Drupal.settings.boost.nid, q: Drupal.settings.boost.q, title: Drupal.settings.boost.title, referer: document.referrer}, function(response) {
$.each(response, function(id, contents) {
if (contents == 'NULL') {
$(id).parent().parent().hide();
}
else {
$(id).html(contents);
}
});
});
</script>
#17
ability for boost_stats.php to handle old (load HTML) and new (JSON) via
js=2vsjs=1. Allows for sites that upgrade to not have flush the cache for boost stats to work.#18
Patch includes Everything:
Ability for boost_stats.php to handle old (load HTML) and new (JSON) via js=2 vs js=1.
Hides the block if show stats is set to false, via reply from AJAX.
Menu hook called boost_stats.php that returns the current html for the block(s).
Got rid of status page warning.
#19
committed
#20
Automatically closed -- issue fixed for 2 weeks with no activity.