2 from __future__
import division
4 from argparse
import ArgumentParser
5 from os
import listdir, makedirs
6 from os.path
import dirname, exists, join
as pjoin
7 from pkg_resources
import resource_filename
8 from shutil
import copy2
9 from textwrap
import dedent
11 from genshi
import HTML
12 from genshi.output
import HTMLSerializer, encode
13 from genshi.template
import MarkupTemplate
15 from DQUtils
import fetch_iovs
16 from DQUtils.general
import timer
17 from DQUtils.logger
import init_logger;
init_logger(verbose=2)
18 from DQUtils.lumi
import fetch_lumi_inputs, compute_lumi_many_channels
20 from DQDefects
import DefectsDB
22 from IPython.Shell
import IPShellEmbed; ip = IPShellEmbed([
"-pdb"])
24 GRAPH_URL =
"http://atlasdqm.web.cern.ch/atlasdqm/defect_graph/HEAD"
34 (
"name_with_url",
"Name"),
35 (
"description",
"Description"),
37 (
"latest_run",
"Latest Run"),
38 (
"rec_lb_frac",
"Potentially Recoverable LB %"),
39 (
"lumi", HTML(
"Luminosity (pb<sup>-1</sup>)")),
44 heading_names, heading_titles = zip(*headings)
47 for x
in heading_names
if x)
51 A defect instance. Forms a row in the table.
53 def __init__(self, name, description, iovs, virtual, intolerable, lumi):
54 if description == name:
59 (self.name, self.description, self.iovs, self.
lumi, self.virtual,
61 name, description, iovs, lumi / 1e6, virtual, intolerable)
63 if iov.since.run == iov.until.run)
65 if iov.recoverable
and iov.since.run == iov.until.run)
77 self.
nruns = len(runs)
if runs
else "-"
83 if self.virtual
or self.intolerable:
84 return '<a href="{0}/{1}.svg">{1}</a>'.
format(GRAPH_URL, self.name)
86 return "{0}".
format(self.name)
90 td =
'<td class="icon {class_}"><span>{0}</span></td>'
91 bot_cls = user_cls =
""
92 if len(self.
users) == 1: user_cls =
"user"
93 if len(self.
users) > 1: user_cls =
"users"
94 if len(self.
bots): bot_cls =
"bots"
96 virt_class =
"virtual" if self.virtual
else "primary"
97 tol_class =
"intolerable" if self.intolerable
else "tolerable"
98 virt_sort = 1
if self.virtual
else 0
99 tol_sort = 1
if self.virtual
else 0
102 td.format(virt_sort, class_=virt_class),
103 td.format(tol_sort, class_=tol_class),
104 td.format(
", ".
join(self.
users), class_=user_cls),
105 td.format(
", ".
join(self.
bots), class_=bot_cls),
106 content_string.format(d=self)
112 Build the HTML content
114 path = resource_filename(
"DQDefects.data",
"table.html")
115 with open(path)
as fd:
116 template = MarkupTemplate(fd, path)
118 stream = template.generate(HTML=HTML, **kwargs)
119 serializer = HTMLSerializer(doctype=
"html5")
120 content =
encode(serializer(stream))
123 def build_defects(descriptions, virtuals, intolerables, lbs, lumis, all_defects):
125 with timer(
"Sort defects by channel"):
126 dbc = all_defects.by_channel
128 with timer(
"Compute luminosities"):
133 for name, description
in sorted(descriptions.iteritems()):
134 virtual, intolerable = name
in virtuals, name
in intolerables
135 defect =
Defect(name, description, dbc[name], virtual, intolerable,
136 lumi_by_defect.get(name, 0))
137 defects.append(defect)
139 defects.sort(key=
lambda d: (d.virtual,
not d.intolerable))
144 art_base =
dirname(resource_filename(
"DQDefects.data",
"table.html"))
146 for filename
in listdir(art_base):
147 _, _, ext = filename.rpartition(
".")
148 if ext.lower()
not in (
"png",
"js",
"css"):
150 copy2(pjoin(art_base, filename), pjoin(target_dir, filename))
155 iov_range =
None,
None
156 iov_range = 185000 << 32, 185500 << 32
159 with timer(
"Fetch defect info"):
160 all_defects = d.retrieve(*iov_range)
162 with timer(
"fetch peripheral information"):
163 descriptions = d.all_defect_descriptions
164 intolerable = d.get_intolerable_defects()
165 virtual = d.virtual_defect_names
167 with timer(
"Fetch lumi inputs"):
170 d =
build_defects(descriptions, virtual, intolerable, lbs, lumis, all_defects)
172 target_dir =
"/afs/cern.ch/user/p/pwaller/www/defects/test"
173 art_dir = pjoin(target_dir,
"extern")
180 content =
build_table(headings=heading_titles, defects=d)
181 with open(pjoin(target_dir,
"test.html"),
"w")
as fd:
185 if __name__ ==
"__main__":