1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

WIP: Added StatsExporter.

This commit is contained in:
Fabian Schlenz 2016-07-09 12:19:36 +02:00
parent 8b361d5547
commit 41f8fc80c5
4 changed files with 210 additions and 0 deletions

View File

@ -18,6 +18,7 @@ package de.fabianonline.telegram_backup;
import de.fabianonline.telegram_backup.TelegramUpdateHandler;
import de.fabianonline.telegram_backup.exporter.HTMLExporter;
import de.fabianonline.telegram_backup.exporter.StatsExporter;
import com.github.badoualy.telegram.api.Kotlogram;
import com.github.badoualy.telegram.api.TelegramApp;
@ -95,6 +96,9 @@ public class CommandLineController {
if (options.export.toLowerCase().equals("html")) {
(new HTMLExporter()).export(user);
System.exit(0);
} else if (options.export.toLowerCase().equals("stats")) {
(new StatsExporter()).export(user);
System.exit(0);
} else {
show_error("Unknown export format.");
}

View File

@ -543,6 +543,41 @@ public class Database {
}
}
public int getMessagesFromUserCount() {
try {
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.getUser().getId());
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public HashMap<String, Integer> getMessageTypesWithCount() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
try {
ResultSet rs = stmt.executeQuery("SELECT message_type, COUNT(id) FROM messages GROUP BY message_type");
while (rs.next()) {
map.put(rs.getString(1), rs.getInt(2));
}
return map;
} catch (Exception e) { throw new RuntimeException(e); }
}
public HashMap<String, Integer> getMessageMediaTypesWithCount() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
try {
ResultSet rs = stmt.executeQuery("SELECT media_type, COUNT(id) FROM messages GROUP BY media_type");
while (rs.next()) {
String s = rs.getString(1);
if (s==null) s="null";
map.put(s, rs.getInt(2));
}
return map;
} catch (Exception e) { throw new RuntimeException(e); }
}
public LinkedList<Chat> getListOfChatsForExport() {
LinkedList<Chat> list = new LinkedList<Chat>();
try {
@ -560,6 +595,7 @@ public class Database {
}
}
public LinkedList<Dialog> getListOfDialogsForExport() {
LinkedList<Dialog> list = new LinkedList<Dialog>();
try {

View File

@ -0,0 +1,85 @@
/* Telegram_Backup
* Copyright (C) 2016 Fabian Schlenz
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
package de.fabianonline.telegram_backup.exporter;
import de.fabianonline.telegram_backup.UserManager;
import de.fabianonline.telegram_backup.Database;
import java.io.File;
import java.io.PrintWriter;
import java.io.BufferedWriter;
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;
import com.github.mustachejava.MustacheFactory;
public class StatsExporter {
public void export(UserManager user) {
try {
Database db = new Database(user, null);
// Create base dir
String base = user.getFileBase() + "export" + File.separatorChar + "stats" + File.separatorChar;
new File(base).mkdirs();
HashMap<String, Object> scope = new HashMap<String, Object>();
// Collect data
LinkedList<Database.Dialog> dialogs = db.getListOfDialogsForExport();
LinkedList<Database.Chat> chats = db.getListOfChatsForExport();
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());
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/stats/index.mustache");
FileWriter w = new FileWriter(base + "index.html");
mustache.execute(w, scope);
w.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Exception above!");
}
}
}

View File

@ -0,0 +1,85 @@
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}}
<html>
<head>
<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();
}
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}}], ['Photo', {{count.messages.media_type.photo}}],
['Audio', {{count.messages.media_type.audio}}], ['Video', {{count.messages.media_type.video}}],
['Geolocation', {{count.messages.media_type.geo}}], ['Document', {{count.messages.media_type.document}}],
['Sticker', {{count.messages.media_type.sticker}}], ['Venue', {{count.messages.media_type.venue}}],
['Contact', {{count.messages.media_type.contact}}]
]);
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>
<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>