Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Custom Reports and Utilities In Admin #1036

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/app/admin/assertions_admin.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Trestle.resource(:assertions) do
collection do
Assertion.eager_load(:flags, molecular_profile: {variants: [:gene]}).order(id: :asc)
Assertion.eager_load(:flags, molecular_profile: {variants: [:feature]}).order(id: :asc)
end

search do |q|
q ? collection.where("genes.name ILIKE ?", "#{q}%").or(collection.where("variants.name ILIKE ?", "#{q}%")) : collection
q ? collection.where("features.name ILIKE ?", "#{q}%").or(collection.where("variants.name ILIKE ?", "#{q}%")) : collection
end

remove_action :destroy
Expand Down
6 changes: 3 additions & 3 deletions server/app/admin/evidence_items_admin.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Trestle.resource(:evidence_items) do
collection do
EvidenceItem.eager_load(:flags, molecular_profile: {variants: [:gene]}).order(id: :asc)
EvidenceItem.eager_load(:flags, molecular_profile: {variants: [:feature]}).order(id: :asc)
end

remove_action :destroy
Expand Down Expand Up @@ -37,7 +37,7 @@
tab :evidence_item do
row do
col(sm: 1) { static_field :id }
col(sm: 1) { static_field evidence_item.molecular_profile.display_name }
col(sm: 1) { evidence_item.molecular_profile.display_name }
col(sm: 2) do
variant_origins = EvidenceItem.variant_origins.keys.map { |variant_origin| [variant_origin, variant_origin] }
select :variant_origin, variant_origins
Expand Down Expand Up @@ -95,7 +95,7 @@

select :phenotype_ids, Phenotype.order(:hpo_class), { label: "Phenotypes" }, multiple: true

collection_select :source_id, Source.order(:description), :id, :display_name
collection_select :source_id, Source.order(:citation), :id, :display_name

divider

Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
Trestle.resource(:genes, model: Features::Gene) do
Trestle.resource(:features) do
collection do
Fatures::Gene.includes(:flags).order(name: :asc)
Fature.includes(:flags).order(name: :asc)
end
acoffman marked this conversation as resolved.
Show resolved Hide resolved

search do |q|
q ? collection.where("genes.name ILIKE ?", "#{q}%") : collection
q ? collection.where("features.name ILIKE ?", "#{q}%") : collection
end

remove_action :destroy
remove_action :new

menu do
item :genes, icon: "fa fa-align-center" #TODO: see if we can use our custom icons here
item :features, icon: "fa fa-align-center" #TODO: see if we can use our custom icons here
end

scope :all
scope :with_variants, -> { Features::Gene.joins(:variants).distinct }, default: true
scope :flagged, -> { Features::Gene.where(flagged: true) }
scope :with_variants, -> { Feature.joins(:variants).distinct }, default: true
scope :flagged, -> { Features.where(flagged: true) }

# Customize the table columns shown on the index view.
table do
column :id
column :entrez_id
column :name
column :official_name
column :full_name
column :flagged
end

# Customize the form fields shown on the new/edit views.
form do |gene|
tab :gene do
form do |feature|
tab :feature do
row do
col(sm: 1) { static_field :id }
col(sm: 1) { static_field :entrez_id }
col(sm: 1) { static_field :name }
col(sm: 8) { static_field :official_name }
if gene.flagged
col(sm: 8) { static_field :full_name }
if feature.flagged
col do
static_field :flagged do
status_tag(icon("fa fa-flag"), :danger)
Expand All @@ -54,8 +52,8 @@
end
end

tab :comments, badge: gene.comments.size do
table gene.comments do
tab :comments, badge: feature.comments.size do
table feature.comments do
column :id do |comment|
link_to comment.id, CommentsAdmin.instance_path(comment)
end
Expand All @@ -67,8 +65,8 @@
end
end

tab :flags, badge: gene.flags.where(state: 'open').exists? do
table gene.flags do
tab :flags, badge: feature.flags.where(state: 'open').exists? do
table feature.flags do
column :id do |flag|
link_to flag.id, FlagsAdmin.instance_path(flag)
end
Expand All @@ -82,8 +80,8 @@
end
end

tab :revisions, badge: gene.revisions.where(status: 'new').exists? do
table gene.revisions do
tab :revisions, badge: feature.revisions.where(status: 'new').exists? do
table feature.revisions do
column :id do |revision|
link_to revision.id, RevisionsAdmin.instance_path(revision)
end
Expand Down
4 changes: 4 additions & 0 deletions server/app/admin/organizations_admin.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Trestle.resource(:organizations) do
controller do
include ActiveStorage::SetCurrent
end

menu do
item :organizations, icon: "fa fa-users"
end
Expand Down
58 changes: 58 additions & 0 deletions server/app/admin/reports_admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Trestle.admin(:reports) do
menu do
item :reports, icon: "fas fa-file-alt"
end

controller do
def index
@reports = Report::AVAILABLE_REPORTS
end

def show
@report = Report::AVAILABLE_REPORTS.find { |x| params[:name] == x.name }
end

def generate_report
@report = Report::AVAILABLE_REPORTS.find { |x| params[:name] == x.name }
report_params = params.permit(@report.inputs.keys).to_h
report_instance = @report.new(report_params.symbolize_keys)
report_instance.perform unless report_instance.errors.any?

if report_instance.errors.any?
flash[:error] = report_instance.errors.join("\n")
render :show
else
if params[:format] == "download"
stream_table(report_instance)
else
@data = report_instance.data
@headers = report_instance.headers
render :result
end
end
end

private
def stream_table(report)
require 'csv'
headers.delete("Content-Length")
headers["Cache-Control"] = "no-cache"
headers["Content-Type"] = "text/csv"
headers["Content-Disposition"] = "attachment; filename=\"#{report.class.name}-#{Date.today}.tsv\""
headers["X-Accel-Buffering"] = "no"
response.status = 200

self.response_body = Enumerator.new do |stream|
stream << CSV.generate_line(report.headers, col_sep: "\t")
report.data.each do |row|
stream << CSV.generate_line(row, col_sep: "\t")
end
end
end
end

routes do
get '/:name', action: :show
post '/:name', action: :generate_report
end
end
4 changes: 4 additions & 0 deletions server/app/admin/users_admin.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Trestle.resource(:users) do
controller do
include ActiveStorage::SetCurrent
end

menu do
item :users, icon: "fa fa-user"
end
Expand Down
35 changes: 35 additions & 0 deletions server/app/admin/utilities_admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Trestle.admin(:utilities) do
menu do
item :utilities, icon: "fas fa-cogs"
end

controller do
def index
@utilities = ActionWrapper::AVAILABLE_ACTIONS
end

def show
@util = ActionWrapper::AVAILABLE_ACTIONS.find { |x| params[:name] == x.name }
end

def perform_action
@util = ActionWrapper::AVAILABLE_ACTIONS.find { |x| params[:name] == x.name }
util_params = params.permit(@util.inputs.keys).to_h
action = @util.new
res = action.perform(util_params.symbolize_keys)

if res.errors.any?
flash[:error] = res.errors.join("\n")
else
flash[:message] = "#{@util.name} Succeeded"
end

render :show
end
end

routes do
get '/:name', action: :show
post '/:name', action: :perform_action
end
end
13 changes: 3 additions & 10 deletions server/app/admin/variants_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
table do
column :id
column :name
column :gene
column :feature
column :variant_aliases do |variant|
variant.variant_aliases.map {|a| a.name }.join(', ')
end
Expand All @@ -37,15 +37,8 @@
col(sm: 1) { static_field :id }
col(sm: 1) { static_field :name }
col(sm: 1) do
static_field :gene do
link_to variant.gene.name, GenesAdmin.instance_path(variant.gene)
end
end
if variant.secondary_gene.present?
col(sm: 1) do
static_field :secondary_gene do
link_to variant.secondary_gene.name, GenesAdmin.instance_path(variant.secondary_gene)
end
static_field :variant do
link_to variant.feature.name, FeaturesAdmin.instance_path(variant.feature)
end
end
if variant.flagged
Expand Down
Loading
Loading