This commit is contained in:
gauthiier
2017-07-27 10:09:33 +02:00
parent 064a05b806
commit 3b01ec68c6
6 changed files with 370 additions and 1 deletions
+53 -1
View File
@@ -1,6 +1,7 @@
from flask import render_template
from flask import render_template, request, jsonify
from www import app
from www import archives
import search.archive
from datetime import datetime
@app.route('/')
@@ -46,6 +47,9 @@ def get_list(list):
@app.route('/<list>/<sublist>')
def get_sublist(list, sublist):
print(list)
print(sublist)
sublist = sublist.replace(' ', '_')
if list in archives.archives_data and sublist in archives.archives_data[list]:
return render_template("threads.html", sublist_name=sublist, threads=archives.archives_data[list][sublist]['threads'])
@@ -81,6 +85,54 @@ def get_follow_ups(list, sublist, index, follow_ups):
else:
'nope nope'
@app.route('/search')
def searh():
if len(request.args) < 1:
k = archives.archives_data.keys()
return render_template("search.html", archives=k)
k_arg = request.args.get('keyword')
l_arg = request.args.get('list')
sl_arg = request.args.get('sublist')
if k_arg is None or k_arg.strip() == '':
return "no keyword..."
if l_arg is None:
return "no list..."
if not (l_arg == "all") and not (l_arg in archives.archives_data):
return "list '" + l_arg + "' does not exist"
if sl_arg is not None:
if not sl_arg in archives.archives_data[l]:
return "sublist '" + sl_arg + "' does not exist in list '" + l_arg + "'"
lists = []
if l_arg == "all":
for k in archives.archives_data.keys():
lists.append(k)
else:
lists.append(l_arg)
################################
##
## need to chache all the below
##
################################
results = []
for l in lists:
a = search.archive.Archive()
a.load(l)
results.append(a.search(k_arg))
return jsonify(result=results)
+150
View File
@@ -0,0 +1,150 @@
$(document).ready(function(){
$('#search').on('submit', function(e) {
e.preventDefault();
args = $(this).serialize();
$.get('/search?'+args, function(data) {
console.log(data);
$('#graph').empty();
$('#results').empty();
$.each(data.result, function(i, item) {
search_result_archive(item);
});
graph(data);
});
});
});
function search_result_archive(a) {
$('<div/>', {
id: a.archive,
class: "archive",
}).appendTo('#results');
$('#' + a.archive).append("<h3>" + a.archive + "</h3>");
$.each(a.results, function(i, r) {
$('<ul/>', {
id: r.thread + "-" + a.archive,
text: r.thread.replace('_', ' ')
}).appendTo('#' + a.archive);
let hits = "<ul>";
$.each(r.hits, function(j, h){
let hit = '<li><a href="' + h.url+ '">' + h.subject + '</a> -- <i>' + h.author_name + '</i></li>';
hits += hit;
});
hits += "</ul>";
$('#' + r.thread + "-" + a.archive).append(hits);
});
}
var min_month = new Date(2000, 0);
var max_month = new Date();
function diff_months(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth();
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
function format(date) {
var month_names = [
"Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct",
"Nov", "Dec"
];
return month_names[date.getMonth()] + ' ' + date.getFullYear();
//return date.getMonth() + ' - ' + date.getFullYear();
}
function graph(data) {
var d = diff_months(min_month, max_month);
var vec = new Array();
for(let ar of data.result) {
let ar_vec = new Array(d + 1).fill(0);
ar_vec[0] = ar.archive;
for(let r of ar.results) {
let date = new Date(Date.parse(r.thread.replace("_", " 1, "))); // this may blow...
let index = diff_months(min_month, date);
ar_vec[index + 1] = r.nbr_hits;
}
vec.push(ar_vec);
}
// var x_axis = new Array(d + 1);
// x_axis[0] = 'x';
// for (let i = 1; i < d+1; i++) {
// let d = new Date(min_month.getFullYear(), min_month.getMonth());
// d.setMonth(d.getMonth() + (i - 1));
// x_axis[i] = format(d);
// }
// vec.push(x_axis);
var x_axis = new Array(d);
for (let i = 0; i < d; i++) {
let d = new Date(min_month.getFullYear(), min_month.getMonth());
d.setMonth(d.getMonth() + i);
x_axis[i] = format(d);
}
console.log(vec);
var chart = c3.generate({
bindto: '#graph',
data: {
columns: vec,
type: 'bar'
},
axis: {
x: {
type: 'category',
categories: x_axis,
tick: {
culling: {
max: 15
},
multiline:false
}
}
},
bar: {
width: {
ratio: 0.9
}
}
});
}
+23
View File
@@ -0,0 +1,23 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='lib/c3.min.css') }}">
<script type=text/javascript src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="{{ url_for('static',filename='lib/d3.min.js') }}" charset="utf-8"></script>
<script type="text/javascript" src="{{ url_for('static',filename='lib/c3.min.js') }}"></script>
<script type=text/javascript src="{{ url_for('static',filename='search.js') }}"></script>
</head>
<body>
<form action="/search" method="get" id="search">
<label>keyword: </label><input type="search" name="keyword">
<select form="search" name="list">
<option value="all">all</option>
{% for a in archives %}
<option value="{{ a }}">{{ a }}</option>
{% endfor %}
</select>
<input type="submit" value="search" id="submit">
</form>
<div id="graph"></div>
<div id="results"></div>
</body>
</html>