-
Notifications
You must be signed in to change notification settings - Fork 3
/
webapp.py
158 lines (126 loc) · 4.24 KB
/
webapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from datetime import datetime
import json
import random
import string
import time
import eventlet
from flask import Flask, request
from flask_migrate import Migrate
from flask_socketio import SocketIO
from sqlalchemy import func
import pgpubsub
from realtime.database import models
from realtime.database.adapter import db
from realtime.database.models import TodoItems, Subscriptions
from realtime.webserver import views
app = Flask(__name__)
app.config.from_object('realtime.config')
db.init_app(app)
socket_io = SocketIO(app)
migrate = Migrate(app, db)
views.admin.init_app(app)
def listen_thread():
pubsub = pgpubsub.connect(database='realtime')
pubsub.listen('todo_items_table_update')
while True:
for event in pubsub.events(yield_timeouts=True):
if event is None:
pass
else:
with app.app_context():
process_message(event)
@app.before_first_request
def start_listener():
eventlet.monkey_patch()
spawn = eventlet.spawn
spawn(listen_thread)
def process_message(event):
data = json.loads(event.payload)
with open('output.json', 'w') as output:
json.dump(data, output, indent=4, sort_keys=True)
# TODO: Query the table if 'row' is not in the data dictionary
# (due to pg_notify's 8kB payload limit)
if data['type'] == 'INSERT':
socket_io.emit('insert', data['row'], namespace='/')
elif data['type'] == 'UPDATE':
socket_io_clients = (
db.session.query(Subscriptions.socket_io_id)
.filter(Subscriptions.record_id == data['id'])
.filter(Subscriptions.table_name == data['table_name'])
.group_by(Subscriptions.socket_io_id)
.all()
)
for client, in socket_io_clients:
socket_io.emit('update',
data['row'],
namespace='/',
room=client)
elif data['type'] == 'DELETE':
socket_io.emit('delete', data['row'], namespace='/')
@app.context_processor
def utility_processor():
def get_record_ids(data, get_pk_value):
table_name = str(data[0].__table__)
record_ids = [get_pk_value(m) for m in data]
record_ids = json.dumps({table_name: record_ids})
return record_ids
return dict(get_record_ids=get_record_ids)
@app.shell_context_processor
def make_shell_context():
return dict(app=app, db=db, TodoItems=TodoItems)
@app.cli.command()
def runserver(debug=True, use_reloader=True):
db.drop_all(bind='sessions_db')
db.create_all(bind='sessions_db')
socket_io.run(app, port=5001, debug=debug, use_reloader=use_reloader)
@app.cli.command()
def add():
for i in range(0, 40):
u = TodoItems(title='Todo Item')
with app.app_context():
db.session.add(u)
db.session.commit()
@app.cli.command()
def update():
with app.app_context():
while True:
update = (
db.session.query(TodoItems)
.order_by(func.random())
.first()
)
update.title = ''.join(random.choice(string.ascii_lowercase)
for x in range(20))
update.timestamp = datetime.now()
db.session.commit()
time.sleep(0.1)
@app.cli.command()
def delete():
with app.app_context():
models.TodoItems.query.delete()
db.session.commit()
print('Deleted all updates.')
@app.cli.command()
def create_dbs():
with app.app_context():
db.create_all()
db.create_all(bind='sessions_db')
@socket_io.on('connect', namespace='/')
def on_connect():
pass
@socket_io.on('record_ids', namespace='/')
def on_message(message):
for table_name in message:
for record_id in message[table_name]:
new_row = Subscriptions(record_id=record_id, table_name=table_name,
socket_io_id=request.sid)
db.session.add(new_row)
db.session.commit()
@socket_io.on('disconnect', namespace='/')
def on_disconnect():
(
db.session.query(Subscriptions)
.filter(Subscriptions.socket_io_id == request.sid)
.delete()
)
db.session.commit()