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

mikeytown2 - June 27, 2009 - 05:51

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

brianmercer - June 27, 2009 - 18:10

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

mikeytown2 - June 27, 2009 - 19:28

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

#4

mikeytown2 - June 30, 2009 - 04:42

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

mikeytown2 - June 29, 2009 - 22:17

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().

#6

mikeytown2 - June 30, 2009 - 09:40
Status:active» needs work

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

AttachmentSize
boost-503628.patch 13.04 KB

#7

mikeytown2 - July 1, 2009 - 05:53
Status:needs work» needs review

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

AttachmentSize
boost-503628-1.patch 14.87 KB

#8

mikeytown2 - July 1, 2009 - 09:03

Need to change code on status page.

#9

mikeytown2 - July 6, 2009 - 06:00

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

#10

mikeytown2 - July 7, 2009 - 09:00

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

#11

mikeytown2 - July 10, 2009 - 00:23
Status:needs review» needs work

committed patch, still need to do other changes

#12

mikeytown2 - July 17, 2009 - 08:04

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

mikeytown2 - July 21, 2009 - 22:39
Status:needs work» needs review

Steps 1 & 2 done

AttachmentSize
boost-503628.patch 9.21 KB

#14

mikeytown2 - July 22, 2009 - 03:08
Status:needs review» active

committed

#15

mikeytown2 - July 22, 2009 - 04:56

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

mikeytown2 - July 23, 2009 - 10:03

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

mikeytown2 - July 24, 2009 - 02:54

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.

#18

mikeytown2 - July 27, 2009 - 23:43
Status:active» needs review

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.

AttachmentSize
boost-503628.patch 11.16 KB

#19

mikeytown2 - July 28, 2009 - 01:11
Status:needs review» fixed

committed

#20

System Message - August 11, 2009 - 01:20
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.