IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 6, 2011, 11:00:22 AM (15 years ago)
Author:
eugene
Message:

merge changes from trunk

Location:
branches/eam_branches/ipp-20110710/ippToPsps/jython
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20110710/ippToPsps/jython

    • Property svn:ignore set to
      *.class
  • branches/eam_branches/ipp-20110710/ippToPsps/jython/ipptopspsdb.py

    r31842 r32337  
    5757
    5858    '''
    59     TODO
    60     '''
    61     def getUnprocessedIDsForThisStage(self, dvoDb, batchType, startDate):
    62 
    63         if batchType == "P2": # TODO define these someplace
    64 
    65             stage = "cam"
    66             sql = "SELECT DISTINCT stage_id \
    67                    FROM gpc1.addRun \
    68                    WHERE stage = '" + stage + "' \
    69                    AND dvodb = '" + dvoDb + "' \
    70                    AND stage_id NOT IN "
    71 
    72         elif batchType == "ST":
    73 
    74             stage = "staticsky"
    75             sql = "SELECT DISTINCT stack_id \
    76                    FROM gpc1.staticskyInput \
    77                    JOIN gpc1.addRun ON(gpc1.staticskyInput.sky_id = gpc1.addRun.stage_id) \
    78                    WHERE stage = '" + stage + "' \
    79                    AND dvodb = '" + dvoDb + "' \
    80                    AND stack_id NOT IN "
    81 
    82         sql = sql + "(SELECT stage_id \
    83               FROM batch \
    84               WHERE batch_type = '" + batchType + "' \
    85               AND timestamp > '" + startDate + "' \
    86               AND loaded_to_datastore)"
     59    Returns a list of processed batch IDs that are merged but not yet deleted
     60    '''
     61    def getLoadedToODMButNotDeletedBatchIDs(self, batchType, epoch, dvoGpc1Label, column):
     62
     63        sql = "SELECT DISTINCT batch_id \
     64               FROM batch \
     65               WHERE timestamp > '" + epoch + "' \
     66               AND batch_type = '" + batchType + "' \
     67               AND dvo_db = '" + dvoGpc1Label + "' \
     68               AND loaded_to_ODM != 0 \
     69               AND " + column + " = 0"
    8770
    8871        ids = []
     
    9275                ids.append(rs.getInt(1))
    9376        except:
    94             self.logger.exception("Can't query for ids in DVO")
     77            self.logger.exception("Can't query for merged batch ids in ipptopsps Db")
    9578
    9679        rs.close()
    9780
    98         self.logger.debug("Found %d unprocessed items in DVO database '%s' for stage='%s'" % (len(ids), dvoDb, stage))
     81        self.logger.debug("Found %d merged but un-deleted items" % len(ids))
    9982
    10083        return ids
    10184
    10285    '''
    103     TODO
    104     '''
    105     def getLastBatchPublished(self, batchType):
    106 
    107         sql = "SELECT TIMESTAMPDIFF(MINUTE, timestamp, now())/60.0 \
     86    Returns a list of processed batch IDs that are merged but not deleted from local disk
     87    '''
     88    def getLoadedToODMButNotDeletedFromLocalDisk(self, batchType, epoch, dvoGpc1Label):
     89        return self.getLoadedToODMButNotDeletedBatchIDs(batchType, epoch, dvoGpc1Label, "deleted_local")
     90
     91    '''
     92    Returns a list of processed batch IDs that are merged but not deleted from datastore
     93    '''
     94    def getLoadedToODMButNotDeletedFromDatastore(self, batchType, epoch, dvoGpc1Label):
     95        return self.getLoadedToODMButNotDeletedBatchIDs(batchType, epoch, dvoGpc1Label, "deleted_datastore")
     96
     97    '''
     98    Returns a list of processed batch IDs that are merged but not deleted from DXLayer
     99    '''
     100    def getLoadedToODMButNotDeletedFromDXLayer(self, batchType, epoch, dvoGpc1Label):
     101        return self.getLoadedToODMButNotDeletedBatchIDs(batchType, epoch, dvoGpc1Label, "deleted_dxlayer")
     102
     103
     104    '''
     105    Returns a list of processed batch IDs that are not loaded to the datastore
     106    '''
     107    def getProcessedButFailedDatastoreBatchIDs(self, epoch, dvoGpc1Label, batchType):
     108
     109        sql = "SELECT DISTINCT batch_id \
     110               FROM batch \
     111               WHERE timestamp > '" + epoch + "' \
     112               AND batch_type = '" + batchType + "' \
     113               AND dvo_db = '" + dvoGpc1Label + "' \
     114               AND processed = 1\
     115               AND loaded_to_datastore = -1"
     116
     117        ids = []
     118        try:
     119            rs = self.executeQuery(sql)
     120            while (rs.next()):
     121                ids.append(rs.getInt(1))
     122        except:
     123            self.logger.exception("Can't query for processed batch ids not loaded to datastore ipptopsps Db")
     124
     125        rs.close()
     126
     127        self.logger.debug("Found %d processed but-not-on-datastore items" % len(ids))
     128
     129        return ids
     130
     131    '''
     132    Returns a list of processed batch IDs that have not yet been loaded to the ODM
     133    '''
     134    def getBatchIDsUnloadedToODM(self, epoch, dvoGpc1Label):
     135
     136        sql = "SELECT DISTINCT batch_id \
     137               FROM batch \
     138               WHERE timestamp > '" + epoch + "' \
     139               AND dvo_db = '" + dvoGpc1Label + "' \
     140               AND loaded_to_datastore = 1 \
     141               AND loaded_to_ODM = 0"
     142
     143
     144        ids = []
     145        try:
     146            rs = self.executeQuery(sql)
     147            while (rs.next()):
     148                ids.append(rs.getInt(1))
     149        except:
     150            self.logger.exception("Can't query for unloaded batch ids in ipptopsps Db")
     151
     152        rs.close()
     153
     154        self.logger.debug("Found %d unloaded items" % len(ids))
     155
     156        return ids
     157
     158    '''
     159    Returns a list of processed batch IDs that have not failed to load and are not yet merged, for this epoch and dvo label
     160    '''
     161    def getUnfinishedBatchIDs(self, epoch, dvoGpc1Label):
     162
     163        sql = "SELECT DISTINCT batch_id \
     164               FROM batch \
     165               WHERE timestamp > '" + epoch + "' \
     166               AND dvo_db = '" + dvoGpc1Label + "' \
     167               AND loaded_to_datastore = 1 \
     168               AND merged != 1 \
     169               AND loaded_to_ODM != -1"
     170
     171
     172        ids = []
     173        try:
     174            rs = self.executeQuery(sql)
     175            while (rs.next()):
     176                ids.append(rs.getInt(1))
     177        except:
     178            self.logger.exception("Can't query for unfinished batch ids in ipptopsps Db")
     179
     180        rs.close()
     181
     182        self.logger.debug("Found %d unfinished items" % len(ids))
     183
     184        return ids
     185
     186    '''
     187    Returns a list of batch IDs for this epoch and dvo label for which the specified column is set to value
     188    '''
     189    def getBatchIDs(self, epoch, dvoGpc1Label, column, value):
     190
     191        sql = "SELECT DISTINCT batch_id \
     192               FROM batch \
     193               WHERE timestamp > '" + epoch + "' \
     194               AND dvo_db = '" + dvoGpc1Label + "' \
     195               AND " + column + " = " + str(value)
     196
     197        ids = []
     198        try:
     199            rs = self.executeQuery(sql)
     200            while (rs.next()):
     201                ids.append(rs.getInt(1))
     202        except:
     203            self.logger.exception("Can't query for batch ids with " + column + " = " + str(value) + " in ipptopsps Db")
     204
     205        rs.close()
     206
     207        self.logger.debug("Found %d batches with %s = %d" % (len(ids), column, value))
     208
     209        return ids
     210
     211    '''
     212    Returns a list of IDs for this batch type, epoch and dvo label (i.e. either
     213            cam_ids or stack_ids) for which the specified column is set to value
     214    '''
     215    def getStageIDs(self, batchType, epoch, dvoGpc1Label, column, value):
     216
     217        sql = "SELECT DISTINCT stage_id \
    108218               FROM batch \
    109219               WHERE batch_type = '" + batchType + "' \
    110                AND loaded_to_datastore \
     220               AND timestamp > '" + epoch + "' \
     221               AND dvo_db = '" + dvoGpc1Label + "' \
     222               AND " + column + " = " + str(value)
     223
     224        ids = []
     225        try:
     226            rs = self.executeQuery(sql)
     227            while (rs.next()):
     228                ids.append(rs.getInt(1))
     229        except:
     230            self.logger.exception("Can't query for ids with " + column + " = " + str(value) + " in ipptopsps Db")
     231
     232        rs.close()
     233
     234        self.logger.debug("Found %d batches with %s = 1 for batch type='%s'" % (len(ids), column, batchType))
     235
     236        return ids
     237
     238    '''
     239    Series of getter methods that utilise getBatchIDs() method above
     240    '''
     241    def getloadedToDatastoreBatchIDs(self, epoch, dvoGpc1Label):
     242        return self.getBatchIDs(epoch, dvoGpc1Label, "loaded_to_datastore", 1)
     243    def getFailedLoadedToODMBatchIDs(self, epoch, dvoGpc1Label):
     244        return self.getBatchIDs(epoch, dvoGpc1Label, "loaded_to_ODM", -1)
     245
     246    '''
     247    Series of getter methods that utilise getStageIDs() method above
     248    '''
     249    def getProcessedIDs(self, batchType, epoch, dvoGpc1Label):
     250        return self.getStageIDs(batchType, epoch, dvoGpc1Label, "processed", 1)
     251    def getloadedToDatastoreIDs(self, batchType, epoch, dvoGpc1Label):
     252        return self.getStageIDs(batchType, epoch, dvoGpc1Label, "loaded_to_datastore", 1)
     253    def getLoadedToODMIDs(self, batchType, epoch, dvoGpc1Label):
     254        return self.getStageIDs(batchType, epoch, dvoGpc1Label, "loaded_to_ODM", 1)
     255    def getMergeWorthyIDs(self, batchType, epoch, dvoGpc1Label):
     256        return self.getStageIDs(batchType, epoch, dvoGpc1Label, "merge_worthy", 1)
     257    def getMergedIDs(self, batchType, epoch, dvoGpc1Label):
     258        return self.getStageIDs(batchType, epoch, dvoGpc1Label, "merged", 1)
     259
     260    '''
     261    Returns the time (as a string) that the last batch was published to the datastore for
     262    this epoch, dvo label and batch type
     263    '''
     264    def getTimeOfLastBatchPublished(self, batchType, epoch, dvoGpc1Label):
     265
     266        minutes = None
     267
     268        sql = "SELECT TIMESTAMPDIFF(MINUTE, timestamp, now()) \
     269               FROM batch \
     270               WHERE batch_type = '" + batchType + "' \
     271               AND loaded_to_datastore = 1 \
     272               AND timestamp > '" + epoch + "' \
     273               AND dvo_db = '" + dvoGpc1Label + "' \
    111274               ORDER BY timestamp DESC LIMIT 1"
    112275
    113276        try:
    114277            rs = self.executeQuery(sql)
    115             rs.first()
    116             hours = rs.getFloat(1)
     278            if rs.first(): minutes = rs.getFloat(1)
    117279        except:
    118280            self.logger.exception("Unable to get last batch published")
    119281
    120         return hours
     282        if not minutes: return "Never"
     283
     284        hours = minutes/60.0
     285        days = hours/24.0
     286        weeks = days/7.0
     287
     288        if minutes < 60: return "%.1f minutes ago" % minutes
     289        elif hours < 48: return "%.1f hours ago" % hours
     290        elif days < 7: return "%.1f days ago" % days
     291        return "%.1f weeks ago" % weeks
    121292
    122293    '''
     
    172343        return bpm
    173344
    174 
    175     '''
    176     TODO
    177     '''
    178     def getTotalFailedBatches(self, batchType, startTime, endTime=""):
    179 
    180         sql = "SELECT COUNT(DISTINCT stage_id) \
    181                FROM batch \
    182                WHERE timestamp > '" + startTime + "' \
    183                AND batch_type = '" + batchType + "' \
    184                AND !processed \
    185                AND stage_id NOT IN \
    186                (SELECT DISTINCT stage_id \
    187                 FROM batch \
    188                 WHERE timestamp > '" + startTime + "'  \
    189                 AND batch_type = '" + batchType + "' \
    190                 AND loaded_to_datastore)"
    191 
    192         try:
    193             rs = self.executeQuery(sql)
    194             rs.first()
    195             total = rs.getInt(1)
    196         except:
    197             self.logger.exception("Unable to count failed batches")
    198 
    199         return total
    200     '''
    201     TODO
    202     '''
    203     def getTotalBatchesPublished(self, batchType, startTime, endTime=""):
    204 
    205         sql = "SELECT COUNT(*) \
    206                FROM batch \
    207                WHERE timestamp > '" + startTime + "' \
    208                AND batch_type = '" + batchType + "' \
    209                AND loaded_to_datastore"
    210 
    211         try:
    212             rs = self.executeQuery(sql)
    213             rs.first()
    214             total = rs.getInt(1)
    215         except:
    216             self.logger.exception("Unable to count batches")
    217 
    218         return total
    219 
    220345    '''
    221346    Updates min/max object ID on this table and batch
    222347    '''
    223     def updateMinMaxObjID(self, batchID, minObjID, maxObjID):
     348    def updateDetectionStats(self, batchID, minObjID, maxObjID, totalDetections):
    224349
    225350        sql = "UPDATE batch SET \
    226351               min_obj_id = " + str(minObjID) + ", \
    227                max_obj_id = " + str(maxObjID) + " \
     352               max_obj_id = " + str(maxObjID) + ", \
     353               total_detections = " + str(totalDetections) + " \
    228354               WHERE batch_id = " + str(batchID)
    229355
     
    231357
    232358    '''
     359    Updates a batch-table column
     360    '''
     361    def updateColumn(self, batchID, column, value):
     362
     363        sql = "UPDATE batch \
     364               SET " + column + " = " + str(value) + " \
     365               WHERE batch_id = " + str(batchID)
     366
     367        self.execute(sql)
     368
     369    '''
    233370    Updates batch processed field
    234371    '''
    235372    def updateProcessed(self, batchID, processed):
    236 
     373        self.updateColumn(batchID, "processed", processed)
     374
     375    '''
     376    Updates batch deleted local field
     377    '''
     378    def updateDeletedLocal(self, batchID, value):
     379        self.updateColumn(batchID, "deleted_local", value)
     380
     381    '''
     382    Updates batch deleted datastore field
     383    '''
     384    def updateDeletedDatastore(self, batchID, value):
     385        self.updateColumn(batchID, "deleted_datastore", value)
     386
     387    '''
     388    Updates batch deleted dxlayer field
     389    '''
     390    def updateDeletedDxlayer(self, batchID, value):
     391        self.updateColumn(batchID, "deleted_dxlayer", value)
     392
     393    '''
     394    Updates batch loaded_to_datastore field
     395    '''
     396    def updateLoadedToDatastore(self, batchID, loadedToDatastore):
     397        self.updateColumn(batchID, "loaded_to_datastore", loadedToDatastore)
     398
     399    '''
     400    Updates ODM status for this batch
     401    '''
     402    def updateOdmStatus(self, batchID, odmStatus):
     403
     404        if odmStatus['LOADFAILED'] == 1: loadedToODM = -1
     405        else: loadedToODM = odmStatus['LOADEDTOODM']
    237406        sql = "UPDATE batch \
    238                SET processed = " + str(processed) + " \
     407               SET loaded_to_ODM = " + str(loadedToODM) + ", \
     408               merge_worthy = " + str(odmStatus['MERGEWORTHY']) + ", \
     409               merged = " + str(odmStatus['MERGED']) + " \
    239410               WHERE batch_id = " + str(batchID)
    240411
     
    242413
    243414    '''
    244     Updates batch loaded_to_datastore field
    245     '''
    246     def updateLoadedToDatastore(self, batchID, loadedToDatastore):
    247 
    248         sql = "UPDATE batch \
    249                SET loaded_to_datastore = " + str(loadedToDatastore) + " \
    250                WHERE batch_id = " + str(batchID)
    251 
    252         self.execute(sql)
    253 
    254     '''
    255415    Have we already processed and published this batch?
    256416    '''
    257     def alreadyProcessed(self, stage_id):
     417    def alreadyProcessed(self, batchType, stage_id, epoch, dvoGpc1Label):
    258418
    259419        sql = "SELECT COUNT(*) \
    260420               FROM batch \
    261421               WHERE stage_id = " + str(stage_id) + " \
    262                AND processed \
    263                AND loaded_to_datastore"
     422               AND batch_type = '" + batchType + "' \
     423               AND timestamp > '" + epoch + "' \
     424               AND dvo_db = '" + dvoGpc1Label + "' \
     425               AND processed = 1 \
     426               AND loaded_to_datastore != 0"
    264427
    265428        try:
     
    267430            rs.first()
    268431            if rs.getInt(1) > 0:
    269                 self.logger.error("Batch with stage_id = "+str(stage_id) + " has already been processed and published to datastore")
     432                self.logger.errorPair(str(stage_id) + " has already been published", "skipping")
    270433                return True
    271434            else:
     
    273436        except:
    274437            self.logger.exception("Unable to check whether this batch has already been processed")
     438
     439    '''
     440    Has this stage_id consistently failed to process?
     441    '''
     442    def consistentlyFailed(self, batchType, stage_id, epoch, dvoGpc1Label, count):
     443
     444        sql = "SELECT COUNT(*) \
     445               FROM batch \
     446               WHERE stage_id = " + str(stage_id) + " \
     447               AND batch_type = '" + batchType + "' \
     448               AND timestamp > '" + epoch + "' \
     449               AND dvo_db = '" + dvoGpc1Label + "' \
     450               AND processed = -1"
     451
     452        try:
     453            rs = self.executeQuery(sql)
     454            rs.first()
     455            if rs.getInt(1) >= count:
     456                self.logger.errorPair(str(stage_id) + " has failed %d times" % count, "skipping")
     457                return True
     458            else:
     459                return False
     460        except:
     461            self.logger.exception("Unable to check whether this batch has consistently failed")
    275462
    276463     
Note: See TracChangeset for help on using the changeset viewer.