This repository has been archived by the owner on Aug 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
157 additions
and
118 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
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,109 @@ | ||
import json | ||
|
||
import psycopg2 | ||
from psycopg2.extras import DictCursor | ||
|
||
import config | ||
|
||
|
||
class Event(object): | ||
|
||
conn = psycopg2.connect( | ||
database=config.PG_DB, | ||
user=config.PG_USER, | ||
password=config.PG_PASS, | ||
host=config.PG_HOST, | ||
port=config.PG_PORT | ||
) | ||
|
||
@classmethod | ||
def _cursor(self): | ||
return self.conn.cursor(cursor_factory=DictCursor) | ||
|
||
@staticmethod | ||
def _prep_data(events): | ||
times = set() | ||
for e in events: | ||
e['info'] = json.loads(e['info']) | ||
times.add(e['time']) | ||
|
||
if len(times) > 0: | ||
last = max(times) | ||
else: | ||
last = None | ||
|
||
return (events, last) | ||
|
||
@classmethod | ||
def init_table(self): | ||
cur = self._cursor() | ||
cur.execute( | ||
""" | ||
CREATE TABLE IF NOT EXISTS events | ||
( | ||
id serial PRIMARY KEY UNIQUE, | ||
service text, | ||
info text, | ||
time timestamp, | ||
event_id text UNIQUE | ||
); | ||
""" | ||
) | ||
self.conn.commit() | ||
cur.close() | ||
|
||
@classmethod | ||
def new(self, service, info, e_time, e_id): | ||
status = { | ||
'success': True, | ||
'error': None | ||
} | ||
e_id = '{0}_{1}'.format(service, e_id) | ||
if isinstance(info, dict): | ||
info = json.dumps(info) | ||
elif isinstance(info, str): | ||
# Check if info is valid json | ||
json.loads(info) | ||
|
||
cur = self._cursor() | ||
try: | ||
cur.execute( | ||
""" | ||
INSERT INTO events | ||
(service, info, time, event_id) | ||
VALUES (%s, %s, %s, %s) | ||
""", (service, info, e_time, e_id) | ||
) | ||
self.conn.commit() | ||
except psycopg2.IntegrityError: | ||
status['success'] = False | ||
status['error'] = 'Event already exists' | ||
self.conn.rollback() | ||
finally: | ||
cur.close() | ||
|
||
return status | ||
|
||
@classmethod | ||
def latest(self, limit=config.EVENT_AMOUNT): | ||
cur = self._cursor() | ||
cur.execute( | ||
'SELECT * FROM events ORDER BY time DESC LIMIT %s', | ||
(limit,) | ||
) | ||
|
||
events = cur.fetchall() | ||
return self._prep_data(events) | ||
|
||
@classmethod | ||
def since(self, time): | ||
cur = self._cursor() | ||
cur.execute( | ||
""" | ||
SELECT * FROM events WHERE time > %s | ||
ORDER BY time DESC | ||
""", (time,) | ||
) | ||
|
||
events = cur.fetchall() | ||
return self._prep_data(events) |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
Flask==0.9 | ||
Jinja2==2.6 | ||
SQLAlchemy==0.7.9 | ||
Werkzeug==0.8.3 | ||
requests==0.14.2 | ||
Flask-DebugToolbar==0.7.1 | ||
Flask-SQLAlchemy==0.16 | ||
psycopg2==2.4.6 |
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from stream import app | ||
from models import Event | ||
Event.init_table() | ||
|
||
|
||
app.run('0.0.0.0') |
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 |
---|---|---|
@@ -1,30 +1,10 @@ | ||
from flask import Flask | ||
from flask.ext.sqlalchemy import SQLAlchemy | ||
from flask_debugtoolbar import DebugToolbarExtension | ||
|
||
app = Flask(__name__) | ||
app.config.from_pyfile('../config.py') | ||
app.config['SQLALCHEMY_DATABASE_URI'] = app.config['DATABASE'] | ||
|
||
db = SQLAlchemy(app) | ||
toolbar = DebugToolbarExtension(app) | ||
|
||
|
||
class Brook(db.Model): | ||
__tablename__ = 'brook' | ||
|
||
id = db.Column(db.Integer, primary_key=True) | ||
service = db.Column(db.String(64), nullable=False) | ||
info = db.Column(db.String(9999), nullable=False) | ||
time = db.Column(db.DateTime, nullable=False) | ||
event_id = db.Column(db.String(128), nullable=False, unique=True) | ||
|
||
def __init__(self, service, info, time, event_id): | ||
self.service = service | ||
self.info = info | ||
self.time = time | ||
self.event_id = event_id | ||
|
||
|
||
import stream.views | ||
import stream.filters |
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
76bf38f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why'd you decide to do this?
76bf38f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@whit537 I can't remember if I had any specific reasoning behind it at the time, but the reason I used sqlalchemy in the first place was because I didn't want to install postgres locally and I had done that anyway by the time I made this commit. (i can't remember why I didn't just leave it how it was though!)
I guess I just prefer to write the sql myself, for small projects at least.
76bf38f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you see the thread on Gittip where we decided to go from SQL to SQLAlchemy?