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
|
|
|
|
import calendar
|
|
|
|
from Date import Date
|
|
|
|
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
|
|
|
|
|
|
|
import Paste
|
|
|
|
|
|
|
|
# ============ 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
|
|
|
|
r_serv_sentiment = Flask_config.r_serv_sentiment
|
2017-04-19 11:02:03 +02:00
|
|
|
|
|
|
|
sentiments = Blueprint('sentiments', __name__, template_folder='templates')
|
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
# ============ FUNCTIONS ============
|
|
|
|
|
|
|
|
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
|
|
|
@sentiments.route("/sentiment_analysis_trending/")
|
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 sentiment_analysis_trending():
|
|
|
|
return render_template("sentiment_analysis_trending.html")
|
|
|
|
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
@sentiments.route("/sentiment_analysis_getplotdata/", 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 sentiment_analysis_getplotdata():
|
|
|
|
# Get the top providers based on number of pastes
|
|
|
|
oneHour = 60*60
|
|
|
|
sevenDays = oneHour*24*7
|
|
|
|
dateStart = datetime.datetime.now()
|
|
|
|
dateStart = dateStart.replace(minute=0, second=0, microsecond=0)
|
|
|
|
dateStart_timestamp = calendar.timegm(dateStart.timetuple())
|
|
|
|
|
|
|
|
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')
|
2018-04-17 16:06:32 +02:00
|
|
|
|
2018-05-04 13:53:29 +02:00
|
|
|
return jsonify(list(range_providers))
|
2016-12-09 08:46:37 +01:00
|
|
|
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
|
2018-04-17 16:06:32 +02:00
|
|
|
|
|
|
|
|
2016-12-09 08:46:37 +01:00
|
|
|
# if still empty, takes from all providers
|
|
|
|
if range_providers == []:
|
2018-04-17 16:06:32 +02:00
|
|
|
print('today provider empty')
|
2016-12-09 08:46:37 +01:00
|
|
|
range_providers = r_serv_charts.smembers('all_provider_set')
|
2018-04-17 16:06:32 +02:00
|
|
|
|
2018-05-04 13:53:29 +02:00
|
|
|
return jsonify(list(range_providers))
|
2016-12-09 08:46:37 +01:00
|
|
|
|
|
|
|
elif provider is not None:
|
|
|
|
to_return = {}
|
|
|
|
|
|
|
|
cur_provider_name = provider + '_'
|
|
|
|
list_date = {}
|
|
|
|
for cur_timestamp in range(int(dateStart_timestamp), int(dateStart_timestamp)-sevenDays-oneHour, -oneHour):
|
|
|
|
cur_set_name = cur_provider_name + str(cur_timestamp)
|
|
|
|
|
|
|
|
list_value = []
|
|
|
|
for cur_id in r_serv_sentiment.smembers(cur_set_name):
|
2018-05-04 13:53:29 +02:00
|
|
|
cur_value = (r_serv_sentiment.get(cur_id))
|
2016-12-09 08:46:37 +01:00
|
|
|
list_value.append(cur_value)
|
|
|
|
list_date[cur_timestamp] = list_value
|
|
|
|
to_return[provider] = list_date
|
|
|
|
|
|
|
|
return jsonify(to_return)
|
|
|
|
return "Bad request"
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
@sentiments.route("/sentiment_analysis_plot_tool/")
|
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 sentiment_analysis_plot_tool():
|
|
|
|
return render_template("sentiment_analysis_plot_tool.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
@sentiments.route("/sentiment_analysis_plot_tool_getdata/", 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 sentiment_analysis_plot_tool_getdata():
|
|
|
|
getProviders = request.args.get('getProviders')
|
|
|
|
|
|
|
|
if getProviders == 'True':
|
|
|
|
providers = []
|
|
|
|
for cur_provider in r_serv_charts.smembers('all_provider_set'):
|
2018-05-04 13:53:29 +02:00
|
|
|
providers.append(cur_provider)
|
2016-12-09 08:46:37 +01:00
|
|
|
return jsonify(providers)
|
|
|
|
|
|
|
|
else:
|
|
|
|
query = request.args.get('query')
|
|
|
|
query = query.split(',')
|
|
|
|
Qdate = request.args.get('Qdate')
|
|
|
|
|
2017-09-27 10:46:20 +02:00
|
|
|
date1 = (Qdate.split('-')[0]).split('/')
|
|
|
|
date1 = datetime.date(int(date1[2]), int(date1[0]), int(date1[1]))
|
2016-12-09 08:46:37 +01:00
|
|
|
|
2017-09-27 10:46:20 +02:00
|
|
|
date2 = (Qdate.split('-')[1]).split('/')
|
|
|
|
date2 = datetime.date(int(date2[2]), int(date2[0]), int(date2[1]))
|
2016-12-09 08:46:37 +01:00
|
|
|
|
|
|
|
timestamp1 = calendar.timegm(date1.timetuple())
|
|
|
|
timestamp2 = calendar.timegm(date2.timetuple())
|
|
|
|
|
|
|
|
oneHour = 60*60
|
|
|
|
oneDay = oneHour*24
|
|
|
|
|
|
|
|
to_return = {}
|
|
|
|
for cur_provider in query:
|
|
|
|
list_date = {}
|
|
|
|
cur_provider_name = cur_provider + '_'
|
|
|
|
for cur_timestamp in range(int(timestamp1), int(timestamp2)+oneDay, oneHour):
|
|
|
|
cur_set_name = cur_provider_name + str(cur_timestamp)
|
|
|
|
|
|
|
|
list_value = []
|
|
|
|
for cur_id in r_serv_sentiment.smembers(cur_set_name):
|
2018-05-04 13:53:29 +02:00
|
|
|
cur_value = (r_serv_sentiment.get(cur_id))
|
2016-12-09 08:46:37 +01:00
|
|
|
list_value.append(cur_value)
|
|
|
|
list_date[cur_timestamp] = list_value
|
|
|
|
to_return[cur_provider] = list_date
|
|
|
|
|
|
|
|
return jsonify(to_return)
|
|
|
|
|
2017-04-19 11:02:03 +02:00
|
|
|
# ========= REGISTRATION =========
|
2018-09-20 10:38:19 +02:00
|
|
|
app.register_blueprint(sentiments, url_prefix=baseUrl)
|