Commit 5ed8592b authored by Ryan Herbert's avatar Ryan Herbert

major my_account refactor

Split query into three, so we can query patient, run and generic tables
This way, we can display the set name on recent analyses
parent 9047edc6
Pipeline #182275 passed with stages
in 13 minutes and 37 seconds
......@@ -11,9 +11,71 @@ if request.env.http_origin:
ACCESS_DENIED = "access denied"
def get_group_fuses():
group_fuses = {}
separator = "|| ';' ||"
fields = ["config.name",
"fused_file.sample_set_id",
"fused_file.config_id ",
"sample_set.sample_type"
]
group_concat = "GROUP_CONCAT(DISTINCT " + separator.join(fields)
group_fuses['patient'] = group_concat + separator + "patient.first_name || ' ' || patient.last_name)"
group_fuses['run'] = group_concat + separator + "run.name)"
group_fuses['set'] = group_concat + separator + "generic.name)"
return group_fuses
def group_permissions():
return "GROUP_CONCAT(DISTINCT auth_permission.name)"
def base_query(group_list):
return (db.auth_group.id.belongs(group_list) &
(db.auth_permission.group_id == db.auth_group.id) &
(db.auth_permission.table_name == 'sample_set'))
def base_left():
return [db.sample_set.on(
db.sample_set.id == db.auth_permission.record_id),
db.sample_set_membership.on(
db.sample_set_membership.sample_set_id == db.sample_set.id)]
def get_permissions(group_list):
return db(base_query(group_list) &
(db.auth_permission.record_id == 0)
).select(
db.auth_group.role,
group_permissions(),
groupby=db.auth_group.role
)
def group_tags():
return "GROUP_CONCAT(DISTINCT tag.name)"
def get_tags(group_list):
left = [
db.sample_set.on(
db.auth_permission.record_id == db.sample_set.id),
db.sample_set_membership.on(
db.sample_set_membership.sample_set_id == db.sample_set.id),
db.tag_ref.on(
(db.tag_ref.table_name == 'sequence_file') &
(db.tag_ref.record_id == db.sample_set_membership.sequence_file_id)),
db.tag.on(
db.tag_ref.tag_id == db.tag.id)
]
return db(base_query(group_list)).select(
db.auth_group.role,
group_tags(),
left=left,
groupby=db.auth_group.role
)
def index():
start = time.time()
two_days_ago = datetime.today() - timedelta(days=2)
if auth.is_admin() and 'group_ids' in request.vars and request.vars['group_ids'] is not None:
group_list = request.vars['group_ids']
if isinstance(group_list, types.StringTypes):
......@@ -21,80 +83,41 @@ def index():
else:
group_list = [int(g.id) for g in auth.get_user_groups() + auth.get_user_group_parents()]
log.debug("group_list: %s" % group_list)
if "filter" not in request.vars :
request.vars["filter"] = ""
search, tags = parse_search(request.vars["filter"])
left = [
db.sample_set.on(
db.sample_set.id == db.auth_permission.record_id),
db.sample_set_membership.on(
db.sample_set_membership.sample_set_id == db.sample_set.id)
]
base_query = (
db.auth_group.id.belongs(group_list) &
(db.auth_permission.group_id == db.auth_group.id) &
(db.auth_permission.table_name == 'sample_set')
)
group_permissions = "GROUP_CONCAT(DISTINCT auth_permission.name)"
perm_query = db(base_query &
(db.auth_permission.record_id == 0)
).select(
db.auth_group.role,
group_permissions,
groupby=db.auth_group.role
)
result = {}
perm_query = get_permissions(group_list)
for r in perm_query:
if(r.auth_group.role not in result):
result[r.auth_group.role] = {'count': []}
result[r.auth_group.role] = {}
for set_type in ['patient', 'run', 'set']:
result[r.auth_group.role][set_type] = {'count': []}
result[r.auth_group.role][set_type]['tags'] = []
result[r.auth_group.role]['statuses'] = ""
result[r.auth_group.role]['fuses'] = []
result[r.auth_group.role]['tags'] = []
result[r.auth_group.role]['permissions'] = [] if r._extra[group_permissions] is None else r._extra[group_permissions]
result[r.auth_group.role]['permissions'] = [] if r._extra[group_permissions()] is None else r._extra[group_permissions()]
query = base_query(group_list)
if (tags is not None and len(tags) > 0):
base_query = (base_query &
query = (query &
(db.tag.name.upper().belongs([t.upper() for t in tags])) &
(db.tag_ref.tag_id == db.tag.id) &
(db.tag_ref.table_name == 'sequence_file') &
(db.tag_ref.record_id == db.sample_set_membership.sequence_file_id)
)
query = db(base_query).select(
db.auth_group.id,
db.auth_group.role,
db.sample_set.sample_type.with_alias('sample_type'),
db.sample_set.id.count(distinct=True).with_alias('num_sets'),
db.sample_set_membership.sequence_file_id.count(distinct=True).with_alias('num_samples'),
left=left,
groupby=(db.auth_group.role, db.sample_set.sample_type)
)
for r in query:
if r.auth_group.role in result:
result[r.auth_group.role]['count'].append(r)
else:
result[r.auth_group.role] = {'count': [r]}
two_days_ago = datetime.today() - timedelta(days=2)
group_statuses = "GROUP_CONCAT(DISTINCT scheduler_task.id || ';' || scheduler_task.status)"
group_fuses = "GROUP_CONCAT(DISTINCT config.name || ';' || fused_file.sample_set_id || ';' || fused_file.config_id || ';' || sample_set.sample_type || ';' || sequence_file.filename)"
group_tags = "GROUP_CONCAT(DISTINCT tag.name)"
group_fuses = get_group_fuses()
left = [
db.sample_set.on(
db.auth_permission.record_id == db.sample_set.id),
db.sample_set_membership.on(
db.sample_set_membership.sample_set_id == db.sample_set.id),
queries = {}
left = base_left() + [
db.sequence_file.on(
db.sequence_file.id == db.sample_set_membership.sequence_file_id),
db.results_file.on(
......@@ -106,30 +129,62 @@ def index():
(db.fused_file.sample_set_id == db.sample_set.id) &
(db.fused_file.fuse_date >= two_days_ago)),
db.config.on(
db.config.id == db.fused_file.config_id),
db.tag_ref.on(
(db.tag_ref.table_name == 'sequence_file') &
(db.tag_ref.record_id == db.sample_set_membership.sequence_file_id)),
db.tag.on(
db.tag_ref.tag_id == db.tag.id)
db.config.id == db.fused_file.config_id)
]
query = db(base_query).select(
select = [
db.auth_group.id,
db.auth_group.role,
db.sample_set.sample_type.with_alias('sample_type'),
db.sample_set.id.count(distinct=True).with_alias('num_sets'),
db.sample_set_membership.sequence_file_id.count(distinct=True).with_alias('num_samples'),
group_statuses,
group_fuses,
group_tags,
]
patient_query = (query &
(db.patient.sample_set_id == db.auth_permission.record_id))
queries['patient'] = db(patient_query).select(
group_fuses['patient'],
*select,
left=left,
groupby=db.auth_group.role,
orderby=~db.scheduler_task.id
groupby=(db.auth_group.role, db.sample_set.sample_type)
)
for r in query:
result[r.auth_group.role]['statuses'] = "" if r._extra[group_statuses] is None else "".join([s.split(';')[1][0] for s in r._extra[group_statuses].split(',')])
run_query = (query &
(db.run.sample_set_id == db.auth_permission.record_id))
queries['run'] = db(run_query).select(
group_fuses['run'],
*select,
left=left,
groupby=(db.auth_group.role, db.sample_set.sample_type)
)
generic_query = (query &
(db.generic.sample_set_id == db.auth_permission.record_id))
queries['set'] = db(generic_query).select(
group_fuses['set'],
*select,
left=left,
groupby=(db.auth_group.role, db.sample_set.sample_type)
)
for key in queries:
query = queries[key]
for r in query:
result[r.auth_group.role][key]['count'].append(r)
result[r.auth_group.role]['statuses'] += "" if r._extra[group_statuses] is None else "".join([s.split(';')[1][0] for s in r._extra[group_statuses].split(',')])
fuses = [] if r._extra[group_fuses[key]] is None else [fuse.split(';') for fuse in r._extra[group_fuses[key]].split(',')]
result[r.auth_group.role]['fuses'] += (fuses)
result[r.auth_group.role]['fuses'] = [] if r._extra[group_fuses] is None else [fuse.split(';') for fuse in r._extra[group_fuses].split(',')]
result[r.auth_group.role]['tags'] = [] if r._extra[group_tags] is None else r._extra[group_tags].split(',')
tags = get_tags(group_list)
for r in tags:
result[r.auth_group.role]['tags'] = [] if r._extra[group_tags()] is None else r._extra[group_tags()].split(',')
involved_group_ids = get_involved_groups()
......
......@@ -16,16 +16,16 @@
{{=role}} ({{=result[role]['permissions']}})
<hr>
</div>
{{ for r in result[role]['count']: }}
{{ if r.sample_type is None: }}
{{ continue }}
<div class="set_cell">
<div class="set_data">
<span class="button_token {{=r.sample_type}}_token">{{=r.sample_type}}s</span>:
{{ for set_type in ['patient', 'run', 'set']: }}
{{ for r in result[role][set_type]['count']: }}
<div class="set_cell">
<div class="set_data">
<span class="button_token {{=r.sample_type}}_token">{{=r.sample_type}}s</span>:
</div>
<div class="set_data">number: {{=r['num_sets']}}</div>
<div class="set_data">associated samples: {{=r['num_samples']}}</div>
</div>
<div class="set_data">number: {{=r['num_sets']}}</div>
<div class="set_data">associated samples: {{=r['num_samples']}}</div>
</div>
{{pass}}
{{pass}}
<div class="set_data margined-top">recent jobs: {{=result[role]['statuses']}}</div>
<div class="set_data margined-top">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment