Blame | Last modification | View Log | Download
# Written by Edward Keyes# see LICENSE.txt for license informationfrom threading import Eventtry:Trueexcept:True = 1False = 0class Statistics_Response:pass # empty classclass Statistics:def __init__(self, upmeasure, downmeasure, connecter, httpdl,ratelimiter, rerequest_lastfailed, fdatflag):self.upmeasure = upmeasureself.downmeasure = downmeasureself.connecter = connecterself.httpdl = httpdlself.ratelimiter = ratelimiterself.downloader = connecter.downloaderself.picker = connecter.downloader.pickerself.storage = connecter.downloader.storageself.torrentmeasure = connecter.downloader.totalmeasureself.rerequest_lastfailed = rerequest_lastfailedself.fdatflag = fdatflagself.fdatactive = Falseself.piecescomplete = Noneself.placesopen = Noneself.storage_totalpieces = len(self.storage.hashes)def set_dirstats(self, files, piece_length):self.piecescomplete = 0self.placesopen = 0self.filelistupdated = Event()self.filelistupdated.set()frange = xrange(len(files))self.filepieces = [[] for x in frange]self.filepieces2 = [[] for x in frange]self.fileamtdone = [0.0 for x in frange]self.filecomplete = [False for x in frange]self.fileinplace = [False for x in frange]start = 0Lfor i in frange:l = files[i][1]if l == 0:self.fileamtdone[i] = 1.0self.filecomplete[i] = Trueself.fileinplace[i] = Trueelse:fp = self.filepieces[i]fp2 = self.filepieces2[i]for piece in range(int(start/piece_length),int((start+l-1)/piece_length)+1):fp.append(piece)fp2.append(piece)start += ldef update(self):s = Statistics_Response()s.upTotal = self.upmeasure.get_total()s.downTotal = self.downmeasure.get_total()s.last_failed = self.rerequest_lastfailed()s.external_connection_made = self.connecter.external_connection_madeif s.downTotal > 0:s.shareRating = float(s.upTotal)/s.downTotalelif s.upTotal == 0:s.shareRating = 0.0else:s.shareRating = -1.0s.torrentRate = self.torrentmeasure.get_rate()s.torrentTotal = self.torrentmeasure.get_total()s.numSeeds = self.picker.seeds_connecteds.numOldSeeds = self.downloader.num_disconnected_seeds()s.numPeers = len(self.downloader.downloads)-s.numSeedss.numCopies = 0.0for i in self.picker.crosscount:if i==0:s.numCopies+=1else:s.numCopies+=1-float(i)/self.picker.numpiecesbreakif self.picker.done:s.numCopies2 = s.numCopies + 1else:s.numCopies2 = 0.0for i in self.picker.crosscount2:if i==0:s.numCopies2+=1else:s.numCopies2+=1-float(i)/self.picker.numpiecesbreaks.discarded = self.downloader.discardeds.numSeeds += self.httpdl.seedsfounds.numOldSeeds += self.httpdl.seedsfoundif s.numPeers == 0 or self.picker.numpieces == 0:s.percentDone = 0.0else:s.percentDone = 100.0*(float(self.picker.totalcount)/self.picker.numpieces)/s.numPeerss.backgroundallocating = self.storage.bgalloc_actives.storage_totalpieces = len(self.storage.hashes)s.storage_active = len(self.storage.stat_active)s.storage_new = len(self.storage.stat_new)s.storage_dirty = len(self.storage.dirty)numdownloaded = self.storage.stat_numdownloadeds.storage_justdownloaded = numdownloadeds.storage_numcomplete = self.storage.stat_numfound + numdownloadeds.storage_numflunked = self.storage.stat_numflunkeds.storage_isendgame = self.downloader.endgamemodes.peers_kicked = self.downloader.kicked.items()s.peers_banned = self.downloader.banned.items()try:s.upRate = int(self.ratelimiter.upload_rate/1000)assert s.upRate < 5000except:s.upRate = 0s.upSlots = self.ratelimiter.slotsif self.piecescomplete is None: # not a multi-file torrentreturn sif self.fdatflag.isSet():if not self.fdatactive:self.fdatactive = Trueelse:self.fdatactive = Falseif self.piecescomplete != self.picker.numgot:for i in xrange(len(self.filecomplete)):if self.filecomplete[i]:continueoldlist = self.filepieces[i]newlist = [ piecefor piece in oldlistif not self.storage.have[piece] ]if len(newlist) != len(oldlist):self.filepieces[i] = newlistself.fileamtdone[i] = ((len(self.filepieces2[i])-len(newlist))/float(len(self.filepieces2[i])) )if not newlist:self.filecomplete[i] = Trueself.filelistupdated.set()self.piecescomplete = self.picker.numgotif ( self.filelistupdated.isSet()or self.placesopen != len(self.storage.places) ):for i in xrange(len(self.filecomplete)):if not self.filecomplete[i] or self.fileinplace[i]:continuewhile self.filepieces2[i]:piece = self.filepieces2[i][-1]if self.storage.places[piece] != piece:breakdel self.filepieces2[i][-1]if not self.filepieces2[i]:self.fileinplace[i] = Trueself.storage.set_file_readonly(i)self.filelistupdated.set()self.placesopen = len(self.storage.places)s.fileamtdone = self.fileamtdones.filecomplete = self.filecompletes.fileinplace = self.fileinplaces.filelistupdated = self.filelistupdatedreturn s