Skip to content
This repository has been archived by the owner on Apr 18, 2018. It is now read-only.

Various upstream changes #2

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 50 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,31 @@ Adding New Reports
It's easiest to explain this with an example. Suppose the report module
specified by the config `module` variable has the following code in it:

from sqlalchemy import *
from kohlrabi.db import *

class DailySignups(Base):
__tablename__ = 'daily_signups_report'
__metaclass__ = ReportMeta

id = Column(Integer, primary_key=True)
date = Column(Date, nullable=False)
referrer = Column(String, nullable=False)
clickthroughs = Column(Integer, nullable=False, default=0)
signups = Column(Integer, nullable=False, default=0)

display_name = 'Daily Signups'
html_table = [
ReportColumn('Referrer', 'referrer'),
ReportColumn('Click-Throughs', 'clickthroughs'),
ReportColumn('Signups', 'signups'),
]

@classmethod
def report_data(cls, date):
return session.query(cls).filter(cls.date == date).order_by(cls.signups.id)
```python
from sqlalchemy import *
from kohlrabi.db import *

class DailySignups(Base):
__tablename__ = 'daily_signups_report'
__metaclass__ = ReportMeta

id = Column(Integer, primary_key=True)
date = Column(Date, nullable=False)
referrer = Column(String, nullable=False)
clickthroughs = Column(Integer, nullable=False, default=0)
signups = Column(Integer, nullable=False, default=0)

display_name = 'Daily Signups'
html_table = [
ReportColumn('Referrer', 'referrer'),
ReportColumn('Click-Throughs', 'clickthroughs'),
ReportColumn('Signups', 'signups'),
]

@classmethod
def report_data(cls, date):
return session.query(cls).filter(cls.date == date).order_by(cls.signups.id)
```

This is a data source that will track users who sign up on your site, based on
the HTTP `Referrer` header. The table has three columns: `referrer` will track
Expand All @@ -68,15 +70,17 @@ have any indexes. In most cases you should probably at least add an index on the
`date` column, and probably an index on the full set of columns you plan on
querying from the `report_data` method:

CREATE TABLE daily_signups_report (
id INTEGER NOT NULL,
date DATE NOT NULL,
referrer VARCHAR NOT NULL,
clickthroughs INTEGER NOT NULL,
signups INTEGER NOT NULL,
PRIMARY KEY (id)
);
CREATE INDEX daily_signups_date_idx ON daily_signups_report (date, signups);
```sql
CREATE TABLE daily_signups_report (
id INTEGER NOT NULL,
date DATE NOT NULL,
referrer VARCHAR NOT NULL,
clickthroughs INTEGER NOT NULL,
signups INTEGER NOT NULL,
PRIMARY KEY (id)
);
CREATE INDEX daily_signups_date_idx ON daily_signups_report (date, signups);
```

OK, that's all the setup you need to do on the Kohlrabi side of things: create a
Python SQLAlchemy class, and create a table in your SQLite database. The second
Expand All @@ -92,18 +96,20 @@ and the following POST parameters:
For instance, if we were running Kohlrabi on `http://localhost:8888`, then the
following Python code would generate a sample report for 2001-01-1:

import json
import urllib

urllib.urlopen('http://localhost:8888/upload',
urllib.urlencode({'date': '2010-01-01',
'data': json.dumps([{'referrer': 'www.yahoo.com',
'clickthroughs': 100,
'signups': 7},
{'referrer': 'www.google.com',
'clickthroughs': 500,
'signups': 42}]),
'table': 'DailySignups'}))
```python
import json
import urllib

urllib.urlopen('http://localhost:8888/upload',
urllib.urlencode({'date': '2010-01-01',
'data': json.dumps([{'referrer': 'www.yahoo.com',
'clickthroughs': 100,
'signups': 7},
{'referrer': 'www.google.com',
'clickthroughs': 500,
'signups': 42}]),
'table': 'DailySignups'}))
```

Just to reiterate: because the interface to Kohlrabi is a normal HTTP request
using JSON, you can use any language to send data to Kohlrabi. You can use Java,
Expand Down
1 change: 1 addition & 0 deletions kohlrabi/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def get(self):
self.env['date'] = date
self.env['data'] = getattr(db, tbl).report_data(date)
self.env['columns'] = getattr(db, tbl).html_table
self.env['first_column'] = getattr(db, tbl).html_table[0].display
self.env['title'] = getattr(db, tbl).display_name + ' ' + date.strftime('%Y-%m-%d')
self.render('report.html')

Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PyYAML
sqlalchemy
tornado
4 changes: 3 additions & 1 deletion templates/report.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
{% block body %}
<div class="vgap"><a href="{{uri('/')}}">Back to All Reports</a></div>
<div class="report-label">{{escape(title)}}</div>
<label for="servlet_filter">Servlet Filter: </label><input type="text" id="servlet_filter"/>
<label for="servlet_filter">{{first_column}} Filter: </label><input type="text" id="servlet_filter"/>
<!--

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just delete this?

<span class="input-comment">
(e.g. "main:biz_details"; may be slow!)
</span>
-->
<table id="kohlrabi_table">
<tr>
{% for c in columns %}
Expand Down