def loadReplicas(self): """ Load replicas from database. """ conn = connectdb.GetDBConn() cursor = conn.cursor() query = "select ReplicaID from AlertReplica where AlertID={0}".format(self.ID) cursor.execute(query) replica_ids = cursor.fetchall() self.replicas = [] for row in replica_ids: replica_id = row[0] query = """select * from AlertReplica where ReplicaID={0}""".format(replica_id) cursor.execute( query ) replica_row = cursor.fetchall()[ 0 ] parent_id = replica_row[ 4 ] astro_id = replica_row[ 5 ] replica_num = replica_row[ 1 ] r = AlertReplica( self, astro_id=astro_id, init_from_db=True, replica_id=replica_id, replica_num=replica_num) self.replicas.append(r) self.replica_counter = len(replica_ids) conn.close() return self.replicas
def annotation( self ): conn = connectdb.GetDBConn() cursor = conn.cursor() sql_query = """select Annotation from Alert where AlertID={0}""".format(self.ID) cursor.execute( sql_query ) return cursor.fetchall()[0][0]
def assembleVector( self, context, propname ): """ The function assembles a vector of all the values of an Property inside a context of the alert replicas of the camera alert. :param string context: the name of the context :param string propname: the name of the Property :return: an array of values :rtype: numpy array """ conn = connectdb.GetDBConn() cursor = conn.cursor() v = [] # TODO: check if propname is in context query = """SELECT Value from PropertyValue where PropName="{}" and ContainerID in (SELECT ReplicaID from AlertReplica where AlertID={})""".format(propname, self.container_id) cursor.execute(query) results = cursor.fetchall() v = [i[0] for i in results] conn.close() return np.array(v)
def divert( self, annotation ): """ Divert the alert. :param string annotation: a short description of why the alert is diverted. """ self.decision = 'D' ## TODO: reflect the change of decision immediately to DB. conn = connectdb.GetDBConn() cursor = conn.cursor() sql_update = """update Alert set Decision="{0}", Annotation="{1}" where AlertID={2}""".format( self.decision, annotation, self.ID ) cursor.execute( sql_update ) conn.commit() conn.close()
def __init__( self, astro_id ): """'continer_id' is the ID of the object that owns the context.""" self.container_id = astro_id self.container_type = 'A' ## Initialize predefined base properties for AO context. for propname in AO_base_properties.keys(): prop = Property( propname, BASE_PROP, self, AO_base_properties[propname][0], 1, description=AO_base_properties[propname][1] ) setattr( self, propname, prop ) ## Connect to mysql database. conn = connectdb.GetDBConn() cur = conn.cursor() # FIXME: not always SDSS row = None query = """select * from AstroObject_NED where No={}""".format(astro_id) cur.execute( query ) row = cur.fetchall() if len(row) != 0: row = row[0] # query = """select * from AstroObject_Fake_SDSS where Objid={}""".format(astro_id) # cur.execute( query ) # row = cur.fetchall() # if len(row) != 0: # row = row[0] # query = """select * from AstroObject_Fake_Chandra where msid={}""".format(astro_id) # cur.execute( query ) # row = cur.fetchall() # if len(row) != 0: # row = row[0] if row is None: conn.close() return for propname in AO_base_properties.keys(): prop = getattr( self, propname ) try: prop.value = row[ AO_base_properties[propname][2] ] except: prop.value = None conn.close()
def commit( self ): """ Commit the alert replica to Locus-aggregated Alerts DB. """ conn = connectdb.GetDBConn() cur = conn.cursor() query = """select * from AlertReplica where ReplicaID={0}""".format(self.ID) cur.execute( query ) if len(cur.fetchall()) == 0: # only insert when not exists if self.astro_id != None: query = """select * from AstroObject where AstroObjectID={0}""".format(self.astro_id) cur.execute( query ) if len(cur.fetchall()) == 0: sql_insert = """insert into AstroObject values({0}, "{1}", {2}, {3}, {4})""".format( self.astro_id, "SDSS", self.astro_id, 1, self.parent.locus_id ) cur.execute( sql_insert ) sql_insert = """insert into AlertReplica(ReplicaID,ReplicaNumber,AlertID,AstroObjectID,LocusID) \ values({0}, {1}, {2}, {3}, {4})""".format( self.ID, self.num, self.parent.ID, self.astro_id, self.parent.locus_id ) else: sql_insert = """insert into AlertReplica(ReplicaID,ReplicaNumber,AlertID,LocusID) \ values({0}, {1}, {2}, {3})""".format( self.ID, self.num, self.parent.ID, self.parent.locus_id ) try: cur.execute( sql_insert ) except: pass self.flushed2DB = True self.AR.commit( cur ) if self.astro_id != None: self.AO.commit( cur ) conn.commit() conn.close()
def commit( self ): """ Commit the alert data to Locus-aggregated Alerts DB. """ conn = connectdb.GetDBConn() cur = conn.cursor() # Nothing to commit for CA context now since all properties are pre-loaded to DB. # self.CA.commit( cur ) if self.decision != 'NA': ## Update corresponding Alert table to reflect decision change. ## Connect to mysql database. sql_update = """update Alert set Decision="{0}" where AlertID={1}""".format( self.decision, self.ID ) cur.execute( sql_update ) # Replicas will be written to DB when they are created. # for replica in self.replicas: # replica.commit() conn.commit() conn.close()
def mark_as_rare( self, annotation ): """ Mark the alert as a rare alert. :param string annotation: a short description of why the alert is rare. """ conn = connectdb.GetDBConn() cursor = conn.cursor() if self.decision == 'R': sql_query = """select Annotation from Alert where AlertID={0}""".format(self.ID) cursor.execute( sql_query ) self._annotation = cursor.fetchall()[0][0] self._annotation += '; ' + annotation else: self._annotation = annotation self.decision = 'R' ## TODO: reflect the change of decision immediately to DB. sql_update = """update Alert set Decision="{0}", Annotation="{1}" where AlertID={2}""".format( self.decision, self._annotation, self.ID ) cursor.execute( sql_update ) conn.commit() conn.close()
def __init__( self, container_id ): """'continer_id' is the ID of the object that owns the context.""" self.container_id = container_id self.conn = connectdb.GetDBConn()