From 2204eb3cd0b02a47636c3863834e4a36033f7749 Mon Sep 17 00:00:00 2001 From: Mariana Marasoiu Date: Fri, 12 Aug 2011 14:59:48 +0300 Subject: [PATCH] ppf: Modify DatabaseWriter and DatabaseCommander to use new SQL shema. --- ppf/db-mysql/DatabaseCommander.py | 87 ++++++++++++++----------- ppf/db-mysql/DatabaseWriter.py | 101 ++++++++++++++---------------- 2 files changed, 97 insertions(+), 91 deletions(-) diff --git a/ppf/db-mysql/DatabaseCommander.py b/ppf/db-mysql/DatabaseCommander.py index 126633d..7312a5a 100644 --- a/ppf/db-mysql/DatabaseCommander.py +++ b/ppf/db-mysql/DatabaseCommander.py @@ -3,8 +3,6 @@ import sys import getopt import MySQLdb -#import julian -#import datetime from DatabaseAccess import DatabaseAccess DEBUG = False @@ -15,22 +13,23 @@ class DatabaseCommander: Wrappers around DatabaseAccess class methods """ - def __init__ (self, dbname): - self.dbname = dbname - self.dba = DatabaseAccess(dbname) + def __init__ (self, dbconf): + self.dbconf = dbconf + self.dba = DatabaseAccess(dbconf) self.dba.connect() - def add_swarm(self, torrent_file, filesize, purpose, source): - self.dba.insert_swarms(torrent_file, filesize, purpose, source); + def add_swarm(self, torrent_filename, data_size, description): + self.dba.insert_swarms(torrent_filename, data_size, description); def add_client_session(self, swarm_id, client_name, system_os, system_os_version, system_ram, system_cpu, public_ip, - public_port, ds_limit, us_limit, timestamp): -# jd = float(julian.stringToJulian(date, time)) + public_port, ds_limit, us_limit, start_time, dht_enabled, + pxe_enabled, streaming_enabled, features, description): client_id = self.dba.select_btclient_id_by_name(client_name) - self.dba.insert_client_sessions(swarm_id, client_id, - system_os, system_os_version, system_ram, system_cpu, - public_ip, public_port, ds_limit, us_limit, timestamp) + self.dba.insert_client_sessions(swarm_id, client_id, system_os, + system_os_version, system_ram, system_cpu, public_ip, + public_port, ds_limit, us_limit, start_time, dht_enabled, + pxe_enabled, streaming_enabled, features, description) def show_swarms(self, swarm_id = -1): self.dba.select_swarms(True, swarm_id) @@ -76,7 +75,7 @@ class DatabaseCommander: def usage(): - print "Usage: python DatabaseCommander.py action target [-i|--id id] [options] database" + print "Usage: python DatabaseCommander.py action target [-i|--id id] [options] dbconf_file" print "action:" print "\t--add" print "\t-a\t\tadd entry to database" @@ -101,7 +100,7 @@ def usage(): print "\t--client name" print "\t-c\t\tspecify client_name" print "\tclient sessions may be listed by specifying swarm_id and, optionally, client name" - print "\tdatabase\t\tSQLite database file" + print "\tdbconf_file\t\tconfiguration file" print "\t--help" print "\t-h\t\t\tprint this help screen" @@ -114,13 +113,12 @@ def read_swarms(dbc, sep = None): line = sys.stdin.readline().strip() if line: message_array = line.split(sep) - + swarm_name = message_array[0].strip() - filesize = int(message_array[1].strip()) - purpose = message_array[2].strip() - source = message_array[3].strip() + data_size = int(message_array[1].strip()) + description = message_array[2].strip() - dbc.add_swarm(swarm_name, filesize, purpose, source) + dbc.add_swarm(swarm_name, data_size, description) else: break @@ -143,15 +141,21 @@ def read_client_sessions(dbc, sep = None): public_port = int(message_array[7].strip()) ds_limit = int(message_array[8].strip()) us_limit = int(message_array[9].strip()) - timestamp = message_array[10].strip()#.split(' ') -# date = timestamp[0] -# time = timestamp[1] + start_time = message_array[10].strip() + dht_enabled = int(message_array[11].strip()) + pxe_enabled = int(message_array[12].strip()) + streaming_enabled = int(message_array[13].strip()) + features = message_array[14].strip() + description = message_array[15].strip() if DEBUG == True: - print "(%d, %s, %s, %s, %d, %d, %s, %d, %d, %d, %s)" %(swarm_id, client_name, system_os, system_os_version, system_ram, system_cpu, public_ip, public_port, ds_limit, us_limit, timestamp) + print "(%d, %s, %s, %s, %d, %d, %s, %d, %d, %d, %s, %d, %d, %d, %s, %s)" %(swarm_id, client_name, system_os, system_os_version, system_ram, system_cpu, public_ip, public_port, ds_limit, us_limit, start_time, dht_enabled, pxe_enabled, streaming_enabled, features, description) dbc.add_client_session(swarm_id, client_name, system_os, - system_os_version, system_ram, system_cpu, public_ip, public_port, ds_limit, us_limit, timestamp) + system_os_version, system_ram, system_cpu, public_ip, + public_port, ds_limit, us_limit, start_time, dht_enabled, + pxe_enabled, streaming_enabled, features, description) + else: break @@ -180,7 +184,7 @@ def main(): swarm = None client = None client_session_list_option = None - database = None + dbconf_file = None sep = None for o, a in opts: @@ -218,11 +222,11 @@ def main(): else: assert False, "unhandled option" - # no database file passed as argument + # no database configuration file passed as argument if len(args) != 1: - print "Error: no database file passed as argument." + print "Error: no database configuration file passed as argument." sys.exit(2) - database = args[0] + dbconf_file = args[0] if action == None: print "Error: no action specified." @@ -260,20 +264,19 @@ def main(): if swarm != None or client != None: print "Error: too many arguments for delete action." - dbc = DatabaseCommander(database) + dbc = DatabaseCommander(dbconf_file) if target == "swarm": if action == "add": print "swarm name (torrent filename without .torrent extension): ", swarm_name = sys.stdin.readline().strip() print "file size (in bytes): ", - file_size = sys.stdin.readline().strip() - print "purpose (what is this warm used for): ", - purpose = sys.stdin.readline().strip() - print "source (URL for downloading .torrent file or \"local\"): ", - source = sys.stdin.readline().strip() + data_size = sys.stdin.readline().strip() + print "description (details about this swarm): ", + description = sys.stdin.readline().strip() + + dbc.add_swarm(swarm_name, data_size, description) - dbc.add_swarm(swarm_name, file_size, purpose, source) if action == "delete": dbc.delete_swarm(id) if action == "list": @@ -305,9 +308,21 @@ def main(): us_limit = sys.stdin.readline().strip() print "start time (YYYY-MM-DD HH:MM:SS): ", start_time = sys.stdin.readline().strip() + print "dht enabled (0-FALSE, 1-TRUE): ", + dht_enabled = sys.stdin.readline().strip() + print "pxe enabled (0-FALSE, 1-TRUE): ", + pxe_enabled = sys.stdin.readline().strip() + print "streaming enabled (0-FALSE, 1-TRUE): ", + streaming_enabled = sys.stdin.readline().strip() + print "features: ", + features = sys.stdin.readline().strip() + print "description: ", + description = sys.stdin.readline().strip() dbc.add_client_session(swarm_id, client_name, system_os, - os_version, ram, cpu, ip, port, ds_limit, us_limit, start_time) + os_version, ram, cpu, ip, port, ds_limit, us_limit, + start_time, dht_enabled, pxe_enabled, streaming_enabled, + features, description) if action == "delete": if client_session_list_option == "id": dbc.delete_client_session_by_id(id) diff --git a/ppf/db-mysql/DatabaseWriter.py b/ppf/db-mysql/DatabaseWriter.py index 31b8af5..81c28d9 100644 --- a/ppf/db-mysql/DatabaseWriter.py +++ b/ppf/db-mysql/DatabaseWriter.py @@ -2,55 +2,49 @@ import sys import getopt -#import julian import datetime import re # regular expression support from DatabaseAccess import DatabaseAccess class DatabaseWriter: - def __init__ (self, dbname): - self.dbname = dbname - self.dba = DatabaseAccess(dbname) + def __init__ (self, dbconf_file): + self.dbconf = dbconf_file + self.dba = DatabaseAccess(dbconf_file) self.dba.connect() - def add_status_message(self, cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta_seconds): -# timestamp = float(julian.stringToJulian(date, time)); - self.dba.insert_status_messages(cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta_seconds) + def add_status_message(self, client_session_id, timestamp, num_peers, num_dht_peers, download_speed, upload_speed, download_size, upload_size, eta_seconds): + self.dba.insert_status_messages(client_session_id, timestamp, num_peers, num_dht_peers, download_speed, upload_speed, download_size, upload_size, eta_seconds) - def add_status_message_datetime(self, cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta_seconds): -# timestamp = float(julian.datetimeToJulian(dt)); - self.dba.insert_status_messages(cs_id, timestamp, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta_seconds) + def add_status_message_datetime(self, client_session_id, timestamp, num_peers, num_dht_peers, download_speed, upload_speed, download_size, upload_size, eta_seconds): + self.dba.insert_status_messages(client_session_id, timestamp, num_peers, num_dht_peers, download_speed, upload_speed, download_size, upload_size, eta_seconds) - def add_verbose_message(self, cs_id, timestamp, direction, peer_ip, peer_port, message_type, index, begin, length, listen_port): -# timestamp = float(julian.stringToJulian(date, time)); - self.dba.insert_verbose_messages(cs_id, timestamp, direction, peer_ip, peer_port, message_type, index, begin, length, listen_port) + def add_verbose_message(self, cs_id, timestamp, transfer_direction_id, peer_ip, peer_port, message_type_id, index, begin, length, listen_port): + self.dba.insert_verbose_messages(cs_id, timestamp, transfer_direction_id, peer_ip, peer_port, message_type_id, index, begin, length, listen_port) - def add_verbose_message_datetime(self, cs_id, timestamp, direction, peer_ip, peer_port, message_type, index, begin, length, listen_port): -# timestamp = float(julian.datetimeToJulian(dt)); - - self.dba.insert_verbose_messages(cs_id, timestamp, direction, peer_ip, peer_port, message_type, index, begin, length, listen_port) + def add_verbose_message_datetime(self, cs_id, timestamp, transfer_direction_id, peer_ip, peer_port, message_type_id, index, begin, length, listen_port): + self.dba.insert_verbose_messages(cs_id, timestamp, transfer_direction_id, peer_ip, peer_port, message_type_id, index, begin, length, listen_port) def show_status_messages(self, cs_id = -1): - self.dba.select_status_messages(True, cs_id) + self.dba.select_status_messages_by_client_session_id(True, cs_id) def select_status_messages(self, cs_id = -1): - return self.dba.select_status_messages(False, cs_id) + return self.dba.select_status_messages_by_client_session_id(False, cs_id) def show_verbose_messages(self, cs_id = -1): - self.dba.select_verbose_messages(True, cs_id) + self.dba.select_verbose_messages_by_client_session_id(True, cs_id) def select_verbose_messages(self, cs_id = -1): - return self.dba.select_verbose_messages(False, cs_id) + return self.dba.select_verbose_messages_by_client_session_id(False, cs_id) def delete_status_messages(self, cs_id = -1): - self.dba.delete_status_messages(cs_id) + self.dba.delete_status_messages_by_client_session_id(cs_id) def delete_verbose_messages(self, cs_id = -1): - self.dba.delete_verbose_messages(cs_id) + self.dba.delete_verbose_messages_by_client_session_id(cs_id) def usage(): - print "Usage: python DatabaseWriter.py action target [-i|--id id] database" + print "Usage: python DatabaseWriter.py action target [-i|--id id] dbconf_file" print "action:" print "\t--add" print "\t-a\t\tadd entry to database" @@ -67,7 +61,7 @@ def usage(): print "id:" print "\t--id" print "\t-i\t\tclient_session_id" - print "\tdatabase\t\tSQLite database file" + print "\tdbconf_file\t\tconfiguration file" print "\t--help" print "\t-h\t\t\tprint this help screen" @@ -79,18 +73,17 @@ def read_status_messages(dbw, sep = None): line = sys.stdin.readline().strip() if line: message_array = line.split(sep) - cs_id = int(message_array[0].strip()) timestamp = message_array[1].strip() -# date = timestamp[0] -# time = timestamp[1] num_peers = int(message_array[2].strip()) - dht = int(message_array[3].strip()) + num_dht_peers = int(message_array[3].strip()) download_speed = int(message_array[4].strip()) upload_speed = int(message_array[5].strip()) download_size = int(message_array[6].strip()) upload_size = int(message_array[7].strip()) eta_string_array = re.split('[dhms]', message_array[8].strip()) + + # dhms format to seconds eta_string_array.remove('') eta_array = [] for i in range(0, len(eta_string_array)): @@ -98,7 +91,8 @@ def read_status_messages(dbw, sep = None): for i in range(len(eta_string_array), 4): eta_array.insert(0, 0) eta = ((eta_array[0]*24 + eta_array[1])*60 + eta_array[2])*60 + eta_array[3] - dbw.add_status_message(cs_id, timestamp, num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta) + + dbw.add_status_message(cs_id, timestamp, num_peers, num_dht_peers, download_speed, upload_speed, download_size, upload_size, eta) else: break @@ -113,19 +107,17 @@ def read_verbose_messages(dbw, sep = None): cs_id = int(message_array[0].strip()) timestamp = message_array[1].strip() -# date = timestamp[0] -# time = timestamp[1] - direction = int(message_array[2].strip()) + direction_id = int(message_array[2].strip()) peer_ip = message_array[3].strip() peer_port = int(message_array[4].strip()) - message_type = int(message_array[5].strip()) + message_type_id = int(message_array[5].strip()) index = int(message_array[6].strip()) begin = int(message_array[7].strip()) length = int(message_array[8].strip()) listen_port = int(message_array[9].strip()) - dbw.add_verbose_message(cs_id, timestamp, direction, peer_ip, peer_port, - message_type, index, begin, length, listen_port) + dbw.add_verbose_message(cs_id, timestamp, direction_id, peer_ip, peer_port, + message_type_id, index, begin, length, listen_port) else: break @@ -180,11 +172,11 @@ def main(): else: assert False, "unhandled option" - # no database file passed as argument + # no database configuration file passed as argument if len(args) != 1: - print "Error: no database file passed as argument." + print "Error: no database configuration file passed as argument." sys.exit(2) - database = args[0] + dbconf_file = args[0] if action == None: print "Error: no action specified." @@ -206,21 +198,20 @@ def main(): print "Error:", action, "action doesn't use a separator argument." sys.exit(2) - dbw = DatabaseWriter(database) - + dbw = DatabaseWriter(dbconf_file) if target == "status": if action == "add": print "client session id: ", cs_id = int(sys.stdin.readline().strip()) - print "timestamp (YYYY-MM-DD HH:MM:SS.ss): ", + print "timestamp (YYYY-MM-DD HH:MM:SS): ", timestamp = sys.stdin.readline().strip() # date = timestamp[0] # time = timestamp[1] print "number of peers: ", num_peers = int(sys.stdin.readline().strip()) - print "dht: ", - dht = int(sys.stdin.readline().strip()) + print "number of dht peers: ", + num_dht_peers = int(sys.stdin.readline().strip()) print "current download speed (KB/s): ", download_speed = int(sys.stdin.readline().strip()) print "current upload speed (KB/s): ", @@ -231,6 +222,8 @@ def main(): upload_size = int(sys.stdin.readline().strip()) print "eta (XdYhZmWs; e.g. 1d12h34m12s): ", eta_string_array = re.split('[dhms]', sys.stdin.readline().strip()) + + # dhms format to seconds eta_string_array.remove('') eta_array = [] for i in range(0, len(eta_string_array)): @@ -238,8 +231,8 @@ def main(): for i in range(len(eta_string_array), 4): eta_array.insert(0, 0) eta = ((eta_array[0]*24 + eta_array[1])*60 + eta_array[2])*60 + eta_array[3] - print eta - dbw.add_status_message(cs_id, timestamp, num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta) + + dbw.add_status_message(cs_id, timestamp, num_peers, num_dht_peers, download_speed, upload_speed, download_size, upload_size, eta) if action == "delete": dbw.delete_status_messages(id) @@ -254,18 +247,16 @@ def main(): if action == "add": print "client session id: ", cs_id = int(sys.stdin.readline().strip()) - print "timestamp (YYYY-MM-DD HH:MM:SS.ss): ", + print "timestamp (YYYY-MM-DD HH:MM:SS): ", timestamp = sys.stdin.readline().strip() -# date = timestamp[0] -# time = timestamp[1] - print "direction (receive=0, send=1): ", - direction = int(sys.stdin.readline().strip()) + print "direction (receive=1, send=2): ", + direction_id = int(sys.stdin.readline().strip()) print "peer IP address (X.Y.Z.T): ", peer_ip = sys.stdin.readline().strip() print "peer port: ", peer_port = int(sys.stdin.readline().strip()) - print "message_type (0, 1, 2, 3, 4, 5, 6): ", - message_type = int(sys.stdin.readline().strip()) + print "message_type_id (1,2,3,4,5,6,7,8,9,10): ", + message_type_id = int(sys.stdin.readline().strip()) print "index: ", index = int(sys.stdin.readline().strip()) print "begin: ", @@ -275,8 +266,8 @@ def main(): print "listen_port: ", listen_port = int(sys.stdin.readline().strip()) - dbw.add_verbose_message(cs_id, timestamp, direction, peer_ip, peer_port, - message_type, index, begin, length, listen_port) + dbw.add_verbose_message(cs_id, timestamp, direction_id, peer_ip, peer_port, + message_type_id, index, begin, length, listen_port) if action == "delete": dbw.delete_verbose_messages(id) -- 2.20.1