Daily Stats Snapshot

Here are some nice graphs that I made. :D They run on a cron job so they are updated every 24 hours. :)
all.png

discussions.png

members.png

Raw data is here:
http://erikswan.net/xenstats/raw.html
 
Here are some nice graphs that I made. :D They run on a cron job so they are updated every 24 hours. :)
Raw data is here:
http://erikswan.net/xenstats/raw.html

Raw Data is available at - http://xenaddicts.com/xen/?html=1 I have a script working out the missing fields and filling them in though...

Discussed this via PM, but I was also building the same thing but using google chart API..

http://xenaddicts.com/xen/?show=Discussion|Members|Messages
xen


http://xenaddicts.com/xen/?show=Discussion|Members
xen



http://xenaddicts.com/xen/?show=Discussion
xen


http://xenaddicts.com/xen/?show=Members
xen


http://xenaddicts.com/xen/?show=Messages
xen
 
Erik, you should release your cron script publicly... I want to see how you did it.
I'm actually surprised it's still running, considering I haven't paid any attention to it in almost a year and it's about the worst piece of code ever written. But if you insist....

Uses the pChart graphing library for drawing the graphs.

PHP:
<?php

include("pChart/pData.class");
include("pChart/pChart.class");

function getPage($url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Stats Scraper, by Eriksrocks');
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$html=curl_exec($ch);
if($html==false){
 $m=curl_error(($ch));
 error_log($m);
}
curl_close($ch);
return $html;
}

function sprintf_escape() {
    $args = func_get_args();
    if (count($args) < 2)
        return false;
    $query = array_shift($args);
    $args = array_map('mysql_real_escape_string', $args);
    array_unshift($args, $query);
    $query = call_user_func_array('sprintf', $args);
    return $query;
}

if(!isset($_SERVER['HTTP_USER_AGENT'])) {
// being run through cron

$dbname = "erik_xenstats";
$u = "**********";
$p = "**********";

$page = getPage('http://xenforo.com/community/');

if(preg_match('/<h3>Forum Statistics<\/h3>\s*<div class="pairsJustified">\s*<dl class="discussionCount"><dt>Discussions:<\/dt>\s*<dd>([0-9,]+)<\/dd><\/dl>\s*<dl class="messageCount"><dt>Messages:<\/dt>\s*<dd>([0-9,]+)<\/dd><\/dl>\s*<dl class="memberCount"><dt>Members:<\/dt>\s*<dd>([0-9,]+)<\/dd><\/dl>/i', $page, $matches) !== 0) {

$discussions = intval(str_replace(',','',$matches[1]));
$messages = intval(str_replace(',','',$matches[2]));
$members = intval(str_replace(',','',$matches[3]));

$conn = mysql_connect("localhost", $u, $p)
or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

$db = mysql_select_db($dbname, $conn)
or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

$sql = sprintf_escape("INSERT INTO data VALUES (CURDATE(), %u, %u, %u)", $discussions, $messages, $members);
$result = mysql_query($sql) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

$sql = "SELECT * FROM data";
$result = mysql_query($sql) or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR);

$DataSet = new pData;
$arr_dates = array();
$arr_discussions = array();
$arr_messages = array();
$arr_members = array();
$html_output = "<table cellpadding=\"10\">
<thead>
<tr>
<th>Date</th>
<th>Discussions</th>
<th>Messages</th>
<th>Members</th>
<tr>
</thead>
<tbody>\n";

while($row = mysql_fetch_assoc($result)) {
$arr_dates[] = strtotime($row['date']);
$arr_discussions[] = $row['discussions'];
$arr_messages[] = $row['messages'];
$arr_members[] = $row['members'];
$html_output .= "\t\t<tr>\n\t\t\t<td>".$row['date']."</td>\n\t\t\t<td>".$row['discussions']."</td>\n\t\t\t<td>".$row['messages']."</td>\n\t\t\t<td>".$row['members']."</td>\n\t\t</tr>\n";
}
$html_output .= "\t\t</tbody>\n</table>";
$fh = fopen('raw.html', 'w');
fwrite($fh, $html_output);
fclose($fh);

$DataSet->AddPoint($arr_dates,"Dates");
$DataSet->AddPoint($arr_discussions,"Discussions");
$DataSet->AddPoint($arr_messages,"Messages");
$DataSet->AddPoint($arr_members,"Members");

$DataSet->AddSerie("Discussions");
$DataSet->AddSerie("Messages");
$DataSet->AddSerie("Members");
$DataSet->SetAbsciseLabelSerie("Dates");
$DataSet->SetXAxisFormat("date");

$Discussions = new pData;
$Discussions->AddPoint($arr_dates,"Dates");
$Discussions->AddPoint($arr_discussions,"Discussions");
$Discussions->AddSerie("Discussions");
$Discussions->SetAbsciseLabelSerie("Dates");
$Discussions->SetXAxisFormat("date");

$Members = new pData;
$Members->AddPoint($arr_dates,"Dates");
$Members->AddPoint($arr_members,"Members");
$Members->AddSerie("Members");
$Members->SetAbsciseLabelSerie("Dates");
$Members->SetXAxisFormat("date");
 
/*** TOTAL GRAPH ***/
// Initialise the graph
$Test = new pChart(790,320);
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->setGraphArea(65,45,760,225);
$Test->drawFilledRoundedRectangle(7,7,783,293,5,240,240,240);
$Test->drawRoundedRectangle(5,5,785,295,5,230,230,230);
$Test->drawGraphArea(255,255,255,TRUE);
$skipvalue = round(count($arr_dates)/17);
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,50,1,false,$skipvalue);
$Test->drawGrid(4,TRUE,230,230,230,50);

// Draw the 0 line
$Test->setFontProperties("Fonts/tahoma.ttf",6);
$Test->drawTreshold(0,143,55,72,TRUE,TRUE);

// Draw the line graph
$Test->setColorPalette(0,126,199,41);
$Test->setColorPalette(2,8,100,195);
$Test->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());
$Test->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255);

// Finish the graph
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->drawLegend(75,55,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties("Fonts/tahoma.ttf",15);
$Test->drawTitle(0,33,"XenForo Stats",50,50,50,790);

//$Test->Render("example1.png");
$Test->Render("all.png");

/*** DISCUSSIONS GRAPH ***/
// Initialise the graph
$Test = new pChart(790,320);
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->setGraphArea(65,45,760,225);
$Test->drawFilledRoundedRectangle(7,7,783,293,5,240,240,240);
$Test->drawRoundedRectangle(5,5,785,295,5,230,230,230);
$Test->drawGraphArea(255,255,255,TRUE);
$skipvalue = round(count($arr_dates)/17);
$Test->drawScale($Discussions->GetData(),$Discussions->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,50,1,false,$skipvalue);
$Test->drawGrid(4,TRUE,230,230,230,50);

// Draw the 0 line
$Test->setFontProperties("Fonts/tahoma.ttf",6);
$Test->drawTreshold(0,143,55,72,TRUE,TRUE);

// Draw the line graph
$Test->setColorPalette(0,126,199,41);
$Test->drawLineGraph($Discussions->GetData(),$Discussions->GetDataDescription());
$Test->drawPlotGraph($Discussions->GetData(),$Discussions->GetDataDescription(),3,2,255,255,255);

// Finish the graph
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->drawLegend(75,55,$Discussions->GetDataDescription(),255,255,255);
$Test->setFontProperties("Fonts/tahoma.ttf",15);
$Test->drawTitle(0,33,"XenForo Discussions Stats",50,50,50,790);

//$Test->Render("example1.png");
$Test->Render("discussions.png");
 
/*** MEMBERS GRAPH ***/
// Initialise the graph
$Test = new pChart(790,320);
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->setGraphArea(65,45,760,225);
$Test->drawFilledRoundedRectangle(7,7,783,293,5,240,240,240);
$Test->drawRoundedRectangle(5,5,785,295,5,230,230,230);
$Test->drawGraphArea(255,255,255,TRUE);
$skipvalue = round(count($arr_dates)/17);
$Test->drawScale($Members->GetData(),$Members->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,50,1,false,$skipvalue);
$Test->drawGrid(4,TRUE,230,230,230,50);

// Draw the 0 line
$Test->setFontProperties("Fonts/tahoma.ttf",6);
$Test->drawTreshold(0,143,55,72,TRUE,TRUE);

// Draw the line graph
$Test->setColorPalette(0,8,100,195);
$Test->drawLineGraph($Members->GetData(),$Members->GetDataDescription());
$Test->drawPlotGraph($Members->GetData(),$Members->GetDataDescription(),3,2,255,255,255);

// Finish the graph
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->drawLegend(75,55,$Members->GetDataDescription(),255,255,255);
$Test->setFontProperties("Fonts/tahoma.ttf",15);
$Test->drawTitle(0,33,"XenForo Members Stats",50,50,50,790);

//$Test->Render("example1.png");
$Test->Render("members.png");
 
} else {
//preg_match error
$message = "Preg match did not match in the XenForo Stats Script. Check if the HTML has changed.";
mail('*******@gmail.com', 'XenForo Stats Preg Match Error', $message);
}
} else echo "This file must be run through cron to function. Permission denied.";
?>

The proper way to do this would be to use PHP's XML features to parse the DOM, and get rid of all the crappy procedural code. And obviously if running next to a local installation of XenForo, using internal function calls would be even better. Using a client-side graphing library like one of the many jQuery graphing libraries would probably be a wise choice as well, although it limits where the graph can be included.
 
I was expecting a spike. Everything's pretty steady. I asked cause I was wondering if my site would spike in activity after switching over.
 
We had 33,000 posts after 2 1/2 years on our old software.

We switched to XenForo just under 6 months ago and now have 65,000 posts.
 
Top Bottom