Auto generate graph div + possibility to query all sentiments data.

This commit is contained in:
Mokaddem 2016-08-25 15:06:18 +02:00
parent 21f2c43572
commit 0b5a3e0cd2
3 changed files with 244 additions and 279 deletions

View file

@ -483,7 +483,12 @@ def sentiment_analysis_getplotdata():
getAllProviders = request.args.get('getProviders')
provider = request.args.get('provider')
allProvider = request.args.get('all')
if getAllProviders == 'True':
if allProvider == "True":
range_providers = r_serv_charts.smembers('all_provider_set')
return jsonify(list(range_providers))
else:
range_providers = r_serv_charts.zrevrangebyscore('providers_set_'+ get_date_range(0)[0], '+inf', '-inf', start=0, num=8)
# if empty, get yesterday top providers
range_providers = r_serv_charts.zrevrangebyscore('providers_set_'+ get_date_range(1)[1], '+inf', '-inf', start=0, num=8) if range_providers == [] else range_providers

View file

@ -1,5 +1,41 @@
/* Functions and config */
function add_new_graph_today(id) {
return "<div id=\"panel-today\" class=\"panel panel-default pannelToday"+id+"\">" +
"<div class=\"panel-heading\">" +
"<strong class=\"sparkLineStatsToday"+id+"t\">Graph "+id+"</strong>" +
"<strong class=\"sparkLineStatsToday"+id+"s pull-right\">Avg</strong>" +
"</div>" +
"<div class=\"panel-body panelInside\">" +
"<table class=\"table\">" +
"<tbody>" +
"<tr>" +
"<td style=\"border-top: 0px solid #ddd;\"><div class=\"sparkLineStatsToday"+id+"\"></div></td> " +
"<td style=\"border-top: 0px solid #ddd;\"><div class=\"sparkLineStatsToday"+id+"b\"></div></td> " +
"</tr>" +
"</tbody>" +
"</table>" +
"</div>" +
"</div>";
};
function add_new_graph_week(id) {
return "<div id=\"panel-week\" class=\"panel panel-default pannelWeek"+id+"\">" +
"<div class=\"panel-heading\">" +
"<strong class=\"sparkLineStatsWeek"+id+"t\">Graph "+id+"</strong>" +
"<strong class=\"sparkLineStatsWeek"+id+"s pull-right\">Avg</strong>" +
"</div>" +
"<div class=\"panel-body panelInside\">" +
"<table class=\"table\">" +
"<tbody>" +
"<tr>" +
"<td style=\"border-top: 0px solid #ddd;\"><div class=\"sparkLineStatsWeek"+id+"\"></div></td> " +
"<td style=\"border-top: 0px solid #ddd;\"><div class=\"sparkLineStatsWeek"+id+"b\"></div></td> " +
"</tr>" +
"</tbody>" +
"</table>" +
"</div>" +
"</div>";
}
function generate_offset_to_time(num){
var to_ret = {};
@ -51,21 +87,43 @@
var all_graph_day_sum = 0.0;
var all_graph_hour_sum = 0.0;
var all_graph_hour_sum_minus = 0.0;
var all_graph_hour_maxVal = 0.0;
var all_day_avg = 0.0;
var all_day_avg_maxVal = 0.0;
var graph_avg = [];
var all_data = [];
var provider_already_loaded = [];
var totNumGraph = 0;
function draw_page() {
$.getJSON("/sentiment_analysis_getplotdata/?getProviders=True",
function draw_page(all) {
$.getJSON("/sentiment_analysis_getplotdata/?getProviders=True&all="+all,
function(data) {
var promises = [];
var the_length = provider_already_loaded.length == 0 ? 0 : provider_already_loaded.length;
for(i=0; i<data.length; i++) {
promises.push(query_and_plot(data[i], i));
if(provider_already_loaded.indexOf(data[i]) != -1) {
continue;
} else {
totNumGraph++;
if(i % 2 == 0) {
$("#today_divl").append(add_new_graph_today(i+the_length+1));
$("#week_divl").append(add_new_graph_week(i+the_length+1));
}
else {
$("#today_divr").append(add_new_graph_today(i+the_length+1));
$("#week_divr").append(add_new_graph_week(i+the_length+1));
}
provider_already_loaded.push(data[i])
promises.push(query_and_plot(data[i], i+the_length));
}
}
$.when.apply($, promises).done( function (arg) {
draw_widgets();
$("#LoadAll").show('fast');
});
}
);
@ -140,6 +198,7 @@ function query_and_plot(provider, graphNum) {
}
all_graph_day_sum += day_sum;
all_graph_hour_sum += hour_sum;
all_graph_hour_sum_minus += hour_sum > 0 ? 0 : 1;
all_graph_hour_maxVal = Math.abs(hour_sum) > all_graph_hour_maxVal ? Math.abs(hour_sum) : all_graph_hour_maxVal;
var curr_avg = curr_sum / (curr_sum_elem);
@ -225,6 +284,7 @@ function query_and_plot(provider, graphNum) {
function draw_widgets() {
/* ---------------- Gauge ---------------- */
var gaugeOptions = {
animateEasing: true,
@ -237,3 +297,154 @@ function draw_widgets() {
arcFillTotal: 20,
incTot: 1.0,
arcBgColorLight: 200,
arcBgColorSat: 0,
arcStrokeFg: 20,
arcStrokeBg: 30,
colorArcFg: '#FF3300',
animateSpeed: 1,
};
// Clone object
var gaugeOptions2 = jQuery.extend(true, {}, gaugeOptions);
var gaugeOptions3 = jQuery.extend(true, {}, gaugeOptions);
gaugeOptions.appendTo = '#gauge_today_last_hour';
gaugeOptions.dialLabel = 'Last hour';
gaugeOptions.elementId = 'gauge1';
var piePercent = (all_graph_hour_sum / (totNumGraph - all_graph_hour_sum_minus)) / all_graph_hour_maxVal;
gaugeOptions.inc = piePercent;
var gauge_today_last_hour = new FlexGauge(gaugeOptions);
gaugeOptions2.appendTo = '#gauge_today_last_days';
gaugeOptions2.dialLabel = 'Today';
gaugeOptions2.elementId = 'gauge2';
piePercent = (all_day_avg / totNumGraph) / all_day_avg_maxVal;
gaugeOptions2.inc = piePercent;
var gauge_today_last_days = new FlexGauge(gaugeOptions2);
gaugeOptions3.appendTo = '#gauge_week';
gaugeOptions3.dialLabel = 'Week';
gaugeOptions3.elementId = 'gauge3';
var graph_avg_sum = 0.0;
var temp_max_val = 0.0;
for (i=0; i<graph_avg.length; i++){
graph_avg_sum += graph_avg[i][1];
temp_max_val = Math.abs(graph_avg[i][1]) > temp_max_val ? Math.abs(graph_avg[i][1]) : temp_max_val;
}
piePercent = (graph_avg_sum / graph_avg.length) / temp_max_val;
gaugeOptions3.inc = piePercent;
var gauge_today_last_days = new FlexGauge(gaugeOptions3);
/* --------- Sort providers -------- */
graph_avg.sort(function(a, b){return b[1]-a[1]});
for (i=1; i<6; i++){
$('.worst'+i).text(graph_avg[7-(i-1)][0]);
$('.best'+i).text(graph_avg[i-1][0]);
}
/* ----------- CanvasJS ------------ */
var comp_sum_day_pos = 0.0;
var comp_sum_day_neg = 0.0;
var comp_sum_hour_pos = 0.0;
var comp_sum_hour_neg = 0.0;
for(graphNum=0; graphNum<totNumGraph; graphNum++){
curr_graphData = all_data[graphNum];
var gauge_data = curr_graphData.slice(curr_graphData.length-24, curr_graphData.length);
for (i=1; i< gauge_data.length; i++){
comp_sum_day_pos += gauge_data[i].compoundPos;
comp_sum_day_neg += gauge_data[i].compoundNeg;
if(i == 23){
comp_sum_hour_pos += gauge_data[i].compoundPos;
comp_sum_hour_neg += gauge_data[i].compoundNeg;
}
}
}
var options_canvasJS_1 = {
animationEnabled: true,
axisY: {
tickThickness: 0,
lineThickness: 0,
valueFormatString: " ",
gridThickness: 0
},
axisX: {
tickThickness: 0,
lineThickness: 0,
labelFontSize: 0.1,
},
data: [
{
toolTipContent: "<span style='\"'color: {color};'\"'><strong>Positive: </strong></span><span><strong>{y}</strong></span>",
type: "bar",
color: "green",
dataPoints: [
{y: comp_sum_hour_pos/totNumGraph}
]
},
{
toolTipContent: "<span style='\"'color: {color};'\"'><strong>Negative: </strong></span><span><strong>{y}</strong></span>",
type: "bar",
color: "red",
dataPoints: [
{y: comp_sum_hour_neg/totNumGraph}
]
}
]
};
var chart_canvas1 = new CanvasJS.Chart("bar_today_last_hour", options_canvasJS_1);
var options_canvasJS_2 = {
animationEnabled: true,
axisY: {
tickThickness: 0,
lineThickness: 0,
valueFormatString: " ",
gridThickness: 0
},
axisX: {
tickThickness: 0,
lineThickness: 0,
labelFontSize: 0.1,
},
data: [
{
toolTipContent: "<span style='\"'color: {color};'\"'><strong>Positive: </strong></span><span><strong>{y}</strong></span>",
type: "bar",
color: "green",
dataPoints: [
{y: comp_sum_day_pos/totNumGraph}
]
},
{
toolTipContent: "<span style='\"'color: {color};'\"'><strong>Negative: </strong></span><span><strong>{y}</strong></span>",
type: "bar",
color: "red",
dataPoints: [
{y: comp_sum_day_neg/totNumGraph}
]
}
]
};
var chart_canvas2 = new CanvasJS.Chart("bar_today_last_days", options_canvasJS_2);
chart_canvas1.render();
chart_canvas2.render();
}

View file

@ -113,6 +113,7 @@
</div>
<!-- /.row -->
<div class="row">
<button id="LoadAll" class="btn btn-info" style="margin: 5px;"><span class="glyphicon glyphicon-download"> </span> Load data from all providers </button>
<!-- Pannel TODAY -->
<div class="row">
@ -128,139 +129,13 @@
<div class="col-lg-9" style="padding-left: 0px;">
<!-- providers charts -->
<div class="col-lg-6">
<div class="sparkLineStats">
<div id="panel-today" class="panel panel-default pannelToday1">
<div class="panel-heading">
<strong class="sparkLineStatsToday1t">Graph 1</strong>
<strong class="sparkLineStatsToday1s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday1"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday1b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-today" class="panel panel-default pannelToday2">
<div class="panel-heading">
<strong class="sparkLineStatsToday2t">Graph 2</strong>
<strong class="sparkLineStatsToday2s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday2"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday2b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-today" class="panel panel-default pannelToday3">
<div class="panel-heading">
<strong class="sparkLineStatsToday3t">Graph 3</strong>
<strong class="sparkLineStatsToday3s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday3"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday3b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-today" class="panel panel-default pannelToday4">
<div class="panel-heading">
<strong class="sparkLineStatsToday4t">Graph 4</strong>
<strong class="sparkLineStatsToday4s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday4"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday4b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="today_divl" class="sparkLineStats">
</div>
</div>
<div class="col-lg-6">
<div class="sparkLineStats">
<div id="panel-today" class="panel panel-default pannelToday5">
<div class="panel-heading">
<strong class="sparkLineStatsToday5t">Graph 5</strong>
<strong class="sparkLineStatsToday5s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday5"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday5b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-today" class="panel panel-default pannelToday6">
<div class="panel-heading">
<strong class="sparkLineStatsToday6t">Graph 6</strong>
<strong class="sparkLineStatsToday6s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday6"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday6b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-today" class="panel panel-default pannelToday7">
<div class="panel-heading">
<strong class="sparkLineStatsToday7t">Graph 7</strong>
<strong class="sparkLineStatsToday7s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday7"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday7b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-today" class="panel panel-default pannelToday8">
<div class="panel-heading">
<strong class="sparkLineStatsToday8t">Graph 8</strong>
<strong class="sparkLineStatsToday8s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday8"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsToday8b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="today_divr" class="sparkLineStats">
</div>
</div>
</div>
@ -304,139 +179,13 @@
<div class="col-lg-9" style="padding-left: 0px;">
<!-- providers charts -->
<div class="col-lg-6">
<div class="sparkLineStats">
<div id="panel-week" class="panel panel-default pannelWeek1">
<div class="panel-heading">
<strong class="sparkLineStatsWeek1t">Graph 1</strong>
<strong class="sparkLineStatsWeek1s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek1"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek1b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-week" class="panel panel-default pannelWeek2">
<div class="panel-heading">
<strong class="sparkLineStatsWeek2t">Graph 2</strong>
<strong class="sparkLineStatsWeek2s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek2"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek2b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-week" class="panel panel-default pannelWeek3">
<div class="panel-heading">
<strong class="sparkLineStatsWeek3t">Graph 3</strong>
<strong class="sparkLineStatsWeek3s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek3"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek3b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-week" class="panel panel-default pannelWeek4">
<div class="panel-heading">
<strong class="sparkLineStatsWeek4t">Graph 4</strong>
<strong class="sparkLineStatsWeek4s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek4"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek4b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="week_divl" class="sparkLineStats">
</div>
</div>
<div class="col-lg-6">
<div class="sparkLineStats">
<div id="panel-week" class="panel panel-default pannelWeek5">
<div class="panel-heading">
<strong class="sparkLineStatsWeek5t">Graph 5</strong>
<strong class="sparkLineStatsWeek5s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek5"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek5b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-week" class="panel panel-default pannelWeek6">
<div class="panel-heading">
<strong class="sparkLineStatsWeek6t">Graph 6</strong>
<strong class="sparkLineStatsWeek6s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek6"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek6b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-week" class="panel panel-default pannelWeek7">
<div class="panel-heading">
<strong class="sparkLineStatsWeek7t">Graph 7</strong>
<strong class="sparkLineStatsWeek7s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek7"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek7b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="panel-week" class="panel panel-default pannelWeek8">
<div class="panel-heading">
<strong class="sparkLineStatsWeek8t">Graph 8</strong>
<strong class="sparkLineStatsWeek8s pull-right">Avg</strong>
</div>
<div class="panel-body panelInside">
<table class="table">
<tbody>
<tr>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek8"></div></td>
<td style="border-top: 0px solid #ddd;"><div class="sparkLineStatsWeek8b"></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="week_divr" class="sparkLineStats">
</div>
</div>
</div>
@ -491,8 +240,6 @@
</div>
<!-- /.row -->
</div>
<!-- /#page-wrapper -->
@ -502,13 +249,15 @@
<!-- import graph function -->
<script src="{{ url_for('static', filename='js/sentiment_trending.js') }}"></script>
<script>
$("#LoadAll").hide();
$(document).ready(function(){
activePage = $('h1.page-header').attr('data-page');
$("#"+activePage).addClass("active");
$('[data-toggle="tooltip"]').tooltip();
draw_page();
$("#LoadAll").click(function(){ draw_page("True"); });
draw_page("False");
// Reload every 30min
setTimeout(function(){ location.reload(); }, 30*60*1000);
});