CMS 3D CMS Logo

hcal_runs_valdas.py
Go to the documentation of this file.
1 import urllib2, re, json, socket
2 import sys
3 
4 URL = "http://vocms00170: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  req = urllib2.Request("%s/query/%s/data?%s" % (URL, query(q), qstring(qs)), headers={"Accept" : "application/json"})
35  resp = urllib2.urlopen(req)
36  if "getcode" in dir(resp) and resp.getcode() == 200:
37  ret = json.loads(resp.read())
38  if len(ret["data"]) > 0:
39  return ret["data"][0][0]
40  return None
41 
42 def get_all(q, qs):
43  qid = query(q)
44  ps = qstring(qs)
45  u = "%s/query/%s/count?%s" % (URL, qid, ps)
46  resp = urllib2.urlopen(u)
47  if "getcode" in dir(resp) and resp.getcode() == 200:
48  data = []
49  c = int(resp.read())
50  p = 1
51  while c > 0:
52  u = "%s/query/%s/page/1000/%d/data?%s" % (URL, qid, p, ps)
53  req = urllib2.Request(u, headers={"Accept" : "application/json"})
54  resp = urllib2.urlopen(req)
55  if "getcode" in dir(resp) and resp.getcode() == 200:
56  j = json.loads(resp.read())
57  data.extend(j["data"])
58  p += 1
59  c -= 1000
60  return data
61  return None
62 
63 def main(rf, rt):
64  ps = {}
65  if rf != None: ps["rf"] = rf
66  if rt != None: ps["rt"] = rt
67  data = get_all(SQL, ps)
68  for r in data:
69  if r[3] != None:
70  for t in ["pedestal","LED","laser"]:
71  if re.search(t, r[3], flags=re.IGNORECASE) != None:
72  d = get_single(TIME, { "p.run": int(r[0])})
73  if d is not None:
74  d = "\"" + d + "\""
75  print r[0], t, d, r[2]
76  break
77 
78 
79 if __name__ == '__main__':
80 
81  rf = sys.argv[1] if len(sys.argv) > 1 else "-"
82  rt = sys.argv[2] if len(sys.argv) > 2 else "-"
83  rf = int(rf) if rf.isdigit() else 0
84  rt = int(rt) if rt.isdigit() else 0
85 
86  main(rf, rt)
def qstring(qstring)
def get_single(q, qs)
Definition: query.py:1
Definition: main.py:1