CMS 3D CMS Logo

hcal_runs.py
Go to the documentation of this file.
1 import urllib2, re, simplejson as json, socket
2 import sys
3 
4 URL = "http://vocms00169:2113"
5 SQL = "select \
6  r.RUNNUMBER,\
7  r.START_TIME,\
8  r.RUN_TRIGGERS,\
9  r.RUN_TYPE\
10  from \
11  hcal.runs r \
12  where \
13  r.RUN_GLOBAL = 0 and \
14  (r.runnumber >= :rf or :rf = 0) and \
15  (r.runnumber <= :rt or :rt = 0) \
16  order by \
17  r.runnumber"
18 
19 TIME = "select to_char(p.time,'YYYY-MM-DD HH24:MI:SS') time from hcal.run_parameters p where p.name like '%TRIGGERS'"
20 
21 def query(query):
22  resp = urllib2.urlopen(URL + "/query", query)
23  if "getcode" in dir(resp) and resp.getcode() == 200:
24  return resp.read()
25 
26 def qstring(qstring):
27  ps = ""
28  for k in qstring.keys():
29  ps += "&" if ps != "" else ""
30  ps += "%s=%s" % (k, qstring[k])
31  return ps
32 
33 def get_single(q, qs):
34  resp = urllib2.urlopen("%s/query/%s/data?%s" % (URL, query(q), qstring(qs)))
35  if "getcode" in dir(resp) and resp.getcode() == 200:
36  return json.loads(resp.read())["data"][0][0]
37  return None
38 
39 def get_all(q, qs):
40  qid = query(q)
41  ps = qstring(qs)
42  u = "%s/query/%s/count?%s" % (URL, qid, ps)
43  resp = urllib2.urlopen(u)
44  if "getcode" in dir(resp) and resp.getcode() == 200:
45  data = []
46  c = int(resp.read())
47  p = 1
48  while c > 0:
49  u = "%s/query/%s/page/1000/%d/data?%s" % (URL, qid, p, ps)
50  resp = urllib2.urlopen(u)
51  if "getcode" in dir(resp) and resp.getcode() == 200:
52  j = json.loads(resp.read())
53  data.extend(j["data"])
54  p += 1
55  c -= 1000
56  return data
57  return None
58 
59 def main(rf, rt):
60  ps = {}
61  if rf != None: ps["rf"] = rf
62  if rt != None: ps["rt"] = rt
63  data = get_all(SQL, ps)
64  for r in data:
65  if r[3] != None:
66  for t in ["pedestal","LED","laser"]:
67  if re.search(t, r[3], flags=re.IGNORECASE) != None:
68  d = get_single(TIME, { "p.run": int(r[0])})
69  print r[0], t, "\"" + d + "\"", r[2]
70  break
71 
72 
73 if __name__ == '__main__':
74 
75  rf = sys.argv[1] if len(sys.argv) > 1 else "-"
76  rt = sys.argv[2] if len(sys.argv) > 2 else "-"
77  rf = int(rf) if rf.isdigit() else 0
78  rt = int(rt) if rt.isdigit() else 0
79 
80  main(rf, rt)
def query(query)
Definition: hcal_runs.py:21
def qstring(qstring)
Definition: hcal_runs.py:26
Definition: query.py:1
def get_all(q, qs)
Definition: hcal_runs.py:39
Definition: main.py:1
def get_single(q, qs)
Definition: hcal_runs.py:33
def main(rf, rt)
Definition: hcal_runs.py:59