From 14b96a6bc7f9372db8b6c73a65c708c156485740 Mon Sep 17 00:00:00 2001 From: Marius Sandu-Popa Date: Fri, 13 Nov 2009 11:17:25 +0200 Subject: [PATCH] viewer version 1.0 --- viewer/DbObjects.py | 32 +++++++++++++++----------- viewer/test2.py | 56 +++++++++++++++------------------------------ 2 files changed, 38 insertions(+), 50 deletions(-) diff --git a/viewer/DbObjects.py b/viewer/DbObjects.py index 711e8ec..bb287ea 100644 --- a/viewer/DbObjects.py +++ b/viewer/DbObjects.py @@ -1,12 +1,15 @@ from DatabaseAccess import DatabaseAccess +import sqlite3 class Swarm: - def __init__(self, dbname, id): + def __init__(self, dbname, id, row=None): dba=DatabaseAccess(dbname); dba.connect(); curs = dba.select_swarms(False, id); - row = curs.fetchone(); - self.id = id; + row = curs.fetchone(); + dba.disconnect(); + + self.id = row[0]; self.torrent = row[1]; self.filesize = row[2]; self.purpose = row[3]; @@ -15,7 +18,7 @@ class Swarm: def __str__(self): return '[%s, %s, %s, %s, %s]' \ %(self.id, self.torrent, self.filesize, self.purpose, self.source); - + class BtClient: def __init__(self, dbname, id, name=None): dba=DatabaseAccess(dbname); @@ -25,6 +28,8 @@ class BtClient: else: curs = dba.select_btclients(False, id); row = curs.fetchone(); + dba.disconnect(); + self.id = id; self.name = row[1]; self.language = row[2]; @@ -42,6 +47,7 @@ class ClientSession: dba.connect(); curs = dba.select_client_sessions_by_id(False, id); row = curs.fetchone(); + dba.disconnect(); self.id = row[0]; self.swarm_id = row[1]; @@ -65,14 +71,14 @@ class ClientSession: self.public_ip, self.public_port, self.ds_limit, self.us_limit, self.start_time); class ClientSessionCollection: - css = []; - def __init__(self, dbname, swarm_id, client_id=None): dba=DatabaseAccess(dbname); dba.connect(); curs = dba.select_client_sessions_by_swarm(False, swarm_id, client_id); + self.css = []; for row in curs: self.css.append(ClientSession(None, None, row)); + dba.disconnect(); def getIter(self): return iter(self.css); @@ -96,16 +102,16 @@ class StatusMessage: self.download_speed, self.upload_speed, self.download_size, self.upload_size, self.eta); class StatusMessageCollection: - stms = []; - def __init__(self, dbname, cs_id, restrictions=None): dba=DatabaseAccess(dbname); dba.connect(); curs = dba.select_status_messages(False, cs_id, restrictions); - for row in curs: + self.stms = [] + for row in curs.fetchall(): #self.stms.append(StatusMessage(client_session, row)) self.stms.append(StatusMessage(row)); - + dba.disconnect(); + def getIter(self): return iter(self.stms); @@ -127,15 +133,15 @@ class VerboseMessage: self.message_type, self.index, self.begin, sself.length, self.listen_port); class VerboseMessageCollection: - vbms = []; - def __init__(self, dbname, cs_id, restrictions=None): dba=DatabaseAccess(dbname); dba.connect(); curs = dba.select_verbose_messages(False, cs_id, restrictions); + self.vbms = []; for row in curs: #self.vbms.append(VerboseMessage(client_session, row)) self.vbms.append(VerboseMessage(row)); - + dba.disconnect(); + def getIter(self): return iter(self.vbms); diff --git a/viewer/test2.py b/viewer/test2.py index 2643ae4..9a805c1 100644 --- a/viewer/test2.py +++ b/viewer/test2.py @@ -6,47 +6,29 @@ from DbObjects import * def main(): dbname = sys.argv[1]; - sw = Swarm(dbname, 1); - #print sw - -# cs = ClientSession(dbname, 8); -# x=[]; -# y=[]; -# stmc = StatusMessageCollection(dbname, cs.id); -# stmit = stmc.getIter(); -# while True: -# try: -# stm = next(stmit) -# except StopIteration: -# break - #print(stm) -# difftime = julian.julianToDatetime(stm.timestamp) - julian.julianToDatetime(cs.start_time) -# x.append(difftime.seconds) -# y.append(stm.download_speed) + csc = ClientSessionCollection(dbname, 1); + #cscit = csc.getIter(); - -# plt.plot(x, y, 'm'); -# plt.grid(linestyle='dashed'); -# plt.show(); - - csc = ClientSessionCollection(dbname, sw.id); - cscit = csc.getIter(); - - for i in range(1,3): - try: - cs = next(cscit) - except StopIteration: + i=1 + for cs in csc.css: + if i == 3: break - #print(cs) + + #try: + #cs = next(cscit) + #except StopIteration: + #break + ##print(cs) stmc = StatusMessageCollection(dbname, cs.id); - stmit = stmc.getIter(); + #stmit = stmc.getIter(); x_local = [] y_local = [] - while True: - try: - stm = next(stmit) - except StopIteration: - break + for stm in stmc.stms: + #while True: + #try: + #stm = next(stmit) + #except StopIteration: + #break #print(stm) difftime = julian.julianToDatetime(stm.timestamp) - julian.julianToDatetime(cs.start_time) x_local.append(difftime.seconds) @@ -55,7 +37,7 @@ def main(): plt.subplot(2, 1, i) plt.plot(x_local, y_local) plt.grid(True) - + i = i+1 # plt.subplot(2,1,1) # plt.plot([1,2,3], [2,3,4]) -- 2.20.1