3 from argparse
import ArgumentParser
4 from os
import listdir, makedirs
5 from os.path
import dirname, exists, join
as pjoin
6 from pkg_resources
import resource_filename
7 from shutil
import copy2
8 from textwrap
import dedent
10 from genshi
import HTML
11 from genshi.output
import HTMLSerializer, encode
12 from genshi.template
import MarkupTemplate
14 from DQUtils
import fetch_iovs
15 from DQUtils.general
import timer
16 from DQUtils.logger
import init_logger;
init_logger(verbose=2)
17 from DQUtils.lumi
import fetch_lumi_inputs, compute_lumi_many_channels
19 from DQDefects
import DefectsDB
21 from IPython.Shell
import IPShellEmbed; ip = IPShellEmbed([
"-pdb"])
23 GRAPH_URL =
"http://atlasdqm.web.cern.ch/atlasdqm/defect_graph/HEAD"
33 (
"name_with_url",
"Name"),
34 (
"description",
"Description"),
36 (
"latest_run",
"Latest Run"),
37 (
"rec_lb_frac",
"Potentially Recoverable LB %"),
38 (
"lumi", HTML(
"Luminosity (pb<sup>-1</sup>)")),
43 heading_names, heading_titles = zip(*headings)
46 for x
in heading_names
if x)
50 A defect instance. Forms a row in the table.
52 def __init__(self, name, description, iovs, virtual, intolerable, lumi):
53 if description == name:
58 (self.name, self.description, self.iovs, self.
lumi, self.virtual,
60 name, description, iovs, lumi / 1e6, virtual, intolerable)
62 if iov.since.run == iov.until.run)
64 if iov.recoverable
and iov.since.run == iov.until.run)
76 self.
nruns = len(runs)
if runs
else "-"
82 if self.virtual
or self.intolerable:
83 return '<a href="{0}/{1}.svg">{1}</a>'.
format(GRAPH_URL, self.name)
85 return "{0}".
format(self.name)
89 td =
'<td class="icon {class_}"><span>{0}</span></td>'
90 bot_cls = user_cls =
""
91 if len(self.
users) == 1: user_cls =
"user"
92 if len(self.
users) > 1: user_cls =
"users"
93 if len(self.
bots): bot_cls =
"bots"
95 virt_class =
"virtual" if self.virtual
else "primary"
96 tol_class =
"intolerable" if self.intolerable
else "tolerable"
97 virt_sort = 1
if self.virtual
else 0
98 tol_sort = 1
if self.virtual
else 0
101 td.format(virt_sort, class_=virt_class),
102 td.format(tol_sort, class_=tol_class),
103 td.format(
", ".
join(self.
users), class_=user_cls),
104 td.format(
", ".
join(self.
bots), class_=bot_cls),
105 content_string.format(d=self)
111 Build the HTML content
113 path = resource_filename(
"DQDefects.data",
"table.html")
114 with open(path)
as fd:
115 template = MarkupTemplate(fd, path)
117 stream = template.generate(HTML=HTML, **kwargs)
118 serializer = HTMLSerializer(doctype=
"html5")
119 content =
encode(serializer(stream))
122 def build_defects(descriptions, virtuals, intolerables, lbs, lumis, all_defects):
124 with timer(
"Sort defects by channel"):
125 dbc = all_defects.by_channel
127 with timer(
"Compute luminosities"):
132 for name, description
in sorted(descriptions.iteritems()):
133 virtual, intolerable = name
in virtuals, name
in intolerables
134 defect =
Defect(name, description, dbc[name], virtual, intolerable,
135 lumi_by_defect.get(name, 0))
136 defects.append(defect)
138 defects.sort(key=
lambda d: (d.virtual,
not d.intolerable))
143 art_base =
dirname(resource_filename(
"DQDefects.data",
"table.html"))
145 for filename
in listdir(art_base):
146 _, _, ext = filename.rpartition(
".")
147 if ext.lower()
not in (
"png",
"js",
"css"):
149 copy2(pjoin(art_base, filename), pjoin(target_dir, filename))
154 iov_range =
None,
None
155 iov_range = 185000 << 32, 185500 << 32
158 with timer(
"Fetch defect info"):
159 all_defects = d.retrieve(*iov_range)
161 with timer(
"fetch peripheral information"):
162 descriptions = d.all_defect_descriptions
163 intolerable = d.get_intolerable_defects()
164 virtual = d.virtual_defect_names
166 with timer(
"Fetch lumi inputs"):
169 d =
build_defects(descriptions, virtual, intolerable, lbs, lumis, all_defects)
171 target_dir =
"/afs/cern.ch/user/p/pwaller/www/defects/test"
172 art_dir = pjoin(target_dir,
"extern")
179 content =
build_table(headings=heading_titles, defects=d)
180 with open(pjoin(target_dir,
"test.html"),
"w")
as fd:
184 if __name__ ==
"__main__":