3 from twisted.application import service
5 class History(service.Service):
6 """Keep track of recent operations, for a status display."""
9 MAX_DOWNLOAD_STATUSES = 10
10 MAX_UPLOAD_STATUSES = 10
11 MAX_MAPUPDATE_STATUSES = 20
12 MAX_PUBLISH_STATUSES = 20
13 MAX_RETRIEVE_STATUSES = 20
15 def __init__(self, stats_provider=None):
16 self.stats_provider = stats_provider
18 self.all_downloads_statuses = weakref.WeakKeyDictionary()
19 self.recent_download_statuses = []
20 self.all_upload_statuses = weakref.WeakKeyDictionary()
21 self.recent_upload_statuses = []
23 self.all_mapupdate_status = weakref.WeakKeyDictionary()
24 self.recent_mapupdate_status = []
25 self.all_publish_status = weakref.WeakKeyDictionary()
26 self.recent_publish_status = []
27 self.all_retrieve_status = weakref.WeakKeyDictionary()
28 self.recent_retrieve_status = []
31 def add_download(self, download_status):
32 self.all_downloads_statuses[download_status] = None
33 self.recent_download_statuses.append(download_status)
34 while len(self.recent_download_statuses) > self.MAX_DOWNLOAD_STATUSES:
35 self.recent_download_statuses.pop(0)
37 def list_all_download_statuses(self):
38 for ds in self.all_downloads_statuses:
41 def add_upload(self, upload_status):
42 self.all_upload_statuses[upload_status] = None
43 self.recent_upload_statuses.append(upload_status)
44 while len(self.recent_upload_statuses) > self.MAX_UPLOAD_STATUSES:
45 self.recent_upload_statuses.pop(0)
47 def list_all_upload_statuses(self):
48 for us in self.all_upload_statuses:
53 def notify_mapupdate(self, p):
54 self.all_mapupdate_status[p] = None
55 self.recent_mapupdate_status.append(p)
56 while len(self.recent_mapupdate_status) > self.MAX_MAPUPDATE_STATUSES:
57 self.recent_mapupdate_status.pop(0)
59 def notify_publish(self, p, size):
60 self.all_publish_status[p] = None
61 self.recent_publish_status.append(p)
62 if self.stats_provider:
63 self.stats_provider.count('mutable.files_published', 1)
64 # We must be told bytes_published as an argument, since the
65 # publish_status does not yet know how much data it will be asked
66 # to send. When we move to MDMF we'll need to find a better way
68 self.stats_provider.count('mutable.bytes_published', size)
69 while len(self.recent_publish_status) > self.MAX_PUBLISH_STATUSES:
70 self.recent_publish_status.pop(0)
72 def notify_retrieve(self, r):
73 self.all_retrieve_status[r] = None
74 self.recent_retrieve_status.append(r)
75 if self.stats_provider:
76 self.stats_provider.count('mutable.files_retrieved', 1)
77 self.stats_provider.count('mutable.bytes_retrieved', r.get_size())
78 while len(self.recent_retrieve_status) > self.MAX_RETRIEVE_STATUSES:
79 self.recent_retrieve_status.pop(0)
82 def list_all_mapupdate_statuses(self):
83 for s in self.all_mapupdate_status:
85 def list_all_publish_statuses(self):
86 for s in self.all_publish_status:
88 def list_all_retrieve_statuses(self):
89 for s in self.all_retrieve_status: