]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/history.py
mutable: move recent operation history management code (MutableWatcher) into history...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / history.py
1
2 import weakref
3 from twisted.application import service
4
5 class History(service.Service):
6     """Keep track of recent operations, for a status display."""
7
8     name = "history"
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
14
15     def __init__(self, stats_provider=None):
16         self.stats_provider = stats_provider
17
18         self.all_downloads_statuses = weakref.WeakKeyDictionary()
19         self.recent_download_statuses = []
20         self.all_upload_statuses = weakref.WeakKeyDictionary()
21         self.recent_upload_statuses = []
22
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 = []
29
30
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)
36
37     def list_all_download_statuses(self):
38         for ds in self.all_downloads_statuses:
39             yield ds
40
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)
46
47     def list_all_upload_statuses(self):
48         for us in self.all_upload_statuses:
49             yield us
50
51
52
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)
58
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
67             # to handle this.
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)
71
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)
80
81
82     def list_all_mapupdate_statuses(self):
83         for s in self.all_mapupdate_status:
84             yield s
85     def list_all_publish_statuses(self):
86         for s in self.all_publish_status:
87             yield s
88     def list_all_retrieve_statuses(self):
89         for s in self.all_retrieve_status:
90             yield s
91
92
93