diff --git a/model/data.py b/model/data.py index ae7eed5..0b46e98 100644 --- a/model/data.py +++ b/model/data.py @@ -14,15 +14,21 @@ } # no auto-primary_keys due to permissions (?) -def model_from_table(name, __tablename__=None, primary_keys=None): +def model_from_table(name, __tablename__=None, columns=None, primary_keys=None): if(__tablename__ is None): __tablename__ = name - connection = SQLAlchemyEngine.connect() - - stmt = 'SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = \'public\' AND table_name = \'' + __tablename__ + '\' ORDER BY ordinal_position ASC;' - columns = connection.execute(stmt).all() - connection.close() + if(columns is None): + connection = SQLAlchemyEngine.connect() + stmt = 'SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = \'public\' AND table_name = \'' + __tablename__ + '\' ORDER BY ordinal_position ASC;' + columns = connection.execute(stmt).all() + + if(len(columns) == 0): + # try a materialized view + stmt = 'SELECT attname AS column_name, format_type(atttypid, atttypmod) AS data_type FROM pg_attribute WHERE attrelid = \'' + __tablename__ + '\'::regclass AND attnum > 0;' + columns = connection.execute(stmt).all() + + connection.close() for key, value in columns: if value not in type_to_sqlalchemy_type: diff --git a/route/data.py b/route/data.py index bd5b6c9..0cba20d 100644 --- a/route/data.py +++ b/route/data.py @@ -70,6 +70,8 @@ def verify_password(username, password): 'dsequence': model_from_table(name='dsequence', primary_keys=('run_id')), 'dsra': model_from_table(name='dsra', primary_keys=('run_id')), 'rfamily': model_from_table(name='rfamily', primary_keys=('run_id')), + # 'rfamily_counts': model_from_table(name='rfamily_counts', columns=[('family_name', 'text'), ('score', 'bigint'), ('percent_identity', 'bigint'), ('count', 'bigint')], primary_keys=('family_name')), + 'rfamily_counts': model_from_table(name='rfamily_counts', primary_keys=('family_name')), 'rphylum': model_from_table(name='rphylum', primary_keys=('run_id')), 'rsequence': model_from_table(name='rsequence', primary_keys=('run_id')), 'rsra': model_from_table(name='rsra', primary_keys=('run_id')), @@ -127,6 +129,15 @@ def data_query(arguments): data_session.close() if('_count' in arguments): + print('memoize query') + stmt = 'SELECT count(*) as count_1 FROM(' + sub('\n', '', str(data_query.statement.compile(dialect=postgresql.dialect()))) + ') AS anon_1' + stmt_md5 = md5(stmt.encode('utf-8')).hexdigest() + print('-->', stmt, stmt_md5) + + # check if it's there + # if it's not calculate and store + # return value + return ( data_query .count()