Correctly handle and display add or remove a term in term-frequency + started sketch of terms-plot tool.

This commit is contained in:
Mokaddem 2016-08-19 16:53:46 +02:00
parent 8d41b109e7
commit 0b6828fb16
4 changed files with 95 additions and 52 deletions

View file

@ -14,6 +14,14 @@ words and their occurency per day.
..note:: Module ZMQ_Something_Q and ZMQ_Something are closely bound, always put ..note:: Module ZMQ_Something_Q and ZMQ_Something are closely bound, always put
the same Subscriber name in both of them. the same Subscriber name in both of them.
zrank for each day
week -> top zrank for each day
Requirements Requirements
------------ ------------

View file

@ -5,6 +5,7 @@ import redis
import ConfigParser import ConfigParser
import json import json
import datetime import datetime
import time
import calendar import calendar
from flask import Flask, render_template, jsonify, request from flask import Flask, render_template, jsonify, request
import flask import flask
@ -577,6 +578,8 @@ def sentiment_analysis_plot_tool_getdata():
def terms_management(): def terms_management():
TrackedTermsSet_Name = "TrackedSetTermSet" TrackedTermsSet_Name = "TrackedSetTermSet"
BlackListTermsSet_Name = "BlackListSetTermSet" BlackListTermsSet_Name = "BlackListSetTermSet"
TrackedTermsDate_Name = "TrackedTermDate"
BlackListTermsDate_Name = "BlackListTermDate"
today = datetime.datetime.now() today = datetime.datetime.now()
today = today.replace(hour=0, minute=0, second=0, microsecond=0) today = today.replace(hour=0, minute=0, second=0, microsecond=0)
@ -586,32 +589,58 @@ def terms_management():
track_list_values = [] track_list_values = []
for tracked_term in r_serv_term.smembers(TrackedTermsSet_Name): for tracked_term in r_serv_term.smembers(TrackedTermsSet_Name):
track_list.append(tracked_term) track_list.append(tracked_term)
track_list_values.append(Term_getValueOverRange(tracked_term, today_timestamp, [1, 7, 31])) value_range = Term_getValueOverRange(tracked_term, today_timestamp, [1, 7, 31])
term_date = r_serv_term.hget(TrackedTermsDate_Name, tracked_term)
term_date = datetime.datetime.utcfromtimestamp(int(term_date)) if term_date is not None else "No date recorded"
value_range.append(term_date)
track_list_values.append(value_range)
black_list = [] black_list = []
for blacked_term in r_serv_term.smembers(BlackListTermsSet_Name): for blacked_term in r_serv_term.smembers(BlackListTermsSet_Name):
black_list.append(blacked_term) term_date = r_serv_term.hget(BlackListTermsDate_Name, blacked_term)
term_date = datetime.datetime.utcfromtimestamp(int(term_date)) if term_date is not None else "No date recorded"
print term_date
black_list.append([blacked_term, term_date])
return render_template("terms_management.html", black_list=black_list, track_list=track_list, track_list_values=track_list_values) return render_template("terms_management.html", black_list=black_list, track_list=track_list, track_list_values=track_list_values)
@app.route("/terms_management_query/") @app.route("/terms_management_query/")
def terms_management_query(): def terms_management_query():
TrackedTermsDate_Name = "TrackedTermDate"
BlackListTermsDate_Name = "BlackListTermDate"
term = request.args.get('term') term = request.args.get('term')
section = request.args.get('section')
today = datetime.datetime.now() today = datetime.datetime.now()
today = today.replace(hour=0, minute=0, second=0, microsecond=0) today = today.replace(hour=0, minute=0, second=0, microsecond=0)
today_timestamp = calendar.timegm(today.timetuple()) today_timestamp = calendar.timegm(today.timetuple())
value_range = Term_getValueOverRange(term, today_timestamp, [1, 7, 31])
print Term_getValueOverRange(term, today_timestamp, [1, 7, 31]) if section == "followTerm":
return jsonify(Term_getValueOverRange(term, today_timestamp, [1, 7, 31])) term_date = r_serv_term.hget(TrackedTermsDate_Name, term)
elif section == "blacklistTerm":
term_date = r_serv_term.hget(BlackListTermsDate_Name, term)
term_date = datetime.datetime.utcfromtimestamp(int(term_date)) if term_date is not None else "No date recorded"
value_range.append(str(term_date))
return jsonify(value_range)
@app.route("/terms_management_action/", methods=['GET']) @app.route("/terms_management_action/", methods=['GET'])
def terms_management_action(): def terms_management_action():
TrackedTermsSet_Name = "TrackedSetTermSet" TrackedTermsSet_Name = "TrackedSetTermSet"
TrackedTermsDate_Name = "TrackedTermDate"
BlackListTermsDate_Name = "BlackListTermDate"
BlackListTermsSet_Name = "BlackListSetTermSet" BlackListTermsSet_Name = "BlackListSetTermSet"
today = datetime.datetime.now()
today = today.replace(microsecond=0)
today_timestamp = calendar.timegm(today.timetuple())
section = request.args.get('section') section = request.args.get('section')
action = request.args.get('action') action = request.args.get('action')
term = request.args.get('term') term = request.args.get('term')
@ -621,11 +650,13 @@ def terms_management_action():
if section == "followTerm": if section == "followTerm":
if action == "add": if action == "add":
r_serv_term.sadd(TrackedTermsSet_Name, term) r_serv_term.sadd(TrackedTermsSet_Name, term)
r_serv_term.hset(TrackedTermsDate_Name, term, today_timestamp)
else: else:
r_serv_term.srem(TrackedTermsSet_Name, term) r_serv_term.srem(TrackedTermsSet_Name, term)
elif section == "blacklistTerm": elif section == "blacklistTerm":
if action == "add": if action == "add":
r_serv_term.sadd(BlackListTermsSet_Name, term) r_serv_term.sadd(BlackListTermsSet_Name, term)
r_serv_term.hset(BlackListTermsDate_Name, term, today_timestamp)
else: else:
r_serv_term.srem(BlackListTermsSet_Name, term) r_serv_term.srem(BlackListTermsSet_Name, term)
else: else:

View file

@ -83,8 +83,8 @@
<table class="table table-striped table-bordered table-hover" id="myTable"> <table class="table table-striped table-bordered table-hover" id="myTable">
<thead> <thead>
<tr> <tr>
<th style="max-width: 800px;">Term name</th> <th style="max-width: 800px;">Term</th>
<th>Date</th> <th>Added date</th>
<th>Day occurence</th> <th>Day occurence</th>
<th>Week occurence</th> <th>Week occurence</th>
<th>Month occurence</th> <th>Month occurence</th>
@ -96,7 +96,7 @@
{% for term in track_list %} {% for term in track_list %}
<tr> <tr>
<td>{{ term }}</td> <td>{{ term }}</td>
<td>{{ i }}</td> <td>{{ track_list_values[i][3] }}</td>
<td>{{ track_list_values[i][0] }}</td> <td>{{ track_list_values[i][0] }}</td>
<td>{{ track_list_values[i][1] }}</td> <td>{{ track_list_values[i][1] }}</td>
<td>{{ track_list_values[i][2] }}</td> <td>{{ track_list_values[i][2] }}</td>
@ -137,8 +137,8 @@
<table class="table table-striped table-bordered table-hover" id="myTable2"> <table class="table table-striped table-bordered table-hover" id="myTable2">
<thead> <thead>
<tr> <tr>
<th style="max-width: 800px;">Term name</th> <th style="max-width: 800px;">Termx</th>
<th>Date</th> <th>Added date</th>
<th>Action</th> <th>Action</th>
</tr> </tr>
</thead> </thead>
@ -146,10 +146,10 @@
{% set i = 0 %} {% set i = 0 %}
{% for term in black_list %} {% for term in black_list %}
<tr> <tr>
<td>{{ term }}</td> <td>{{ black_list[i][0] }}</td>
<td>{{ i }}</td> <td>{{ black_list[i][1] }}</td>
<td><p style="margin: 0px;"> <td><p style="margin: 0px;">
<button class="btn-link btn-interaction" data-toggle="tooltip" data-placement="right" title="Remove this term" data-content="{{ term }}" data-section="blacklistTerm" data-action="delete"><span class="glyphicon glyphicon-trash"></span></button> <button class="btn-link btn-interaction" data-toggle="tooltip" data-placement="right" title="Remove this term" data-content="{{ black_list[i][0] }}" data-section="blacklistTerm" data-action="delete"><span class="glyphicon glyphicon-trash"></span></button>
</p></td> </p></td>
</tr> </tr>
{% set i = i + 1 %} {% set i = i + 1 %}
@ -211,29 +211,32 @@ function perform_operation(){
if(json.section == "followTerm") { if(json.section == "followTerm") {
if(json.action == "add") { if(json.action == "add") {
// query data // query data
$.get("{{ url_for('terms_management_query') }}", { term: json.term }, function(data2, status){ $.get("{{ url_for('terms_management_query') }}", { term: json.term, section: json.section }, function(data2, status){
var action_button = "<button class=\"btn-link btn-interaction\" data-toggle=\"tooltip\" data-placement=\"right\" title=\"Remove this term\" data-content=\"" + json.term + "\" data-section=\"followTerm\" data-action=\"delete\"><span class=\"glyphicon glyphicon-trash\"></span></button>" var action_button = "<button class=\"btn-link btn-interaction\" data-toggle=\"tooltip\" data-placement=\"right\" title=\"Remove this term\" data-content=\"" + json.term + "\" data-section=\"followTerm\" data-action=\"delete\"><span class=\"glyphicon glyphicon-trash\"></span></button>"
table_track.row.add( [ json.term, 'now', data2[0], data2[1], data2[2], action_button ] ).draw( false ); table_track.row.add( [ json.term, data2[3], data2[0], data2[1], data2[2], action_button ] ).draw( false );
perform_binding(); perform_binding();
}); });
} else if (json.action == "delete") { } else if (json.action == "delete") {
// Find indexes of row which have the term in the first column // Find indexes of row which have the term in the first column
var index = table_track.row().eq( 0 ).filter( function (rowIdx) { var index = table_track.rows().eq( 0 ).filter( function (rowIdx) {
return table_track.cell( rowIdx, 0 ).data() == json.term ? true : false; return table_track.cell( rowIdx, 0 ).data() === json.term;
} ); } );
table_track.row().remove(index).draw( false ); table_track.rows(index).remove().draw( false );
} }
} else if(json.section == "blacklistTerm"){ } else if(json.section == "blacklistTerm"){
if(json.action == "add") { if(json.action == "add") {
var action_button = "<button class=\"btn-link btn-interaction\" data-toggle=\"tooltip\" data-placement=\"right\" title=\"Remove this term\" data-content=\"" + json.term + "\" data-section=\"blacklistTerm\" data-action=\"delete\"><span class=\"glyphicon glyphicon-trash\"></span></button>" $.get("{{ url_for('terms_management_query') }}", { term: json.term, section: json.section }, function(data2, status){
table_black.row.add( [ json.term, 'now', action_button ] ).draw( false ); console.log(data2);
perform_binding(); var action_button = "<button class=\"btn-link btn-interaction\" data-toggle=\"tooltip\" data-placement=\"right\" title=\"Remove this term\" data-content=\"" + json.term + "\" data-section=\"blacklistTerm\" data-action=\"delete\"><span class=\"glyphicon glyphicon-trash\"></span></button>"
table_black.row.add( [ json.term, data2[3], action_button ] ).draw( false );
perform_binding();
});
} else if (json.action == "delete") { } else if (json.action == "delete") {
// Find indexes of row which have the term in the first column // Find indexes of row which have the term in the first column
var index = table_black.row().eq( 0 ).filter( function (rowIdx) { var index = table_black.rows().eq( 0 ).filter( function (rowIdx) {
return table_black.cell( rowIdx, 0 ).data() == json.term ? true : false; return table_black.cell( rowIdx, 0 ).data() === json.term;
} ); } );
table_black.row().remove(index).draw( false ); table_black.rows(index).remove().draw( false );
} }
} }

View file

@ -54,7 +54,7 @@
<div id="page-wrapper"> <div id="page-wrapper">
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<h1 class="page-header" data-page="page-sentiment" >Terms plot tool</h1> <h1 class="page-header" data-page="page-termfrequency" >Terms plot tool</h1>
</div> </div>
<!-- /.col-lg-12 --> <!-- /.col-lg-12 -->
</div> </div>
@ -71,34 +71,15 @@
<strong>Select options</strong> <strong>Select options</strong>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<div class="row"> <div aria-disabled="false" class="slider sliderRange sliderBlue ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all" style="margin-bottom: 5px;"></div>
<!-- left column --> <strong>Date:</strong> <input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
<div class="col-lg-9">
<!-- providers charts --> <div class="form-group input-group" style="margin-top: 30px;">
<div class="col-lg-6"> <span class="input-group-addon"><span class="glyphicon glyphicon-stats"></span></span>
<div class="sparkLineStats"> <input id="followTermInput" class="form-control" placeholder="Term to plot" type="text" style="max-width: 400px;">
<ul id="providerList1"> <button class="btn btn-info btn-interaction" style="margin-left: 10px;" data-section="followTerm" data-action="add"> Plot a term</button>
</ul>
</div>
</div>
<div class="col-lg-6">
<div class="sparkLineStats">
<ul id="providerList2">
</ul>
</div>
</div>
</div>
<!-- right column -->
<div class="col-lg-3">
<div aria-disabled="false" class="slider sliderRange sliderBlue ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all" style="margin-bottom: 5px;"></div>
<strong>Date:</strong> <input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
</div>
</div>
<!-- /.row -->
<div class="row">
<button id="plot_btn" class="btn btn-info" style="margin-right: 8px; margin-left: 8px;float: right;">Plot!</button>
<label style="float: right;"><input id="checkbox_stacked" type="checkbox" checked=true> Stacked graph?</label>
</div> </div>
</div> </div>
<!-- /.panel-body --> <!-- /.panel-body -->
</div> </div>
@ -142,9 +123,29 @@
$(document).ready(function(){ $(document).ready(function(){
activePage = $('h1.page-header').attr('data-page'); activePage = $('h1.page-header').attr('data-page');
$("#"+activePage).addClass("active"); $("#"+activePage).addClass("active");
/* Create the slider and button*/
var today = Date.now();
var old_day = today - (12*31*24*60*60)*1000;
$( ".sliderRange" ).slider({
range: true,
min: old_day,
max: today,
values: [ today - (7*24*60*60)*1000, today ],
step: 24*60*60*1000,
slide: function( event, ui ) {
$( "#amount" ).val( new Date(ui.values[ 0 ]).toLocaleDateString() + " - " + new Date(ui.values[ 1 ]).toLocaleDateString() );
}
});
$( "#amount" ).val( new Date($( ".sliderRange" ).slider( "values", 0 )).toLocaleDateString() +
" - " + new Date($( ".sliderRange" ).slider( "values", 1 )).toLocaleDateString() );
$('#plot_btn').click(plotData);
}); });
</script> </script>
<script src="{{ url_for('static', filename='js/sentiment_plot.js') }}"></script>
</body> </body>
</html> </html>