]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/web/reliability.py
web: fix handling of reliability page when Numeric is not available
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / web / reliability.py
1
2 from nevow import rend, inevow, tags as T
3 reliability = None # might not be usable
4 try:
5     from allmydata import reliability # requires Numeric and PIL
6 except ImportError:
7     pass
8 from allmydata.web.common import getxmlfile, get_arg
9
10
11 DAY=24*60*60
12 MONTH=31*DAY
13 YEAR=365*DAY
14
15 def is_available():
16     if reliability:
17         return True
18     return False
19
20 def yandm(seconds):
21     return "%dy.%dm" % (int(seconds/YEAR), int( (seconds%YEAR)/MONTH))
22
23 class ReliabilityTool(rend.Page):
24     addSlash = True
25     docFactory = getxmlfile("reliability.xhtml")
26
27     DEFAULT_PARAMETERS = [
28         ("drive_lifetime", "8Y", "time"),
29         ("k", 3, "int"),
30         ("R", 7, "int"),
31         ("N", 10, "int"),
32         ("delta", "1M", "time"),
33         ("check_period", "1M", "time"),
34         ("report_period", "3M", "time"),
35         ("report_span", "5Y", "time"),
36         ]
37
38     def parse_time(self, s):
39         if s.endswith("M"):
40             return int(s[:-1]) * MONTH
41         if s.endswith("Y"):
42             return int(s[:-1]) * YEAR
43         return int(s)
44
45     def format_time(self, s):
46         if s%YEAR == 0:
47             return "%dY" % (s/YEAR)
48         if s%MONTH == 0:
49             return "%dM" % (s/MONTH)
50         return "%d" % s
51
52     def get_parameters(self, ctx):
53         req = inevow.IRequest(ctx)
54         parameters = {}
55         for name,default,argtype in self.DEFAULT_PARAMETERS:
56             v = get_arg(ctx, name, default)
57             if argtype == "time":
58                 value = self.parse_time(v)
59             else:
60                 value = int(v)
61             parameters[name] = value
62         return parameters
63
64     def renderHTTP(self, ctx):
65         self.parameters = self.get_parameters(ctx)
66         self.results = reliability.ReliabilityModel.run(**self.parameters)
67         return rend.Page.renderHTTP(self, ctx)
68
69     def make_input(self, name, old_value):
70         return T.input(name=name, type="text",
71                        value=self.format_time(old_value))
72
73     def render_forms(self, ctx, data):
74         f = T.form(action=".", method="get")
75         table = []
76         for name, default_value, argtype in self.DEFAULT_PARAMETERS:
77             old_value = self.parameters[name]
78             i = self.make_input(name, old_value)
79             table.append(T.tr[T.td[name+":"], T.td[i]])
80         go = T.input(type="submit", value="Recompute")
81         return [T.h2["Simulation Parameters:"],
82                 f[T.table[table], go],
83                 ]
84
85     def data_simulation_table(self, ctx, data):
86         for row in self.results.samples:
87             yield row
88
89     def render_simulation_row(self, ctx, row):
90         (when, unmaintained_shareprobs, maintained_shareprobs,
91          P_repaired_last_check_period,
92          cumulative_number_of_repairs,
93          cumulative_number_of_new_shares,
94          P_dead_unmaintained, P_dead_maintained) = row
95         ctx.fillSlots("t", yandm(when))
96         ctx.fillSlots("P_repair", "%.6f" % P_repaired_last_check_period)
97         ctx.fillSlots("P_dead_unmaintained", "%.6g" % P_dead_unmaintained)
98         ctx.fillSlots("P_dead_maintained", "%.6g" % P_dead_maintained)
99         return ctx.tag
100
101     def render_report_span(self, ctx, row):
102         (when, unmaintained_shareprobs, maintained_shareprobs,
103          P_repaired_last_check_period,
104          cumulative_number_of_repairs,
105          cumulative_number_of_new_shares,
106          P_dead_unmaintained, P_dead_maintained) = self.results.samples[-1]
107         return ctx.tag[yandm(when)]
108
109     def render_P_loss_unmaintained(self, ctx, row):
110         (when, unmaintained_shareprobs, maintained_shareprobs,
111          P_repaired_last_check_period,
112          cumulative_number_of_repairs,
113          cumulative_number_of_new_shares,
114          P_dead_unmaintained, P_dead_maintained) = self.results.samples[-1]
115         return ctx.tag["%.6g (%1.8f%%)" % (P_dead_unmaintained,
116                                            100*P_dead_unmaintained)]
117
118     def render_P_loss_maintained(self, ctx, row):
119         (when, unmaintained_shareprobs, maintained_shareprobs,
120          P_repaired_last_check_period,
121          cumulative_number_of_repairs,
122          cumulative_number_of_new_shares,
123          P_dead_unmaintained, P_dead_maintained) = self.results.samples[-1]
124         return ctx.tag["%.6g (%1.8f%%)" % (P_dead_maintained,
125                                            100*P_dead_maintained)]
126
127     def render_P_repair_rate(self, ctx, row):
128         (when, unmaintained_shareprobs, maintained_shareprobs,
129          P_repaired_last_check_period,
130          cumulative_number_of_repairs,
131          cumulative_number_of_new_shares,
132          P_dead_unmaintained, P_dead_maintained) = self.results.samples[-1]
133         freq = when / cumulative_number_of_repairs
134         return ctx.tag["%.6g" % freq]
135
136     def render_P_repair_shares(self, ctx, row):
137         (when, unmaintained_shareprobs, maintained_shareprobs,
138          P_repaired_last_check_period,
139          cumulative_number_of_repairs,
140          cumulative_number_of_new_shares,
141          P_dead_unmaintained, P_dead_maintained) = self.results.samples[-1]
142         generated_shares = cumulative_number_of_new_shares / cumulative_number_of_repairs
143         return ctx.tag["%1.2f" % generated_shares]
144
145