2014-08-06 09:43:40 +00:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
import redis
|
|
|
|
import ConfigParser
|
|
|
|
import json
|
2016-07-21 11:44:22 +00:00
|
|
|
import datetime
|
2014-12-24 14:42:20 +00:00
|
|
|
from flask import Flask, render_template, jsonify, request
|
2014-08-06 09:43:40 +00:00
|
|
|
import flask
|
2014-08-26 15:33:28 +00:00
|
|
|
import os
|
2016-07-05 14:53:03 +00:00
|
|
|
import sys
|
|
|
|
sys.path.append(os.path.join(os.environ['AIL_BIN'], 'packages/'))
|
|
|
|
import Paste
|
2016-07-21 11:44:22 +00:00
|
|
|
from Date import Date
|
2014-12-24 14:42:20 +00:00
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
# CONFIG #
|
2014-08-26 15:33:28 +00: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 09:43:40 +00:00
|
|
|
cfg = ConfigParser.ConfigParser()
|
2014-08-26 15:33:28 +00:00
|
|
|
cfg.read(configfile)
|
2016-07-06 12:30:32 +00:00
|
|
|
|
2016-07-08 08:19:24 +00:00
|
|
|
max_preview_char = int(cfg.get("Flask", "max_preview_char")) # Maximum number of character to display in the tooltip
|
|
|
|
max_preview_modal = int(cfg.get("Flask", "max_preview_modal")) # Maximum number of character to display in the modal
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
# REDIS #
|
|
|
|
r_serv = redis.StrictRedis(
|
2014-08-14 15:55:18 +00:00
|
|
|
host=cfg.get("Redis_Queues", "host"),
|
|
|
|
port=cfg.getint("Redis_Queues", "port"),
|
|
|
|
db=cfg.getint("Redis_Queues", "db"))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
r_serv_log = redis.StrictRedis(
|
2014-08-14 15:55:18 +00:00
|
|
|
host=cfg.get("Redis_Log", "host"),
|
|
|
|
port=cfg.getint("Redis_Log", "port"),
|
|
|
|
db=cfg.getint("Redis_Log", "db"))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2016-07-21 11:44:22 +00:00
|
|
|
r_serv_charts = redis.StrictRedis(
|
|
|
|
host=cfg.get("Redis_Level_DB_Trending", "host"),
|
|
|
|
port=cfg.getint("Redis_Level_DB_Trending", "port"),
|
|
|
|
db=cfg.getint("Redis_Level_DB_Trending", "db"))
|
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
app = Flask(__name__, static_url_path='/static/')
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
|
2014-08-06 09:43:40 +00: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 15:55:18 +00:00
|
|
|
|
2014-08-26 15:33:28 +00:00
|
|
|
def get_queues(r):
|
|
|
|
# We may want to put the llen in a pipeline to do only one query.
|
2014-08-29 17:37:56 +00:00
|
|
|
return [(queue, int(card)) for queue, card in
|
|
|
|
r.hgetall("queues").iteritems()]
|
2014-08-26 15:33:28 +00:00
|
|
|
|
|
|
|
|
2016-07-05 14:53:03 +00:00
|
|
|
def list_len(s):
|
|
|
|
return len(s)
|
|
|
|
app.jinja_env.filters['list_len'] = list_len
|
|
|
|
|
2016-07-19 08:48:44 +00:00
|
|
|
def parseStringToList(the_string):
|
|
|
|
strList = ""
|
|
|
|
elemList = []
|
|
|
|
for c in the_string:
|
|
|
|
if c != ']':
|
|
|
|
if c != '[' and c !=' ' and c != '"':
|
|
|
|
strList += c
|
|
|
|
else:
|
|
|
|
the_list = strList.split(',')
|
|
|
|
if len(the_list) == 2:
|
|
|
|
elemList.append(the_list)
|
|
|
|
elif len(the_list) > 1:
|
|
|
|
elemList.append(the_list[1:])
|
|
|
|
strList = ""
|
|
|
|
return elemList
|
2016-07-08 08:19:24 +00:00
|
|
|
|
|
|
|
def showpaste(content_range):
|
|
|
|
requested_path = request.args.get('paste', '')
|
|
|
|
paste = Paste.Paste(requested_path)
|
|
|
|
p_date = str(paste._get_p_date())
|
|
|
|
p_date = p_date[6:]+'/'+p_date[4:6]+'/'+p_date[0:4]
|
|
|
|
p_source = paste.p_source
|
|
|
|
p_encoding = paste._get_p_encoding()
|
|
|
|
p_language = paste._get_p_language()
|
|
|
|
p_size = paste.p_size
|
|
|
|
p_mime = paste.p_mime
|
|
|
|
p_lineinfo = paste.get_lines_info()
|
|
|
|
p_content = paste.get_p_content().decode('utf-8', 'ignore')
|
2016-07-19 08:48:44 +00:00
|
|
|
p_duplicate_full_list = parseStringToList(paste._get_p_duplicate())
|
|
|
|
p_duplicate_list = []
|
|
|
|
p_simil_list = []
|
|
|
|
|
|
|
|
for dup_list in p_duplicate_full_list:
|
|
|
|
path, simil_percent = dup_list
|
|
|
|
p_duplicate_list.append(path)
|
|
|
|
p_simil_list.append(simil_percent)
|
|
|
|
|
2016-07-08 08:19:24 +00:00
|
|
|
if content_range != 0:
|
|
|
|
p_content = p_content[0:content_range]
|
|
|
|
|
2016-07-19 08:48:44 +00:00
|
|
|
return render_template("show_saved_paste.html", date=p_date, source=p_source, encoding=p_encoding, language=p_language, size=p_size, mime=p_mime, lineinfo=p_lineinfo, content=p_content, initsize=len(p_content), duplicate_list = p_duplicate_list, simil_list = p_simil_list)
|
2016-07-08 08:19:24 +00:00
|
|
|
|
2016-07-21 11:44:22 +00:00
|
|
|
def get_date_range(num_day):
|
|
|
|
curr_date = datetime.date.today()
|
|
|
|
date = Date(str(curr_date.year)+str(curr_date.month).zfill(2)+str(curr_date.day).zfill(2))
|
|
|
|
date_list = []
|
|
|
|
|
|
|
|
for i in range(0, num_day+1):
|
|
|
|
date_list.append(date.substract_day(i))
|
|
|
|
return date_list
|
|
|
|
|
2016-07-26 10:07:52 +00:00
|
|
|
def get_top_relevant_data(server, module_name):
|
|
|
|
redis_progression_name_set = 'top_'+ module_name +'_set'
|
|
|
|
days = 0
|
|
|
|
for date in get_date_range(15):
|
|
|
|
member_set = []
|
|
|
|
for keyw in server.smembers(redis_progression_name_set):
|
|
|
|
redis_progression_name = module_name+'-'+keyw
|
|
|
|
keyw_value = server.hget(date ,redis_progression_name)
|
|
|
|
keyw_value = keyw_value if keyw_value is not None else 0
|
|
|
|
member_set.append((keyw, int(keyw_value)))
|
|
|
|
member_set.sort(key=lambda tup: tup[1], reverse=True)
|
|
|
|
if member_set[0][1] == 0: #No data for this date
|
|
|
|
days += 1
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
member_set.insert(0, ("passed_days", days))
|
|
|
|
return member_set
|
2016-07-21 11:44:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ============ ROUTES ============
|
2016-07-08 08:19:24 +00:00
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
@app.route("/_logs")
|
|
|
|
def logs():
|
|
|
|
return flask.Response(event_stream(), mimetype="text/event-stream")
|
|
|
|
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
@app.route("/_stuff", methods=['GET'])
|
2014-08-06 09:43:40 +00:00
|
|
|
def stuff():
|
2014-08-26 15:33:28 +00:00
|
|
|
return jsonify(row1=get_queues(r_serv))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2016-07-21 11:44:22 +00:00
|
|
|
@app.route("/_progressionCharts", methods=['GET'])
|
|
|
|
def progressionCharts():
|
|
|
|
#To be used later
|
|
|
|
attribute_name = request.args.get('attributeName')
|
|
|
|
trending_name = request.args.get('trendingName')
|
|
|
|
bar_requested = True if request.args.get('bar') == "true" else False
|
|
|
|
|
|
|
|
if (bar_requested):
|
|
|
|
num_day = int(request.args.get('days'))
|
|
|
|
bar_values = []
|
|
|
|
|
|
|
|
date_range = get_date_range(num_day)
|
|
|
|
# Retreive all data from the last num_day
|
|
|
|
for date in date_range:
|
|
|
|
curr_value = r_serv_charts.hget(attribute_name, date)
|
|
|
|
bar_values.append([date[0:4]+'/'+date[4:6]+'/'+date[6:8], int(curr_value if curr_value is not None else 0)])
|
|
|
|
return jsonify(bar_values)
|
|
|
|
|
|
|
|
else:
|
|
|
|
redis_progression_name = 'top_progression_'+trending_name
|
|
|
|
redis_progression_name_set = 'top_progression_'+trending_name+'_set'
|
|
|
|
|
|
|
|
member_set = []
|
|
|
|
for keyw in r_serv_charts.smembers(redis_progression_name_set):
|
|
|
|
keyw_value = r_serv_charts.hget(redis_progression_name, keyw)
|
|
|
|
keyw_value = keyw_value if keyw_value is not None else 0
|
|
|
|
member_set.append((keyw, int(keyw_value)))
|
|
|
|
member_set.sort(key=lambda tup: tup[1], reverse=True)
|
|
|
|
if len(member_set) == 0:
|
|
|
|
member_set.append(("No relevant data", int(100)))
|
|
|
|
return jsonify(member_set)
|
|
|
|
|
2016-07-25 14:38:57 +00:00
|
|
|
@app.route("/_moduleCharts", methods=['GET'])
|
|
|
|
def modulesCharts():
|
|
|
|
#To be used later
|
|
|
|
keyword_name = request.args.get('keywordName')
|
|
|
|
module_name = request.args.get('moduleName')
|
|
|
|
bar_requested = True if request.args.get('bar') == "true" else False
|
|
|
|
|
|
|
|
if (bar_requested):
|
|
|
|
num_day = int(request.args.get('days'))
|
|
|
|
bar_values = []
|
|
|
|
|
|
|
|
date_range = get_date_range(num_day)
|
|
|
|
# Retreive all data from the last num_day
|
|
|
|
for date in date_range:
|
|
|
|
curr_value = r_serv_charts.hget(date, module_name+'-'+keyword_name)
|
|
|
|
bar_values.append([date[0:4]+'/'+date[4:6]+'/'+date[6:8], int(curr_value if curr_value is not None else 0)])
|
|
|
|
return jsonify(bar_values)
|
|
|
|
|
|
|
|
else:
|
2016-07-26 10:07:52 +00:00
|
|
|
member_set = get_top_relevant_data(r_serv_charts, module_name)
|
2016-07-25 14:38:57 +00:00
|
|
|
if len(member_set) == 0:
|
|
|
|
member_set.append(("No relevant data", int(100)))
|
|
|
|
return jsonify(member_set)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/_sizeCharts", methods=['GET'])
|
|
|
|
def sizeCharts():
|
|
|
|
#To be used later
|
|
|
|
keyword_name = request.args.get('keywordName')
|
|
|
|
module_name = request.args.get('moduleName')
|
|
|
|
bar_requested = True if request.args.get('bar') == "true" else False
|
|
|
|
|
|
|
|
if (bar_requested):
|
|
|
|
num_day = int(request.args.get('days'))
|
|
|
|
bar_values = []
|
|
|
|
|
|
|
|
date_range = get_date_range(num_day)
|
|
|
|
# Retreive all data from the last num_day
|
|
|
|
for date in date_range:
|
|
|
|
curr_value = r_serv_charts.hget(keyword_name+'_size', date)
|
|
|
|
bar_values.append([date[0:4]+'/'+date[4:6]+'/'+date[6:8], float(curr_value if curr_value is not None else 0)])
|
|
|
|
return jsonify(bar_values)
|
|
|
|
|
|
|
|
else:
|
|
|
|
redis_progression_name_set = 'top_size_set'
|
|
|
|
|
|
|
|
member_set = []
|
|
|
|
for keyw in r_serv_charts.smembers(redis_progression_name_set):
|
|
|
|
redis_progression_name = keyw+'_size'
|
|
|
|
keyw_value = r_serv_charts.hget(redis_progression_name, get_date_range(0)[0])
|
|
|
|
keyw_value = keyw_value if keyw_value is not None else 0
|
|
|
|
member_set.append((keyw, float(keyw_value)))
|
|
|
|
member_set.sort(key=lambda tup: tup[1], reverse=True)
|
|
|
|
if len(member_set) == 0:
|
|
|
|
member_set.append(("No relevant data", float(100)))
|
|
|
|
return jsonify(member_set)
|
|
|
|
|
|
|
|
|
2016-07-27 07:27:32 +00:00
|
|
|
@app.route("/_numberChart", methods=['GET'])
|
|
|
|
def numberChart():
|
|
|
|
#To be used later
|
|
|
|
keyword_name = request.args.get('keywordName')
|
|
|
|
module_name = request.args.get('moduleName')
|
|
|
|
bar_requested = True if request.args.get('bar') == "true" else False
|
|
|
|
|
|
|
|
if (bar_requested):
|
|
|
|
num_day = int(request.args.get('days'))
|
|
|
|
bar_values = []
|
|
|
|
|
|
|
|
date_range = get_date_range(num_day)
|
|
|
|
# Retreive all data from the last num_day
|
|
|
|
for date in date_range:
|
|
|
|
curr_value = r_serv_charts.hget(keyword_name+'_num', date)
|
|
|
|
bar_values.append([date[0:4]+'/'+date[4:6]+'/'+date[6:8], int(curr_value if curr_value is not None else 0)])
|
|
|
|
return jsonify(bar_values)
|
|
|
|
|
|
|
|
else:
|
|
|
|
redis_provider_name_set = 'providers_set'
|
|
|
|
|
|
|
|
member_set = []
|
|
|
|
for keyw in r_serv_charts.smembers(redis_provider_name_set):
|
|
|
|
redis_provider_name = keyw+'_num'
|
|
|
|
keyw_value = r_serv_charts.hget(redis_provider_name, get_date_range(0)[0])
|
|
|
|
keyw_value = keyw_value if keyw_value is not None else 0
|
|
|
|
member_set.append((keyw, int(keyw_value)))
|
|
|
|
member_set.sort(key=lambda tup: tup[1], reverse=True)
|
|
|
|
if len(member_set) == 0:
|
|
|
|
member_set.append(("No relevant data", int(100)))
|
|
|
|
return jsonify(member_set)
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
|
2014-12-24 14:42:20 +00:00
|
|
|
@app.route("/search", methods=['POST'])
|
|
|
|
def search():
|
|
|
|
query = request.form['query']
|
|
|
|
q = []
|
|
|
|
q.append(query)
|
2016-07-08 08:19:24 +00:00
|
|
|
r = [] #complete path
|
|
|
|
c = [] #preview of the paste content
|
2016-07-07 14:38:00 +00:00
|
|
|
paste_date = []
|
|
|
|
paste_size = []
|
2014-12-24 14:42:20 +00:00
|
|
|
# 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])
|
2016-07-07 14:38:00 +00:00
|
|
|
paste = Paste.Paste(x.items()[0][1])
|
|
|
|
content = paste.get_p_content().decode('utf8', 'ignore')
|
2016-07-05 14:53:03 +00:00
|
|
|
content_range = max_preview_char if len(content)>max_preview_char else len(content)-1
|
|
|
|
c.append(content[0:content_range])
|
2016-07-08 07:16:43 +00:00
|
|
|
curr_date = str(paste._get_p_date())
|
|
|
|
curr_date = curr_date[0:4]+'/'+curr_date[4:6]+'/'+curr_date[6:]
|
|
|
|
paste_date.append(curr_date)
|
2016-07-07 14:38:00 +00:00
|
|
|
paste_size.append(paste._get_p_size())
|
2016-07-07 14:43:18 +00:00
|
|
|
return render_template("search.html", r=r, c=c, query=request.form['query'], paste_date=paste_date, paste_size=paste_size, char_to_display=max_preview_modal)
|
2014-12-24 14:42:20 +00:00
|
|
|
|
2016-07-08 08:19:24 +00:00
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2016-07-13 13:57:33 +00:00
|
|
|
default_minute = cfg.get("Flask", "minute_processed_paste")
|
|
|
|
return render_template("index.html", default_minute = default_minute)
|
2014-08-14 15:55:18 +00:00
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
@app.route("/monitoring/")
|
|
|
|
def monitoring():
|
|
|
|
for queue in r_serv.smembers("queues"):
|
2014-08-14 15:55:18 +00:00
|
|
|
return render_template("Queue_live_Monitoring.html", last_value=queue)
|
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
@app.route("/wordstrending/")
|
|
|
|
def wordstrending():
|
2016-07-12 08:02:01 +00:00
|
|
|
default_display = cfg.get("Flask", "default_display")
|
|
|
|
return render_template("Wordstrending.html", default_display = default_display)
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
|
2016-07-01 14:59:08 +00:00
|
|
|
@app.route("/protocolstrending/")
|
|
|
|
def protocolstrending():
|
2016-07-12 08:02:01 +00:00
|
|
|
default_display = cfg.get("Flask", "default_display")
|
|
|
|
return render_template("Protocolstrending.html", default_display = default_display)
|
2016-07-01 14:59:08 +00:00
|
|
|
|
2016-07-08 08:19:24 +00:00
|
|
|
|
2016-07-08 13:08:37 +00:00
|
|
|
@app.route("/trending/")
|
|
|
|
def trending():
|
2016-07-11 14:57:45 +00:00
|
|
|
default_display = cfg.get("Flask", "default_display")
|
|
|
|
return render_template("Trending.html", default_display = default_display)
|
2016-07-01 14:59:08 +00:00
|
|
|
|
2016-07-08 08:19:24 +00:00
|
|
|
|
2016-07-25 14:38:57 +00:00
|
|
|
@app.route("/moduletrending/")
|
|
|
|
def moduletrending():
|
|
|
|
return render_template("Moduletrending.html")
|
|
|
|
|
|
|
|
|
2016-07-08 08:19:24 +00:00
|
|
|
@app.route("/showsavedpaste/") #completely shows the paste in a new tab
|
2016-07-05 14:53:03 +00:00
|
|
|
def showsavedpaste():
|
2016-07-08 08:19:24 +00:00
|
|
|
return showpaste(0)
|
|
|
|
|
2016-07-06 12:30:32 +00:00
|
|
|
|
|
|
|
@app.route("/showpreviewpaste/")
|
|
|
|
def showpreviewpaste():
|
2016-07-08 08:19:24 +00:00
|
|
|
return showpaste(max_preview_modal)
|
|
|
|
|
2016-07-05 14:53:03 +00:00
|
|
|
|
2016-07-06 14:54:27 +00:00
|
|
|
@app.route("/getmoredata/")
|
|
|
|
def getmoredata():
|
|
|
|
requested_path = request.args.get('paste', '')
|
|
|
|
paste = Paste.Paste(requested_path)
|
|
|
|
p_content = paste.get_p_content().decode('utf-8', 'ignore')
|
2016-07-08 10:09:33 +00:00
|
|
|
to_return = p_content[max_preview_modal-1:]
|
2016-07-06 14:54:27 +00:00
|
|
|
return to_return
|
2016-07-01 14:59:08 +00:00
|
|
|
|
2016-07-08 08:19:24 +00:00
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
if __name__ == "__main__":
|
2014-08-14 15:55:18 +00:00
|
|
|
app.run(host='0.0.0.0', port=7000, threaded=True)
|