- Timestamp:
- Mar 5, 2012, 5:19:48 PM (14 years ago)
- Location:
- branches/meh_branches/ppstack_test
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
ippToPsps/jython/metrics.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/meh_branches/ppstack_test
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/ippToPsps/jython/metrics.py
r32112 r33415 1 1 import math 2 import time 2 3 import logging 3 4 import smtplib 4 5 import sys 5 6 import os 7 import datetime 6 8 7 from java.lang import Math 8 from org.apache.commons.math.special import Erf 9 from xml.etree.ElementTree import ElementTree, Element, tostring 10 11 from pslogger import PSLogger 12 from ipptopspsdb import IppToPspsDb 9 from ipptopsps import IppToPsps 10 from czardb import CzarDb 13 11 from gpc1db import Gpc1Db 14 12 15 13 ''' 16 M akes a gnuplot of the data14 Metrics class 17 15 ''' 18 def plotMe(batchType, file):16 class Metrics(IppToPsps): 19 17 20 OUTPUTFILE = "plots/" + DVOLABEL + "_" + batchType + ".png" 21 f=os.popen('gnuplot', 'w') 18 ''' 19 Constructor 20 ''' 21 def __init__(self, argv): 22 super(Metrics, self).__init__(argv) 22 23 23 if 0: 24 TERM = "X11" 25 else: 26 TERM = "png font \"/usr/share/fonts/corefonts/arial.ttf\" 8" 24 if len(sys.argv) > 2: self.parsePollTimeArg(sys.argv[2]) 25 26 # create database objects 27 self.czarDb = CzarDb(self.logger, self.config) 28 self.gpc1Db = Gpc1Db(self.logger, self.config) 29 30 ''' 31 Run... 32 ''' 33 def run(self): 34 35 while True: 36 37 self.logger.infoTitle("ippToPsps loading summary") 38 now = datetime.datetime.now() 39 self.logger.infoPair("Time now", now.strftime("%Y-%m-%d %H:%M:%S")) 40 self.logger.infoPair("Loading epoch", self.config.epoch) 41 self.logger.infoPair("DVO label", self.config.dvoLabel) 42 self.config.printBoxCoords() 27 43 28 print >> f, "set term " + TERM + "; \ 29 set output \"" + OUTPUTFILE + "\"; \ 30 set title \"ippToPsps : " + batchType + " loading for " + DVOLABEL + "\"; \ 31 set boxwidth; \ 32 set xtic rotate by -90 scale 0; \ 33 set style data histogram; \ 34 set style histogram rowstacked; \ 35 set style fill solid border -1; \ 36 set ylabel \"Items processed per loading stage\"; \ 37 set boxwidth 0.75; \ 38 plot '" + file + "' using 2:xtic(1) notitle lt 2, '' using 3 notitle lt 1, '' using 4 notitle lt rgb \"#808080\"" 44 self.logger.info("+----+------------------+---------------+-------------------+------------------+----------------+") 45 self.logger.info("|Type| batches per hour | last 24 hours | per day this week | total detections | last published |") 46 self.logger.info("+----+------------------+---------------+-------------------+------------------+----------------+") 47 rateP2 = self.printStats("P2") 48 rateST = self.printStats("ST") 49 self.logger.info("+----+------------------+---------------+-------------------+------------------+----------------+") 50 51 self.logger.info("") 52 53 stages = self.ippToPspsDb.getStages() 54 colCount = len(stages) 55 self.writeTableSeparator(colCount) 56 sys.stdout.write("|Type| DVO ") 57 58 for stage in stages: sys.stdout.write("|%19s" % stage) 59 sys.stdout.write("|\n") 60 sys.stdout.write("| | ") 61 for stage in stages: sys.stdout.write("| Pend Succ Fail ") 62 sys.stdout.write("|\n") 63 self.writeTableSeparator(colCount) 64 pendP2Processed = self.printTableRow(stages, "P2") 65 pendSTProcessed = self.printTableRow(stages, "ST") 66 self.writeTableSeparator(colCount) 39 67 40 f.flush() 68 self.logger.info("") 69 try: self.logger.infoPair("Estimated time for P2s", "%.1f hours" % (pendP2Processed / rateP2)) 70 except: pass 71 try: self.logger.infoPair("Estimated time for STs", "%.1f hours" % (pendSTProcessed / rateST)) 72 except: pass 73 74 self.checkClientStatus() 75 if not self.waitForPollTime(): break 41 76 42 '''43 Prepares a string of the format:44 77 45 n-processed(n-pending) 78 ''' 79 Formats an int to a string 80 ''' 81 def getIntAsString(self, value): 82 if value == 0: return "" 83 return str(value) 84 85 ''' 86 Prints (to the log) one row of data (one row being one batch-type) 87 ''' 88 def printTableRow(self, stages, batchType): 89 90 # get a master list of IDs in DVO for this batch type 91 #allIDs = self.gpc1Db.getIDsInThisDVODbForThisStage(self.config.dvoLabel, batchType, self.config.minRa, self.config.maxRa, self.config.minDec, self.config.maxDec) 92 allIDs = self.gpc1Db.getIDsInThisDVODbForThisStage(self.config.dvoLabel, batchType) 93 prevList = allIDs 94 numEverything = len(allIDs) 95 96 sys.stdout.write("| %2s |%6d " % (batchType, numEverything)) 97 98 tempFilename = batchType + '.dat' 99 DATFILE = open(tempFilename,'w') 100 print >> DATFILE, "DVO", numEverything, 0, 0 101 for stage in stages: 102 103 # get lists. Use unions with prev list to make sure the right stuff is included 104 success = list(set(self.ippToPspsDb.getStageIDs(batchType, self.config.epoch, self.config.dvoLabel, stage, 1)) & set(prevList)) 105 fail = list(set(self.ippToPspsDb.getStageIDs(batchType, self.config.epoch, self.config.dvoLabel, stage, -1)) & set(prevList) - set(success)) 106 pending = list(set(prevList) - set(success) - set(fail)) 107 108 # count stuff 109 numSuccess = len(success) 110 numFail = len(fail) 111 numPending = len(pending) 112 113 #if stage == 'loaded_to_datastore' and batchType == "P2": 114 # for n in fail: print n 115 116 str = "\033[1;34m%5s\033[1;m \033[1;32m%5s\033[1;m \033[1;31m%5s\033[1;m " % (self.getIntAsString(numPending), self.getIntAsString(numSuccess), self.getIntAsString(numFail)) 117 sys.stdout.write("|%-19s" % str) 118 119 prevList = success 120 print >> DATFILE, stage, numSuccess, numFail, numPending 121 122 self.czarDb.insertStats(stage, self.config.dvoLabel, batchType, numPending, numSuccess, numFail) 123 124 if stage == 'processed': numPendingProcessed = numPending 125 126 sys.stdout.write("|\n") 127 DATFILE.close() 128 129 return numPendingProcessed 130 131 ''' 132 Writes table separator 133 ''' 134 def writeTableSeparator(self, colCount): 135 136 sys.stdout.write("+----+-------") 137 for i in range(0, colCount): sys.stdout.write("+-------------------") 138 sys.stdout.write("+\n") 139 140 ''' 141 Prints various timing stats for this batch type 142 ''' 143 def printStats(self, batchType): 144 145 rate = self.ippToPspsDb.countBatchesInLastPeriod(batchType, self.config.epoch, self.config.dvoLabel, "1 HOUR") / 1.0 146 147 self.logger.info("| %2s | %16.1f | %13d | %17.1f | %16d | %14s |" % 148 (batchType, 149 rate, 150 self.ippToPspsDb.countBatchesInLastPeriod(batchType, self.config.epoch, self.config.dvoLabel, "24 HOUR"), 151 self.ippToPspsDb.countBatchesInLastPeriod(batchType, self.config.epoch, self.config.dvoLabel, "1 WEEK") / 7.0, 152 self.ippToPspsDb.getTotalDetectionsPublished(batchType, self.config.epoch, self.config.dvoLabel), 153 self.ippToPspsDb.getTimeOfLastBatchPublished(batchType, self.config.epoch, self.config.dvoLabel))) 154 155 return rate 156 46 157 47 ''' 48 def prepareStr(masterList, prevList, thisList, FILE, name): 158 ''' 159 Overrides base-class version 160 ''' 161 def printUsage(self): 162 super(Metrics, self).printUsage() 163 print " [hours]" 49 164 50 unsuccessful = list(set(prevList) - set(thisList))51 165 52 countTotal = len(masterList)53 countSuccessful = len(thisList)54 countUnsuccessful = len(unsuccessful)55 56 str = "%d" % countSuccessful57 if countUnsuccessful: str = str + "(%d)" % countUnsuccessful58 59 print >> FILE, name, countSuccessful, countUnsuccessful, (countTotal - countSuccessful - countUnsuccessful)60 #;sys.stdout.softspace=0;61 return str62 63 '''64 Prints (to the log) one row of data (one row being one batch-type)65 '''66 def printTableRow(batchType):67 68 allIDs = gpc1Db.getIDsInThisDVODbForThisStage(DVOLABEL, batchType)69 processedIDs = ippToPspsDb.getProcessedIDs(batchType, EPOCH, DVOLABEL)70 datastoreIDs = ippToPspsDb.getloadedToDatastoreIDs(batchType, EPOCH, DVOLABEL)71 loadedToODMIDs = ippToPspsDb.getLoadedToODMIDs(batchType, EPOCH, DVOLABEL)72 mergeWorthyIDs = ippToPspsDb.getMergeWothyIDs(batchType, EPOCH, DVOLABEL)73 mergedIDs = ippToPspsDb.getMergedIDs(batchType, EPOCH, DVOLABEL)74 75 tempFilename = batchType + '.dat'76 DATFILE = open(tempFilename,'w')77 prepareStr(allIDs, allIDs, allIDs, DATFILE, "inDVO"),78 79 logger.info("| %2s | %-12d | %-12s | %-12s | %-12s | %-12s | %-12s | %-12s | %-19s |" % (80 batchType,81 len(allIDs),82 prepareStr(allIDs, allIDs, processedIDs, DATFILE, "processed"),83 prepareStr(allIDs, processedIDs, datastoreIDs, DATFILE, "onDatastore"),84 prepareStr(allIDs, datastoreIDs, loadedToODMIDs, DATFILE, "loadedToODM"),85 prepareStr(allIDs, loadedToODMIDs, mergeWorthyIDs, DATFILE, "MergeWorthy"),86 prepareStr(allIDs, mergeWorthyIDs, mergedIDs, DATFILE, "Merged"),87 len(list(set(allIDs) - set(mergedIDs))),88 ippToPspsDb.getTimeOfLastBatchPublished(batchType, EPOCH, DVOLABEL)))89 90 plotMe(batchType, tempFilename)91 DATFILE.close()92 #os.remove(tempFilename)93 166 94 167 ''' 95 168 Start of program. 96 169 ''' 97 if len(sys.argv) > 1: CONFIG = sys.argv[1] 98 else: 99 print "** Usage: " + sys.argv[0] + " <configPath>" 100 sys.exit(1) 101 102 # open config file 103 configDoc = ElementTree(file=CONFIG) 104 105 logging.setLoggerClass(PSLogger) 106 logger = logging.getLogger("metrics") 107 logger.setup(configDoc, "metrics") 108 109 ippToPspsDb = IppToPspsDb(logger, configDoc) 110 gpc1Db = Gpc1Db(logger, configDoc) 111 112 DVOLABEL = configDoc.find("dvo/gpc1Label").text 113 EPOCH = configDoc.find("options/epoch").text 114 115 logger.infoTitle("ippToPsps loading summary") 116 logger.infoPair("Loading epoch", EPOCH) 117 logger.infoPair("DVO label", DVOLABEL) 118 logger.info("") 119 logger.info("+------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------------+") 120 logger.info("| Type | Total in DVO | Processed | On datastore | Loaded to ODM | Merge-worthy | Merged | Total missing | Last published |") 121 logger.info("+------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------------+") 122 printTableRow("P2") 123 printTableRow("ST") 124 logger.info("+------+----------------+----------------+----------------+----------------+----------------+----------------+----------------+----------------------+") 170 metrics = Metrics(sys.argv) 171 metrics.run() 172 metrics.exitProgram("finished") 125 173 126 174
Note:
See TracChangeset
for help on using the changeset viewer.
