fixRunHistoryEndTimesViaNearline.py
Go to the documentation of this file.
1 import psycopg2
2 import csv
3 from datetime import tzinfo,timedelta
4 
5 try:
6  conn = psycopg2.connect("dbname=nova_prod host=novadaq-far-db-03.fnal.gov port=5432 user=novadaq")
7 # conn = psycopg2.connect("dbname=nova_dev host=ifdbdev.fnal.gov port=5433")
8 except:
9  print "I am unable to connect to the database"
10  exit(0)
11 
12 try:
13  conn2 = psycopg2.connect("dbname=nova_prod host=ifdbprod.fnal.gov port=5433 user=nova_reader")
14 except:
15  print "I am unable to connect to the database"
16  exit(0)
17 
18 cur = conn.cursor()
19 cur2 = conn2.cursor()
20 
21 SQL = "select run,tstart from fardet.runs where tstop is NULL order by run desc";
22 
23 cur.execute(SQL)
24 
25 rows = cur.fetchall()
26 runlist = (x for x in rows[1:]) # strip first run from list
27 
28 for run in runlist:
29  SQL = "select max(fardet.nearline_metrics.subrun) from fardet.nearline_metrics where fardet.nearline_metrics.run={}".format(run[0])
30  cur2.execute(SQL)
31  maxsubrun = cur2.fetchone()[0]
32  SQL = "select min(fardet.nearline_metrics.subrun) from fardet.nearline_metrics where fardet.nearline_metrics.run={}".format(run[0])
33  cur2.execute(SQL)
34  minsubrun = cur2.fetchone()[0]
35  if (minsubrun is not None)and(maxsubrun is not None):
36  SQL = "select fardet.nearline_metrics.lasteventtime from fardet.nearline_metrics where fardet.nearline_metrics.run={} and fardet.nearline_metrics.subrun={}".format(run[0],maxsubrun)
37  cur2.execute(SQL)
38  endtime = cur2.fetchall()
39  SQL = "select fardet.nearline_metrics.firsteventtime from fardet.nearline_metrics where fardet.nearline_metrics.run={} and fardet.nearline_metrics.subrun={}".format(run[0],minsubrun)
40  cur2.execute(SQL)
41  starttime = cur2.fetchall()
42  tdiff = run[1]-starttime[0][0]+timedelta(minutes=5)
43  nhour = int(tdiff.total_seconds()/(60*60))
44  if (nhour == 5 or nhour == 6):
45  toff = timedelta(hours=nhour)
46  newtimestamp = endtime[0][0]+toff
47  # print "{} | {} | {} | {} | {}".format(run[0],run[1],starttime[0][0],endtime[0][0],newtimestamp)
48  if newtimestamp>run[1]:
49  SQL = "update fardet.runs set (tstop,nsubruns)=(%s,%s) where run=%s"
50  print cur.mogrify(SQL,(newtimestamp,maxsubrun+1,run[0]))
51  cur.execute(SQL,(newtimestamp,maxsubrun+1,run[0]))
52 
53 conn.commit()
54 
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
exit(0)