From 1c92f02422ec232425ac678d1450dd4500303c27 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 6 Jul 2016 13:58:25 +0200 Subject: [PATCH] Added a new database to track runs of this tool in order to be able to monitor them later on (Nagios, Icinga and so on). --- .../telegram_backup/Database.java | 22 ++++++++++++++++++- .../telegram_backup/DownloadManager.java | 3 +++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/fabianonline/telegram_backup/Database.java b/src/main/java/de/fabianonline/telegram_backup/Database.java index 351b5bc..274e96b 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Database.java +++ b/src/main/java/de/fabianonline/telegram_backup/Database.java @@ -64,6 +64,7 @@ class Database { private void init() { try { + System.out.println("Opening database..."); int version; ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'"); rs.next(); @@ -122,13 +123,19 @@ class Database { } if (version==3) { System.out.println(" Updating to version 4..."); - stmt.executeUpdate("CREATE TABLE messages_new (id INTEGER PRIMARY KEY ASC, dialog_id INTEGER, to_id INTEGER, from_id INTEGER, from_type TEXT, text TEXT, time INTEGER, has_media BOOLEAN, sticker TEXT, data BLOB,type TEXT);"); + stmt.executeUpdate("CREATE TABLE messages_new (id INTEGER PRIMARY KEY ASC, dialog_id INTEGER, to_id INTEGER, from_id INTEGER, from_type TEXT, text TEXT, time INTEGER, has_media BOOLEAN, sticker TEXT, data BLOB, type TEXT);"); stmt.executeUpdate("INSERT INTO messages_new SELECT * FROM messages"); stmt.executeUpdate("DROP TABLE messages"); stmt.executeUpdate("ALTER TABLE messages_new RENAME TO 'messages'"); stmt.executeUpdate("INSERT INTO database_versions (version) VALUES (4)"); version = 4; } + if (version==4) { + System.out.println(" Updating to version 5..."); + stmt.executeUpdate("CREATE TABLE runs (id INTEGER PRIMARY KEY ASC, time INTEGER, start_id INTEGER, end_id INTEGER, count_missing INTEGER)"); + stmt.executeUpdate("INSERT INTO database_versions (version) VALUES (5)"); + version = 5; + } System.out.println("Database is ready."); } catch (SQLException e) { @@ -147,6 +154,19 @@ class Database { } } + public void logRun(int start_id, int end_id, int count) { + try { + PreparedStatement ps = conn.prepareStatement("INSERT INTO runs "+ + "(time, start_id, end_id, count_missing) "+ + "VALUES "+ + "(DateTime('now'), ?, ?, ? )"); + ps.setInt(1, start_id); + ps.setInt(2, end_id); + ps.setInt(3, count); + ps.execute(); + } catch (SQLException e) {} + } + public int getMessageCount() { try { ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM messages"); diff --git a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java index 71d75db..6b75aa1 100644 --- a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java @@ -125,12 +125,14 @@ class DownloadManager { prog.onMessageDownloadFinished(); } + int count_missing = 0; System.out.println("Checking message database for completeness..."); if (db.getMessageCount() != db.getTopMessageID()) { if (limit != null) { System.out.println("You are missing messages in your database. But since you're using '--limit-messages', I won't download these now."); } else { LinkedList ids = db.getMissingIDs(); + count_missing = ids.size(); System.out.println("Downloading " + ids.size() + " messages that are missing in your database."); prog.onMessageDownloadStart(ids.size()); while (ids.size()>0) { @@ -149,6 +151,7 @@ class DownloadManager { prog.onMessageDownloadFinished(); } } + db.logRun(Math.min(max_database_id + 1, max_message_id), max_message_id, count_missing); } public void downloadMedia() throws RpcErrorException, IOException {