Skip to content

Commit

Permalink
feat/data api (#70)
Browse files Browse the repository at this point in the history
* feat: added support for materialized views
           added rfamily_counts
  • Loading branch information
almosnow authored Oct 23, 2023
1 parent e5e9a77 commit 75f94b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
18 changes: 12 additions & 6 deletions model/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions route/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 75f94b6

Please sign in to comment.