-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.py
More file actions
275 lines (220 loc) · 7.9 KB
/
application.py
File metadata and controls
275 lines (220 loc) · 7.9 KB
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
# -*- coding: utf-8 -*-
"""
SQLpie™ is a simple, sleek, intuitive, and powerful API platform for prototyping projects that have data intelligence needs.
SQLpie is 100% written in Python and sits on top of a MySQL database, which means that it's easy to maintain and most (if not all) of the data processing heavy lifting is done in SQL, a proven and somewhat scalable technology.
SQLpie License (MIT License)
Copyright (c) 2011-2016 André Lessa, http://sqlpie.com
See LICENSE file.
"""
from flask import Flask, request, jsonify, g, Response
from flask import current_app
from flask import render_template
from flaskext.mysql import MySQL
import json, threading, time, sys, traceback, os, logging, random
import sqlpie
application = Flask(__name__)
sqlpie_config = sqlpie.Config().load()
setup = sqlpie.DBSetup(sqlpie_config)
setup.init(application)
mysql = setup.db()
@application.before_request
def db_connect():
try:
g.conn = mysql.connect()
g.cursor = g.conn.cursor()
except:
pass
@application.teardown_request
def db_disconnect(response):
try:
g.cursor.close()
g.conn.close()
except:
pass
return response
#
# Routes
#
@application.route('/')
def index():
return 'Hello world, this is SQLpie.'
#
# Documents
#
@application.route("/document/<command>", methods=['POST'])
def document(command):
command = command.lower()
if command == "put":
resp = sqlpie.DocumentController.put(request)
elif command == "get":
resp = sqlpie.DocumentController.get(request)
elif command == "remove":
resp = sqlpie.DocumentController.remove(request)
elif command == "reset":
resp = sqlpie.DocumentController.reset(request)
else:
resp = Response(None, status=404, mimetype='application/json')
return resp
#
# Observations
#
@application.route("/observation/<command>", methods=['POST'])
def observation(command):
command = command.lower()
if command == "put":
resp = sqlpie.ObservationController.put(request)
elif command == "get":
resp = sqlpie.ObservationController.get(request)
elif command == "remove":
resp = sqlpie.ObservationController.remove(request)
elif command == "reset":
resp = sqlpie.ObservationController.reset(request)
else:
resp = Response(None, status=404, mimetype='application/json')
return resp
#
# Other
#
@application.route("/docs", methods=["GET"])
def docs():
return sqlpie.HealthController.docs(request)
@application.route("/stats", methods=["GET","POST"])
def stats():
return sqlpie.HealthController.stats(request)
@application.route("/ping", methods=["GET","POST"])
def ping():
return sqlpie.HealthController.ping(request)
#
# Search
#
@application.route("/service/index", methods=["POST"])
def service_index():
return sqlpie.SearchController.service_index(request)
@application.route("/service/search", methods=["POST"])
def service_search():
return sqlpie.SearchController.service_search(request)
#
# Classifier
#
@application.route("/service/classifier/init", methods=["POST"])
def service_classifier():
return sqlpie.ClassifierController.classifier_init(request)
@application.route("/service/classifier/train", methods=["POST"])
def service_classifier_train():
return sqlpie.ClassifierController.classifier_train(request)
@application.route("/service/classifier/clear", methods=["POST"])
def service_classifier_clear():
return sqlpie.ClassifierController.classifier_clear(request)
@application.route("/service/classifier/reset", methods=["POST"])
def service_classifier_reset():
return sqlpie.ClassifierController.classifier_reset(request)
@application.route("/service/classifier/predict", methods=["POST"])
def service_classifier_predict():
return sqlpie.ClassifierController.classifier_predict(request)
@application.route("/service/classifier/predictions", methods=["POST"])
def service_classifier_predictions():
return sqlpie.ClassifierController.classifier_predictions(request)
#
# Matching
#
@application.route("/service/matching/", methods=["POST"])
def service_matching():
return sqlpie.MatchingController.matching(request)
#
# Recommendation
#
@application.route("/service/collaborative/recommendation", methods=["POST"])
def service_recommend():
return sqlpie.CollaborativeController.service_recommend(request)
@application.route("/service/collaborative/similarity", methods=["POST"])
def service_similarity():
return sqlpie.CollaborativeController.service_similarity(request)
#
# Summarization
#
@application.route("/service/summarization", methods=["POST"])
def service_summarization():
return sqlpie.SummarizationController.service_summarization(request)
#
# Caching
#
@application.route("/caching/initialize", methods=["POST"])
def caching_initialize():
return sqlpie.CachingController.caching_initialize(request)
@application.route("/caching/add", methods=["POST"])
def caching_add():
return sqlpie.CachingController.caching_add(request)
@application.route("/caching/put", methods=["POST"])
def caching_put():
return sqlpie.CachingController.caching_put(request)
@application.route("/caching/get", methods=["POST"])
def caching_get():
return sqlpie.CachingController.caching_get(request)
@application.route("/caching/remove", methods=["POST"])
def caching_remove():
return sqlpie.CachingController.caching_remove(request)
@application.route("/caching/flush", methods=["POST"])
def caching_flush():
return sqlpie.CachingController.caching_flush(request)
@application.route("/caching/reset", methods=["POST"])
def caching_reset():
return sqlpie.CachingController.caching_reset(request)
@application.route("/caching/destroy", methods=["POST"])
def caching_destroy():
return sqlpie.CachingController.caching_destroy(request)
#
# Global Vars
#
if "options" in sqlpie_config:
options = sqlpie_config["options"]
sqlpie.global_cache[sqlpie.Config.OPTIONS] = sqlpie.Caching(sqlpie.Config.OPTIONS,1)
sqlpie.global_cache[sqlpie.Config.OPTIONS].put("options", options)
search_stopwords = sqlpie.Config.get(sqlpie.Config.SEARCH_STOPWORDS)
if search_stopwords is not None:
sqlpie.global_cache[sqlpie.Config.STOPWORDS] = sqlpie.Caching(sqlpie.Config.STOPWORDS,5000)
for stopword_file in search_stopwords:
words = sqlpie.Config.load_data(stopword_file)
for w in words:
sqlpie.global_cache[sqlpie.Config.STOPWORDS].add(w.strip())
#
# Background Threads
#
def handle_indexing(app, mysql):
with app.app_context():
while(True):
try:
g.conn = mysql.connect()
g.cursor = g.conn.cursor()
g.conn.begin()
# run indexing servie every 300 seconds
time.sleep(300)
sqlpie.Indexer().index_documents()
g.conn.commit()
except Exception as e:
if sqlpie.Util.is_debug():
traceback.print_tb(sys.exc_info()[2])
try:
g.conn.rollback()
except:
pass
finally:
# if the MySQL Server is not running, this will fail.
try:
g.cursor.close()
g.conn.close()
except:
pass
if sqlpie.DBSetup().environment() != "test":
if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
threads = []
if sqlpie.Config.get(sqlpie.Config.BACKGROUND_INDEXER) == True:
t = threading.Thread(name='handle_indexing', target=handle_indexing, args=(application, mysql))
t.setDaemon(True)
threads.append(t)
t.start()
# for t in threads:
# t.join()
if sqlpie.Util.is_debug():
application.debug=True
if __name__ == "__main__":
application.run(host="0.0.0.0", debug=sqlpie.Util.is_debug(), port=sqlpie.Config.get(sqlpie.Config.SERVER_PORT))