WIP: Changes to the exporter. Stats are included in HTML output; Heatmap added.

This commit is contained in:
Fabian Schlenz 2016-07-25 18:21:52 +02:00
parent 627cef0ae2
commit 75566ef53a
4 changed files with 149 additions and 4 deletions

View File

@ -439,6 +439,20 @@ public class Database {
return map;
} catch (Exception e) { throw new RuntimeException(e); }
}
public int[][] getMessageTimesMatrix(Integer dialog_id, Integer chat_id) {
int result[][] = new int[24][7];
try {
ResultSet rs = stmt.executeQuery("SELECT STRFTIME('%H', time, 'unixepoch') AS hour, " +
"STRFTIME('%w', time, 'unixepoch') as DAY, " +
"COUNT(id) FROM messages GROUP BY hour, day " +
"ORDER BY hour, day");
while (rs.next()) {
result[rs.getInt(1)][rs.getInt(2) == 0 ? 6 : rs.getInt(2)-1] = rs.getInt(3);
}
return result;
} catch (Exception e) { throw new RuntimeException(e); }
}
public LinkedList<Chat> getListOfChatsForExport() {

View File

@ -26,6 +26,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.HashMap;
import java.util.Map;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
@ -37,7 +38,7 @@ public class HTMLExporter {
Database db = new Database(user, null);
// Create base dir
String base = user.getFileBase() + "export" + File.separatorChar + "html" + File.separatorChar;
String base = user.getFileBase() + "files" + File.separatorChar;
new File(base).mkdirs();
new File(base + "dialogs").mkdirs();
@ -50,6 +51,34 @@ public class HTMLExporter {
scope.put("dialogs", dialogs);
scope.put("chats", chats);
// Collect stats data
scope.put("count.chats", chats.size());
scope.put("count.dialogs", dialogs.size());
int count_messages_chats = 0;
int count_messages_dialogs = 0;
for (Database.Chat c : chats) count_messages_chats += c.count;
for (Database.Dialog d : dialogs) count_messages_dialogs += d.count;
scope.put("count.messages", count_messages_chats + count_messages_dialogs);
scope.put("count.messages.chats", count_messages_chats);
scope.put("count.messages.dialogs", count_messages_dialogs);
scope.put("count.messages.from_me", db.getMessagesFromUserCount());
scope.put("heatmap_data", intArrayToString(db.getMessageTimesMatrix(null, null)));
HashMap<String, Integer> types;
types = db.getMessageTypesWithCount();
for (Map.Entry<String, Integer> entry : types.entrySet()) {
scope.put("count.messages.type." + entry.getKey(), entry.getValue());
}
types = db.getMessageMediaTypesWithCount();
for (Map.Entry<String, Integer> entry : types.entrySet()) {
scope.put("count.messages.media_type." + entry.getKey(), entry.getValue());
}
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile("templates/html/index.mustache");
FileWriter w = new FileWriter(base + "index.html");
@ -84,4 +113,20 @@ public class HTMLExporter {
throw new RuntimeException("Exception above!");
}
}
private String intArrayToString(int[][] data) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int x=0; x<data.length; x++) {
if (x>0) sb.append(", ");
sb.append("[");
for (int y=0; y<data[x].length; y++) {
if (y>0) sb.append(", ");
sb.append(data[x][y]);
}
sb.append("]");
}
sb.append("]");
return sb.toString();
}
}

View File

@ -26,9 +26,9 @@
<span class="sender">{{user_first_name}}</span>
<span class="time">{{formatted_time}}</span>
{{#text}}<span class="text">{{text}}</span>{{/text}}
{{#media_sticker}}<span class="sticker"><img src="../../../../stickers/{{media_file}}" /></span>{{/media_sticker}}
{{#media_photo}}<span class="photo"><img src="../../../files/{{media_file}}" /></span>{{/media_photo}}
{{#media_document}}<span class="document"><a href="../../../files/{{media_file}}">{{media_file}}</a></span>{{/media_document}}
{{#media_sticker}}<span class="sticker"><img src="../../../stickers/{{media_file}}" /></span>{{/media_sticker}}
{{#media_photo}}<span class="photo"><img src="../{{media_file}}" /></span>{{/media_photo}}
{{#media_document}}<span class="document"><a href="../{{media_file}}">{{media_file}}</a></span>{{/media_document}}
</li>
{{/messages}}
</ul>

View File

@ -2,6 +2,61 @@
<html>
<head>
<title>Telegram Backup for {{user.getUserString}}</title>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
drawChartChatTypes();
drawChartMessageTypes();
drawChartMediaTypes();
}
function drawChartChatTypes() {
var data = google.visualization.arrayToDataTable([
['Type', 'Count'],
['Dialog', {{count.dialogs}}],
['Chat', {{count.chats}}]
]);
var options = {
title: 'Chat types',
legend: 'none',
pieSliceText: 'label',
};
var chart = new google.visualization.PieChart(document.getElementById('chart_chat_types'));
chart.draw(data, options);
}
function drawChartMediaTypes() {
var data = google.visualization.arrayToDataTable([
['Type', 'Count'],
['No media', {{count.messages.media_type.null}}+0], ['Photo', {{count.messages.media_type.photo}}+0],
['Audio', {{count.messages.media_type.audio}}+0], ['Video', {{count.messages.media_type.video}}+0],
['Geolocation', {{count.messages.media_type.geo}}+0], ['Document', {{count.messages.media_type.document}}+0],
['Sticker', {{count.messages.media_type.sticker}}+0], ['Venue', {{count.messages.media_type.venue}}+0],
['Contact', {{count.messages.media_type.contact}}+0]
]);
var options = { title: 'Media types', legend: 'none', pieSliceText: 'label' };
var chart = new google.visualization.PieChart(document.getElementById('chart_media_types'));
chart.draw(data, options);
}
function drawChartMessageTypes() {
var data = google.visualization.arrayToDataTable([
['Type', 'Count'],
['Normal messages', {{count.messages.type.message}}],
['Empty messages', {{count.messages.type.empty_message}}],
['Service messages', {{count.messages.type.service_message}}]
]);
var options = { title: 'Message types', legend: 'none', pieSliceText: 'label' };
var chart = new google.visualization.PieChart(document.getElementById('chart_message_types'));
chart.draw(data, options);
}
</script>
</head>
<body>
@ -32,5 +87,36 @@
</li>
{{/chats}}
</ul>
<!--
count.chats: {{count.chats}}
count.dialogs: {{count.dialogs}}
count.messages: {{count.messages}}
count.messages.chats: {{count.messages.chats}}
count.messages.dialogs: {{count.messages.dialogs}}
count.messages.from_me: {{count.messages.from_me}}
count.messages.type.message: {{count.messages.type.message}}
count.messages.type.empty_message: {{count.messages.type.empty_message}}
count.messages.type.service_message: {{count.messages.type.service_message}}
count.messages.media_type.null: {{count.messages.media_type.null}}
count.messages.media_type.photo:{{count.messages.media_type.photo}}
count.messages.media_type.audio:{{count.messages.media_type.audio}}
count.messages.media_type.video:{{count.messages.media_type.video}}
count.messages.media_type.geo:{{count.messages.media_type.geo}}
count.messages.media_type.document:{{count.messages.media_type.document}}
count.messages.media_type.sticker:{{count.messages.media_type.sticker}}
count.messages.media_type.venue:{{count.messages.media_type.venue}}
count.messages.media_type.contact:{{count.messages.media_type.contact}}
{{heatmap_data}}
-->
<div id="chart_chat_types" style="width: 900px; height: 500px;"></div>
<div id="chart_message_types" style="width: 900px; height: 500px;"></div>
<div id="chart_media_types" style="width: 900px; height: 500px;"></div>
</body>
</html>