Skip to content

Instantly share code, notes, and snippets.

@rfunduk
Created August 16, 2011 16:35
Show Gist options
  • Save rfunduk/1149493 to your computer and use it in GitHub Desktop.
Save rfunduk/1149493 to your computer and use it in GitHub Desktop.
#!(path-to-python, eg. /usr/bin/python)
import sqlite3
db = sqlite3.connect( "stats.sqlite3" )
db.cursor().execute( "CREATE TABLE IF NOT EXISTS " \
"visits ( page TEXT, timestamp TEXT, ip TEXT )" )
#!(path-to-python, eg. /usr/bin/python)
import cgi
from datetime import date
from time import mktime, time
from sqlite import connect
from os import environ
from sys import exit
db = connect( "stats.sqlite3" )
cursor = db.cursor()
method = environ.get( 'REQUEST_METHOD' )
if method == "POST":
data = cgi.FieldStorage()
if not data.has_key( 'page' ): exit( 1 )
page = data['page'].value
timestamp = mktime( date.today().timetuple() )
ip_address = str( environ.get( 'REMOTE_ADDR' ) )
cursor.execute( "INSERT INTO visits VALUES ( ?, ?, ? )",
( page, timestamp, ip_address ) )
db.commit()
print "Content-Type: text/plain\r\n\r\n"
print "%s:%d:%s" % ( page, timestamp, ip_address )
<script type="text/javascript">
$(document).ready( function () {
$.ajax( {
type: 'post',
url: '/cgi-bin/stats.py',
data: 'page=some_page.html',
success: function( r ) { alert( r ); }
} );
} );
</script>
else:
data = []
cursor.execute( "SELECT DISTINCT timestamp " \
"FROM visits ORDER BY timestamp" )
for row, in cursor.fetchall():
timestamp = int( row )
cursor.execute( "SELECT COUNT(*) " \
"FROM visits " \
"WHERE timestamp = ?", ( timestamp, ) )
count, = cursor.fetchone()
data.append( [ timestamp * 1000 - 7200000, count ] )
max_y = max( [ count for timestamp, count in data ] )
print "Content-Type: text/json\r\n\r\n"
print "{ 'data': %s, 'max_y': %d }" \
% ( data, max_y + int( max_y / 2 ) + 1 )
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/jquery.flot.js"></script>
<script type="text/javascript">
$(document).ready( function () {
$.getJSON( "/cgi-bin/stats.py", function( json ) {
data = [ { data: json.data,
lines: { show: true },
points: { show: true },
color: "#DD5522" } ];
options = { xaxis: { mode: "time" },
yaxis: { min: 0, max: json.max_y },
grid: { color: '#888' } };
$.plot( $('#plot'), data, options );
} );
} );
</script>
<!--[if IE]>
<script type="text/javascript" src="/js/excanvas.js"></script>
<![endif]-->
<div id="plot" style="width: 500px; height: 250px;"></div>
#!(path-to-python, eg. /usr/bin/python)
import cgi
from datetime import date, timedelta
from time import mktime, time
from sqlite3 import connect
from random import randint
# create the db table if it doesn't exist already
db = connect( "stats.sqlite3" )
cursor = db.cursor()
print "Content-Type: text/plain\r\n\r\n"
for i in range( 1000 ):
delta = timedelta( days = randint( 0, 95 ) )
page = "test"
fake_date = date.today() - delta
timestamp = mktime( fake_date.timetuple() )
ip_address = "None"
cursor.execute( "INSERT INTO visits VALUES ( ?, ?, ? )", \
( page, timestamp, ip_address ) )
print fake_date.strftime( '%Y-%m-%d' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment