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):
if results:
return tag return tag
return "" return ""
d.addCallback(_got_results)
return d
@renderer @renderer
def started(self, req, tag): def started(self, req, tag):
@ -673,23 +669,19 @@ 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):
if servermap is None:
return tag("None") return tag("None")
ul = tags.ul() ul = tags.ul()
for peerid in sorted(servermap.keys()): for peerid in sorted(servermap.keys()):
@ -700,34 +692,24 @@ class DownloadStatusElement(Element):
plural(servermap[peerid]), plural(servermap[peerid]),
shares_s))) shares_s)))
return ul 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)
def _got(server_problems):
if not server_problems: if not server_problems:
return tag("") return ""
ul = T.ul() ul = T.ul()
for peerid in sorted(server_problems.keys()): for peerid in sorted(server_problems.keys()):
peerid_s = idlib.shortnodeid_b2a(peerid) peerid_s = idlib.shortnodeid_b2a(peerid)
ul(tags.li("[%s]: %s" % (peerid_s, server_problems[peerid]))) ul(tags.li("[%s]: %s" % (peerid_s, server_problems[peerid])))
return tags.li("Server Problems:", ul) 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,9 +775,7 @@ 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"))
def _render(per_server):
if per_server is None: if per_server is None:
return "" return ""
ul = tags.ul() ul = tags.ul()
@ -808,8 +785,6 @@ class DownloadStatusElement(Element):
for t in per_server[peerid]]) for t in per_server[peerid]])
ul(tags.li("[%s]: %s" % (peerid_s, times_s))) ul(tags.li("[%s]: %s" % (peerid_s, times_s)))
return tags.li("Per-Server Segment Fetch Response Times: ", ul) return tags.li("Per-Server Segment Fetch Response Times: ", ul)
d.addCallback(_render)
return d
class RetrieveStatusPage(MultiFormatResource): class RetrieveStatusPage(MultiFormatResource):