From 39db60cc2bf1a72e541f6b30aa296510c4753bce Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Thu, 7 Aug 2008 13:12:14 -0700
Subject: [PATCH] diskwatcher.tac: include total-bytes-used

---
 misc/munin/tahoe_diskusage     |  2 +-
 misc/munin/tahoe_doomsday      |  2 +-
 misc/spacetime/diskwatcher.tac | 28 ++++++++++++++++++++++++----
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/misc/munin/tahoe_diskusage b/misc/munin/tahoe_diskusage
index a4ccf7b0..8f7fdfcb 100644
--- a/misc/munin/tahoe_diskusage
+++ b/misc/munin/tahoe_diskusage
@@ -24,7 +24,7 @@ rate_4wk.draw LINE2"""
     sys.exit(0)
 
 url = os.environ["url"]
-timespans = simplejson.load(urllib.urlopen(url))
+timespans = simplejson.load(urllib.urlopen(url))["rates"]
 
 data = dict([(name, growth)
              for (name, timespan, growth, timeleft) in timespans])
diff --git a/misc/munin/tahoe_doomsday b/misc/munin/tahoe_doomsday
index 08f642ca..e2006963 100644
--- a/misc/munin/tahoe_doomsday
+++ b/misc/munin/tahoe_doomsday
@@ -24,7 +24,7 @@ days_4wk.draw LINE2"""
     sys.exit(0)
 
 url = os.environ["url"]
-timespans = simplejson.load(urllib.urlopen(url))
+timespans = simplejson.load(urllib.urlopen(url))["rates"]
 
 data = dict([(name, timeleft)
              for (name, timespan, growth, timeleft) in timespans])
diff --git a/misc/spacetime/diskwatcher.tac b/misc/spacetime/diskwatcher.tac
index 9780e2dd..5fa72d89 100644
--- a/misc/spacetime/diskwatcher.tac
+++ b/misc/spacetime/diskwatcher.tac
@@ -150,7 +150,7 @@ class DiskWatcher(service.MultiService, resource.Resource):
         Sample(store=self.store,
                url=unicode(url), when=when, used=used, avail=avail)
 
-    def calculate(self):
+    def calculate_growth_timeleft(self):
         timespans = []
         total_avail_space = self.find_total_avail_space()
         pairs = [ (timespan,name)
@@ -183,6 +183,23 @@ class DiskWatcher(service.MultiService, resource.Resource):
                 total_avail_space += latest[0].avail
         return total_avail_space
 
+    def find_total_used_space(self):
+        # this returns the sum of disk-used stats for all servers that 1) are
+        # listed in urls.txt and 2) have responded recently.
+        now = extime.Time()
+        recent = now - timedelta(seconds=2*self.POLL_INTERVAL)
+        total_used_space = 0
+        for url in self.get_urls():
+            url = unicode(url)
+            latest = list(self.store.query(Sample,
+                                           AND(Sample.url == url,
+                                               Sample.when > recent),
+                                           sort=Sample.when.descending,
+                                           limit=1))
+            if latest:
+                total_used_space += latest[0].used
+        return total_used_space
+
 
     def growth(self, timespan):
         """Calculate the bytes-per-second growth of the total disk-used stat,
@@ -268,12 +285,15 @@ class DiskWatcher(service.MultiService, resource.Resource):
         data = ""
         if t == "html":
             data = ""
-            for (name, timespan, growth, timeleft) in self.calculate():
+            for (name, timespan, growth, timeleft) in self.calculate_growth_timeleft():
                 data += "%f bytes per second, %s remaining (over %s)\n" % \
                         (growth, self.abbreviate_time(timeleft), name)
+            used = self.find_total_used_space()
+            data += "total used: %d bytes\n" % used
         elif t == "json":
-            current = self.calculate()
-            #data = str(current) + "\n" # isn't that convenient? almost.
+            current = {"rates": self.calculate_growth_timeleft(),
+                       "used": self.find_total_used_space(),
+                       }
             data = simplejson.dumps(current, indent=True)
         else:
             req.setResponseCode(http.BAD_REQUEST)
-- 
2.45.2