1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-23 01:06:17 +00:00

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).

This commit is contained in:
Fabian Schlenz 2016-07-06 13:58:25 +02:00
parent 70ee7566fd
commit 1c92f02422
2 changed files with 24 additions and 1 deletions

View File

@ -64,6 +64,7 @@ class Database {
private void init() { private void init() {
try { try {
System.out.println("Opening database...");
int version; int version;
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'"); ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'");
rs.next(); rs.next();
@ -129,6 +130,12 @@ class Database {
stmt.executeUpdate("INSERT INTO database_versions (version) VALUES (4)"); stmt.executeUpdate("INSERT INTO database_versions (version) VALUES (4)");
version = 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."); System.out.println("Database is ready.");
} catch (SQLException e) { } 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() { public int getMessageCount() {
try { try {
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM messages"); ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM messages");

View File

@ -125,12 +125,14 @@ class DownloadManager {
prog.onMessageDownloadFinished(); prog.onMessageDownloadFinished();
} }
int count_missing = 0;
System.out.println("Checking message database for completeness..."); System.out.println("Checking message database for completeness...");
if (db.getMessageCount() != db.getTopMessageID()) { if (db.getMessageCount() != db.getTopMessageID()) {
if (limit != null) { 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."); System.out.println("You are missing messages in your database. But since you're using '--limit-messages', I won't download these now.");
} else { } else {
LinkedList<Integer> ids = db.getMissingIDs(); LinkedList<Integer> ids = db.getMissingIDs();
count_missing = ids.size();
System.out.println("Downloading " + ids.size() + " messages that are missing in your database."); System.out.println("Downloading " + ids.size() + " messages that are missing in your database.");
prog.onMessageDownloadStart(ids.size()); prog.onMessageDownloadStart(ids.size());
while (ids.size()>0) { while (ids.size()>0) {
@ -149,6 +151,7 @@ class DownloadManager {
prog.onMessageDownloadFinished(); prog.onMessageDownloadFinished();
} }
} }
db.logRun(Math.min(max_database_id + 1, max_message_id), max_message_id, count_missing);
} }
public void downloadMedia() throws RpcErrorException, IOException { public void downloadMedia() throws RpcErrorException, IOException {