-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
285 lines (245 loc) · 10.7 KB
/
app.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from flask import Flask, Response, render_template, jsonify, request
from datetime import date, datetime
import threading
import Queue
import atexit
import json
import singmasterMath as math
import pascalDB as db
# SSE
class ServerSentEvent(object):
def __init__(self, data, event=None):
self.data = data
self.event = event
self.id = None
self.desc_map = {
self.data: "data",
self.event: "event",
self.id: "id"
}
def encode(self):
if not self.data:
return ""
lines = ["%s: %s" % (v, k)
for k, v in self.desc_map.iteritems() if k]
return "%s\n\n" % "\n".join(lines)
class ProcessThread(threading.Thread):
def __init__(self, thread_id, function, on=True):
threading.Thread.__init__(self)
self.thread_id = thread_id
self.function = function
self.on = on
def run(self):
pushStreamEvent(data={'message': 'Process Thread {!s} has started'.format(self.thread_id),
'header': 'control',
'flag': db.SUCCESS_FLAG},
log=True, event='message', blocking=True)
while self.on:
self.function()
pushStreamEvent(data={'message': 'Process Thread {!s} has terminated'.format(self.thread_id),
'header': 'control',
'flag': db.WARNING_FLAG},
log=True, event='message', blocking=True)
def close(self):
pushStreamEvent(data={'message': 'Process Thread {!s} is stopping...'.format(self.thread_id),
'header': 'control',
'flag': db.WARNING_FLAG},
log=True, event='message', blocking=True)
self.on = False
def pushStreamEvent(data, log=True, event='message', blocking=True):
if 'ts' not in data:
data.update({'ts': datetime.now().strftime('%Y-%m-%d %H:%M:%S')})
if log:
db.logLine(message=data['message'], header=data['header'], flag=data['flag'])
#data.update({'logline': '[ {!s} ] == {!s}'.format(data['ts'], data['msg'])})
for sub in subscriptions[:]:
try:
data.update({'event': event})
sub.put(data, blocking)
except Queue.Full: # Or maybe use flask signals
pass
app = Flask(__name__)
# Control variables
streamQueueLock = threading.Lock()
steamQueue = Queue.Queue(0)
process = {'thread': None,
'initial': None,
'on': False,
'threadCount': 1000}
progress = {'center': 0, 'p': 0.0}
subscriptions = []
buttons = {'start-btn': {'state': True},
'stop-btn': {'state': False},
'reset-btn': {'state': False}}
process['initial'] = db.resumeSearch()
progress.update({'center': process['initial']['lastCenterIndex']})
def searchProcess():
# main process code
# centers = 200
if process['initial']:
centerIndex= process['initial']['lastCenterIndex']
searchStartIndices = process['initial']['searchStartIndices']
else:
centerIndex = 3
searchStartIndices = list(range(centerIndex + 1))
while process['on']: # and center < centers:
pushStreamEvent(data={'message': 'Searching for {!s}{!s} center ...'.format(centerIndex,ordinal(centerIndex)),
'header': 'info',
'flag': db.INFO_FLAG},
log=True, event='message', blocking=True)
centerValue = math.pascalTriCenter(centerIndex)
data = {'center': {'diagonal': centerIndex, 'value': centerValue, 'matchCount': 0},
'results': []}
centerSearchTime = 0
for diagonalIndex in range(2, centerIndex + 1):
target = centerValue * math.factorial(diagonalIndex)
try:
searchStartIndices[diagonalIndex]
except IndexError:
searchStartIndices.append(diagonalIndex)
binarySearchResults = math.binarySearch(searchStartIndices[diagonalIndex],
target,
lambda x: math.product(math.Pascal(diagonalIndex, x).integers))
incrementSearch = math.Pascal(diagonal=diagonalIndex, n=binarySearchResults['result'])
while math.product(incrementSearch.integers) < target:
incrementSearch.incrementIndex()
matchFound = (math.product(incrementSearch.integers) == target)
if matchFound:
trivialResult = (incrementSearch.n == centerIndex)
pushStreamEvent(data={'message': 'Match found! Diagonal: {!s}; Position: {!s}'.format(str(diagonalIndex), str(incrementSearch.n)),
'header': 'info' if trivialResult else 'success',
'flag': db.INFO_FLAG if trivialResult else db.SUCCESS_FLAG}, log=True, event='message', blocking=True)
data['center']['matchCount'] += 1
data['results'].append({'diagonal': diagonalIndex,
'index': incrementSearch.n,
'match': matchFound,
'binarySearchExpandCount': binarySearchResults['expandCount'],
'binarySearchNarrowCount': binarySearchResults['narrowCount'],
'searchTime': binarySearchResults['searchTime']})
searchStartIndices[diagonalIndex] = incrementSearch.n - 1
centerSearchTime += binarySearchResults['searchTime']
pushStreamEvent(data={'center': centerIndex,
'p': (diagonalIndex-2.0)/(centerIndex-2.0)},
log=False, event='progress', blocking=False)
data['center'].update({'centerSearchTime': centerSearchTime})
db.uploadResult(data)
pushStreamEvent(data={'center': centerIndex,
'centerSearchTime': centerSearchTime,
'matchCount': data['center']['matchCount']},
log=False, event='data', blocking=True)
centerIndex += 1
progress.update({'center': centerIndex, 'p': 0.0})
process['initial'] = db.resumeSearch()
process['on'] = False
def ordinal(n):
return {1: 'st', 2: 'nd', 3: 'rd'}.get(4 if 10 <= n % 100 < 20 else n % 10, "th")
@app.route('/')
def main():
return render_template('index.html',
title='Singmaster''s Conjecture',
year=date.today().year,
progress=progress,
terminal_text='Loading status terminal...\n',
buttons=buttons)
@app.route('/start')
def start():
buttons.update({'start-btn': {'state': False},
'stop-btn': {'state': False},
'reset-btn': {'state': False}})
process['threadCount'] += 1
process['on'] = True
process['thread'] = ProcessThread(process['threadCount'], searchProcess)
process['thread'].start()
buttonStates = {'start-btn': {'state': False},
'stop-btn': {'state': True},
'reset-btn': {'state': False}}
buttons.update(buttonStates)
return jsonify({'log': 'Starting process',
'buttons': buttonStates,
'alert': {'title': 'Start: ',
'body': 'The search is beginning.',
'flag': db.SUCCESS_FLAG}})
#'class': 'alert-success'}})
@app.route('/stop')
def stop():
buttons.update({'start-btn': {'state': False},
'stop-btn': {'state': False},
'reset-btn': {'state': False}})
process['on'] = False
process['thread'].close()
buttonStates = {'start-btn': {'state': True, 'label': 'Resume'},
'stop-btn': {'state': False},
'reset-btn': {'state': True}}
buttons.update(buttonStates)
return jsonify({'log': 'Stopping process',
'buttons': buttonStates,
'alert': {'title': 'Stop: ',
'body': 'The search has stopped.',
'flag': db.WARNING_FLAG}})
#'class': 'alert-warning'}})
@app.route('/reset')
def reset():
buttons.update({'start-btn': {'state': False},
'stop-btn': {'state': False},
'reset-btn': {'state': False}})
pushStreamEvent(data={'message': 'Resetting progress', 'header': 'reset', 'flag': db.DANGER_FLAG}, log=True, event='message', blocking=True)
process['thread'].join() # Wait for process to terminate
process['initial'] = None
db.recreateTables() # Reset function
buttonStates = {'start-btn': {'state': True, 'label': 'Start'},
'stop-btn': {'state': False},
'reset-btn': {'state': False}}
buttons.update(buttonStates)
return jsonify({'log': 'Resetting progress',
'buttons': buttonStates,
'alert': {'title': 'Reset: ',
'body': 'The search results have been reset.',
'flag': db.DANGER_FLAG}})
#'class': 'alert-danger'}})
@app.route('/dataStream')
def dataStream():
def gen():
try:
q = Queue.Queue(5)
subscriptions.append(q)
while True:
status = q.get(True)
ev = ServerSentEvent(str(json.dumps(status)), event=status.get('event', None))
yield ev.encode()
except GeneratorExit:
subscriptions.remove(q)
return Response(gen(), mimetype='text/event-stream')
@app.route('/reload')
def reload():
return jsonify({'logs': db.pullLog(), 'viz': db.pullVizData()})
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
@app.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_server()
return 'Server shutting down...'
# This hook ensures that a connection is opened to handle any queries
# generated by the request.
@app.before_request
def _db_connect():
db.db.connect()
# This hook ensures that the connection is closed when we've finished
# processing the request.
@app.teardown_request
def _db_close(exc):
if not db.db.is_closed():
db.db.close()
if __name__ == "__main__":
app.debug = True
#db.recreateTables()
#db.createLog()
app.run(threaded=True, use_reloader=False)
@atexit.register
def goodbye():
print('Exiting Flask app')
if process['thread']:
process['thread'].close()
db.db.close()