From d9e907f6e3bb719eae8d06da631f5249967a77a7 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Sun, 1 Nov 2009 09:31:28 +0200 Subject: [PATCH] added client_session_id confirmation when running StatusParser.py; updated DatabaseAccess.py, DatabaseCommander.py, DatabaseWriter.py accordingly --- auto/db/DatabaseAccess.py | 72 ++++++++++++++++++++------- auto/db/DatabaseCommander.py | 26 ++++++++-- auto/db/DatabaseWriter.py | 10 +++- log-parser/libtorrent/StatusParser.py | 53 ++++++++++++++++++-- 4 files changed, 132 insertions(+), 29 deletions(-) diff --git a/auto/db/DatabaseAccess.py b/auto/db/DatabaseAccess.py index 81b7a50..565a78c 100644 --- a/auto/db/DatabaseAccess.py +++ b/auto/db/DatabaseAccess.py @@ -57,14 +57,18 @@ class DatabaseAccess: def insert_swarms(self, torrent_file, filesize, purpose, source): self.insert_swarms_row([torrent_file, filesize, purpose, source]) - def select_swarms(self, swarm_id = -1): + def select_swarms(self, show = True, swarm_id = -1): try: if swarm_id == -1: self.cursor.execute("select * from swarms") else: self.cursor.execute("select * from swarms where id='%s'" %swarm_id) - for row in self.cursor: - print row + + if show == True: + for row in self.cursor: + print row + else: + return self.cursor except sqlite3.Error, e: print("[swarms]An error ocurred: ", e.args[0]) @@ -88,11 +92,30 @@ class DatabaseAccess: def insert_btclients(self, client_name, language, dht, streaming): btclients([client_name, language, dht, streaming]) - def select_btclient(self, client_name): + def select_btclients(self, show = True, id = -1): + print "show = ", show + print "client_id = ", id + try: + if id == -1: + self.cursor.execute("""select * from btclients""") + else: + self.cursor.execute("""select * from btclients where id='%s'""" %id) + if show == True: + for row in self.cursor: + print row + else: + return self.cursor + except sqlite3.Error, e: + print ("[btclients]An error ocurred: ", e.args[0]) + + def select_btclient_by_name(self, showclient_name, show = True): try: self.cursor.execute("""select * from btclients where name='%s'""" %client_name) - for row in self.cursor: - print row + if show == True: + for row in self.cursor: + print row + else: + return self.cursor except sqlite3.Error, e: print ("[btclients]An error ocurred: ", e.args[0]) @@ -114,33 +137,38 @@ class DatabaseAccess: def insert_client_sessions(self, swarm_id, client_id, system_os, system_os_version, system_ram, system_cpu, public_ip, public_port, ds_limit, us_limit, start_time): self.insert_client_sessions_row([swarm_id, client_id, system_os, system_os_version, system_ram, system_cpu, public_ip, public_port, ds_limit, us_limit, start_time]); - def select_client_sessions_by_id(self, cs_id = -1): + def select_client_sessions_by_id(self, show = True, cs_id = -1): try: if cs_id == -1: self.cursor.execute("""select * from client_sessions""") else: self.cursor.execute("""select * from client_sessions where id='%s'""" %cs_id) - for row in self.cursor: - print row + if show == True: + for row in self.cursor: + print row + else: + return self.cursor except sqlite3.Error, e: print ("[client_sessions]An error ocurred: ", e.args[0]) - def select_client_sessions_by_swarm(self, swarm_id = -1, client_id = None): + def select_client_sessions_by_swarm(self, show = True, swarm_id = -1, client_id = None): try: if client_id == None: if swarm_id == -1: self.cursor.execute("""select * from client_sessions""") else: self.cursor.execute("""select * from client_sessions where swarm_id=?""", (swarm_id, )) - for row in self.cursor: - print row else: if swarm_id == -1: self.cursor.execute("""select * from client_sessions where client_id=?""", (client_id, )) else: self.cursor.execute("""select * from client_sessions where swarm_id=? and client_id=?""", (swarm_id, client_id)) + + if show == True: for row in self.cursor: print row + else: + return self.cursor except sqlite3.Error, e: print ("[client_sessions]An error ocurred: ", e.args[0]) @@ -182,14 +210,17 @@ class DatabaseAccess: def insert_status_messages(self, cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta): self.insert_status_messages_row([cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta]) - def select_status_messages(self, cs_id = -1): + def select_status_messages(self, show = True, cs_id = -1): try: if cs_id == -1: self.cursor.execute("select * from status_messages") else: self.cursor.execute("select * from status_messages where cs_id=?", (cs_id, )) - for row in self.cursor: - print row + if show == True: + for row in self.cursor: + print row + else: + return self.cursor except sqlite3.Error, e: print("[status_messages]An error ocurred: ", e.args[0]) @@ -215,14 +246,17 @@ class DatabaseAccess: def insert_verbose_messages(self, cs_id, timestamp, peer_ip, peer_port, message_type, _index, begin, length, listen_port): self.insert_verbose_messages_row([cs_id, timestamp, peer_ip, peer_port, message_type, _index, begin, length, listen_port]) - def select_verbose_messages(self, cs_id = -1): + def select_verbose_messages(self, show = True, cs_id = -1): try: if cs_id == -1: self.cursor.execute("select * from verbose_messages") else: self.cursor.execute("select * from verbose_messages where cs_id=?", (cs_id, )) - for row in self.cursor: - print row + if show == True: + for row in self.cursor: + print row + else: + return self.cursor except sqlite3.Error, e: print("[status_messages]An error ocurred: ", e.args[0]) @@ -278,7 +312,7 @@ def main(): dba.get_status() - dba.select_btclient("Tribler") + dba.select_btclient_by_name("Tribler") dba.disconnect() diff --git a/auto/db/DatabaseCommander.py b/auto/db/DatabaseCommander.py index 411d39a..bfb02a1 100644 --- a/auto/db/DatabaseCommander.py +++ b/auto/db/DatabaseCommander.py @@ -29,16 +29,34 @@ class DatabaseCommander: public_ip, public_port, ds_limit, us_limit, start_time) def show_swarms(self, swarm_id = -1): - self.dba.select_swarms(swarm_id) + self.dba.select_swarms(True, swarm_id) + + def select_swarms(self, swarm_id = -1): + return self.dba.select_swarms(False, swarm_id) + + def show_btclients(self, client_id = -1): + self.dba.select_btclients(True, client_id) + + def select_btclients(self, client_id = -1): + return self.dba.select_btclients(False, client_id) def show_client_sessions_by_id(self, client_id = -1): - self.dba.select_client_sessions_by_id(client_id); + self.dba.select_client_sessions_by_id(True, client_id); + + def select_client_sessions_by_id(self, client_id = -1): + return self.dba.select_client_sessions_by_id(False, client_id); def show_client_sessions_by_swarm(self, swarm_id, client_name = None): client_id = None if client_name != None: client_id = self.dba.select_btclient_id_by_name(client_name) - self.dba.select_client_sessions_by_swarm(swarm_id, client_id) + self.dba.select_client_sessions_by_swarm(True, swarm_id, client_id) + + def select_client_sessions_by_swarm(self, swarm_id, client_name = None): + client_id = None + if client_name != None: + client_id = self.dba.select_btclient_id_by_name(client_name) + self.dba.select_client_sessions_by_swarm(False, swarm_id, client_id) def delete_swarm(self, swarm_id = -1): self.dba.delete_swarms(swarm_id) @@ -54,7 +72,7 @@ class DatabaseCommander: def usage(): - print "Usage: python DatabaseTorrentSessionHandler.py action target [-i|--id id] [options] database" + print "Usage: python DatabaseCommander.py action target [-i|--id id] [options] database" print "action:" print "\t--add" print "\t-a\t\tadd entry to database" diff --git a/auto/db/DatabaseWriter.py b/auto/db/DatabaseWriter.py index 162d32e..2a37e61 100644 --- a/auto/db/DatabaseWriter.py +++ b/auto/db/DatabaseWriter.py @@ -24,10 +24,16 @@ class DatabaseWriter: self.dba.insert_verbose_messages(cs_id, timestamp, peer_ip, peer_port, message_type, index, begin, length, listen_port) def show_status_messages(self, cs_id = -1): - self.dba.select_status_messages(cs_id) + self.dba.select_status_messages(True, cs_id) + + def select_status_messages(self, cs_id = -1): + self.dba.select_status_messages(False, cs_id) def show_verbose_messages(self, cs_id = -1): - self.dba.select_verbose_messages(cs_id) + self.dba.select_verbose_messages(True, cs_id) + + def select_verbose_messages(self, cs_id = -1): + self.dba.select_verbose_messages(False, cs_id) def delete_status_messages(self, cs_id = -1): self.dba.delete_status_messages(cs_id) diff --git a/log-parser/libtorrent/StatusParser.py b/log-parser/libtorrent/StatusParser.py index 6c30e09..9259498 100644 --- a/log-parser/libtorrent/StatusParser.py +++ b/log-parser/libtorrent/StatusParser.py @@ -4,6 +4,7 @@ import sys import getopt import re from DatabaseWriter import DatabaseWriter +from DatabaseCommander import DatabaseCommander DEBUG = False @@ -97,9 +98,7 @@ def libtorrent_parse_status_line(line): return (num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta) -def libtorrent_parse_status_file(database, client_session_id, filename): - dbw = DatabaseWriter(database) - +def libtorrent_parse_status_file(dbw, client_session_id, filename): try: fin = open(filename, "r") while 1: @@ -158,7 +157,53 @@ def main(): sys.exit(2) database = args[0] - libtorrent_parse_status_file(database, client_session_id, filename) + dbc = DatabaseCommander(database) + + # check for client_session_id, swarm_id, btclient_id + cursor = dbc.select_client_sessions_by_id(client_session_id) + if cursor == None: + print "Error: no client session id (%d) in database." % client_session_id + sys.exit(2) + for session_row in cursor: + pass + + swarm_id = session_row[1] + btclient_id = session_row[2] + + cursor = dbc.select_swarms(swarm_id) + if cursor == None: + print "Error: no swarm id (%d) in database." % swarm_id + sys.exit(2) + for swarm_row in cursor: + pass + + cursor = dbc.select_btclients(btclient_id) + if cursor == None: + print "Error: no client id (%d) in database." % btclient_id + sys.exit(2) + for btclient_row in cursor: + pass + + print "Client session row is: " + print session_row + print "Swarm row is: " + print swarm_row + print "Client row is: " + print btclient_row + + print "\nContinue parsing on file %s? (y/n) " % filename, + try: + ans = sys.stdin.readline().strip() + if ans != "y": + sys.exit(0) + except IOError: + print "Error reading standard input." + sys.exit(2) + + # parse status file + dbw = DatabaseWriter(database) + libtorrent_parse_status_file(dbw, client_session_id, filename) + if __name__ == "__main__": sys.exit(main()) -- 2.20.1