allTimeWatchdog.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 import os, sys, shutil
3 import sys, datetime
4 import os
5 # all of root
6 from ROOT import *
7 # My utilities
8 import canMan, HTMLTools as html
9 import psycopg2
10 from WatchdogFx import *
11 from collections import OrderedDict
12 
13 os.umask(002)
14 
15 webDir = "/nusoft/app/web/htdoc/nova/datacheck/processinghistory/"
16 tmpWebDir = "/nusoft/app/web/htdoc/nova/datacheck/processinghistory_test/"
17 
18 index = os.path.join(pubContext, "Commissioning", "Watchdog", "index.html")
19 
20 if not os.path.isdir(tmpWebDir):
21  os.mkdir(tmpWebDir)
22 
23 
24 thumbSize = "400"
25 endl = '\n'
26 
27 
28 intervalLength = 7
29 
30 fullNames = OrderedDict()
31 fullNames["fardet"] = "Far Detector"
32 fullNames["neardet"] = "Near Detector"
33 
34 outputDirs = OrderedDict()
35 for detector in fullNames.keys():
36  outputDirs[detector] = os.path.join(tmpWebDir, detector)
37  if not os.path.isdir(outputDirs[detector]):
38  os.mkdir(outputDirs[detector])
39 
40 
41 boundaries = OrderedDict()
42 timeStamps = OrderedDict()
43 timeStrs = OrderedDict()
44 for detector in fullNames.keys():
45  allTimeMinRun = fetchTable(db[detector].cur, "select min(run) from "+detector+".runs")[0][0]
46  allTimeMaxRun = fetchTable(db[detector].cur, "select max(run) from "+detector+".runs")[0][0]
47  lastRun = 999999999
48  lastTimeStamp = fetchTable(db[detector].cur, "select CURRENT_TIMESTAMP;")[0][0]
49  print lastTimeStamp, type(lastTimeStamp), str(lastTimeStamp)
50  boundaries[detector] = [allTimeMaxRun]
51  timeStamps[detector] = [fetchTable(db[detector].cur, "select CURRENT_TIMESTAMP;")[0][0]]
52  nIntervals = 1
53  timeStrs[detector] = []
54  while lastRun > allTimeMinRun:
55  minRunInInterval = fetchTable(db[detector].cur, "select min(run) from "+detector+".runs where tstart >= (CURRENT_TIMESTAMP - interval '"+str(intervalLength * nIntervals) +"' day);")[0][0]
56  minTimeInInterval = fetchTable(db[detector].cur, "select tstart from "+detector+".runs where run = " + str(minRunInInterval))[0][0]
57  timeStrs[detector].append(str(minTimeInInterval.strftime("%Y/%m/%d, %H:%M") + " - " + lastTimeStamp.strftime("%Y/%m/%d, %H:%M") + '\n'))
58  lastRun = minRunInInterval
59  lastTimeStamp = minTimeInInterval
60  boundaries[detector].append(minRunInInterval)
61  timeStamps[detector].append(minTimeInInterval)
62  nIntervals += 1
63 
64  print boundaries[detector]
65  print timeStamps[detector]
66  print len(boundaries[detector]), len(timeStamps[detector])
67 
68 
69 
70 
71 for detector in fullNames.keys():
72  historyPage = open(os.path.join(outputDirs[detector], "history.html"), 'w')
73  historyPage.write(html.preventCaching())
74 
75  for i in range(1, len(boundaries[detector])):
76  historyPage.write(html.heading2(timeStrs[detector][i-1].strip()))
77  historyPage.write('<table border=0 class="fixed" cellpadding="20">')
78  print i
79  maxRun = boundaries[detector][i-1]
80  minRun = boundaries[detector][i]
81  onMonRunsHist = getCompletedRunsHist(onMonDirs[detector], "OnMon Files", maxRun, minRun, lineStyle=7, lineColor=onMonColor, additionalDir=onMonTriggerDirs[detector])
82  dbRunsHist = getDBRunsHist(minRun, maxRun, detector, lineStyle=1, lineColor=dbColor)
83 
84  for key, path in paths.items():
85  if not detector in key:
86  continue
87  plotName = path.name+ str(i).zfill(3) + ".png"
88  (can,hists) = path.getPlotCanvas(maxRun, minRun, dbRunsHist, onMonRunsHist)
89  can.SaveAs(os.path.join(outputDirs[detector], plotName))
90  historyPage.write('<td> <center>'+ html.heading3(path.title) + html.link('<img src="' + plotName + '" width=' + thumbSize + 'px style="border-style: none"/ >', plotName) + '</center></td>' ) #opens table entry
91 
92  historyPage.write("</tr>") #closes table row
93  historyPage.write("</table>" + endl)
94 
95 
96  historyPage.close()
97 
98 shutil.rmtree(webDir)
99 shutil.move(tmpWebDir, webDir)
100 
101 
102 shutil.copy(index, webDir)
103 shutil.copy(index, os.path.join(webDir, "history.html"))
def fetchTable(cur, query)
void append()
Definition: append.C:24
procfile open("FD_BRL_v0.txt")
def getCompletedRunsHist(path, name, maxRunNum, minRunNum, testStr="", skipStr=None, additionalDir="", checkKeys=False, printRunNum=False, lineStyle=1, lineColor=kBlack)
def getDBRunsHist(minRun, maxRun, table="testbeam", lineStyle=1, lineColor=dbColor)