This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2006 from gittip/events
Eating elephant, first step (events, db schema)
- Loading branch information
Showing
6 changed files
with
150 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import heapq | ||
import psycopg2 | ||
from gittip import wireup | ||
|
||
def main(): | ||
db = wireup.db() | ||
with db.get_cursor() as c: | ||
|
||
claimed = c.all(""" | ||
SELECT claimed_time as ts, id, 'claim' as action | ||
FROM participants | ||
WHERE claimed_time IS NOT NULL | ||
ORDER BY claimed_time | ||
""") | ||
usernames = c.all(""" | ||
SELECT claimed_time + interval '0.01 s' as ts, id, 'set' as action, username | ||
FROM participants | ||
WHERE claimed_time IS NOT NULL | ||
ORDER BY claimed_time | ||
""") | ||
api_keys = c.all(""" | ||
SELECT a.mtime as ts, p.id as id, 'set' as action, a.api_key | ||
FROM api_keys a | ||
JOIN participants p | ||
ON a.participant = p.username | ||
ORDER BY ts | ||
""") | ||
goals = c.all(""" | ||
SELECT g.mtime as ts, p.id as id, 'set' as action, g.goal::text | ||
FROM goals g | ||
JOIN participants p | ||
ON g.participant = p.username | ||
ORDER BY ts | ||
""") | ||
|
||
for event in heapq.merge(claimed, usernames, api_keys, goals): | ||
payload = dict(action=event.action, id=event.id) | ||
if event.action == 'set': | ||
payload['values'] = { event._fields[-1]: event[-1] } | ||
c.run(""" | ||
INSERT INTO events (ts, type, payload) | ||
VALUES (%s, %s, %s) | ||
""", (event.ts, 'participant', psycopg2.extras.Json(payload))) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
BEGIN; | ||
DROP TABLE IF EXISTS events; | ||
CREATE TABLE events | ||
( id serial PRIMARY KEY | ||
, ts timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
, type text NOT NULL | ||
, payload json | ||
); | ||
|
||
CREATE INDEX events_ts ON events(ts ASC); | ||
CREATE INDEX events_type ON events(type); | ||
|
||
/* run branch.py before this | ||
DROP RULE log_api_key_changes ON participants; | ||
DROP RULE log_goal_changes ON participants; | ||
DROP TABLE goals, api_keys; | ||
DROP SEQUENCE api_keys_id_seq, goals_id_seq; | ||
*/ | ||
|
||
END; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from decimal import Decimal | ||
|
||
from aspen import Response, log | ||
from aspen.utils import to_age | ||
from gittip.utils import get_participant, get_avatar_url | ||
|
||
db = website.db | ||
|
||
[-----------------------------------------------------------------------------] | ||
|
||
participant = get_participant(request, restrict=True) | ||
hero = "Events" | ||
title = "%s - %s" % (participant.username, hero) | ||
locked = False | ||
|
||
SQL = """ | ||
SELECT * FROM events WHERE type = 'participant' AND payload->>'id' = %s ORDER BY ts ASC | ||
""" | ||
|
||
events = db.all(SQL, (unicode(participant.id),)) | ||
|
||
[-----------------------------------------------------------------------------] | ||
{% extends "templates/profile.html" %} | ||
{% block page %} | ||
|
||
<style> | ||
td { padding: 4px; } | ||
</style> | ||
<table id="events" class="centered"> | ||
|
||
{% for e in events %} | ||
<tr> | ||
<td>{{ e.ts }}</td> | ||
<td>{{ e.type }}</td> | ||
<td>{{ e.payload }}</td> | ||
|
||
</tr> | ||
{% endfor %} | ||
|
||
</table> | ||
|
||
{% endblock %} |