]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/status.py
webish: this file is too big, start breaking it into pieces, beginning with status
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / status.py
1
2 import time
3 from twisted.internet import defer
4 from nevow import rend, loaders, tags as T
5 from nevow.util import resource_filename
6 from allmydata.util import base32, idlib
7 from allmydata.web.common import IClient
8 from allmydata.interfaces import IUploadStatus, IDownloadStatus, \
9      IPublishStatus, IRetrieveStatus
10
11 def getxmlfile(name):
12     return loaders.xmlfile(resource_filename('allmydata.web', '%s' % name))
13
14 def plural(sequence_or_length):
15     if isinstance(sequence_or_length, int):
16         length = sequence_or_length
17     else:
18         length = len(sequence_or_length)
19     if length == 1:
20         return ""
21     return "s"
22
23 class RateAndTimeMixin:
24
25     def render_time(self, ctx, data):
26         # 1.23s, 790ms, 132us
27         if data is None:
28             return ""
29         s = float(data)
30         if s >= 1.0:
31             return "%.2fs" % s
32         if s >= 0.01:
33             return "%dms" % (1000*s)
34         if s >= 0.001:
35             return "%.1fms" % (1000*s)
36         return "%dus" % (1000000*s)
37
38     def render_rate(self, ctx, data):
39         # 21.8kBps, 554.4kBps 4.37MBps
40         if data is None:
41             return ""
42         r = float(data)
43         if r > 1000000:
44             return "%1.2fMBps" % (r/1000000)
45         if r > 1000:
46             return "%.1fkBps" % (r/1000)
47         return "%dBps" % r
48
49 class UploadResultsRendererMixin(RateAndTimeMixin):
50     # this requires a method named 'upload_results'
51
52     def render_sharemap(self, ctx, data):
53         d = self.upload_results()
54         d.addCallback(lambda res: res.sharemap)
55         def _render(sharemap):
56             if sharemap is None:
57                 return "None"
58             l = T.ul()
59             for shnum in sorted(sharemap.keys()):
60                 l[T.li["%d -> %s" % (shnum, sharemap[shnum])]]
61             return l
62         d.addCallback(_render)
63         return d
64
65     def render_servermap(self, ctx, data):
66         d = self.upload_results()
67         d.addCallback(lambda res: res.servermap)
68         def _render(servermap):
69             if servermap is None:
70                 return "None"
71             l = T.ul()
72             for peerid in sorted(servermap.keys()):
73                 peerid_s = idlib.shortnodeid_b2a(peerid)
74                 shares_s = ",".join(["#%d" % shnum
75                                      for shnum in servermap[peerid]])
76                 l[T.li["[%s] got share%s: %s" % (peerid_s,
77                                                  plural(servermap[peerid]),
78                                                  shares_s)]]
79             return l
80         d.addCallback(_render)
81         return d
82
83     def data_file_size(self, ctx, data):
84         d = self.upload_results()
85         d.addCallback(lambda res: res.file_size)
86         return d
87
88     def _get_time(self, name):
89         d = self.upload_results()
90         d.addCallback(lambda res: res.timings.get(name))
91         return d
92
93     def data_time_total(self, ctx, data):
94         return self._get_time("total")
95
96     def data_time_storage_index(self, ctx, data):
97         return self._get_time("storage_index")
98
99     def data_time_contacting_helper(self, ctx, data):
100         return self._get_time("contacting_helper")
101
102     def data_time_existence_check(self, ctx, data):
103         return self._get_time("existence_check")
104
105     def data_time_cumulative_fetch(self, ctx, data):
106         return self._get_time("cumulative_fetch")
107
108     def data_time_helper_total(self, ctx, data):
109         return self._get_time("helper_total")
110
111     def data_time_peer_selection(self, ctx, data):
112         return self._get_time("peer_selection")
113
114     def data_time_total_encode_and_push(self, ctx, data):
115         return self._get_time("total_encode_and_push")
116
117     def data_time_cumulative_encoding(self, ctx, data):
118         return self._get_time("cumulative_encoding")
119
120     def data_time_cumulative_sending(self, ctx, data):
121         return self._get_time("cumulative_sending")
122
123     def data_time_hashes_and_close(self, ctx, data):
124         return self._get_time("hashes_and_close")
125
126     def _get_rate(self, name):
127         d = self.upload_results()
128         def _convert(r):
129             file_size = r.file_size
130             time = r.timings.get(name)
131             if time is None:
132                 return None
133             try:
134                 return 1.0 * file_size / time
135             except ZeroDivisionError:
136                 return None
137         d.addCallback(_convert)
138         return d
139
140     def data_rate_total(self, ctx, data):
141         return self._get_rate("total")
142
143     def data_rate_storage_index(self, ctx, data):
144         return self._get_rate("storage_index")
145
146     def data_rate_encode(self, ctx, data):
147         return self._get_rate("cumulative_encoding")
148
149     def data_rate_push(self, ctx, data):
150         return self._get_rate("cumulative_sending")
151
152     def data_rate_encode_and_push(self, ctx, data):
153         d = self.upload_results()
154         def _convert(r):
155             file_size = r.file_size
156             if file_size is None:
157                 return None
158             time1 = r.timings.get("cumulative_encoding")
159             if time1 is None:
160                 return None
161             time2 = r.timings.get("cumulative_sending")
162             if time2 is None:
163                 return None
164             try:
165                 return 1.0 * file_size / (time1+time2)
166             except ZeroDivisionError:
167                 return None
168         d.addCallback(_convert)
169         return d
170
171     def data_rate_ciphertext_fetch(self, ctx, data):
172         d = self.upload_results()
173         def _convert(r):
174             fetch_size = r.ciphertext_fetched
175             if fetch_size is None:
176                 return None
177             time = r.timings.get("cumulative_fetch")
178             if time is None:
179                 return None
180             try:
181                 return 1.0 * fetch_size / time
182             except ZeroDivisionError:
183                 return None
184         d.addCallback(_convert)
185         return d
186
187 class UploadStatusPage(UploadResultsRendererMixin, rend.Page):
188     docFactory = getxmlfile("upload-status.xhtml")
189
190     def __init__(self, data):
191         rend.Page.__init__(self, data)
192         self.upload_status = data
193
194     def upload_results(self):
195         return defer.maybeDeferred(self.upload_status.get_results)
196
197     def render_results(self, ctx, data):
198         d = self.upload_results()
199         def _got_results(results):
200             if results:
201                 return ctx.tag
202             return ""
203         d.addCallback(_got_results)
204         return d
205
206     def render_started(self, ctx, data):
207         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
208         started_s = time.strftime(TIME_FORMAT,
209                                   time.localtime(data.get_started()))
210         return started_s
211
212     def render_si(self, ctx, data):
213         si_s = base32.b2a_or_none(data.get_storage_index())
214         if si_s is None:
215             si_s = "(None)"
216         return si_s
217
218     def render_helper(self, ctx, data):
219         return {True: "Yes",
220                 False: "No"}[data.using_helper()]
221
222     def render_total_size(self, ctx, data):
223         size = data.get_size()
224         if size is None:
225             size = "(unknown)"
226         return size
227
228     def render_progress_hash(self, ctx, data):
229         progress = data.get_progress()[0]
230         # TODO: make an ascii-art bar
231         return "%.1f%%" % (100.0 * progress)
232
233     def render_progress_ciphertext(self, ctx, data):
234         progress = data.get_progress()[1]
235         # TODO: make an ascii-art bar
236         return "%.1f%%" % (100.0 * progress)
237
238     def render_progress_encode_push(self, ctx, data):
239         progress = data.get_progress()[2]
240         # TODO: make an ascii-art bar
241         return "%.1f%%" % (100.0 * progress)
242
243     def render_status(self, ctx, data):
244         return data.get_status()
245
246 class DownloadResultsRendererMixin(RateAndTimeMixin):
247     # this requires a method named 'download_results'
248
249     def render_servermap(self, ctx, data):
250         d = self.download_results()
251         d.addCallback(lambda res: res.servermap)
252         def _render(servermap):
253             if servermap is None:
254                 return "None"
255             l = T.ul()
256             for peerid in sorted(servermap.keys()):
257                 peerid_s = idlib.shortnodeid_b2a(peerid)
258                 shares_s = ",".join(["#%d" % shnum
259                                      for shnum in servermap[peerid]])
260                 l[T.li["[%s] has share%s: %s" % (peerid_s,
261                                                  plural(servermap[peerid]),
262                                                  shares_s)]]
263             return l
264         d.addCallback(_render)
265         return d
266
267     def render_servers_used(self, ctx, data):
268         d = self.download_results()
269         d.addCallback(lambda res: res.servers_used)
270         def _got(servers_used):
271             if not servers_used:
272                 return ""
273             peerids_s = ", ".join(["[%s]" % idlib.shortnodeid_b2a(peerid)
274                                    for peerid in servers_used])
275             return T.li["Servers Used: ", peerids_s]
276         d.addCallback(_got)
277         return d
278
279     def render_problems(self, ctx, data):
280         d = self.download_results()
281         d.addCallback(lambda res: res.server_problems)
282         def _got(server_problems):
283             if not server_problems:
284                 return ""
285             l = T.ul()
286             for peerid in sorted(server_problems.keys()):
287                 peerid_s = idlib.shortnodeid_b2a(peerid)
288                 l[T.li["[%s]: %s" % (peerid_s, server_problems[peerid])]]
289             return T.li["Server Problems:", l]
290         d.addCallback(_got)
291         return d
292
293     def data_file_size(self, ctx, data):
294         d = self.download_results()
295         d.addCallback(lambda res: res.file_size)
296         return d
297
298     def _get_time(self, name):
299         d = self.download_results()
300         d.addCallback(lambda res: res.timings.get(name))
301         return d
302
303     def data_time_total(self, ctx, data):
304         return self._get_time("total")
305
306     def data_time_peer_selection(self, ctx, data):
307         return self._get_time("peer_selection")
308
309     def data_time_uri_extension(self, ctx, data):
310         return self._get_time("uri_extension")
311
312     def data_time_hashtrees(self, ctx, data):
313         return self._get_time("hashtrees")
314
315     def data_time_segments(self, ctx, data):
316         return self._get_time("segments")
317
318     def data_time_cumulative_fetch(self, ctx, data):
319         return self._get_time("cumulative_fetch")
320
321     def data_time_cumulative_decode(self, ctx, data):
322         return self._get_time("cumulative_decode")
323
324     def data_time_cumulative_decrypt(self, ctx, data):
325         return self._get_time("cumulative_decrypt")
326
327     def _get_rate(self, name):
328         d = self.download_results()
329         def _convert(r):
330             file_size = r.file_size
331             time = r.timings.get(name)
332             if time is None:
333                 return None
334             try:
335                 return 1.0 * file_size / time
336             except ZeroDivisionError:
337                 return None
338         d.addCallback(_convert)
339         return d
340
341     def data_rate_total(self, ctx, data):
342         return self._get_rate("total")
343
344     def data_rate_segments(self, ctx, data):
345         return self._get_rate("segments")
346
347     def data_rate_fetch(self, ctx, data):
348         return self._get_rate("cumulative_fetch")
349
350     def data_rate_decode(self, ctx, data):
351         return self._get_rate("cumulative_decode")
352
353     def data_rate_decrypt(self, ctx, data):
354         return self._get_rate("cumulative_decrypt")
355
356     def render_server_timings(self, ctx, data):
357         d = self.download_results()
358         d.addCallback(lambda res: res.timings.get("fetch_per_server"))
359         def _render(per_server):
360             if per_server is None:
361                 return ""
362             l = T.ul()
363             for peerid in sorted(per_server.keys()):
364                 peerid_s = idlib.shortnodeid_b2a(peerid)
365                 times_s = ", ".join([self.render_time(None, t)
366                                      for t in per_server[peerid]])
367                 l[T.li["[%s]: %s" % (peerid_s, times_s)]]
368             return T.li["Per-Server Segment Fetch Response Times: ", l]
369         d.addCallback(_render)
370         return d
371
372 class DownloadStatusPage(DownloadResultsRendererMixin, rend.Page):
373     docFactory = getxmlfile("download-status.xhtml")
374
375     def __init__(self, data):
376         rend.Page.__init__(self, data)
377         self.download_status = data
378
379     def download_results(self):
380         return defer.maybeDeferred(self.download_status.get_results)
381
382     def render_results(self, ctx, data):
383         d = self.download_results()
384         def _got_results(results):
385             if results:
386                 return ctx.tag
387             return ""
388         d.addCallback(_got_results)
389         return d
390
391     def render_started(self, ctx, data):
392         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
393         started_s = time.strftime(TIME_FORMAT,
394                                   time.localtime(data.get_started()))
395         return started_s
396
397     def render_si(self, ctx, data):
398         si_s = base32.b2a_or_none(data.get_storage_index())
399         if si_s is None:
400             si_s = "(None)"
401         return si_s
402
403     def render_helper(self, ctx, data):
404         return {True: "Yes",
405                 False: "No"}[data.using_helper()]
406
407     def render_total_size(self, ctx, data):
408         size = data.get_size()
409         if size is None:
410             size = "(unknown)"
411         return size
412
413     def render_progress(self, ctx, data):
414         progress = data.get_progress()
415         # TODO: make an ascii-art bar
416         return "%.1f%%" % (100.0 * progress)
417
418     def render_status(self, ctx, data):
419         return data.get_status()
420
421 class RetrieveStatusPage(rend.Page, RateAndTimeMixin):
422     docFactory = getxmlfile("retrieve-status.xhtml")
423
424     def __init__(self, data):
425         rend.Page.__init__(self, data)
426         self.retrieve_status = data
427
428     def render_started(self, ctx, data):
429         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
430         started_s = time.strftime(TIME_FORMAT,
431                                   time.localtime(data.get_started()))
432         return started_s
433
434     def render_si(self, ctx, data):
435         si_s = base32.b2a_or_none(data.get_storage_index())
436         if si_s is None:
437             si_s = "(None)"
438         return si_s
439
440     def render_helper(self, ctx, data):
441         return {True: "Yes",
442                 False: "No"}[data.using_helper()]
443
444     def render_current_size(self, ctx, data):
445         size = data.get_size()
446         if size is None:
447             size = "(unknown)"
448         return size
449
450     def render_progress(self, ctx, data):
451         progress = data.get_progress()
452         # TODO: make an ascii-art bar
453         return "%.1f%%" % (100.0 * progress)
454
455     def render_status(self, ctx, data):
456         return data.get_status()
457
458     def render_encoding(self, ctx, data):
459         k, n = data.get_encoding()
460         return ctx.tag["Encoding: %s of %s" % (k, n)]
461
462     def render_search_distance(self, ctx, data):
463         d = data.get_search_distance()
464         return ctx.tag["Search Distance: %s peer%s" % (d, plural(d))]
465
466     def render_problems(self, ctx, data):
467         problems = data.problems
468         if not problems:
469             return ""
470         l = T.ul()
471         for peerid in sorted(problems.keys()):
472             peerid_s = idlib.shortnodeid_b2a(peerid)
473             l[T.li["[%s]: %s" % (peerid_s, problems[peerid])]]
474         return ctx.tag["Server Problems:", l]
475
476     def _get_rate(self, data, name):
477         file_size = self.retrieve_status.get_size()
478         time = self.retrieve_status.timings.get(name)
479         if time is None:
480             return None
481         try:
482             return 1.0 * file_size / time
483         except ZeroDivisionError:
484             return None
485
486     def data_time_total(self, ctx, data):
487         return self.retrieve_status.timings.get("total")
488     def data_rate_total(self, ctx, data):
489         return self._get_rate(data, "total")
490
491     def data_time_peer_selection(self, ctx, data):
492         return self.retrieve_status.timings.get("peer_selection")
493
494     def data_time_fetch(self, ctx, data):
495         return self.retrieve_status.timings.get("fetch")
496     def data_rate_fetch(self, ctx, data):
497         return self._get_rate(data, "fetch")
498
499     def data_time_decode(self, ctx, data):
500         return self.retrieve_status.timings.get("decode")
501     def data_rate_decode(self, ctx, data):
502         return self._get_rate(data, "decode")
503
504     def data_time_decrypt(self, ctx, data):
505         return self.retrieve_status.timings.get("decrypt")
506     def data_rate_decrypt(self, ctx, data):
507         return self._get_rate(data, "decrypt")
508
509     def render_server_timings(self, ctx, data):
510         per_server = self.retrieve_status.timings.get("fetch_per_server")
511         if not per_server:
512             return ""
513         l = T.ul()
514         for peerid in sorted(per_server.keys()):
515             peerid_s = idlib.shortnodeid_b2a(peerid)
516             times_s = ", ".join([self.render_time(None, t)
517                                  for t in per_server[peerid]])
518             l[T.li["[%s]: %s" % (peerid_s, times_s)]]
519         return T.li["Per-Server Fetch Response Times: ", l]
520
521
522 class PublishStatusPage(rend.Page):
523     docFactory = getxmlfile("publish-status.xhtml")
524
525     def render_started(self, ctx, data):
526         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
527         started_s = time.strftime(TIME_FORMAT,
528                                   time.localtime(data.get_started()))
529         return started_s
530
531     def render_si(self, ctx, data):
532         si_s = base32.b2a_or_none(data.get_storage_index())
533         if si_s is None:
534             si_s = "(None)"
535         return si_s
536
537     def render_helper(self, ctx, data):
538         return {True: "Yes",
539                 False: "No"}[data.using_helper()]
540
541     def render_current_size(self, ctx, data):
542         size = data.get_size()
543         if size is None:
544             size = "(unknown)"
545         return size
546
547     def render_progress(self, ctx, data):
548         progress = data.get_progress()
549         # TODO: make an ascii-art bar
550         return "%.1f%%" % (100.0 * progress)
551
552     def render_status(self, ctx, data):
553         return data.get_status()
554
555 class Status(rend.Page):
556     docFactory = getxmlfile("status.xhtml")
557     addSlash = True
558
559     def data_active_operations(self, ctx, data):
560         active =  (IClient(ctx).list_active_uploads() +
561                    IClient(ctx).list_active_downloads() +
562                    IClient(ctx).list_active_publish() +
563                    IClient(ctx).list_active_retrieve())
564         return active
565
566     def data_recent_operations(self, ctx, data):
567         recent = [o for o in (IClient(ctx).list_recent_uploads() +
568                               IClient(ctx).list_recent_downloads() +
569                               IClient(ctx).list_recent_publish() +
570                               IClient(ctx).list_recent_retrieve())
571                   if not o.get_active()]
572         recent.sort(lambda a,b: cmp(a.get_started(), b.get_started()))
573         recent.reverse()
574         return recent
575
576     def render_row(self, ctx, data):
577         s = data
578
579         TIME_FORMAT = "%H:%M:%S %d-%b-%Y"
580         started_s = time.strftime(TIME_FORMAT,
581                                   time.localtime(s.get_started()))
582         ctx.fillSlots("started", started_s)
583
584         si_s = base32.b2a_or_none(s.get_storage_index())
585         if si_s is None:
586             si_s = "(None)"
587         ctx.fillSlots("si", si_s)
588         ctx.fillSlots("helper", {True: "Yes",
589                                  False: "No"}[s.using_helper()])
590
591         size = s.get_size()
592         if size is None:
593             size = "(unknown)"
594         ctx.fillSlots("total_size", size)
595
596         progress = data.get_progress()
597         if IUploadStatus.providedBy(data):
598             link = "up-%d" % data.get_counter()
599             ctx.fillSlots("type", "upload")
600             # TODO: make an ascii-art bar
601             (chk, ciphertext, encandpush) = progress
602             progress_s = ("hash: %.1f%%, ciphertext: %.1f%%, encode: %.1f%%" %
603                           ( (100.0 * chk),
604                             (100.0 * ciphertext),
605                             (100.0 * encandpush) ))
606             ctx.fillSlots("progress", progress_s)
607         elif IDownloadStatus.providedBy(data):
608             link = "down-%d" % data.get_counter()
609             ctx.fillSlots("type", "download")
610             ctx.fillSlots("progress", "%.1f%%" % (100.0 * progress))
611         elif IPublishStatus.providedBy(data):
612             link = "publish-%d" % data.get_counter()
613             ctx.fillSlots("type", "publish")
614             ctx.fillSlots("progress", "%.1f%%" % (100.0 * progress))
615         else:
616             assert IRetrieveStatus.providedBy(data)
617             ctx.fillSlots("type", "retrieve")
618             link = "retrieve-%d" % data.get_counter()
619             ctx.fillSlots("progress", "%.1f%%" % (100.0 * progress))
620         ctx.fillSlots("status", T.a(href=link)[s.get_status()])
621         return ctx.tag
622
623     def childFactory(self, ctx, name):
624         client = IClient(ctx)
625         stype,count_s = name.split("-")
626         count = int(count_s)
627         if stype == "up":
628             for s in client.list_recent_uploads():
629                 if s.get_counter() == count:
630                     return UploadStatusPage(s)
631             for s in client.list_all_uploads():
632                 if s.get_counter() == count:
633                     return UploadStatusPage(s)
634         if stype == "down":
635             for s in client.list_recent_downloads():
636                 if s.get_counter() == count:
637                     return DownloadStatusPage(s)
638             for s in client.list_all_downloads():
639                 if s.get_counter() == count:
640                     return DownloadStatusPage(s)
641         if stype == "publish":
642             for s in client.list_recent_publish():
643                 if s.get_counter() == count:
644                     return PublishStatusPage(s)
645             for s in client.list_all_publish():
646                 if s.get_counter() == count:
647                     return PublishStatusPage(s)
648         if stype == "retrieve":
649             for s in client.list_recent_retrieve():
650                 if s.get_counter() == count:
651                     return RetrieveStatusPage(s)
652             for s in client.list_all_retrieve():
653                 if s.get_counter() == count:
654                     return RetrieveStatusPage(s)
655
656