From 75566ef53a46d187c66eac692d13a4d2c8abdda2 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Mon, 25 Jul 2016 18:21:52 +0200 Subject: [PATCH] WIP: Changes to the exporter. Stats are included in HTML output; Heatmap added. --- .../telegram_backup/Database.java | 14 +++ .../exporter/HTMLExporter.java | 47 +++++++++- .../resources/templates/html/chat.mustache | 6 +- .../resources/templates/html/index.mustache | 86 +++++++++++++++++++ 4 files changed, 149 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/fabianonline/telegram_backup/Database.java b/src/main/java/de/fabianonline/telegram_backup/Database.java index bd17c7d..7d7d609 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Database.java +++ b/src/main/java/de/fabianonline/telegram_backup/Database.java @@ -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 getListOfChatsForExport() { diff --git a/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java b/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java index 1239bd7..411fb46 100644 --- a/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java +++ b/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java @@ -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 types; + types = db.getMessageTypesWithCount(); + for (Map.Entry entry : types.entrySet()) { + scope.put("count.messages.type." + entry.getKey(), entry.getValue()); + } + + types = db.getMessageMediaTypesWithCount(); + for (Map.Entry 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; x0) sb.append(", "); + sb.append("["); + for (int y=0; y0) sb.append(", "); + sb.append(data[x][y]); + } + sb.append("]"); + } + sb.append("]"); + return sb.toString(); + } } diff --git a/src/main/resources/templates/html/chat.mustache b/src/main/resources/templates/html/chat.mustache index 8bb9dec..ecc004b 100644 --- a/src/main/resources/templates/html/chat.mustache +++ b/src/main/resources/templates/html/chat.mustache @@ -26,9 +26,9 @@ {{user_first_name}} {{formatted_time}} {{#text}}{{text}}{{/text}} - {{#media_sticker}}{{/media_sticker}} - {{#media_photo}}{{/media_photo}} - {{#media_document}}{{media_file}}{{/media_document}} + {{#media_sticker}}{{/media_sticker}} + {{#media_photo}}{{/media_photo}} + {{#media_document}}{{media_file}}{{/media_document}} {{/messages}} diff --git a/src/main/resources/templates/html/index.mustache b/src/main/resources/templates/html/index.mustache index 7c22174..eb25c77 100644 --- a/src/main/resources/templates/html/index.mustache +++ b/src/main/resources/templates/html/index.mustache @@ -2,6 +2,61 @@ Telegram Backup for {{user.getUserString}} + + + @@ -32,5 +87,36 @@ {{/chats}} + + + + + +
+
+