From aae827674cdbe46c7f1ce5d2246bf485a24dbbb3 Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Sun, 19 Jun 2016 16:03:26 +0000 Subject: gn_server: SQL handling --- etc/default_settings.py | 1 + wqflask/dbFunction/webqtlDatabaseFunction.py | 26 ++++++++++++++++++++------ wqflask/runserver.py | 4 +++- wqflask/utility/logger.py | 16 +++++++++++++--- wqflask/utility/tools.py | 1 + 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/etc/default_settings.py b/etc/default_settings.py index f3a3e1ea..296ffcb0 100644 --- a/etc/default_settings.py +++ b/etc/default_settings.py @@ -41,6 +41,7 @@ LOG_LEVEL_DEBUG = 1 # Debug log level (0-5) (nyi) LOG_SQL = 'False' # Log SQL/backend calls USE_REDIS = True # REDIS caching (note that redis will be phased out) +USE_GN_SERVER = True # Use GN_SERVER SQL calls # Path overrides for Genenetwork HOME=os.environ['HOME'] diff --git a/wqflask/dbFunction/webqtlDatabaseFunction.py b/wqflask/dbFunction/webqtlDatabaseFunction.py index 1bc67992..5b968d7e 100644 --- a/wqflask/dbFunction/webqtlDatabaseFunction.py +++ b/wqflask/dbFunction/webqtlDatabaseFunction.py @@ -20,11 +20,13 @@ # # This module is used by GeneNetwork project (www.genenetwork.org) +from inspect import stack from flask import Flask, g import MySQLdb import string from base import webqtlConfig +from utility.tools import USE_GN_SERVER from utility.logger import getLogger logger = getLogger(__name__ ) @@ -35,13 +37,31 @@ logger = getLogger(__name__ ) ########################################################################### def getCursor(): try: + logger.warning("Creating new MySQLdb cursor") con = MySQLdb.Connect(db=webqtlConfig.DB_NAME, host=webqtlConfig.MYSQL_SERVER, user=webqtlConfig.DB_USER, passwd=webqtlConfig.DB_PASSWD) cursor = con.cursor() return cursor except: return None +def fetchone(cursor,query): + result = g.db.execute("""select Species.Name from Species, InbredSet where InbredSet.Name = %s and InbredSet.SpeciesId = Species.Id""", (group)).fetchone()[0] + return cursor.execute(query) +def retrieve_species(group): + """Get the species of a group (e.g. returns string "mouse" on "BXD" + + """ + if USE_GN_SERVER: + raise Exception("NYI") + else: + res = logger.sql(stack()[0][3],"select Species.Name from Species, InbredSet where InbredSet.Name = '%s' and InbredSet.SpeciesId = Species.Id" % (group), g.db.execute) + result = res.fetchone()[0] + + # result = g.db.execute("""select Species.Name from Species, InbredSet where InbredSet.Name = %s and InbredSet.SpeciesId = Species.Id""", (group)).fetchone()[0] + + logger.info(result,type(result)) + return result ########################################################################### #input: cursor, groupName (string) @@ -78,12 +98,6 @@ def getAllSpecies(cursor=None): allSpecies = cursor.fetchall() return allSpecies -def retrieve_species(group): - logger.debug("retrieve_species",group) - logger.sql(""""select Species.Name from Species, InbredSet where InbredSet.Name = %s and InbredSet.SpeciesId = Species.Id""", (group)) - - return g.db.execute("""select Species.Name from Species, InbredSet where InbredSet.Name = -%s and InbredSet.SpeciesId = Species.Id""", (group)).fetchone()[0] def retrieve_species_id(group): return g.db.execute("select SpeciesId from InbredSet where Name = %s", (group)).fetchone()[0] diff --git a/wqflask/runserver.py b/wqflask/runserver.py index 9339acf8..12ec904e 100644 --- a/wqflask/runserver.py +++ b/wqflask/runserver.py @@ -16,9 +16,11 @@ GREEN = '\033[92m' BOLD = '\033[1m' ENDC = '\033[0m' - logger.info("GN2 is running. Visit %shttp://localhost:5003/%s" % (BLUE,ENDC)) +import os +app.config['SECRET_KEY'] = os.urandom(24) + from utility.tools import WEBSERVER_MODE werkzeug_logger = logging.getLogger('werkzeug') diff --git a/wqflask/utility/logger.py b/wqflask/utility/logger.py index bc0578c9..0d374f1a 100644 --- a/wqflask/utility/logger.py +++ b/wqflask/utility/logger.py @@ -62,14 +62,24 @@ class GNLogger: """Call logging.error for multiple args""" self.collect(self.logger.error,*args) + def infof(self,*args): + """Call logging.info for multiple args lazily""" + # only evaluate function when logging + if self.logger.getEffectiveLevel() < 30: + self.collectf(self.logger.debug,*args) + def debugf(self,*args): """Call logging.debug for multiple args lazily""" - if self.logger.getEffectiveLevel() <= 10: - self.debug("Calling debug function!") + # only evaluate function when logging + if self.logger.getEffectiveLevel() < 20: self.collectf(self.logger.debug,*args) - def sql(self, sqlcommand, fun = None): + def sql(self, description, sqlcommand, fun = None): """Log SQL command, optionally invoking a timed fun""" + self.info(description,sqlcommand) + if fun: + self.info("Invoking function") + return fun(sqlcommand) def collect(self,fun,*args): """Collect arguments and use fun to output one by one""" diff --git a/wqflask/utility/tools.py b/wqflask/utility/tools.py index 2affd130..fca5917e 100644 --- a/wqflask/utility/tools.py +++ b/wqflask/utility/tools.py @@ -172,6 +172,7 @@ LOG_LEVEL = get_setting('LOG_LEVEL') LOG_LEVEL_DEBUG = get_setting_int('LOG_LEVEL_DEBUG') LOG_SQL = get_setting_bool('LOG_SQL') USE_REDIS = get_setting_bool('USE_REDIS') +USE_GN_SERVER = get_setting_bool('USE_GN_SERVER') PYLMM_COMMAND = pylmm_command() GEMMA_COMMAND = gemma_command() -- cgit v1.2.3