]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/web/introweb.py
wui: use standard time format (#1077)
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / introweb.py
index 5a58ca5d08468242a0a1f08b8f7ec5df93cbfd56..2cfe6a55fb58064b0a8d7668f14a94886db6650e 100644 (file)
 
-from nevow import rend
-from foolscap.referenceable import SturdyRef
-from twisted.internet import address
+import time, os
+from nevow import rend, inevow
+from nevow.static import File as nevow_File
+from nevow.util import resource_filename
 import allmydata
+import simplejson
 from allmydata import get_package_versions_string
 from allmydata.util import idlib
-from common import getxmlfile, IClient
+from allmydata.web.common import getxmlfile, get_arg, render_time
+
 
 class IntroducerRoot(rend.Page):
 
     addSlash = True
     docFactory = getxmlfile("introducer.xhtml")
 
+    child_operations = None
+
+    def __init__(self, introducer_node):
+        self.introducer_node = introducer_node
+        self.introducer_service = introducer_node.getServiceNamed("introducer")
+        rend.Page.__init__(self, introducer_node)
+        static_dir = resource_filename("allmydata.web", "static")
+        for filen in os.listdir(static_dir):
+            self.putChild(filen, nevow_File(os.path.join(static_dir, filen)))
+
+    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):
+        res = {}
+
+        counts = {}
+        for s in self.introducer_service.get_subscribers():
+            if s.service_name not in counts:
+                counts[s.service_name] = 0
+            counts[s.service_name] += 1
+        res["subscription_summary"] = counts
+
+        announcement_summary = {}
+        for ad in self.introducer_service.get_announcements():
+            service_name = ad.service_name
+            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) + "\n"
+
+    # FIXME: This code is duplicated in root.py and introweb.py.
+    def data_rendered_at(self, ctx, data):
+        return render_time(time.time())
     def data_version(self, ctx, data):
         return get_package_versions_string()
     def data_import_path(self, ctx, data):
-        return str(allmydata)
+        return str(allmydata).replace("/", "/ ") # XXX kludge for wrapping
     def data_my_nodeid(self, ctx, data):
-        return idlib.nodeid_b2a(IClient(ctx).nodeid)
-
-    def data_known_storage_servers(self, ctx, data):
-        i = IClient(ctx).getServiceNamed("introducer")
-        storage = [1
-                   for (furl, service_name, ri_name, nickname, ver, oldest)
-                   in i.get_announcements()
-                   if service_name == "storage"]
-        return len(storage)
-
-    def data_num_clients(self, ctx, data):
-        i = IClient(ctx).getServiceNamed("introducer")
-        num_clients = 0
-        subscribers = i.get_subscribers()
-        for service_name,who in subscribers.items():
-            num_clients += len(who)
-        return num_clients
+        return idlib.nodeid_b2a(self.introducer_node.nodeid)
+
+    def render_announcement_summary(self, ctx, data):
+        services = {}
+        for ad in self.introducer_service.get_announcements():
+            if ad.service_name not in services:
+                services[ad.service_name] = 0
+            services[ad.service_name] += 1
+        service_names = services.keys()
+        service_names.sort()
+        return ", ".join(["%s: %d" % (service_name, services[service_name])
+                          for service_name in service_names])
+
+    def render_client_summary(self, ctx, data):
+        counts = {}
+        for s in self.introducer_service.get_subscribers():
+            if s.service_name not in counts:
+                counts[s.service_name] = 0
+            counts[s.service_name] += 1
+        return ", ".join([ "%s: %d" % (name, counts[name])
+                           for name in sorted(counts.keys()) ] )
 
     def data_services(self, ctx, data):
-        i = IClient(ctx).getServiceNamed("introducer")
-        ann = list(i.get_announcements())
-        ann.sort(lambda a,b: cmp( (a[1], a), (b[1], b) ) )
-        return ann
-
-    def render_service_row(self, ctx, announcement):
-        (furl, service_name, ri_name, nickname, ver, oldest) = announcement
-        sr = SturdyRef(furl)
-        nodeid = sr.tubID
-        advertised = [loc.split(":")[0] for loc in sr.locationHints]
-        ctx.fillSlots("peerid", "%s %s" % (idlib.nodeid_b2a(nodeid), nickname))
-        ctx.fillSlots("advertised", " ".join(advertised))
+        services = self.introducer_service.get_announcements(False)
+        services.sort(key=lambda ad: (ad.service_name, ad.nickname))
+        return services
+
+    def render_service_row(self, ctx, ad):
+        ctx.fillSlots("serverid", ad.serverid)
+        ctx.fillSlots("nickname", ad.nickname)
+        ctx.fillSlots("connection-hints",
+                      "connection hints: " + " ".join(ad.connection_hints))
         ctx.fillSlots("connected", "?")
-        ctx.fillSlots("since", "?")
-        ctx.fillSlots("announced", "?")
-        ctx.fillSlots("version", ver)
-        ctx.fillSlots("service_name", service_name)
+        when_s = render_time(ad.when)
+        ctx.fillSlots("announced", when_s)
+        ctx.fillSlots("version", ad.version)
+        ctx.fillSlots("service_name", ad.service_name)
         return ctx.tag
 
     def data_subscribers(self, ctx, data):
-        i = IClient(ctx).getServiceNamed("introducer")
-        s = []
-        for service_name, subscribers in i.get_subscribers().items():
-            for rref in subscribers:
-                s.append( (service_name, rref) )
-        s.sort()
-        return s
+        return self.introducer_service.get_subscribers()
 
     def render_subscriber_row(self, ctx, s):
-        (service_name, rref) = s
-        sr = rref.getSturdyRef()
-        nodeid = sr.tubID
-        ctx.fillSlots("peerid", "%s" % idlib.nodeid_b2a(nodeid))
-        advertised = [loc.split(":")[0] for loc in sr.locationHints]
-        ctx.fillSlots("advertised", " ".join(advertised))
-        remote_host = rref.tracker.broker.transport.getPeer()
-        if isinstance(remote_host, address.IPv4Address):
-            remote_host_s = "%s:%d" % (remote_host.host, remote_host.port)
-        else:
-            # loopback is a non-IPv4Address
-            remote_host_s = str(remote_host)
-        ctx.fillSlots("connected", remote_host_s)
-        ctx.fillSlots("since", "?")
-        ctx.fillSlots("service_name", service_name)
+        ctx.fillSlots("nickname", s.nickname)
+        ctx.fillSlots("tubid", s.tubid)
+        ctx.fillSlots("connected", s.remote_address)
+        since_s = render_time(s.when)
+        ctx.fillSlots("since", since_s)
+        ctx.fillSlots("version", s.version)
+        ctx.fillSlots("service_name", s.service_name)
         return ctx.tag
 
-