From 1a56396eb592c5ecde72037487bb1c6ce62d8ef4 Mon Sep 17 00:00:00 2001 From: zmagod Date: Mon, 8 Aug 2016 16:02:17 +0200 Subject: [PATCH] add workflow image to project overview page [fixes SCI-41] --- Dockerfile | 2 +- Gemfile | 1 + app/controllers/canvas_controller.rb | 3 ++ app/controllers/experiments_controller.rb | 6 +-- app/models/experiment.rb | 42 +++++++++++++++++++ app/views/projects/show/_experiment.html.erb | 3 ++ config/routes.rb | 2 +- ...d_attachment_workflowimg_to_experiments.rb | 11 +++++ db/schema.rb | 20 +++++---- 9 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20160808083040_add_attachment_workflowimg_to_experiments.rb diff --git a/Dockerfile b/Dockerfile index e5f0c982b..9e51a29f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM rails:4.2.5 MAINTAINER BioSistemika # additional dependecies -RUN apt-get update -qq && apt-get install -y default-jre-headless unison sudo --no-install-recommends && rm -rf /var/lib/apt/lists/* +RUN apt-get update -qq && apt-get install -y default-jre-headless unison sudo graphviz --no-install-recommends && rm -rf /var/lib/apt/lists/* # heroku tools RUN wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh diff --git a/Gemfile b/Gemfile index 69333230c..df84bb580 100644 --- a/Gemfile +++ b/Gemfile @@ -52,6 +52,7 @@ gem 'aws-sdk', '~> 2.2.8' gem 'aws-sdk-v1' gem 'delayed_job_active_record' gem 'devise-async' +gem 'ruby-graphviz', '~> 1.2' # Graphviz for rails group :development, :test do gem 'byebug' diff --git a/app/controllers/canvas_controller.rb b/app/controllers/canvas_controller.rb index c07e59ad3..79647ac09 100644 --- a/app/controllers/canvas_controller.rb +++ b/app/controllers/canvas_controller.rb @@ -231,6 +231,9 @@ class CanvasController < ApplicationController end end + # Create workflow image + @experiment.generate_workflow_img + flash[:success] = t( "experiments.canvas.update.success_flash") redirect_to canvas_experiment_path(@experiment) diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index 90755aa54..b1143ac30 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -57,12 +57,8 @@ class ExperimentsController < ApplicationController if @experiment.save flash[:success] = t('experiments.update.success_flash', experiment: @experiment.name) -<<<<<<< HEAD - + redirect_to canvas_experiment_path(@experiment) -======= - redirect_to project_path(@experiment.project) ->>>>>>> a4ec5585965a7a8e565d7ee9072a26e52ccd9fd3 else flash[:alert] = t('experiments.update.error_flash') redirect_to :back diff --git a/app/models/experiment.rb b/app/models/experiment.rb index eb27ccebb..7824bc1a2 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -1,3 +1,5 @@ +require 'graphviz' + class Experiment < ActiveRecord::Base include ArchivableModel, SearchableModel @@ -11,6 +13,9 @@ class Experiment < ActiveRecord::Base has_many :my_module_groups, inverse_of: :experiment, dependent: :destroy has_many :report_elements, inverse_of: :experiment, dependent: :destroy + has_attached_file :workflowimg + validates_attachment_content_type :workflowimg, content_type: /\Aimage\/.*\Z/ + validates :name, presence: true, length: { minimum: 4, maximum: 50 }, @@ -195,6 +200,43 @@ class Experiment < ActiveRecord::Base return true end + def generate_workflow_img + graph = GraphViz.new( :G, type: :digraph, use: :neato ) + label = 'T' + my_module_groups.each do |group| + group.ordered_modules.each_with_index do |my_module, index| + if(my_module.outputs.any?) + parent = graph.add_nodes("N-#{index}", label: label, shape: 'circle', pos: "#{my_module.x},-#{my_module.y}!") + my_module.outputs.each_with_index do |output, i| + child_mod = MyModule.find_by_id(output.input_id) + child_node = graph.add_nodes("N-O#{child_mod.id}-#{i}", label: label, shape: 'circle', pos: "#{child_mod.x},-#{child_mod.y}!") + graph.add_edges(parent, child_node) + end + elsif(my_module.inputs.any?) + parent = graph.add_nodes("N-#{index}", label: label, shape: 'circle', pos: "#{my_module.x},-#{my_module.y}!") + my_module.inputs.each_with_index do |input, i| + child_mod = MyModule.find_by_id(input.output_id) + child_node = graph.add_nodes("N-I#{child_mod.id}-#{i}", label: label, shape: 'circle', pos: "#{child_mod.x},-#{child_mod.y}!") + graph.add_edges(child_node, parent) + end + end + end + end + + graph[:size] = '5,3' + file_location = "/tmp/workflowimg_#{Process.pid}.png" + graph.output(png: file_location) + + begin + file = File.open(file_location) + self.workflowimg = file + file.close + save! + rescue => ex + logger.error ex.message + end + end + private # Archive all modules. Receives an array of module integer IDs. diff --git a/app/views/projects/show/_experiment.html.erb b/app/views/projects/show/_experiment.html.erb index 35eab7159..3ec4fd069 100644 --- a/app/views/projects/show/_experiment.html.erb +++ b/app/views/projects/show/_experiment.html.erb @@ -32,6 +32,9 @@
+ <% if experiment.workflowimg? %> + <%= image_tag experiment.workflowimg, class: 'img-responsive center-block' %> + <% end %> <%= localize(experiment.created_at, format: t('time.formats.full_date')) %> diff --git a/config/routes.rb b/config/routes.rb index 8c8b15afb..17a14c84d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -111,7 +111,7 @@ Rails.application.routes.draw do get 'users/edit', to: 'user_projects#index_edit' end - resources :experiments, only: :show do + resources :experiments do member do get 'canvas' # Overview/structure for single experiment # AJAX-loaded canvas edit mode (from canvas) diff --git a/db/migrate/20160808083040_add_attachment_workflowimg_to_experiments.rb b/db/migrate/20160808083040_add_attachment_workflowimg_to_experiments.rb new file mode 100644 index 000000000..094296915 --- /dev/null +++ b/db/migrate/20160808083040_add_attachment_workflowimg_to_experiments.rb @@ -0,0 +1,11 @@ +class AddAttachmentWorkflowimgToExperiments < ActiveRecord::Migration + def self.up + change_table :experiments do |t| + t.attachment :workflowimg + end + end + + def self.down + remove_attachment :experiments, :workflowimg + end +end diff --git a/db/schema.rb b/db/schema.rb index dae069f44..2fd020478 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160803082801) do +ActiveRecord::Schema.define(version: 20160808083040) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -138,18 +138,22 @@ ActiveRecord::Schema.define(version: 20160803082801) do add_index "delayed_jobs", ["queue"], name: "delayed_jobs_queue", using: :btree create_table "experiments", force: :cascade do |t| - t.string "name", null: false + t.string "name", null: false t.text "description" - t.integer "project_id", null: false - t.integer "created_by_id", null: false - t.integer "last_modified_by_id", null: false - t.boolean "archived", default: false, null: false + t.integer "project_id", null: false + t.integer "created_by_id", null: false + t.integer "last_modified_by_id", null: false + t.boolean "archived", default: false, null: false t.integer "archived_by_id" t.datetime "archived_on" t.integer "restored_by_id" t.datetime "restored_on" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "workflowimg_file_name" + t.string "workflowimg_content_type" + t.integer "workflowimg_file_size" + t.datetime "workflowimg_updated_at" end add_index "experiments", ["archived_by_id"], name: "index_experiments_on_archived_by_id", using: :btree