IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 4, 2011, 3:20:38 PM (15 years ago)
Author:
watersc1
Message:

Merging trunk into this branch

Location:
branches/czw_branch/20110406
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20110406

  • branches/czw_branch/20110406/ippToPsps/jython/ipptopspsdb.py

    r31253 r31434  
    66import logging
    77
     8from mysql import MySql
    89from java.sql import *
    9 from xml.etree.ElementTree import ElementTree
    1010
    1111'''
    1212Class for ippToPsps database connectivity
    1313'''
    14 class IppToPspsDb(object):
    15 
    16     driverName="com.mysql.jdbc.Driver"
     14class IppToPspsDb(MySql):
    1715
    1816    '''
    1917    Constructor
    20 
    2118    '''
    2219    def __init__(self, logger):
    23 
    24         # setup logging
    25         self.logger = logger
    26         self.logger.debug("IppToPspsDb Constructor")
    27         # open config
    28         doc = ElementTree(file="config.xml")
    29 
    30         # set up JDBC connection
    31         dbName = doc.find("ipptopspsdatabase/name").text
    32         dbHost = doc.find("ipptopspsdatabase/host").text
    33         dbUser = doc.find("ipptopspsdatabase/user").text
    34         dbPass = doc.find("ipptopspsdatabase/password").text
    35         self.url = "jdbc:mysql://"+dbHost+"/"+dbName+"?user="+dbUser+"&password="+dbPass
    36         self.con = DriverManager.getConnection(self.url)
    37         self.stmt = self.con.createStatement()
     20        super(IppToPspsDb, self).__init__(logger,"ipptopspsdatabase")
    3821
    3922    '''
    4023    Creates a new batch
    4124    '''
    42     def createNewBatch(self, expID, surveyType, batchType, dvoDb, datastoreProduct):
     25    def createNewBatch(self, batchType, survey, dvoDb, datastoreProduct):
    4326
    44         sql = "SELECT batch_id FROM batches ORDER BY batch_id DESC LIMIT 1"
     27        sql = "INSERT INTO batch ( \
     28               batch_type, \
     29               survey, \
     30               dvo_db, \
     31               datastore_product \
     32               ) VALUES ( \
     33               '" + batchType + "', \
     34               '" + survey + "', \
     35               '" + dvoDb + "', \
     36               '" + datastoreProduct + "' \
     37               )"
     38
     39        self.stmt.execute(sql)
     40
     41        sql = "SELECT MAX(batch_id) FROM batch"
    4542
    4643        batchID = -1;
     
    5350            self.logger.exception("Unable to get batch ID")
    5451
    55         if batchID > 0:
    56             batchID = batchID + 1
    57 
    58             sql = "INSERT INTO batches \
    59                    (batch_id, \
    60                     exp_id, \
    61                     survey_id, \
    62                     batch_type, \
    63                     dvo_db, \
    64                     datastore_product) \
    65                    VALUES \
    66                    ("+str(batchID)+", \
    67                     " + str(expID) + ", \
    68                     '"+surveyType+"', \
    69                     '"+batchType+"', \
    70                     '"+dvoDb+"', \
    71                     '"+datastoreProduct+"')"
    72 
    73             self.stmt.execute(sql)
    74 
    75         self.logger.info("Creating new batch in ippToPsps database with batchID = %d" % batchID)
     52        self.logger.info("Created new batch in ippToPsps database with batchID = %d" % batchID)
    7653
    7754        return batchID;
     55
     56    '''
     57    Updates min/max object ID on this table and batch
     58    '''
     59    def updateMinMaxObjID(self, batchID, minObjID, maxObjID):
     60
     61        sql = "UPDATE batch SET \
     62               min_obj_id = " + str(minObjID) + ", \
     63               max_obj_id = " + str(maxObjID) + " \
     64               WHERE batch_id = " + str(batchID)
     65
     66        self.stmt.execute(sql)
     67
     68    '''
     69    Updates batch processed field
     70    '''
     71    def updateProcessed(self, batchID, processed):
     72
     73        sql = "UPDATE batch \
     74               SET processed = " + str(processed) + " \
     75               WHERE batch_id = " + str(batchID)
     76
     77        self.stmt.execute(sql)
     78
     79    '''
     80    Updates batch loaded_to_datastore field
     81    '''
     82    def updateLoadedToDatastore(self, batchID, loadedToDatastore):
     83
     84        sql = "UPDATE batch \
     85               SET loaded_to_datastore = " + str(loadedToDatastore) + " \
     86               WHERE batch_id = " + str(batchID)
     87
     88        self.stmt.execute(sql)
     89
     90    '''
     91    Have we already processed and published this batch?
     92    '''
     93    def alreadyProcessed(self, table, col, value):
     94
     95        sql = "SELECT COUNT(*) FROM \
     96               " + table + " \
     97               JOIN batch USING(batch_id) \
     98               WHERE " + col + " = " + str(value) + " \
     99               AND processed \
     100               AND loaded_to_datastore"
     101
     102        try:
     103            rs = self.stmt.executeQuery(sql)
     104            rs.first()
     105            if rs.getInt(1) > 0:
     106                self.logger.error("Batch with "+col+" = "+str(value)+" has already been processed and published to datastore")
     107                return True
     108            else:
     109                return False
     110        except:
     111            self.logger.exception("Unable to check whether this batch has already been processed")
     112
     113     
     114
     115    '''
     116    Inserts some detection metadata for this batch ID
     117    '''
     118    def insertDetectionMeta(self, batchID, expID, filter):
     119
     120        sql = "INSERT INTO detection ( \
     121               batch_id \
     122               ,exp_id \
     123               ,filter \
     124               ) VALUES ( \
     125               " + str(batchID) + " \
     126               ," + str(expID) + " \
     127               ,'" + filter + "' \
     128               )"
     129
     130        self.stmt.execute(sql)
     131
     132    '''
     133    Inserts some stack metadata for this batch ID
     134    '''
     135    def insertStackMeta(self, batchID, skyID, stackID, filter, stackType):
     136
     137        sql = "INSERT INTO stack ( \
     138               batch_id \
     139               ,sky_id \
     140               ,stack_id \
     141               ,filter \
     142               ,stack_type \
     143               ) VALUES ( \
     144               " + str(batchID) + " \
     145               ," + str(skyID) + " \
     146               ," + str(stackID) + " \
     147               ,'" + filter + "' \
     148               ,'" + stackType + "' \
     149               )"
     150
     151        self.stmt.execute(sql)
     152
    78153
    79154    '''
Note: See TracChangeset for help on using the changeset viewer.