From 3b3f3aa89cd5ccee94c2cbdc51192f2cdbafdd2d Mon Sep 17 00:00:00 2001 From: = Date: Wed, 23 Aug 2017 12:20:39 +0200 Subject: [PATCH] Flask: Added support of LevelDB database depending of the year --- var/www/modules/Flask_config.py | 8 ---- .../browsepastes/Flask_browsepastes.py | 37 +++++++++++++++---- .../templates/browse_important_paste.html | 32 ++++++++++++++-- .../templates/important_paste_by_module.html | 3 +- 4 files changed, 61 insertions(+), 19 deletions(-) diff --git a/var/www/modules/Flask_config.py b/var/www/modules/Flask_config.py index 14468fa1..8b6b01de 100644 --- a/var/www/modules/Flask_config.py +++ b/var/www/modules/Flask_config.py @@ -11,7 +11,6 @@ from datetime import datetime # FLASK # app = None -curYear = None #can be set to fit the needs, Correspond to the level-DB year to be used # CONFIG # configfile = os.path.join(os.environ['AIL_BIN'], 'packages/config.cfg') @@ -40,13 +39,6 @@ r_serv_charts = redis.StrictRedis( port=cfg.getint("Redis_Level_DB_Trending", "port"), db=cfg.getint("Redis_Level_DB_Trending", "db")) -# port generated automatically depending on the date -curYear = datetime.now().year if curYear is None else curYear -r_serv_db = redis.StrictRedis( - host=cfg.get("Redis_Level_DB", "host"), - port=curYear, - db=cfg.getint("Redis_Level_DB", "db")) - r_serv_sentiment = redis.StrictRedis( host=cfg.get("Redis_Level_DB_Sentiment", "host"), port=cfg.getint("Redis_Level_DB_Sentiment", "port"), diff --git a/var/www/modules/browsepastes/Flask_browsepastes.py b/var/www/modules/browsepastes/Flask_browsepastes.py index d3b1bb4d..30853c38 100644 --- a/var/www/modules/browsepastes/Flask_browsepastes.py +++ b/var/www/modules/browsepastes/Flask_browsepastes.py @@ -7,6 +7,8 @@ import redis import json import flask +import os +from datetime import datetime from flask import Flask, render_template, jsonify, request, Blueprint import Paste @@ -18,7 +20,25 @@ app = Flask_config.app cfg = Flask_config.cfg max_preview_char = Flask_config.max_preview_char max_preview_modal = Flask_config.max_preview_modal -r_serv_db = Flask_config.r_serv_db + +#init all lvlDB servers +curYear = datetime.now().year +r_serv_db = {} +# port generated automatically depending on available levelDB date +yearList = [] +lvdbdir= os.path.join(os.environ['AIL_HOME'], "LEVEL_DB_DATA/") +for year in os.listdir(lvdbdir): + try: + intYear = int(year) + except: + continue + + yearList.append([year, intYear, int(curYear) == intYear]) + r_serv_db[intYear] = redis.StrictRedis( + host=cfg.get("Redis_Level_DB", "host"), + port=intYear, + db=cfg.getint("Redis_Level_DB", "db")) +yearList.sort(reverse=True) browsepastes = Blueprint('browsepastes', __name__, template_folder='templates') @@ -31,9 +51,9 @@ def getPastebyType(server, module_name): return all_path -def event_stream_getImportantPasteByModule(module_name): +def event_stream_getImportantPasteByModule(module_name, year): index = 0 - all_pastes_list = getPastebyType(r_serv_db, module_name) + all_pastes_list = getPastebyType(r_serv_db[year], module_name) for path in all_pastes_list: index += 1 paste = Paste.Paste(path) @@ -57,18 +77,19 @@ def event_stream_getImportantPasteByModule(module_name): @browsepastes.route("/browseImportantPaste/", methods=['GET']) def browseImportantPaste(): module_name = request.args.get('moduleName') - return render_template("browse_important_paste.html") + return render_template("browse_important_paste.html", year_list=yearList, selected_year=curYear) @browsepastes.route("/importantPasteByModule/", methods=['GET']) def importantPasteByModule(): module_name = request.args.get('moduleName') + currentSelectYear = int(request.args.get('year')) all_content = [] paste_date = [] paste_linenum = [] all_path = [] - allPastes = getPastebyType(r_serv_db, module_name) + allPastes = getPastebyType(r_serv_db[currentSelectYear], module_name) for path in allPastes[0:10]: all_path.append(path) @@ -88,6 +109,7 @@ def importantPasteByModule(): return render_template("important_paste_by_module.html", moduleName=module_name, + year=currentSelectYear, all_path=all_path, content=all_content, paste_date=paste_date, @@ -95,10 +117,11 @@ def importantPasteByModule(): char_to_display=max_preview_modal, finished=finished) -@browsepastes.route("/_getImportantPasteByModule") +@browsepastes.route("/_getImportantPasteByModule", methods=['GET']) def getImportantPasteByModule(): module_name = request.args.get('moduleName') - return flask.Response(event_stream_getImportantPasteByModule(module_name), mimetype="text/event-stream") + currentSelectYear = int(request.args.get('year')) + return flask.Response(event_stream_getImportantPasteByModule(module_name, currentSelectYear), mimetype="text/event-stream") # ========= REGISTRATION ========= diff --git a/var/www/modules/browsepastes/templates/browse_important_paste.html b/var/www/modules/browsepastes/templates/browse_important_paste.html index 5d7d84e3..89fc8aaa 100644 --- a/var/www/modules/browsepastes/templates/browse_important_paste.html +++ b/var/www/modules/browsepastes/templates/browse_important_paste.html @@ -63,6 +63,20 @@ + +
+
+ Year: + +
+
+
+ +
@@ -117,9 +131,10 @@ $("#"+activePage).addClass("active"); var dataPath = 'credential'; - $.get("{{ url_for('browsepastes.importantPasteByModule') }}"+"?moduleName="+dataPath, function(data, status){ + $.get("{{ url_for('browsepastes.importantPasteByModule') }}"+"?moduleName="+dataPath+"&year="+currentSelectYear, function(data, status){ $('#'+dataPath+'-tab').html(data); }); + }); @@ -127,12 +142,23 @@