chg: [chat viewer] heatmap messages, add year selector

This commit is contained in:
terrtia 2024-12-20 13:52:15 +01:00
parent 9a388dc9cb
commit d21ace8f66
No known key found for this signature in database
GPG key ID: 1E1B1F50D84613D0
3 changed files with 29 additions and 13 deletions

View file

@ -762,7 +762,7 @@ def api_get_chats_selector():
selector.append({'id': chat.get_global_id(), 'name': f'{chat.get_chat_instance()}: {chat.get_label()}'})
return selector
def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, page=-1, messages=True):
def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, page=-1, messages=True, heatmap=False):
chat = Chats.Chat(chat_id, chat_instance_uuid)
if not chat.exists():
return {"status": "error", "reason": "Unknown chat"}, 404
@ -778,6 +778,8 @@ def api_get_chat(chat_id, chat_instance_uuid, translation_target=None, nb=-1, pa
if messages:
meta['messages'], meta['pagination'], meta['tags_messages'] = chat.get_messages(translation_target=translation_target, nb=nb, page=page)
meta['messages'] = get_chat_object_messages_meta(meta['messages'])
if heatmap:
meta['years'] = chat.get_message_years()
return meta, 200
def api_get_nb_message_by_week(chat_type, chat_instance_uuid, chat_id):
@ -805,7 +807,7 @@ def api_get_nb_year_messages(chat_type, chat_instance_uuid, chat_id, year):
year = datetime.now().year
nb_max, nb = chat.get_nb_year_messages(year)
nb = [[date, value] for date, value in nb.items()]
return {'max': nb_max, 'nb': nb}, 200
return {'max': nb_max, 'nb': nb, 'year': year}, 200
def api_get_chat_participants(chat_type, chat_subtype, chat_id):

View file

@ -93,7 +93,7 @@ def chats_explorer_chat():
target = None
nb_messages = request.args.get('nb')
page = request.args.get('page')
chat = chats_viewer.api_get_chat(chat_id, instance_uuid, translation_target=target, nb=nb_messages, page=page)
chat = chats_viewer.api_get_chat(chat_id, instance_uuid, translation_target=target, nb=nb_messages, page=page, heatmap=True)
if chat[1] != 200:
return create_json_response(chat[0], chat[1])
else:

View file

@ -114,7 +114,18 @@
{% endif %}
<h5>Messages by year:</h5>
<div id="heatmapyear" style="width: 100%;height: 300px;"></div>
<div>
<div class="row">
<div class="col-12 col-lg-11">
<div id="heatmapyear" style="width: 100%;height: 300px;"></div>
</div>
<div class="col-12 col-lg-1">
{% for year in chat['years'] %}
<div><button class="btn btn-info mt-1" onclick="update_heatmap_year({{ year }})">{{ year }}</button></div>
{% endfor %}
</div>
</div>
</div>
{% with translate_url=url_for('chats_explorer.chats_explorer_chat', subtype=chat['subtype']), obj_id=chat['id'], pagination=chat['pagination'] %}
{% include 'chats_explorer/block_translation.html' %}
@ -248,8 +259,7 @@ optionheatmap = {
calendar: [
{
orient: 'horizontal',
//range: new Date().getFullYear(),
range: '2024',
range: new Date().getFullYear(),
},
],
series: [
@ -263,14 +273,18 @@ optionheatmap = {
};
heatyearChart.setOption(optionheatmap);
$.getJSON("{{ url_for('chats_explorer.chats_explorer_messages_stats_year') }}?type=chat&subtype={{ chat['subtype'] }}&id={{ chat['id'] }}")
.done(function(data) {
optionheatmap['visualMap']['max'] = data['max']
optionheatmap['series'][0]['data'] = data['nb']
heatyearChart.setOption(optionheatmap)
update_heatmap_year(null);
function update_heatmap_year(year) {
$.getJSON("{{ url_for('chats_explorer.chats_explorer_messages_stats_year') }}?type=chat&subtype={{ chat['subtype'] }}&id={{ chat['id'] }}&year=" + year)
.done(function(data) {
optionheatmap['visualMap']['max'] = data['max']
optionheatmap['calendar'][0]['range'] = data['year']
optionheatmap['series'][0]['data'] = data['nb']
heatyearChart.setOption(optionheatmap)
}
);
}
);
}
</script>