diff --git a/README.md b/README.md index a67d7e1..3cc710b 100644 --- a/README.md +++ b/README.md @@ -137,8 +137,9 @@ then run 1. ``CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';`` 2. ``GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' WITH GRANT OPTION;`` 3. ``flush privileges;`` - More information https://dev.mysql.com/doc/refman/5.5/en/adding-users.html +4. Set environment variable BIOM_MASS that holds user's password + ``export BIOM_MASS="password"`` load_local_database.py script needs mysql.connect module. diff --git a/database.py b/database.py index e267010..49db8d8 100644 --- a/database.py +++ b/database.py @@ -1,14 +1,56 @@ - # Calls to obtain values from the data structures (to be populated by the local database next) - +import mysql.connector +from mysql.connector import pooling +import os +import dbqueries import const +# add increment for aggregations def add_key_increment(dictionary, key): if not key in dictionary: dictionary[key]=0 dictionary[key]+=1 class Data(object): + + def __init__(self): + self.conn_pool = mysql.connector.connect(use_pure=False, pool_name="portal", + pool_size=10, + pool_reset_session=True, + database='portal_ui', + user='biom_mass', + password=os.environ['BIOM_MASS']) + + def __exit__(self): + self.conn_pool.close() + + # get connection from pool + def get_pool(self): + conn = mysql.connector.connect(pool_name="portal") + cursor = conn.cursor(buffered=True,dictionary=True) + return conn, cursor + + # release connection back to pool + def release_pool(self,conn,cursor): + cursor.close() + conn.close() + + # runs query and returns results + def fetch_results(self,query): + conn,cursor=self.get_pool() + cursor.execute(query) + data=[row for row in cursor] + self.release_pool(conn,cursor) + return data + + # get current version from db + def get_current_version(self): + version_data=self.fetch_results(dbqueries.version_query) + del version_data[0]['updated'] + del version_data[0]['id'] + return version_data[0] + + def load_data(self): self.data = const.DB() @@ -78,9 +120,21 @@ def get_project_aggregations(self, projects): return project_aggregates + + # get all counts for front page summary from db def get_current_counts(self): - self.load_data() - return self.data.CURRENT_COUNTS + import schema + + counts_data = self.fetch_results(dbqueries.all_counts_query) + + return schema.Count( + projects=counts_data[0]['total'], + participants=counts_data[1]['total'], + samples=counts_data[2]['total'], + dataFormats=counts_data[3]['total'], + rawFiles=counts_data[4]['total'], + processedFiles=counts_data[5]['total']) + def get_file_aggregations(self, files): import schema diff --git a/dbqueries.py b/dbqueries.py new file mode 100644 index 0000000..0f96b0d --- /dev/null +++ b/dbqueries.py @@ -0,0 +1,9 @@ +# This file holds all queries run in database.py +all_counts_query='''select count(id) as total from project + union all select count(id) as total from participant + union all select count(id) as total from sample + union all select count(distinct data_format) as total from file_sample + union all select count(id) as total from file_sample where type="rawFiles" + union all select count(id) as total from file_sample where type="processedFiles"''' + +version_query="select * from version order by updated desc limit 1" diff --git a/load_local_database.py b/load_local_database.py index f83f0f0..1a58ba8 100644 --- a/load_local_database.py +++ b/load_local_database.py @@ -23,7 +23,7 @@ def parse_arguments(args): required=False) parser.add_argument( "--key-file", - default="biom-mass-fdcadb440fdf.json", + default="/home/hutlab_public/compute_engine_service_account_key/biom-mass-8dc9ab934396.json", help="google project service account private key file path\n]", required=False) parser.add_argument( @@ -256,7 +256,7 @@ def main(): new_version = str(0.1) now = datetime.datetime.now() - release_date ="Date Release "+new_version+" "+ now.strftime("%b %d, %Y") + release_date ="Data Release "+new_version+" - "+ now.strftime("%b %d, %Y") commit ="commit_"+now.strftime("%m%d%Y") query_insert_version ='''INSERT INTO `version` ( commit, diff --git a/schema.py b/schema.py index 13b23d4..d106c4a 100644 --- a/schema.py +++ b/schema.py @@ -427,5 +427,5 @@ def resolve_samples(self, info, namespace, workspace): json_result = query_firecloud.call_api(url) obj_result = utilities.json2obj(json.dumps(json_result)) return obj_result - + diff --git a/server.py b/server.py index 4154751..4676bc0 100644 --- a/server.py +++ b/server.py @@ -10,11 +10,13 @@ import os import flask +import flask_cors import flask_graphql import graphene import schema import const +from database import data NAME = "firecloud_graphql" HOST = "0.0.0.0" @@ -41,6 +43,8 @@ def process_query(request, schema): def main(): # create the graphql flask app app = flask.Flask(NAME) + # allow initial OPTIONS requests + flask_cors.CORS(app) # add the root graphql queries @app.route('/graphql', methods=["POST"]) @@ -55,7 +59,7 @@ def get_schema(name): # add static endpoint for version/status @app.route('/status', methods=["GET"]) def get_version(): - return flask.jsonify(const.VERSION) + return flask.jsonify(data.get_current_version()) # add end point for graphql gui app.add_url_rule('/test', view_func=flask_graphql.GraphQLView.as_view(