introweb.py: add ?t=json, to provide machine-readable subscriber counts

This commit is contained in:
Brian Warner 2008-03-25 12:56:12 -07:00
parent 4b46f1cd53
commit 4531d1e953
2 changed files with 46 additions and 4 deletions

View File

@ -1,6 +1,6 @@
from base64 import b32encode from base64 import b32encode
import os, sys, time, re import os, sys, time, re, simplejson
from cStringIO import StringIO from cStringIO import StringIO
from twisted.trial import unittest from twisted.trial import unittest
from twisted.internet import defer, reactor from twisted.internet import defer, reactor
@ -880,13 +880,30 @@ class SystemTest(testutil.SignalMixin, testutil.PollMixin, unittest.TestCase):
try: try:
self.failUnless("allmydata: %s" % str(allmydata.__version__) self.failUnless("allmydata: %s" % str(allmydata.__version__)
in res) 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: except unittest.FailTest:
print print
print "GET %s output was:" % self.introweb_url print "GET %s output was:" % self.introweb_url
print res print res
raise raise
d.addCallback(_check) 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 return d
def _do_publish1(self, res): def _do_publish1(self, res):

View File

@ -1,18 +1,43 @@
import time import time
from nevow import rend from nevow import rend, inevow
from foolscap.referenceable import SturdyRef from foolscap.referenceable import SturdyRef
from twisted.internet import address from twisted.internet import address
import allmydata import allmydata
import simplejson
from allmydata import get_package_versions_string from allmydata import get_package_versions_string
from allmydata.util import idlib from allmydata.util import idlib
from common import getxmlfile, IClient from common import getxmlfile, get_arg, IClient
class IntroducerRoot(rend.Page): class IntroducerRoot(rend.Page):
addSlash = True addSlash = True
docFactory = getxmlfile("introducer.xhtml") 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): def data_version(self, ctx, data):
return get_package_versions_string() return get_package_versions_string()
def data_import_path(self, ctx, data): def data_import_path(self, ctx, data):