2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-12-09 08:46:37 +01:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
'''
|
|
|
|
Flask functions and routes for the trending modules page
|
|
|
|
'''
|
|
|
|
import redis
|
|
|
|
import datetime
|
2016-12-23 15:44:46 +01:00
|
|
|
from Date import Date
|
2016-12-09 08:46:37 +01:00
|
|
|
import flask
|
2017-04-19 11:02:03 +02:00
|
|
|
from flask import Flask, render_template, jsonify, request, Blueprint
|
2019-06-19 17:02:09 +02:00
|
|
|
|
2019-11-20 16:15:08 +01:00
|
|
|
from Role_Manager import login_admin, login_analyst, login_read_only
|
2019-05-02 17:31:14 +02:00
|
|
|
from flask_login import login_required
|
2016-12-09 08:46:37 +01:00
|
|
|
|
|
|
|
# ============ VARIABLES ============
|
|
|
|
import Flask_config
|
|
|
|
|
|
|
|
app = Flask_config.app
|
2018-09-20 10:38:19 +02:00
|
|
|
baseUrl = Flask_config.baseUrl
|
2016-12-09 08:46:37 +01:00
|
|
|
r_serv_charts = Flask_config.r_serv_charts
|
2017-04-19 11:02:03 +02:00
|
|
|
|
|
|
|
trendingmodules = Blueprint('trendingmodules', __name__, template_folder='templates')
|
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
# ============ FUNCTIONS ============
|
|
|
|
|
|
|
|
# Iterate over elements in the module provided and return the today data or the last data
|
|
|
|
# return format: [('passed_days', num_of_passed_days), ('elem_name1', elem_value1), ('elem_name2', elem_value2)]]
|
|
|
|
def get_top_relevant_data(server, module_name):
|
|
|
|
days = 0
|
|
|
|
for date in get_date_range(15):
|
|
|
|
redis_progression_name_set = 'top_'+ module_name +'_set_' + date
|
|
|
|
member_set = server.zrevrangebyscore(redis_progression_name_set, '+inf', '-inf', withscores=True)
|
2018-04-17 16:06:32 +02:00
|
|
|
|
2018-05-04 13:53:29 +02:00
|
|
|
if len(member_set) == 0: #No data for this date
|
2016-12-09 08:46:37 +01:00
|
|
|
days += 1
|
|
|
|
else:
|
2018-05-04 13:53:29 +02:00
|
|
|
member_set.insert(0, ("passed_days", days))
|
|
|
|
return member_set
|
2016-12-09 08:46:37 +01: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
|
|
|
|
|
|
|
|
# ============ ROUTES ============
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
@trendingmodules.route("/_moduleCharts", methods=['GET'])
|
2019-05-02 17:31:14 +02:00
|
|
|
@login_required
|
2019-11-20 16:15:08 +01:00
|
|
|
@login_read_only
|
2016-12-09 08:46:37 +01:00
|
|
|
def modulesCharts():
|
|
|
|
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)])
|
|
|
|
bar_values.insert(0, keyword_name)
|
|
|
|
return jsonify(bar_values)
|
|
|
|
|
|
|
|
else:
|
|
|
|
member_set = get_top_relevant_data(r_serv_charts, module_name)
|
2016-12-26 16:33:10 +01:00
|
|
|
member_set = member_set if member_set is not None else []
|
2016-12-09 08:46:37 +01:00
|
|
|
if len(member_set) == 0:
|
|
|
|
member_set.append(("No relevant data", int(100)))
|
|
|
|
return jsonify(member_set)
|
|
|
|
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
@trendingmodules.route("/_providersChart", methods=['GET'])
|
2019-05-02 17:31:14 +02:00
|
|
|
@login_required
|
2019-11-20 16:15:08 +01:00
|
|
|
@login_read_only
|
2016-12-09 08:46:37 +01:00
|
|
|
def providersChart():
|
|
|
|
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:
|
2018-04-17 16:06:32 +02:00
|
|
|
curr_value_size = ( r_serv_charts.hget(keyword_name+'_'+'size', date) )
|
|
|
|
if curr_value_size is not None:
|
2018-05-04 13:53:29 +02:00
|
|
|
curr_value_size = curr_value_size
|
2018-04-17 16:06:32 +02:00
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
curr_value_num = r_serv_charts.hget(keyword_name+'_'+'num', date)
|
2018-04-17 16:06:32 +02:00
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
curr_value_size_avg = r_serv_charts.hget(keyword_name+'_'+'avg', date)
|
2018-04-17 16:06:32 +02:00
|
|
|
if curr_value_size_avg is not None:
|
2018-05-04 13:53:29 +02:00
|
|
|
curr_value_size_avg = curr_value_size_avg
|
2018-04-17 16:06:32 +02:00
|
|
|
|
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
if module_name == "size":
|
|
|
|
curr_value = float(curr_value_size_avg if curr_value_size_avg is not None else 0)
|
|
|
|
else:
|
|
|
|
curr_value = float(curr_value_num if curr_value_num is not None else 0.0)
|
|
|
|
|
|
|
|
bar_values.append([date[0:4]+'/'+date[4:6]+'/'+date[6:8], curr_value])
|
|
|
|
bar_values.insert(0, keyword_name)
|
|
|
|
return jsonify(bar_values)
|
|
|
|
|
|
|
|
else:
|
|
|
|
#redis_provider_name_set = 'top_size_set' if module_name == "size" else 'providers_set'
|
|
|
|
redis_provider_name_set = 'top_avg_size_set_' if module_name == "size" else 'providers_set_'
|
|
|
|
redis_provider_name_set = redis_provider_name_set + get_date_range(0)[0]
|
2018-04-17 16:06:32 +02:00
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
member_set = r_serv_charts.zrevrangebyscore(redis_provider_name_set, '+inf', '-inf', withscores=True, start=0, num=8)
|
2018-04-17 16:06:32 +02:00
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
# Member set is a list of (value, score) pairs
|
2018-05-04 13:53:29 +02:00
|
|
|
if len(member_set) == 0:
|
2018-05-04 14:25:47 +02:00
|
|
|
member_set.append(("No relevant data", float(100)))
|
2018-05-04 13:53:29 +02:00
|
|
|
return jsonify(member_set)
|
2016-12-09 08:46:37 +01:00
|
|
|
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
@trendingmodules.route("/moduletrending/")
|
2019-05-02 17:31:14 +02:00
|
|
|
@login_required
|
2019-11-20 16:15:08 +01:00
|
|
|
@login_read_only
|
2016-12-09 08:46:37 +01:00
|
|
|
def moduletrending():
|
|
|
|
return render_template("Moduletrending.html")
|
|
|
|
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
# ========= REGISTRATION =========
|
2018-09-20 10:38:19 +02:00
|
|
|
app.register_blueprint(trendingmodules, url_prefix=baseUrl)
|