<commit_before>import os,sys,string
#import simWS
import MySQLdb
import MySQLdb.cursors
from logger import Log

### STB To Do Make this variable from the SimWS file
#from SimulatorService_v2_0_2_types import *

_runTable = "run"
_populationAxisTable = "population_axis"
_simulatedPopulationTable = "simulated_population"
_simulatedPopulationAxisValueTable = "simulated_population_axis_value"
_timeSeriesTable = "time_series"
_softwareIdentificationTable = "software_identification"

class ApolloDB:
    
    def __init__(self,host_="warhol-fred.psc.edu",
		 user_="apolloext",dbname_="apollo201",logger_=None):

        self._host = host_
        self._user = user_
        self._dbname = dbname_
        self._conn = None
        self._DictCursor = None
        self._RegularCursor = None
        self.populationAxis = None
        if logger_ is None:
            self.logger = Log("./db.test.log")
            self.logger.start()
        else:
            self.logger = logger_


    def connect(self):
        if self._conn is not None:
            print "Connection to Apollo Database has already been established"
            return
        try:
            self._conn = MySQLdb.connect(host=self._host,
                                         user=self._user,
                                         db=self._dbname)
            self._conn.autocommit(True)
            self.logger.update('DB_CONNECT_SUCCESS')
        except MySQLdb.Error, e:
            print "Problem connecting to Apollo database: %d %s" % (e.args[0], e.args[1])
            self.logger.update('DB_CONNECT_FAILED')
            raise e
            

        self._cursor = self._conn.cursor(MySQLdb.cursors.DictCursor)
        self.populationAxis = self._populationAxis()
        
    def close(self):
        if self._conn is None:
            raise RuntimeError("A connection to the Apollo database"\
                               "has not been made before close is made")

        self._conn.close()

    def query(self,SQLString):
        if self._conn is None:
            raise RuntimeError("A connection to the Apollo database "\
			                   "has not been made before queury is made")
    
        try:
            self._cursor.execute(SQLString)
        except Exception as e:
            self.logger.update('DB_QUERY_FAILED',message="%s,%s"%(str(e),SQLString))
            raise
        
        rows = self._cursor.fetchall()
        return rows

    def insertID(self):
        return self._conn.insert_id()

    def _populationAxis(self):
        populationAxisDict = {}
        SQLStatement = "SELECT * from %s"%_populationAxisTable

        result = self.query(SQLStatement)

        for row in result:
            populationAxisDict[row["label"]] = row["id"]

        return populationAxisDict

    def getRuns(self):
        runList = []
        SQLStatement = "SELECT * from %s"%_runTable
        result = self.query(SQLStatement)
        for row in result:
            runList.append(row['label'])
        return runList

    def getTranslatorServiceKey(self):
        SQLStatement = 'SELECT * from %s where service_type = "translator"'%(_softwareIdentificationTable)
        result = self.query(SQLStatement)
        row = result[0]
        return row['id']

    def getSoftwareIdentificationForRunId(self,runId):
        SQLStatement = 'SELECT t1.name,t1.developer,t1.version from software_identification t1, '\
            + 'run t2 where t1.id = t2.software_id and t2.id = %s'%str(runId)
        
        result = self.query(SQLStatement)
        if len(result) == 0:
            self.logger.update('DB_QUERY_NO_RESULTS',message=SQLString)
            return ('unknown','Could not find runId: %s in the ApolloDB')
        if len(result) > 1:
            self.logger.update('DB_QUERY_MORE_THAN_ONE',messsage=SQLString)
            raise

        return (result[0]['name'],result[0]['developer'],result[0]['version'])

    def getSimulatorServiceKey(self, developer, name, version):
        SQLStatement = 'SELECT * from %s where developer = "%s" and name = "%s"  and version = "%s"'\<commit_msg>Added a function to get the software identification id from name and version of software.<commit_after>import os,sys,string
#import simWS
import MySQLdb
import MySQLdb.cursors
from logger import Log

### STB To Do Make this variable from the SimWS file
#from SimulatorService_v2_0_2_types import *

_runTable = "run"
_populationAxisTable = "population_axis"
_simulatedPopulationTable = "simulated_population"
_simulatedPopulationAxisValueTable = "simulated_population_axis_value"
_timeSeriesTable = "time_series"
_softwareIdentificationTable = "software_identification"

class ApolloDB:
    
    def __init__(self,host_="warhol-fred.psc.edu",
		 user_="apolloext",dbname_="apollo201",logger_=None):

        self._host = host_
        self._user = user_
        self._dbname = dbname_
        self._conn = None
        self._DictCursor = None
        self._RegularCursor = None
        self.populationAxis = None
        if logger_ is None:
            self.logger = Log("./db.test.log")
            self.logger.start()
        else:
            self.logger = logger_


    def connect(self):
        if self._conn is not None:
            print "Connection to Apollo Database has already been established"
            return
        try:
            self._conn = MySQLdb.connect(host=self._host,
                                         user=self._user,
                                         db=self._dbname)
            self._conn.autocommit(True)
            self.logger.update('DB_CONNECT_SUCCESS')
        except MySQLdb.Error, e:
            print "Problem connecting to Apollo database: %d %s" % (e.args[0], e.args[1])
            self.logger.update('DB_CONNECT_FAILED')
            raise e
            

        self._cursor = self._conn.cursor(MySQLdb.cursors.DictCursor)
        self.populationAxis = self._populationAxis()
        
    def close(self):
        if self._conn is None:
            raise RuntimeError("A connection to the Apollo database"\
                               "has not been made before close is made")

        self._conn.close()

    def query(self,SQLString):
        if self._conn is None:
            raise RuntimeError("A connection to the Apollo database "\
			                   "has not been made before queury is made")
    
        try:
            self._cursor.execute(SQLString)
        except Exception as e:
            self.logger.update('DB_QUERY_FAILED',message="%s,%s"%(str(e),SQLString))
            raise
        
        rows = self._cursor.fetchall()
        return rows

    def insertID(self):
        return self._conn.insert_id()

    def _populationAxis(self):
        populationAxisDict = {}
        SQLStatement = "SELECT * from %s"%_populationAxisTable

        result = self.query(SQLStatement)

        for row in result:
            populationAxisDict[row["label"]] = row["id"]

        return populationAxisDict

    def getRuns(self):
        runList = []
        SQLStatement = "SELECT * from %s"%_runTable
        result = self.query(SQLStatement)
        for row in result:
            runList.append(row['label'])
        return runList

    def getTranslatorServiceKey(self):
        SQLStatement = 'SELECT * from %s where service_type = "translator"'%(_softwareIdentificationTable)
        result = self.query(SQLStatement)
        row = result[0]
        return row['id']
    
    def getSoftwareIdentificationId(self,name_="SEIR",version_="3.0"):
        SQLStatement = 'SELECT id from %s where name = "%s" and version = "%s"'%(_softwareIdentificationTable,name_,version_)
        results = self.query(SQLStatement)
        if len(result) == 0:
            self.logger.update('DB_QUERY_NO_RESULTS',message=SQLString)
            return -1
        return row['id']
    
    def getSoftwareIdentificationForRunId(self,runId):
        SQLStatement = 'SELECT t1.name,t1.developer,t1.version from software_identification t1, '\
            + 'run t2 where t1.id = t2.software_id and t2.id = %s'%str(runId)
        
        result = self.query(SQLStatement)
        if len(result) == 0:
            self.logger.update('DB_QUERY_NO_RESULTS',message=SQLString)
            return ('unknown','Could not find runId: %s in the ApolloDB')
        if len(result) > 1:
            self.logger.update('DB_QUERY_MORE_THAN_ONE',messsage=SQLString)
            raise

        return (result[0]['name'],result[0]['developer'],result[0]['version'])

    def getSimulatorServiceKey(self, developer, name, version):
        SQLStatement = 'SELECT * from %s where developer = "%s" and name = "%s"  and version = "%s"'\
{'fraction_non_alphanumeric': 0.09906692777329801, 'fraction_numerical': 0.0055293168989747725, 'mean_word_length': 2.3011406844106466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3422743', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2391, 'n_words': 582}