IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33354 for trunk/ippToPsps


Ignore:
Timestamp:
Feb 23, 2012, 4:16:51 PM (14 years ago)
Author:
rhenders
Message:

lots of new methods access new tables for clients, boxes and pending

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippToPsps/jython/ipptopspsdb.py

    r33293 r33354  
    3838        try:
    3939            rs = self.executeQuery(sql)
    40             while (rs.next()):
    41                 ids.append(rs.getInt(1))
     40            while (rs.next()): ids.append(rs.getInt(1))
     41            rs.close()
    4242        except:
    4343            self.logger.exception("Can't query for merged batch ids in ipptopsps Db")
    44 
    45         rs.close()
    4644
    4745        self.logger.debug("Found %d merged but un-deleted items" % len(ids))
     
    6563        try:
    6664            rs = self.executeQuery(sql)
    67             while (rs.next()):
    68                 ids.append(rs.getInt(1))
     65            while (rs.next()): ids.append(rs.getInt(1))
     66            rs.close()
    6967        except:
    7068            self.logger.exception("Can't query for merged batch ids in ipptopsps Db")
    7169
    72         rs.close()
    7370
    7471        self.logger.debug("Found %d merged but un-deleted items" % len(ids))
     
    9289        try:
    9390            rs = self.executeQuery(sql)
    94             while (rs.next()):
    95                 ids.append(rs.getInt(1))
     91            while (rs.next()): ids.append(rs.getInt(1))
     92            rs.close()
    9693        except:
    9794            self.logger.exception("Can't query for merged batch ids in ipptopsps Db")
    98 
    99         rs.close()
    10095
    10196        self.logger.debug("Found %d merged but un-deleted items" % len(ids))
     
    517512            rs.first()
    518513            if rs.getInt(1) > 0:
    519                 self.logger.errorPair(str(stage_id) + " is already being processed", "skipping")
     514                self.logger.debugPair(str(stage_id) + " is already being processed", "skipping")
    520515                return True
    521516            else:
     
    560555            rs.first()
    561556            if rs.getInt(1) > 0:
    562                 self.logger.errorPair(str(stage_id) + " has already been published", "skipping")
     557                self.logger.debugPair(str(stage_id) + " has already been published", "skipping")
    563558                return True
    564559            else:
     
    725720    Insert client
    726721    '''
    727     def insertClient(self, host, pid):
    728         self.execute("INSERT INTO clients (timestamp, host, pid) VALUES (now(), '" + host + "', " + str(pid) + ")")
     722    def insertClient(self, type, host, pid, config):
     723        self.execute("INSERT INTO clients (timestamp, type, host, pid, config) VALUES (now(), '" + type + "', '" + host + "', " + str(pid) + ", '" + config + "')")
    729724
    730725    '''
    731726    Update clients, or inserts it for the first time
    732727    '''
    733     def updateClient(self, host, pid):
    734 
    735         try:
    736             self.insertClient(host, pid)
     728    def updateClient(self, type, host, pid, config):
     729
     730        try:
     731            self.insertClient(type, host, pid, config)
    737732        except:
    738733            self.execute("UPDATE clients SET timestamp = now() WHERE host = '" + host + "' AND pid = " + str(pid))
     
    771766
    772767    '''
     768    Populates Config object
     769    '''
     770    def readConfig(self, host, pid):
     771
     772        # first get config defined for this client
     773        sql = "SELECT config \
     774               FROM clients \
     775               WHERE host = '" + host + "' \
     776               AND pid = " + str(pid)
     777
     778        try:
     779            rs = self.executeQuery(sql)
     780            rs.first()
     781            self.config.name = rs.getString(1)
     782        except:
     783            self.logger.errorPair("No config set for", "%s (pid=%d)" % (host, pid))
     784            self.config.isLoaded = False
     785            return False
     786
     787        # now load that config
     788        sql = "SELECT \
     789        datastore_product, \
     790        datastore_type, \
     791        datastore_publish, \
     792        dvo_label, \
     793        dvo_location, \
     794        min_ra, \
     795        max_ra, \
     796        min_dec, \
     797        max_dec, \
     798        box_size, \
     799        base_path, \
     800        data_release, \
     801        delete_local, \
     802        delete_datastore, \
     803        delete_dxlayer, \
     804        epoch, \
     805        survey, \
     806        psps_survey, \
     807        queue_P2, \
     808        queue_ST \
     809        FROM config \
     810        WHERE name = '" + self.config.name + "'"
     811
     812        self.config.batchTypes = []
     813        try:
     814            rs = self.executeQuery(sql)
     815            rs.first()
     816            self.config.datastoreProduct = rs.getString(1)
     817            self.config.datastoreType = rs.getString(2)
     818            if rs.getInt(3) == 1: self.config.datastorePublishing = True
     819            else: self.config.datastorePublishing = False
     820            self.config.dvoLabel = rs.getString(4)
     821            self.config.dvoLocation = rs.getString(5)
     822            self.config.minRa = rs.getDouble(6)
     823            self.config.maxRa = rs.getDouble(7)
     824            self.config.minDec = rs.getDouble(8)
     825            self.config.maxDec = rs.getDouble(9)
     826
     827            self.config.boxSize = rs.getDouble(10)
     828            self.config.halfBox = self.config.boxSize/2.0
     829            self.config.boxSizeWithBorder = self.config.boxSize + (self.config.BORDER * 2)
     830
     831            self.config.basePath = rs.getString(11)
     832            self.config.dataRelease = rs.getInt(12)
     833            self.config.deleteLocal = rs.getInt(13)
     834            self.config.deleteDatastore = rs.getInt(14)
     835            self.config.deleteDxLayer = rs.getInt(15)
     836            self.config.epoch = rs.getString(16)
     837            self.config.survey = rs.getString(17)
     838            self.config.pspsSurvey = rs.getString(18)
     839            if rs.getInt(19) == 1: self.config.batchTypes.append("P2")
     840            if rs.getInt(20) == 1: self.config.batchTypes.append("ST")
     841            self.config.force = False # TODO
     842            self.config.test = False # TODO
     843            self.config.isLoaded = True
     844        except:
     845            self.logger.errorPair("Could not read config with name", self.config.name)
     846            self.config.isLoaded = False
     847
     848        return self.config.isLoaded
     849
     850    '''
     851    Gets id of box with these coords
     852    '''
     853    def getBoxId(self, config, ra, dec, boxSide):
     854
     855        sql = "SELECT id FROM box \
     856               WHERE config  = '" + config + "' \
     857               AND ra_center = " + str(ra) + " \
     858               AND dec_center = " + str(dec) + " \
     859               AND box_side = " + str(boxSide)
     860
     861        id = -1
     862        try:
     863            rs = self.executeQuery(sql)
     864            rs.first()
     865            id = rs.getInt(1)
     866        except:
     867            pass
     868
     869        return id
     870
     871
     872    '''
     873    Remove all boxes and associated ids in pending table
     874    '''
     875    def removeAllBoxes(self, config):
     876        self.execute("DELETE FROM box WHERE config = '" + config + "'")
     877
     878    '''
     879    Removes a box and it's associated ids in the pending table
     880    '''
     881    def removeBox(self, config, ra, dec, boxSide):
     882        self.execute("DELETE FROM box \
     883                WHERE config = '" + config + "' \
     884                AND ra_center = " + str(ra) + " \
     885                AND dec_center = " + str(dec) + " \
     886                AND box_side = " + str(boxSide))
     887
     888    '''
     889    Inserts new box into box table. If it already exisits, it returns existing box id
     890    '''
     891    def insertBox(self, config, ra, dec, boxSide):
     892
     893        id = self.getBoxId(config, ra, dec, boxSide)
     894
     895        # does not already exists so insert
     896        if id < 0:
     897
     898            sql = "INSERT INTO box \
     899                   (config, ra_center, dec_center, box_side) \
     900                   VALUES \
     901                   ('" + config + "', " + str(ra) + ", " + str(dec) + ", " + str(boxSide) + ")"
     902
     903            self.execute(sql)
     904            id = self.getBoxId(config, ra, dec, boxSide)
     905
     906        return id
     907
     908    '''
     909    Inserts new box ids into the pending table
     910    '''
     911    def insertPending(self, box_id, batchType, ids):
     912
     913        # first delete old pending items
     914        self.execute("DELETE FROM pending WHERE box_id = " + str(box_id))
     915
     916        for id in ids:
     917            sql = "INSERT INTO pending \
     918               (box_id, batch_type, stage_id) \
     919               VALUES \
     920               (" + str(box_id) + ", '" + batchType + "', " + str(id) + ")"
     921
     922            self.execute(sql)
     923
     924    '''
     925    Returns ids for all boxes with pending items
     926    '''
     927    def getBoxIds(self, config):
     928
     929        sql = "SELECT DISTINCT id \
     930               FROM box \
     931               JOIN pending ON (id = box_id)  \
     932               WHERE config = '" + config + "' \
     933               ORDER BY ra_center, dec_center"
     934
     935        ids = []
     936        try:
     937            rs = self.executeQuery(sql)
     938            while (rs.next()): ids.append(rs.getInt(1))
     939            rs.close()
     940        except:
     941            self.logger.errorPair("Can't get boxes", sql)
     942
     943        return ids
     944
     945    '''
     946    Returns dimensions of this box
     947    '''
     948    def getBoxDimensions(self, boxId):
     949
     950        sql = "SELECT ra_center, dec_center, box_side FROM box WHERE id = " + str(boxId)
     951
     952        boxDim = {}
     953        rs = self.executeQuery(sql)
     954        rs.first()
     955
     956        boxDim['RA'] = rs.getFloat(1)
     957        boxDim['DEC'] = rs.getFloat(2)
     958        boxDim['SIDE'] = rs.getFloat(3)
     959
     960        return boxDim
     961
     962    '''
     963    Returns ids for pending items in this box for this batch type
     964    '''
     965    def getPendingIds(self, boxId, batchType):
     966
     967        sql = "SELECT stage_id FROM pending WHERE batch_type = '" + batchType + "' AND box_id = " + str(boxId)
     968
     969        ids = []
     970        try:
     971            rs = self.executeQuery(sql)
     972            while (rs.next()): ids.append(rs.getInt(1))
     973            rs.close()
     974        except:
     975            self.logger.errorPair("Can't get ids from this box", sql)
     976
     977        return ids
     978
     979    '''
     980    Returns max number of pensing items in a box for this config
     981    '''
     982    def getMaxPendingInBox(self, config):
     983
     984        sql = "SELECT MAX(pending) \
     985               FROM (SELECT COUNT(stage_id) AS pending \
     986                       FROM pending \
     987                       JOIN box ON (id = box_id) \
     988                       WHERE config = '" + config + "' \
     989                       GROUP BY box_id) AS t"
     990
     991        try:
     992            rs = self.executeQuery(sql)
     993            rs.first()
     994            return rs.getInt(1)
     995        except:
     996            self.logger.errorPair("Could not count max pending", sql)
     997            return 100
     998
     999    '''
     1000    Writes density-plot data to file for pending stuff over sky for given config
     1001    '''
     1002    def createPendingDensityPlotData(self, config, DATFILE):
     1003
     1004        sql = "SELECT ra_center, dec_center, COUNT(stage_id) \
     1005               FROM box \
     1006               LEFT JOIN pending ON (id = box_id) \
     1007               WHERE config = '" + config + "' \
     1008               GROUP BY id \
     1009               ORDER BY ra_center, dec_center"
     1010
     1011        try:
     1012            rs = self.executeQuery(sql)
     1013            while (rs.next()):
     1014                print >> DATFILE, rs.getFloat(1), rs.getFloat(2), rs.getInt(3)
     1015            rs.close()
     1016        except:
     1017            self.logger.errorPair("Can't get density plot data", sql)
     1018
     1019    '''
    7731020    Destructor
    7741021    '''
    7751022    def __del__(self):
    7761023
    777         self.logger.debug("IppToPspsDb Desstructor")
     1024        self.logger.debug("IppToPspsDb Destructor")
    7781025        self.stmt.close()
    7791026        self.con.close()
Note: See TracChangeset for help on using the changeset viewer.