]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/introweb.py
introweb.py: add ?t=json, to provide machine-readable subscriber counts
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / introweb.py
1
2 import time
3 from nevow import rend, inevow
4 from foolscap.referenceable import SturdyRef
5 from twisted.internet import address
6 import allmydata
7 import simplejson
8 from allmydata import get_package_versions_string
9 from allmydata.util import idlib
10 from common import getxmlfile, get_arg, IClient
11
12 class IntroducerRoot(rend.Page):
13
14     addSlash = True
15     docFactory = getxmlfile("introducer.xhtml")
16
17     def renderHTTP(self, ctx):
18         t = get_arg(inevow.IRequest(ctx), "t")
19         if t == "json":
20             return self.render_JSON(ctx)
21         return rend.Page.renderHTTP(self, ctx)
22
23     def render_JSON(self, ctx):
24         i = IClient(ctx).getServiceNamed("introducer")
25         res = {}
26         clients = i.get_subscribers()
27         subscription_summary = dict([ (name, len(clients[name]))
28                                       for name in clients ])
29         res["subscription_summary"] = subscription_summary
30
31         announcement_summary = {}
32         for ann in i.get_announcements():
33             (furl, service_name, ri_name, nickname, ver, oldest) = ann
34             if service_name not in announcement_summary:
35                 announcement_summary[service_name] = 0
36             announcement_summary[service_name] += 1
37         res["announcement_summary"] = announcement_summary
38
39         return simplejson.dumps(res, indent=1)
40
41     def data_version(self, ctx, data):
42         return get_package_versions_string()
43     def data_import_path(self, ctx, data):
44         return str(allmydata)
45     def data_my_nodeid(self, ctx, data):
46         return idlib.nodeid_b2a(IClient(ctx).nodeid)
47
48     def render_announcement_summary(self, ctx, data):
49         i = IClient(ctx).getServiceNamed("introducer")
50         services = {}
51         for ann in i.get_announcements():
52             (furl, service_name, ri_name, nickname, ver, oldest) = ann
53             if service_name not in services:
54                 services[service_name] = 0
55             services[service_name] += 1
56         service_names = services.keys()
57         service_names.sort()
58         return ", ".join(["%s: %d" % (service_name, services[service_name])
59                           for service_name in service_names])
60
61     def render_client_summary(self, ctx, data):
62         i = IClient(ctx).getServiceNamed("introducer")
63         clients = i.get_subscribers()
64         service_names = clients.keys()
65         service_names.sort()
66         return ", ".join(["%s: %d" % (service_name, len(clients[service_name]))
67                           for service_name in service_names])
68
69     def data_services(self, ctx, data):
70         i = IClient(ctx).getServiceNamed("introducer")
71         ann = [(since,a)
72                for (a,since) in i.get_announcements().items()
73                if a[1] != "stub_client"]
74         ann.sort(lambda a,b: cmp( (a[1][1], a), (b[1][1], b) ) )
75         return ann
76
77     def render_service_row(self, ctx, (since,announcement)):
78         (furl, service_name, ri_name, nickname, ver, oldest) = announcement
79         sr = SturdyRef(furl)
80         nodeid = sr.tubID
81         advertised = [loc.split(":")[0] for loc in sr.locationHints
82                       if not loc.startswith("127.0.0.1:")]
83         ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
84         ctx.fillSlots("advertised", " ".join(advertised))
85         ctx.fillSlots("connected", "?")
86         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
87         ctx.fillSlots("announced",
88                       time.strftime(TIME_FORMAT, time.localtime(since)))
89         ctx.fillSlots("version", ver)
90         ctx.fillSlots("service_name", service_name)
91         return ctx.tag
92
93     def data_subscribers(self, ctx, data):
94         i = IClient(ctx).getServiceNamed("introducer")
95         # use the "stub_client" announcements to get information per nodeid
96         clients = {}
97         for ann in i.get_announcements():
98             if ann[1] != "stub_client":
99                 continue
100             (furl, service_name, ri_name, nickname, ver, oldest) = ann
101             sr = SturdyRef(furl)
102             nodeid = sr.tubID
103             clients[nodeid] = ann
104
105         # then we actually provide information per subscriber
106         s = []
107         for service_name, subscribers in i.get_subscribers().items():
108             for (rref, timestamp) in subscribers.items():
109                 sr = rref.getSturdyRef()
110                 nodeid = sr.tubID
111                 ann = clients.get(nodeid)
112                 s.append( (service_name, rref, timestamp, ann) )
113         s.sort()
114         return s
115
116     def render_subscriber_row(self, ctx, s):
117         (service_name, rref, since, ann) = s
118         nickname = "?"
119         version = "?"
120         if ann:
121             (furl, service_name_2, ri_name, nickname, version, oldest) = ann
122
123         sr = rref.getSturdyRef()
124         # if the subscriber didn't do Tub.setLocation, nodeid will be None
125         nodeid = sr.tubID or "?"
126         ctx.fillSlots("peerid", "%s %s" % (nodeid, nickname))
127         advertised = [loc.split(":")[0] for loc in sr.locationHints
128                       if not loc.startswith("127.0.0.1:")]
129         ctx.fillSlots("advertised", " ".join(advertised))
130         remote_host = rref.tracker.broker.transport.getPeer()
131         if isinstance(remote_host, address.IPv4Address):
132             remote_host_s = "%s:%d" % (remote_host.host, remote_host.port)
133         else:
134             # loopback is a non-IPv4Address
135             remote_host_s = str(remote_host)
136         ctx.fillSlots("connected", remote_host_s)
137         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
138         ctx.fillSlots("since",
139                       time.strftime(TIME_FORMAT, time.localtime(since)))
140         ctx.fillSlots("version", version)
141         ctx.fillSlots("service_name", service_name)
142         return ctx.tag
143
144