shutoffs.py
Go to the documentation of this file.
1 from ROOT import TSQLServer, TSQLStatement
2 from datetime import timedelta, date
3 import time
4 import subprocess
5 import sys
6 import os
7 import os.path
8 import shutil
9 
10 #Software release
11 rel = 'S13-02-16'
12 
13 #Where files get writen to
14 resultpath ='/home/nearline/jancode/results/'
15 
16 #Read in password
17 dbpswfile = os.environ.get('NOVADBPWDFILE')
18 dbpsw =''
19 pfile = open(dbpswfile,'r')
20 for line in pfile:
21  dbpsw = line.rstrip()
22  break
23 pfile.close()
24 
25 #Starting and end days
26 week = timedelta(weeks=1)
27 day = timedelta(days=1)
28 date1 = date.today()
29 date2 = date1 - week
30 date2 = date2 - 3*day
31 
32 #Query to select possible runs
33 query = 'select run, min(firstnonemptyeventtime), max(lastnonemptyeventtime) from ndos.nearline_metrics '
34 query+= "where triggertype = 2 and softwarerelease = '"+rel+"' and firstnonemptyeventtime > TIMESTAMP'"+str(date2)+"' "
35 query+= "and lastnonemptyeventtime <= TIMESTAMP'"+str(date1+day)+"' and firstnonemptyeventtime > TIMESTAMP'2010-01-01' "
36 query+= "and lastnonemptyeventtime >= firstnonemptyeventtime "
37 query+= "group by run order by run;"
38 
39 #Connection information
40 db= TSQLServer.Connect("pgsql://ifdbprod.fnal.gov:5433/nova_prod", "nova_reader", dbpsw)
41 if not db:
42  print "Cannot connect to database"
43  sys.exit(1)
44 
45 stmt = db.Statement(query,1000)
46 if stmt.Process():
47  runs=[]
48  stmt.StoreResult()
49  prevend = 0
50  while stmt.NextResultRow():
51  run = stmt.GetString(0)
52  start = stmt.GetDay(1)
53  end = stmt.GetDay(2)
54  starttime=time.mktime(time.strptime(stmt.GetString(1),"%Y-%m-%d %H:%M:%S"))
55  endtime =time.mktime(time.strptime(stmt.GetString(2),"%Y-%m-%d %H:%M:%S"))
56  if end-start!=0:
57  runs.append(run)
58  prevend=endtime
59  elif starttime-prevend<2:
60  runs.append(run)
61 else:
62  print "Problem with Statement"
63  sys.exit(2)
64 
65 #Clean up statement and connection
66 del stmt
67 db.Close()
68 del db
69 
70 #Now run the febshutoff script over all the selected runs
71 #Where the script lives
72 path = '/home/nearline/Nearline/Commissioning/FEBShutoff/febshutoff_auto.py'
73 failures=''
74 #Run the script for all overnight runs selected, get the error codes and make it work
75 for run in runs:
76  print run, "Processing"
77  endid = subprocess.call(["python",path,run])
78  if endid == 0: continue
79  elif endid ==1:
80  failures+=run+' has not been processed\n'
81  elif endid ==2:
82  failures+=run+' has no reco files\n'
83  elif endid ==3:
84  failures+=run+' failed to connect to the database\n'
85  elif endid ==4:
86  failures+=run+' problem with database query\n'
87  elif endid ==5:
88  failures+=run+' failed to connect to hardware database\n'
89  elif endid ==6:
90  failures+=run+' problem with hardware database query\n'
91  elif endid ==7:
92  failures+=run+' dcm name in hdb out of range\n'
93  elif endid ==8:
94  failures+=run+' coating in hdb out of range\n'
95  elif endid ==9:
96  failures+=run+' failed to connect to production database\n'
97  elif endid ==10:
98  failures+=run+' dcm in production database out of range\n'
99  elif endid ==11:
100  failures+=run+' problem with production database query\n'
101  else:
102  print "Exit code out of range", endid
103  sys.exit(3)
104 
105 #Make a comprehensive output file
106 filename = resultpath+str(date2)+'_'+str(date1)+'.txt'
107 outfile = open(filename,'w')
108 outfile.write(failures+'\n')
109 for run in runs:
110  file = resultpath+run+'febshutoff_auto.txt'
111  if os.path.exists(file):
112  infile = open(file,'r')
113  for line in infile:
114  outfile.write(line)
115  infile.close()
116  outfile.write('\n')
117 outfile.close()
118 
119 #Now move it to the webarea
120 #webdir = '/nusoft/app/web/htdoc/nova/datacheck/febshutoff/'
121 #print "Moving text file to web area"
122 #shutil.copy(filename,webdir)
123 
procfile open("FD_BRL_v0.txt")