Clean up and refactoring for back-end moduleStats

pull/65/head
Mokaddem 2016-07-27 11:55:57 +02:00
parent 27e0d5c578
commit 5a02a7ffce
4 changed files with 17 additions and 48 deletions

View File

@ -116,6 +116,8 @@ def get_date_range(num_day):
date_list.append(date.substract_day(i)) date_list.append(date.substract_day(i))
return date_list return date_list
# 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): def get_top_relevant_data(server, module_name):
redis_progression_name_set = 'top_'+ module_name +'_set' redis_progression_name_set = 'top_'+ module_name +'_set'
days = 0 days = 0
@ -149,7 +151,6 @@ def stuff():
@app.route("/_progressionCharts", methods=['GET']) @app.route("/_progressionCharts", methods=['GET'])
def progressionCharts(): def progressionCharts():
#To be used later
attribute_name = request.args.get('attributeName') attribute_name = request.args.get('attributeName')
trending_name = request.args.get('trendingName') trending_name = request.args.get('trendingName')
bar_requested = True if request.args.get('bar') == "true" else False bar_requested = True if request.args.get('bar') == "true" else False
@ -169,6 +170,7 @@ def progressionCharts():
redis_progression_name = 'top_progression_'+trending_name redis_progression_name = 'top_progression_'+trending_name
redis_progression_name_set = 'top_progression_'+trending_name+'_set' redis_progression_name_set = 'top_progression_'+trending_name+'_set'
# Iterate over element in top_x_set and retreive their value
member_set = [] member_set = []
for keyw in r_serv_charts.smembers(redis_progression_name_set): for keyw in r_serv_charts.smembers(redis_progression_name_set):
keyw_value = r_serv_charts.hget(redis_progression_name, keyw) keyw_value = r_serv_charts.hget(redis_progression_name, keyw)
@ -181,7 +183,6 @@ def progressionCharts():
@app.route("/_moduleCharts", methods=['GET']) @app.route("/_moduleCharts", methods=['GET'])
def modulesCharts(): def modulesCharts():
#To be used later
keyword_name = request.args.get('keywordName') keyword_name = request.args.get('keywordName')
module_name = request.args.get('moduleName') module_name = request.args.get('moduleName')
bar_requested = True if request.args.get('bar') == "true" else False bar_requested = True if request.args.get('bar') == "true" else False
@ -204,9 +205,8 @@ def modulesCharts():
return jsonify(member_set) return jsonify(member_set)
@app.route("/_sizeCharts", methods=['GET']) @app.route("/_providersChart", methods=['GET'])
def sizeCharts(): def providersChart():
#To be used later
keyword_name = request.args.get('keywordName') keyword_name = request.args.get('keywordName')
module_name = request.args.get('moduleName') module_name = request.args.get('moduleName')
bar_requested = True if request.args.get('bar') == "true" else False bar_requested = True if request.args.get('bar') == "true" else False
@ -218,18 +218,19 @@ def sizeCharts():
date_range = get_date_range(num_day) date_range = get_date_range(num_day)
# Retreive all data from the last num_day # Retreive all data from the last num_day
for date in date_range: for date in date_range:
curr_value = r_serv_charts.hget(keyword_name+'_size', date) curr_value = r_serv_charts.hget(keyword_name+'_'+module_name, date)
bar_values.append([date[0:4]+'/'+date[4:6]+'/'+date[6:8], float(curr_value if curr_value is not None else 0)]) bar_values.append([date[0:4]+'/'+date[4:6]+'/'+date[6:8], float(curr_value if curr_value is not None else 0.0)])
return jsonify(bar_values) return jsonify(bar_values)
else: else:
redis_progression_name_set = 'top_size_set' redis_provider_name_set = 'top_size_set' if module_name == "size" else 'providers_set'
# Iterate over element in top_x_set and retreive their value
member_set = [] member_set = []
for keyw in r_serv_charts.smembers(redis_progression_name_set): for keyw in r_serv_charts.smembers(redis_provider_name_set):
redis_progression_name = keyw+'_size' redis_provider_name = keyw+'_'+module_name
keyw_value = r_serv_charts.hget(redis_progression_name, get_date_range(0)[0]) 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 keyw_value = keyw_value if keyw_value is not None else 0.0
member_set.append((keyw, float(keyw_value))) member_set.append((keyw, float(keyw_value)))
member_set.sort(key=lambda tup: tup[1], reverse=True) member_set.sort(key=lambda tup: tup[1], reverse=True)
if len(member_set) == 0: if len(member_set) == 0:
@ -237,38 +238,6 @@ def sizeCharts():
return jsonify(member_set) return jsonify(member_set)
@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)
@app.route("/search", methods=['POST']) @app.route("/search", methods=['POST'])
def search(): def search():

View File

@ -39,7 +39,7 @@ function plot_top_graph(module_name){
/**** Pie Chart ****/ /**** Pie Chart ****/
// moduleCharts is used the decide the url to request data // moduleCharts is used the decide the url to request data
var moduleCharts = "size" == module_name ? "sizeCharts" : ("number" == module_name ? "numberChart" : "moduleCharts"); var moduleCharts = "size" == module_name ? "providersChart" : ("num" == module_name ? "providersChart" : "moduleCharts");
var tot_sum = 0; // used to detect elements putted in 'Other' pie's part var tot_sum = 0; // used to detect elements putted in 'Other' pie's part
var data_other = []; // used to detect elements putted in 'Other' pie's part var data_other = []; // used to detect elements putted in 'Other' pie's part

View File

@ -71,7 +71,7 @@
plot_top_graph("credential"); plot_top_graph("credential");
plot_top_graph("mail"); plot_top_graph("mail");
plot_top_graph("size"); plot_top_graph("size");
plot_top_graph("number"); plot_top_graph("num");
setTimeout(refreshPlot, 10000); setTimeout(refreshPlot, 10000);
} }

View File

@ -64,8 +64,8 @@
<div class="flot-chart-content col-lg-6" id="flot-bar-chart-size" style="height:250px; width:48%;"><div class="alert alert-info">Click on a part</div></div> <div class="flot-chart-content col-lg-6" id="flot-bar-chart-size" style="height:250px; width:48%;"><div class="alert alert-info">Click on a part</div></div>
<h4>Number of paste by provider </h4> <h4>Number of paste by provider </h4>
<div class="flot-chart-content col-lg-6" id="flot-pie-chart-number" style="height:250px; width:48%;"></div> <div class="flot-chart-content col-lg-6" id="flot-pie-chart-num" style="height:250px; width:48%;"></div>
<div class="flot-chart-content col-lg-6" id="flot-bar-chart-number" style="height:250px; width:48%;"><div class="alert alert-info">Click on a part</div></div> <div class="flot-chart-content col-lg-6" id="flot-bar-chart-num" style="height:250px; width:48%;"><div class="alert alert-info">Click on a part</div></div>
</div> </div>
<!-- /.row --> <!-- /.row -->