from django.db import models from torrents.models import TorrentSnapshot ## -------------------------------------- ## Peer ## -------------------------------------- class Peer(models.Model): UNKNOWN_USERAGENT = "Unknown" ipaddress = models.IPAddressField(null=False) useragent = models.CharField(max_length=128,default=UNKNOWN_USERAGENT,null=False,blank=True) country = models.CharField(max_length=2,null=True,blank=True) last_seen = models.DateTimeField(auto_now_add=True,blank=True) created = models.DateTimeField(auto_now_add=True,blank=True) class Meta: unique_together = ('ipaddress', 'useragent') def __unicode__(self): ret = unicode(self.ipaddress) + u"/" + unicode(self.useragent) return ret def snapshots(self): return PeerSnapshot.objects.filter(peer__exact=self) snapshots.short_description = 'Peer Snapshots' def snapshots_link(self): return "View %d" % (self.id, len(self.snapshots())) snapshots_link.short_description = 'Peer Snapshots' snapshots_link.allow_tags = True ## -------------------------------------- ## PeerSnapshot ## -------------------------------------- class PeerSnapshot(models.Model): peer = models.ForeignKey(Peer) torrent_snapshot = models.ForeignKey(TorrentSnapshot) upload_speed = models.DecimalField(null=True, blank=True, max_digits=12, decimal_places=4) download_speed = models.DecimalField(null=True, blank=True, max_digits=12, decimal_places=4) payload_upload_speed = models.DecimalField(null=True, blank=True, max_digits=12, decimal_places=4) payload_download_speed = models.DecimalField(null=True, blank=True, max_digits=12, decimal_places=4) total_upload = models.PositiveIntegerField(default=0) total_download = models.PositiveIntegerField(default=0) fail_count = models.PositiveIntegerField(default=0) hashfail_count = models.PositiveIntegerField(default=0) progress = models.DecimalField(null=False,blank=False,default="0.0",max_digits=8, decimal_places=7) created = models.DateTimeField(auto_now_add=True,blank=True) class Meta: unique_together = ('peer', 'torrent_snapshot') verbose_name = "Peer Snapshot" def debug(self): ret = unicode(self.peer.ipaddress) + u":\n" ret += u" Torrent: " + self.torrent_snapshot.torrent.name + "\n" ret += u" Created: " + unicode(self.created) + "\n" ret += u" Progress: " + unicode(self.progress) + "\n" ret += u" UploadSpeed: " + unicode(self.upload_speed) + "\n" ret += u" DownloadSpeed: " + unicode(self.download_speed) + "\n" ret += u" PayloadUploadSpeed: " + unicode(self.payload_upload_speed) + "\n" ret += u" PayloadDownloadSpeed: " + unicode(self.payload_download_speed) + "\n" ret += u" TotalUpload: " + unicode(self.total_upload) + "\n" ret += u" TotalDownload: " + unicode(self.total_download) + "\n" ret += u" Fail Count: " + unicode(self.fail_count) + "\n" ret += u" HashFail Count: " + unicode(self.hashfail_count) return ret def __unicode__(self): return u"[" + unicode(self.torrent_snapshot.torrent_id) + u"] " + unicode(self.peer) + u" " + unicode(self.created)