2014-08-06 11:43:40 +02:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2014-08-14 17:55:18 +02:00
|
|
|
import redis
|
|
|
|
import ConfigParser
|
|
|
|
import json
|
2014-12-24 15:42:20 +01:00
|
|
|
from flask import Flask, render_template, jsonify, request
|
2014-08-06 11:43:40 +02:00
|
|
|
import flask
|
2014-08-26 17:33:28 +02:00
|
|
|
import os
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-12-24 15:42:20 +01:00
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
# CONFIG #
|
2014-08-26 17:33:28 +02:00
|
|
|
configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg')
|
|
|
|
if not os.path.exists(configfile):
|
|
|
|
raise Exception('Unable to find the configuration file. \
|
|
|
|
Did you set environment variables? \
|
|
|
|
Or activate the virtualenv.')
|
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
cfg = ConfigParser.ConfigParser()
|
2014-08-26 17:33:28 +02:00
|
|
|
cfg.read(configfile)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
# REDIS #
|
|
|
|
r_serv = redis.StrictRedis(
|
2014-08-14 17:55:18 +02:00
|
|
|
host=cfg.get("Redis_Queues", "host"),
|
|
|
|
port=cfg.getint("Redis_Queues", "port"),
|
|
|
|
db=cfg.getint("Redis_Queues", "db"))
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
r_serv_log = redis.StrictRedis(
|
2014-08-14 17:55:18 +02:00
|
|
|
host=cfg.get("Redis_Log", "host"),
|
|
|
|
port=cfg.getint("Redis_Log", "port"),
|
|
|
|
db=cfg.getint("Redis_Log", "db"))
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__, static_url_path='/static/')
|
|
|
|
|
2014-08-14 17:55:18 +02:00
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
def event_stream():
|
|
|
|
pubsub = r_serv_log.pubsub()
|
|
|
|
pubsub.psubscribe("Script" + '.*')
|
|
|
|
for msg in pubsub.listen():
|
|
|
|
level = msg['channel'].split('.')[1]
|
|
|
|
if msg['type'] == 'pmessage' and level != "DEBUG":
|
|
|
|
yield 'data: %s\n\n' % json.dumps(msg)
|
|
|
|
|
2014-08-14 17:55:18 +02:00
|
|
|
|
2014-08-26 17:33:28 +02:00
|
|
|
def get_queues(r):
|
|
|
|
# We may want to put the llen in a pipeline to do only one query.
|
2014-08-29 19:37:56 +02:00
|
|
|
return [(queue, int(card)) for queue, card in
|
|
|
|
r.hgetall("queues").iteritems()]
|
2014-08-26 17:33:28 +02:00
|
|
|
|
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
@app.route("/_logs")
|
|
|
|
def logs():
|
|
|
|
return flask.Response(event_stream(), mimetype="text/event-stream")
|
|
|
|
|
|
|
|
|
2014-08-14 17:55:18 +02:00
|
|
|
@app.route("/_stuff", methods=['GET'])
|
2014-08-06 11:43:40 +02:00
|
|
|
def stuff():
|
2014-08-26 17:33:28 +02:00
|
|
|
return jsonify(row1=get_queues(r_serv))
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-14 17:55:18 +02:00
|
|
|
|
2014-12-24 15:42:20 +01:00
|
|
|
@app.route("/search", methods=['POST'])
|
|
|
|
def search():
|
|
|
|
query = request.form['query']
|
|
|
|
q = []
|
|
|
|
q.append(query)
|
|
|
|
r = []
|
|
|
|
# Search
|
|
|
|
from whoosh import index
|
|
|
|
from whoosh.fields import Schema, TEXT, ID
|
|
|
|
schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
|
|
|
|
|
|
|
|
indexpath = os.path.join(os.environ['AIL_HOME'], cfg.get("Indexer", "path"))
|
|
|
|
ix = index.open_dir(indexpath)
|
|
|
|
from whoosh.qparser import QueryParser
|
|
|
|
with ix.searcher() as searcher:
|
|
|
|
query = QueryParser("content", ix.schema).parse(" ".join(q))
|
|
|
|
results = searcher.search(query, limit=None)
|
|
|
|
for x in results:
|
|
|
|
r.append(x.items()[0][1])
|
|
|
|
return render_template("search.html", r=r)
|
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2014-08-26 09:25:28 +02:00
|
|
|
return render_template("index.html")
|
2014-08-14 17:55:18 +02:00
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
@app.route("/monitoring/")
|
|
|
|
def monitoring():
|
|
|
|
for queue in r_serv.smembers("queues"):
|
2014-08-14 17:55:18 +02:00
|
|
|
return render_template("Queue_live_Monitoring.html", last_value=queue)
|
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
@app.route("/wordstrending/")
|
|
|
|
def wordstrending():
|
|
|
|
return render_template("Wordstrending.html")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-08-14 17:55:18 +02:00
|
|
|
app.run(host='0.0.0.0', port=7000, threaded=True)
|