AJAX print stats in a block.

easy but slow way
stats.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.

Comments

mikeytown2’s picture

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.

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

    $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.

brianmercer’s picture

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?

mikeytown2’s picture

Disable the stats module, and the yellow bar goes away.

mikeytown2’s picture

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.

mikeytown2’s picture

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 = FALSE for 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().

mikeytown2’s picture

Status: Active » Needs work
StatusFileSize
new13.04 KB

First try at rewrite... it still needs work.

mikeytown2’s picture

Status: Needs work » Needs review
StatusFileSize
new14.87 KB

I'm happy with this patch. A new block controls stats.

mikeytown2’s picture

Need to change code on status page.

mikeytown2’s picture

make the html the same, thus controlling the display is done entirely in boost_stats.php

mikeytown2’s picture

in JS if boost_stats.php doesn't exits, fall back to a boost/ajax menu hook that does what I need.

mikeytown2’s picture

Status: Needs review » Needs work

committed patch, still need to do other changes

mikeytown2’s picture

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.

mikeytown2’s picture

Status: Needs work » Needs review
StatusFileSize
new9.21 KB

Steps 1 & 2 done

mikeytown2’s picture

Status: Needs review » Active

committed

mikeytown2’s picture

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.

mikeytown2’s picture

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>
mikeytown2’s picture

ability for boost_stats.php to handle old (load HTML) and new (JSON) via js=2 vs js=1. Allows for sites that upgrade to not have flush the cache for boost stats to work.

mikeytown2’s picture

Status: Active » Needs review
StatusFileSize
new11.16 KB

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.

mikeytown2’s picture

Status: Needs review » Fixed

committed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.