]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
introweb.py: add ?t=json, to provide machine-readable subscriber counts
authorBrian Warner <warner@allmydata.com>
Tue, 25 Mar 2008 19:56:12 +0000 (12:56 -0700)
committerBrian Warner <warner@allmydata.com>
Tue, 25 Mar 2008 19:56:12 +0000 (12:56 -0700)
src/allmydata/test/test_system.py
src/allmydata/web/introweb.py

index 6800bab5239f1b04ae5ea9aa1b0deda9b336c204..8227b52813023a4a88e16689f6090485479f3224 100644 (file)
@@ -1,6 +1,6 @@
 
 from base64 import b32encode
-import os, sys, time, re
+import os, sys, time, re, simplejson
 from cStringIO import StringIO
 from twisted.trial import unittest
 from twisted.internet import defer, reactor
@@ -880,13 +880,30 @@ class SystemTest(testutil.SignalMixin, testutil.PollMixin, unittest.TestCase):
             try:
                 self.failUnless("allmydata: %s" % str(allmydata.__version__)
                                 in res)
-                self.failUnless("Summary: storage: 5, stub_client: 5" in res)
+                self.failUnless("Announcement Summary: storage: 5, stub_client: 5" in res)
+                self.failUnless("Subscription Summary: storage: 5" in res)
             except unittest.FailTest:
                 print
                 print "GET %s output was:" % self.introweb_url
                 print res
                 raise
         d.addCallback(_check)
+        d.addCallback(lambda res:
+                      getPage(self.introweb_url + "?t=json",
+                              method="GET", followRedirect=True))
+        def _check_json(res):
+            data = simplejson.loads(res)
+            try:
+                self.failUnlessEqual(data["subscription_summary"],
+                                     {"storage": 5})
+                self.failUnlessEqual(data["announcement_summary"],
+                                     {"storage": 5, "stub_client": 5})
+            except unittest.FailTest:
+                print
+                print "GET %s?t=json output was:" % self.introweb_url
+                print res
+                raise
+        d.addCallback(_check_json)
         return d
 
     def _do_publish1(self, res):
index 9722357346a3b3e4fb8849785dc9aab3fe833779..592ef6bb35f6297355b4601ed8be676e2a3ec73f 100644 (file)
@@ -1,18 +1,43 @@
 
 import time
-from nevow import rend
+from nevow import rend, inevow
 from foolscap.referenceable import SturdyRef
 from twisted.internet import address
 import allmydata
+import simplejson
 from allmydata import get_package_versions_string
 from allmydata.util import idlib
-from common import getxmlfile, IClient
+from common import getxmlfile, get_arg, IClient
 
 class IntroducerRoot(rend.Page):
 
     addSlash = True
     docFactory = getxmlfile("introducer.xhtml")
 
+    def renderHTTP(self, ctx):
+        t = get_arg(inevow.IRequest(ctx), "t")
+        if t == "json":
+            return self.render_JSON(ctx)
+        return rend.Page.renderHTTP(self, ctx)
+
+    def render_JSON(self, ctx):
+        i = IClient(ctx).getServiceNamed("introducer")
+        res = {}
+        clients = i.get_subscribers()
+        subscription_summary = dict([ (name, len(clients[name]))
+                                      for name in clients ])
+        res["subscription_summary"] = subscription_summary
+
+        announcement_summary = {}
+        for ann in i.get_announcements():
+            (furl, service_name, ri_name, nickname, ver, oldest) = ann
+            if service_name not in announcement_summary:
+                announcement_summary[service_name] = 0
+            announcement_summary[service_name] += 1
+        res["announcement_summary"] = announcement_summary
+
+        return simplejson.dumps(res, indent=1)
+
     def data_version(self, ctx, data):
         return get_package_versions_string()
     def data_import_path(self, ctx, data):