From 4531d1e953156711d798afe8f438c7dcc8368609 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Tue, 25 Mar 2008 12:56:12 -0700
Subject: [PATCH] introweb.py: add ?t=json, to provide machine-readable
 subscriber counts

---
 src/allmydata/test/test_system.py | 21 +++++++++++++++++++--
 src/allmydata/web/introweb.py     | 29 +++++++++++++++++++++++++++--
 2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/src/allmydata/test/test_system.py b/src/allmydata/test/test_system.py
index 6800bab5..8227b528 100644
--- a/src/allmydata/test/test_system.py
+++ b/src/allmydata/test/test_system.py
@@ -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):
diff --git a/src/allmydata/web/introweb.py b/src/allmydata/web/introweb.py
index 97223573..592ef6bb 100644
--- a/src/allmydata/web/introweb.py
+++ b/src/allmydata/web/introweb.py
@@ -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):
-- 
2.45.2