simple user agent tracking and collection added to perf javascript

This commit is contained in:
John Bender 2011-11-10 00:16:29 -08:00
parent 3edb68506d
commit f74d117aec
3 changed files with 43 additions and 10 deletions

View file

@ -1,21 +1,23 @@
<?php
$db = new PDO('sqlite:./stats.db');
$db->query('CREATE TABLE IF NOT EXISTS stats (id INTEGER, agent TEXT, point TEXT, value REAL, time TIMESTAMP, pathname TEXT, PRIMARY KEY (id))');
$db->query('CREATE TABLE IF NOT EXISTS stats (id INTEGER, agent TEXT, agent_version Text, agent_full TEXT, point TEXT, value REAL, time TIMESTAMP, pathname TEXT, PRIMARY KEY (id))');
// making a sad attempt here to provide a clean REST-respecting url scheme.
// stats with a GET returns - wait for it - the stats, and a post with the
// the right params will create a new entry
if ( $_SERVER['REQUEST_METHOD'] == "GET" ) {
$json = Array();
$st = $db->prepare( 'SELECT point, avg(value) as avg_value, pathname, strftime(\'%Y-%m-%d\', time) as day FROM stats GROUP BY pathname, point, strftime(\'%Y-%m-%d\', time) ORDER BY time;' );
$st = $db->prepare( "SELECT agent, agent_version, point, avg(value) as avg_value, pathname, strftime('%Y-%m-%d', time) as day FROM stats WHERE agent_full like '%Mobile%' or agent_full like '%mobile%' GROUP BY agent, agent_version, pathname, point, strftime('%Y-%m-%d', time) ORDER BY time;");
$st->execute();
$result = $st->fetchAll(PDO::FETCH_ASSOC);
header("Content-Type: application/json");
echo json_encode($result);
} elseif ( $_POST['datapoint'] && $_POST['value'] && $_POST['agent'] && $_POST['pathname']) {
$st = $db->prepare('INSERT INTO stats (agent, point, value, pathname, time) VALUES (:agent, :data_point, :value, :pathname, DATETIME(\'now\'))');
} elseif ( $_POST['datapoint'] && $_POST['value'] && $_POST['agent'] && $_POST['pathname'] && $_POST['agentVersion']) {
$st = $db->prepare('INSERT INTO stats (agent, agent_full, agent_version, point, value, pathname, time) VALUES (:agent, :agent_full, :agent_version, :data_point, :value, :pathname, DATETIME(\'now\'))');
$st->execute(array(
':agent' => $_POST['agent'],
':agent_full' => $_POST['agentFull'],
':agent_version' => $_POST['agentVersion'],
':data_point' => $_POST['datapoint'],
':value' => $_POST['value'],
':pathname' => $_POST['pathname']

View file

@ -6,12 +6,22 @@ window.Perf = (function($, Perf) {
// should be defined before report or poll are run
currentRev: undefined,
agents: {
ANDROID: "Android",
WP: "Windows Phone OS"
},
vRegexs: {},
report: function( data, after ) {
var self = this;
$.extend(data, {
pathname: location.pathname,
agent: this.agent(),
agentFull: window.navigator.userAgent,
agentVersion: this.agentVersion()
});
data.pathname = location.pathname;
$.post( self.reportUrl, data, after );
$.post( this.reportUrl, data, after );
},
poll: function() {
@ -34,8 +44,31 @@ window.Perf = (function($, Perf) {
$.get( self.revUrl, function( data ) {
self.currentRev = data;
});
},
agent: function() {
var agent = window.navigator.userAgent;
for( name in this.agents ) {
if( agent.indexOf( this.agents[name] ) > -1 ) {
return this.agents[name];
}
}
return agent;
},
agentVersion: function() {
var agent = window.navigator.userAgent;
agent.search(this.vRegexs[this.agent()] || "");
return RegExp.$1 ? RegExp.$1 : "0.0";
}
});
Perf.vRegexs[Perf.agents.ANDROID] = /([0-9].[0-9].[0-9]);/;
Perf.vRegexs[Perf.agents.WP] = /Windows Phone OS ([0-9].[0-9]);/;
return Perf;
})(jQuery, window.Perf || {});

View file

@ -22,7 +22,6 @@
// report the time taken for a full app boot
Perf.report({
agent: window.navigator.userAgent,
datapoint: "fullboot",
value: Perf.pageLoadEnd - Perf.pageLoadStart
});
@ -30,7 +29,6 @@
// record the time taken to load and enhance the page
// start polling for a new revision
Perf.report({
agent: window.navigator.userAgent,
datapoint: "pageload",
value: Perf.pageCreateStart - Perf.pageLoadStart,
after: function() {