source file: /home/buildslave/tahoe/edgy/build/src/allmydata/history.py
file stats: 76 lines, 73 executed: 96.1% covered
coverage versus previous test: 0 lines added, 0 lines removed
    1. 
    2. import weakref
    3. 
    4. class History:
    5.     """Keep track of recent operations, for a status display."""
    6. 
    7.     name = "history"
    8.     MAX_DOWNLOAD_STATUSES = 10
    9.     MAX_UPLOAD_STATUSES = 10
   10.     MAX_MAPUPDATE_STATUSES = 20
   11.     MAX_PUBLISH_STATUSES = 20
   12.     MAX_RETRIEVE_STATUSES = 20
   13. 
   14.     def __init__(self, stats_provider=None):
   15.         self.stats_provider = stats_provider
   16. 
   17.         self.all_downloads_statuses = weakref.WeakKeyDictionary()
   18.         self.recent_download_statuses = []
   19.         self.all_upload_statuses = weakref.WeakKeyDictionary()
   20.         self.recent_upload_statuses = []
   21. 
   22.         self.all_mapupdate_status = weakref.WeakKeyDictionary()
   23.         self.recent_mapupdate_status = []
   24.         self.all_publish_status = weakref.WeakKeyDictionary()
   25.         self.recent_publish_status = []
   26.         self.all_retrieve_status = weakref.WeakKeyDictionary()
   27.         self.recent_retrieve_status = []
   28. 
   29.         self.all_helper_upload_statuses = weakref.WeakKeyDictionary()
   30.         self.recent_helper_upload_statuses = []
   31. 
   32. 
   33.     def add_download(self, download_status):
   34.         self.all_downloads_statuses[download_status] = None
   35.         self.recent_download_statuses.append(download_status)
   36.         while len(self.recent_download_statuses) > self.MAX_DOWNLOAD_STATUSES:
   37.             self.recent_download_statuses.pop(0)
   38. 
   39.     def list_all_download_statuses(self):
   40.         for ds in self.all_downloads_statuses:
   41.             yield ds
   42. 
   43.     def add_upload(self, upload_status):
   44.         self.all_upload_statuses[upload_status] = None
   45.         self.recent_upload_statuses.append(upload_status)
   46.         while len(self.recent_upload_statuses) > self.MAX_UPLOAD_STATUSES:
   47.             self.recent_upload_statuses.pop(0)
   48. 
   49.     def list_all_upload_statuses(self):
   50.         for us in self.all_upload_statuses:
   51.             yield us
   52. 
   53. 
   54. 
   55.     def notify_mapupdate(self, p):
   56.         self.all_mapupdate_status[p] = None
   57.         self.recent_mapupdate_status.append(p)
   58.         while len(self.recent_mapupdate_status) > self.MAX_MAPUPDATE_STATUSES:
   59.             self.recent_mapupdate_status.pop(0)
   60. 
   61.     def notify_publish(self, p, size):
   62.         self.all_publish_status[p] = None
   63.         self.recent_publish_status.append(p)
   64.         if self.stats_provider:
   65.             self.stats_provider.count('mutable.files_published', 1)
   66.             # We must be told bytes_published as an argument, since the
   67.             # publish_status does not yet know how much data it will be asked
   68.             # to send. When we move to MDMF we'll need to find a better way
   69.             # to handle this.
   70.             self.stats_provider.count('mutable.bytes_published', size)
   71.         while len(self.recent_publish_status) > self.MAX_PUBLISH_STATUSES:
   72.             self.recent_publish_status.pop(0)
   73. 
   74.     def notify_retrieve(self, r):
   75.         self.all_retrieve_status[r] = None
   76.         self.recent_retrieve_status.append(r)
   77.         if self.stats_provider:
   78.             self.stats_provider.count('mutable.files_retrieved', 1)
   79.             self.stats_provider.count('mutable.bytes_retrieved', r.get_size())
   80.         while len(self.recent_retrieve_status) > self.MAX_RETRIEVE_STATUSES:
   81.             self.recent_retrieve_status.pop(0)
   82. 
   83. 
   84.     def list_all_mapupdate_statuses(self):
   85.         for s in self.all_mapupdate_status:
   86.             yield s
   87.     def list_all_publish_statuses(self):
   88.         for s in self.all_publish_status:
   89.             yield s
   90.     def list_all_retrieve_statuses(self):
   91.         for s in self.all_retrieve_status:
   92.             yield s
   93. 
   94.     def notify_helper_upload(self, s):
   95.         self.all_helper_upload_statuses[s] = None
   96.         self.recent_helper_upload_statuses.append(s)
   97.         while len(self.recent_helper_upload_statuses) > self.MAX_UPLOAD_STATUSES:
   98.             self.recent_helper_upload_statuses.pop(0)
   99. 
  100.     def list_all_helper_statuses(self):
  101.         for s in self.all_helper_upload_statuses:
  102.             yield s
  103.