From 4efb832e8d7b5d99b9642a464a67fa9c562d0b08 Mon Sep 17 00:00:00 2001 From: Marius Sandu-Popa Date: Fri, 6 Nov 2009 17:55:28 +0200 Subject: [PATCH] Viewer created --- viewer/DbObjects.py | 141 ++++++++++++++++++++++++++++++++++++++++++++ viewer/run_sample | 3 + viewer/run_sample2 | 3 + viewer/test.py | 39 ++++++++++++ viewer/test2.py | 70 ++++++++++++++++++++++ 5 files changed, 256 insertions(+) create mode 100644 viewer/DbObjects.py create mode 100755 viewer/run_sample create mode 100755 viewer/run_sample2 create mode 100644 viewer/test.py create mode 100644 viewer/test2.py diff --git a/viewer/DbObjects.py b/viewer/DbObjects.py new file mode 100644 index 0000000..711e8ec --- /dev/null +++ b/viewer/DbObjects.py @@ -0,0 +1,141 @@ +from DatabaseAccess import DatabaseAccess + +class Swarm: + def __init__(self, dbname, id): + dba=DatabaseAccess(dbname); + dba.connect(); + curs = dba.select_swarms(False, id); + row = curs.fetchone(); + self.id = id; + self.torrent = row[1]; + self.filesize = row[2]; + self.purpose = row[3]; + self.source = row[4]; + + 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); + dba.connect(); + if name: + curs = dba.select_btclient_by_name(name, False); + else: + curs = dba.select_btclients(False, id); + row = curs.fetchone(); + self.id = id; + self.name = row[1]; + self.language = row[2]; + self.dht = row[3]; + self.streaming = row[4]; + + def __str__(self): + return '[%s %s, %s, %s, %s]' \ + %(self.id, self.name, self.language, self.dht, self.streaming); + +class ClientSession: + def __init__(self, dbname=None, id=None, row=None): + if not row: + dba=DatabaseAccess(dbname); + dba.connect(); + curs = dba.select_client_sessions_by_id(False, id); + row = curs.fetchone(); + + self.id = row[0]; + self.swarm_id = row[1]; + self.client_id = row[2]; + #self.swarm = Swarm(curs[1]); + #self.btclient = BtClient(curs[2]); + self.system_os = row[3]; + self.system_os_version = row[4]; + self.system_ram = row[5]; + self.system_cpu = row[6]; + self.public_ip = row[7]; + self.public_port = row[8]; + self.ds_limit = row[9]; + self.us_limit = row[10]; + self.start_time = row[11]; + + def __str__(self): + return '[%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s]' \ + %(self.id, self.swarm_id, self.client_id, self.system_os, \ + self.system_os_version, self.system_ram, self.system_cpu, \ + 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); + for row in curs: + self.css.append(ClientSession(None, None, row)); + + def getIter(self): + return iter(self.css); + + +class StatusMessage: + def __init__(self, row): + self.client_session = row[0] + self.timestamp = row[1]; + self.peers_num = row[2]; + self.dht = row[3]; + self.download_speed = row[4]; + self.upload_speed = row[5]; + self.download_size = row[6]; + self.upload_size = row[7]; + self.eta = row[8]; + + def __str__(self): + return '[%s, %s, %s, %s, %s, %s, %s, %s, %s]' \ + %(self.client_session, self.timestamp, self.peers_num, self.dht, \ + 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.append(StatusMessage(client_session, row)) + self.stms.append(StatusMessage(row)); + + def getIter(self): + return iter(self.stms); + +class VerboseMessage: + def __init__(self, row): + self.client_session = row[0]; + self.timestamp = row[1]; + self.peer_ip = row[2]; + self.peer_port = row[3]; + self.message_type = row[4]; + self.index = row[5]; + self.begin = row[6]; + self.length = row[7]; + self.listen_port = row[8]; + + def __str__(self): + return '[%s, %s, %s, %s, %s, %s, %s, %s, %s]' \ + %(self.client_session, self.timestamp, self.peer_ip, self.peer_port, \ + 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); + for row in curs: + #self.vbms.append(VerboseMessage(client_session, row)) + self.vbms.append(VerboseMessage(row)); + + def getIter(self): + return iter(self.vbms); diff --git a/viewer/run_sample b/viewer/run_sample new file mode 100755 index 0000000..ae7385a --- /dev/null +++ b/viewer/run_sample @@ -0,0 +1,3 @@ +#!/bin/bash + +PYTHONPATH=../auto/db/ python test.py ../auto/db/client-limitation-experiment.db diff --git a/viewer/run_sample2 b/viewer/run_sample2 new file mode 100755 index 0000000..392f995 --- /dev/null +++ b/viewer/run_sample2 @@ -0,0 +1,3 @@ +#!/bin/bash + +PYTHONPATH=../auto/db/ python test2.py ../auto/db/client-limitation-experiment.db diff --git a/viewer/test.py b/viewer/test.py new file mode 100644 index 0000000..620a366 --- /dev/null +++ b/viewer/test.py @@ -0,0 +1,39 @@ +import matplotlib.pyplot as plt +import sys +import getopt +import sqlite3 +import julian +from datetime import datetime +from datetime import timedelta +from DatabaseWriter import DatabaseWriter + + +def main(): + dbw=DatabaseWriter(sys.argv[1]) + curs = dbw.select_status_messages(2) + x=[] + y1=[] + y2=[] + + for row in curs: + diff = datetime.today() - julian.julianToDatetime(row[1]) + x.append(diff.seconds) + y1.append(row[4]) + y2.append(row[5]) + weeks, days = divmod(diff.days, 7) + min, sec = divmod(diff.seconds, 60) + hours, min = divmod(min, 60) + #print hours, min, sec + plt.plot(x, y1, 'b', x, y2, 'r') + plt.grid(linestyle='-') + plt.show() + #plt.savefig("test.png", dpi=3000) + +if __name__ == "__main__": + sys.exit(main()) + +#plt.plot([1,2,3,4], [1,4,9,16], 'b', [1,2,3,4], [1,2,3,14], 'r') +#plt.axis([0, 6, 0, 20]) +#plt.grid(linestyle='-') +#plt.savefig("test.svg", dpi=100) +#plt.show() diff --git a/viewer/test2.py b/viewer/test2.py new file mode 100644 index 0000000..2643ae4 --- /dev/null +++ b/viewer/test2.py @@ -0,0 +1,70 @@ +import matplotlib.pyplot as plt +import sys +import getopt +import julian +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) + + +# 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: + break + #print(cs) + stmc = StatusMessageCollection(dbname, cs.id); + stmit = stmc.getIter(); + x_local = [] + y_local = [] + 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) + y_local.append(stm.download_speed) + + plt.subplot(2, 1, i) + plt.plot(x_local, y_local) + plt.grid(True) + + +# plt.subplot(2,1,1) +# plt.plot([1,2,3], [2,3,4]) +# plt.grid(linestyle='dashed') +# plt.subplot(2,1,2) +# plt.plot([4,5,6], [2,7,8]) +# #print xy_global +# plt.grid(linestyle='dashed') + plt.show() + +if __name__ == "__main__": + sys.exit(main()) \ No newline at end of file -- 2.20.1