Avoid use of deferred within an Element

This causes the final HTML to be rendered funny, with a bunch of
"<Deferred current result=...>", which is clearly not what we want.
This commit is contained in:
Sajith Sasidharan 2020-07-07 20:24:56 -04:00
parent 7877d8a5cc
commit 0c4d24a2ac

View File

@ -439,7 +439,7 @@ class DownloadStatusElement(Element):
# #
# See #3310: https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3310 # See #3310: https://tahoe-lafs.org/trac/tahoe-lafs/ticket/3310
def download_results(self): def download_results(self):
return defer.maybeDeferred(self._download_status.get_results) return self._download_status.get_results()
def _relative_time(self, t): def _relative_time(self, t):
if t is None: if t is None:
@ -629,13 +629,9 @@ class DownloadStatusElement(Element):
@renderer @renderer
def results(self, req, tag): def results(self, req, tag):
d = self.download_results() if self.download_results():
def _got_results(results): return tag
if results: return ""
return tag
return ""
d.addCallback(_got_results)
return d
@renderer @renderer
def started(self, req, tag): def started(self, req, tag):
@ -673,61 +669,47 @@ class DownloadStatusElement(Element):
@renderer @renderer
def servers_used(self, req, tag): def servers_used(self, req, tag):
d = self.download_results() servers_used = self.download_results().servers_used
d.addCallback(lambda res: res.servers_used)
def _got(servers_used): if not servers_used:
if not servers_used: return ""
return ""
peerids_s = ", ".join(["[%s]" % idlib.shortnodeid_b2a(peerid) peerids_s = ", ".join(["[%s]" % idlib.shortnodeid_b2a(peerid)
for peerid in servers_used]) for peerid in servers_used])
return tags.li("Servers Used: ", peerids_s) return tags.li("Servers Used: ", peerids_s)
d.addCallback(_got)
return d
@renderer @renderer
def servermap(self, req, tag): def servermap(self, req, tag):
d = self.download_results() servermap = self.download_results().servermap
d.addCallback(lambda res: res.servermap) if not servermap:
def _render(servermap): return tag("None")
if servermap is None: ul = tags.ul()
return tag("None") for peerid in sorted(servermap.keys()):
ul = tags.ul() peerid_s = idlib.shortnodeid_b2a(peerid)
for peerid in sorted(servermap.keys()): shares_s = ",".join(["#%d" % shnum
peerid_s = idlib.shortnodeid_b2a(peerid) for shnum in servermap[peerid]])
shares_s = ",".join(["#%d" % shnum ul(tags.li("[%s] has share%s: %s" % (peerid_s,
for shnum in servermap[peerid]]) plural(servermap[peerid]),
ul(tags.li("[%s] has share%s: %s" % (peerid_s, shares_s)))
plural(servermap[peerid]), return ul
shares_s)))
return ul
d.addCallback(_render)
return d
@renderer @renderer
def problems(self, req, tag): def problems(self, req, tag):
d = self.download_results() server_problems = self.download_results().server_problems
d.addCallback(lambda res: res.server_problems) if not server_problems:
def _got(server_problems): return ""
if not server_problems: ul = T.ul()
return tag("") for peerid in sorted(server_problems.keys()):
ul = T.ul() peerid_s = idlib.shortnodeid_b2a(peerid)
for peerid in sorted(server_problems.keys()): ul(tags.li("[%s]: %s" % (peerid_s, server_problems[peerid])))
peerid_s = idlib.shortnodeid_b2a(peerid) return tags.li("Server Problems:", ul)
ul(tags.li("[%s]: %s" % (peerid_s, server_problems[peerid])))
return tags.li("Server Problems:", ul)
d.addCallback(_got)
return d
@renderer @renderer
def file_size(self, req, tag): def file_size(self, req, tag):
d = self.download_results() return tag(str(self.download_results().file_size))
d.addCallback(lambda res: str(res.file_size))
return d
def _get_time(self, name): def _get_time(self, name):
d = self.download_results() return self.download_results().timings.get(name)
d.addCallback(lambda res: res.timings.get(name))
return d
@renderer @renderer
def time_total(self, req, tag): def time_total(self, req, tag):
@ -766,13 +748,10 @@ class DownloadStatusElement(Element):
return tag(str(self._get_time("paused"))) return tag(str(self._get_time("paused")))
def _get_rate(self, name): def _get_rate(self, name):
d = self.download_results() r = self.download_results()
def _convert(r): file_size = r.file_size
file_size = r.file_size duration = r.timings.get(name)
duration = r.timings.get(name) return compute_rate(file_size, duration)
return compute_rate(file_size, duration)
d.addCallback(_convert)
return d
@renderer @renderer
def rate_total(self, req, tag): def rate_total(self, req, tag):
@ -796,20 +775,16 @@ class DownloadStatusElement(Element):
@renderer @renderer
def server_timings(self, req, tag): def server_timings(self, req, tag):
d = self.download_results() per_server = self._get_time("fetch_per_server")
d.addCallback(lambda res: res.timings.get("fetch_per_server")) if per_server is None:
def _render(per_server): return ""
if per_server is None: ul = tags.ul()
return "" for peerid in sorted(per_server.keys()):
ul = tags.ul() peerid_s = idlib.shortnodeid_b2a(peerid)
for peerid in sorted(per_server.keys()): times_s = ", ".join([abbreviate_time(t)
peerid_s = idlib.shortnodeid_b2a(peerid) for t in per_server[peerid]])
times_s = ", ".join([abbreviate_time(t) ul(tags.li("[%s]: %s" % (peerid_s, times_s)))
for t in per_server[peerid]]) return tags.li("Per-Server Segment Fetch Response Times: ", ul)
ul(tags.li("[%s]: %s" % (peerid_s, times_s)))
return tags.li("Per-Server Segment Fetch Response Times: ", ul)
d.addCallback(_render)
return d
class RetrieveStatusPage(MultiFormatResource): class RetrieveStatusPage(MultiFormatResource):