]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/introweb.py
replace TIME_FORMAT constant with format_time func
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / introweb.py
1
2 import time, os
3 from nevow import rend, inevow
4 from nevow.static import File as nevow_File
5 from nevow.util import resource_filename
6 import allmydata
7 import simplejson
8 from allmydata import get_package_versions_string
9 from allmydata.util import idlib
10 from allmydata.util.time_format import format_time
11 from allmydata.web.common import getxmlfile, get_arg
12
13
14 class IntroducerRoot(rend.Page):
15
16     addSlash = True
17     docFactory = getxmlfile("introducer.xhtml")
18
19     child_operations = None
20
21     def __init__(self, introducer_node):
22         self.introducer_node = introducer_node
23         self.introducer_service = introducer_node.getServiceNamed("introducer")
24         rend.Page.__init__(self, introducer_node)
25         static_dir = resource_filename("allmydata.web", "static")
26         for filen in os.listdir(static_dir):
27             self.putChild(filen, nevow_File(os.path.join(static_dir, filen)))
28
29     def renderHTTP(self, ctx):
30         t = get_arg(inevow.IRequest(ctx), "t")
31         if t == "json":
32             return self.render_JSON(ctx)
33         return rend.Page.renderHTTP(self, ctx)
34
35     def render_JSON(self, ctx):
36         res = {}
37
38         counts = {}
39         for s in self.introducer_service.get_subscribers():
40             if s.service_name not in counts:
41                 counts[s.service_name] = 0
42             counts[s.service_name] += 1
43         res["subscription_summary"] = counts
44
45         announcement_summary = {}
46         for ad in self.introducer_service.get_announcements():
47             service_name = ad.service_name
48             if service_name not in announcement_summary:
49                 announcement_summary[service_name] = 0
50             announcement_summary[service_name] += 1
51         res["announcement_summary"] = announcement_summary
52
53         return simplejson.dumps(res, indent=1) + "\n"
54
55     # FIXME: This code is duplicated in root.py and introweb.py.
56     def data_rendered_at(self, ctx, data):
57         return format_time(time.localtime())
58     def data_version(self, ctx, data):
59         return get_package_versions_string()
60     def data_import_path(self, ctx, data):
61         return str(allmydata).replace("/", "/ ") # XXX kludge for wrapping
62     def data_my_nodeid(self, ctx, data):
63         return idlib.nodeid_b2a(self.introducer_node.nodeid)
64
65     def render_announcement_summary(self, ctx, data):
66         services = {}
67         for ad in self.introducer_service.get_announcements():
68             if ad.service_name not in services:
69                 services[ad.service_name] = 0
70             services[ad.service_name] += 1
71         service_names = services.keys()
72         service_names.sort()
73         return ", ".join(["%s: %d" % (service_name, services[service_name])
74                           for service_name in service_names])
75
76     def render_client_summary(self, ctx, data):
77         counts = {}
78         for s in self.introducer_service.get_subscribers():
79             if s.service_name not in counts:
80                 counts[s.service_name] = 0
81             counts[s.service_name] += 1
82         return ", ".join([ "%s: %d" % (name, counts[name])
83                            for name in sorted(counts.keys()) ] )
84
85     def data_services(self, ctx, data):
86         services = self.introducer_service.get_announcements(False)
87         services.sort(key=lambda ad: (ad.service_name, ad.nickname))
88         return services
89
90     def render_service_row(self, ctx, ad):
91         ctx.fillSlots("serverid", ad.serverid)
92         ctx.fillSlots("nickname", ad.nickname)
93         ctx.fillSlots("connection-hints",
94                       "connection hints: " + " ".join(ad.connection_hints))
95         ctx.fillSlots("connected", "?")
96         when_s = time.strftime("%H:%M:%S %d-%b-%Y", time.localtime(ad.when))
97         ctx.fillSlots("announced", when_s)
98         ctx.fillSlots("version", ad.version)
99         ctx.fillSlots("service_name", ad.service_name)
100         return ctx.tag
101
102     def data_subscribers(self, ctx, data):
103         return self.introducer_service.get_subscribers()
104
105     def render_subscriber_row(self, ctx, s):
106         ctx.fillSlots("nickname", s.nickname)
107         ctx.fillSlots("tubid", s.tubid)
108         ctx.fillSlots("connected", s.remote_address)
109         since_s = time.strftime("%H:%M:%S %d-%b-%Y", time.localtime(s.when))
110         ctx.fillSlots("since", since_s)
111         ctx.fillSlots("version", s.version)
112         ctx.fillSlots("service_name", s.service_name)
113         return ctx.tag
114