Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
relf committed Jun 10, 2020
1 parent 971c06b commit 92cfb74
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 142 deletions.
2 changes: 1 addition & 1 deletion app/controllers/analyses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def destroy
@mda.destroy!
redirect_to mdas_url, notice: "Analysis #{@mda.name} was successfully deleted."
end
rescue Operation::ForbiddenRemovalException => exc
rescue Operation::ForbiddenRemovalError => exc
redirect_to mdas_url, alert: "Can not delete analysis #{@mda.name} for following reason: "+exc.message
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/meta_models_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def create
@meta_model = mda.disciplines.last.build_meta_model( # just one plain discipline in the analysis
operation: mm_ope,
default_surrogate_kind: meta_model_params[:kind],
default_options_attributes: meta_model_params[:options] || []
default_options_attributes: meta_model_params[:options] || []
)
@meta_model.build_surrogates
if @meta_model.save
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/exception_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module ExceptionHandler
json_response({ message: e.message }, :unprocessable_entity)
end

rescue_from Operation::ForbiddenRemovalException do |e|
rescue_from Operation::ForbiddenRemovalError do |e|
Rails.logger.error "Operation forbidden removal: " + e.message
json_response({ message: e.message }, :forbidden)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/operations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def destroy
begin
@ope.destroy
redirect_to mdas_url, notice: "Operation was successfully destroyed."
rescue Operation::ForbiddenRemovalException => exc
rescue Operation::ForbiddenRemovalError => exc
redirect_to mdas_url, alert: exc.message
end
end
Expand Down
18 changes: 11 additions & 7 deletions app/models/analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class Analysis < ApplicationRecord

include WhatsOpt::OpenmdaoModule
include Ownable

resourcify

has_rich_text :note

has_ancestry

class AncestorUpdateError < StandardError
end
class AncestorUpdateError < StandardError; end

class ForbiddenRemovalError < StandardError; end

has_many :disciplines, -> { includes(:variables).order(position: :asc) }, dependent: :destroy
accepts_nested_attributes_for :disciplines,
Expand All @@ -24,8 +24,8 @@ class AncestorUpdateError < StandardError
has_one :analysis_discipline, dependent: :destroy
has_one :super_discipline, through: :analysis_discipline, source: :discipline

has_one :meta_model_prototype, foreign_key: :prototype_id
has_one :meta_model, through: :meta_model_prototype, inverse_of: :prototype
has_many :meta_model_prototypes, foreign_key: :prototype_id, dependent: :destroy
has_many :meta_models, through: :meta_model_prototypes, inverse_of: :prototype

has_many :operations, dependent: :destroy

Expand Down Expand Up @@ -60,7 +60,7 @@ def is_metamodel?
end

def is_metamodel_prototype?
Analysis.where(id: self).joins(:meta_model_prototype).count > 0
disciplines.nodes.count == 1 && disciplines.first.is_metamodel_prototype?
end

def uq_mode?
Expand Down Expand Up @@ -633,6 +633,10 @@ def _ensure_openmdao_impl_presence
end

def _check_allowed_destruction
# to do check ancestry: forbid if parent
if is_metamodel_prototype?
mms = self.meta_models
msg = mms.map {|mm| "##{mm.analysis.id} #{mm.analysis.name}"}.join(', ')
raise ForbiddenRemovalError.new("Can not delete analysis '#{self.name}' as it is a prototype for metamodels in use in #{msg}")
end
end
end
7 changes: 7 additions & 0 deletions app/models/meta_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MetaModel < ApplicationRecord
has_many :surrogates, dependent: :destroy

validates :discipline, presence: true
validates :prototype, presence: true

after_initialize :_set_defaults
before_destroy :_destroy_related_operation
Expand Down Expand Up @@ -49,6 +50,10 @@ def analysis
discipline.analysis # a metamodel a no existence out of analysis context
end

def is_primary?
prototype.blank?
end

def build_surrogates
opts = default_options.map {|o| {name: o[:name], value: o[:value]}}
analysis.response_variables.each do |v|
Expand All @@ -60,6 +65,8 @@ def build_surrogates

def create_copy!(mda=nil, discipline=nil)
mm_copy = self.dup
# if no prototype it is a primary created meta_model
# so the prototype of the copy is the analysis of this metamodel
mm_copy.prototype = prototype || analysis
if discipline
mm_copy.discipline = discipline
Expand Down
9 changes: 2 additions & 7 deletions app/models/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Operation < ApplicationRecord
BATCH_COUNT = 10 # nb of log lines processed together
LOGDIR = File.join(Rails.root, "upload/logs")

class ForbiddenRemovalException < Exception; end
class ForbiddenRemovalError < StandardError; end

belongs_to :analysis
has_many :options, as: :optionizable, dependent: :destroy
Expand Down Expand Up @@ -351,14 +351,9 @@ def _update_cases(case_attrs)
end

def _check_allowed_destruction
# unless self.meta_models.empty?
# mdas = self.meta_models.map(&:analysis)
# msg = mdas.map {|a| "##{a.id} #{a.name}"}.join(', ')
# raise ForbiddenRemovalException.new("Can not delete operation '#{self.name}' as meta_models are in use: #{msg} (to be deleted first)")
# end
unless self.derived_operations.empty?
msg = self.derived_operations.map(&:name).join(', ')
raise ForbiddenRemovalException.new("Can not delete operation '#{self.name}' as another operation depends on it: #{msg} (to be deleted first)")
raise ForbiddenRemovalError.new("Can not delete operation '#{self.name}' as another operation depends on it: #{msg} (to be deleted first)")
end
end
end
3 changes: 2 additions & 1 deletion app/policies/meta_model_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def resolve
if user.admin?
scope
else
scope.joins(:meta_model_prototype).where(meta_model_prototypes: {prototype: AnalysisPolicy::Scope.new(user, Analysis).resolve.roots})
scope.where.not(id: scope.joins(:meta_model_prototype))
.joins(:discipline).where(disciplines: {analysis_id: AnalysisPolicy::Scope.new(user, Analysis).resolve})
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/serializers/meta_model_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def name
object.analysis.name
end

def owner_email
def owner_email
p object
object.prototype.owner.email
object.analysis.owner.email
end

def note
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/api/v1/analyses_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class Api::V1::AnalysesControllerTest < ActionDispatch::IntegrationTest
get api_v1_mdas_url, as: :json, headers: @auth_headers
assert_response :success
analyses = JSON.parse(response.body)
assert_equal Analysis.count-2, analyses.size # ALL - {one user3 private, one sub-analysis}
assert_equal Analysis.count-2, analyses.size # ALL - {user2 private, one sub-analysis}
end

test "should get all mdas even sub ones" do
get api_v1_mdas_url(with_sub_analyses: true), as: :json, headers: @auth_headers
assert_response :success
analyses = JSON.parse(response.body)
assert_equal Analysis.count-1, analyses.size # ALL - {one user3 private}
assert_equal Analysis.count-1, analyses.size # ALL - {user2 private}
end

test "should create a mda" do
Expand Down
18 changes: 10 additions & 8 deletions test/controllers/api/v1/meta_models_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,42 @@ class Api::V1::MetaModelsControllerTest < ActionDispatch::IntegrationTest
get api_v1_meta_models_url, as: :json, headers: @auth_headers
assert_response :success
mms = JSON.parse(response.body)
assert_equal 3, mms.count
assert_equal 2, mms.count # out of 2 primary mm, one is private for user3 and user1 member
assert_equal ["created_at", "id", "name", "owner_email"], mms.first.keys.sort
end

test "should get list of metamodels for user2" do
get api_v1_meta_models_url, as: :json, headers: @auth_headers2
assert_response :success
mms = JSON.parse(response.body)
assert_equal 3, mms.count # user2 is memeber of cicav_meta_model2
assert_equal 1, mms.count # out of 2 primary mm, one is private for user3 and user1 member
end

test "should get list of metamodels for user3" do
get api_v1_meta_models_url, as: :json, headers: @auth_headers3
assert_response :success
mms = JSON.parse(response.body)
assert_equal 1, mms.count # mm_proto_cicav_meta_model is private
assert_equal 1, mms.count # out of 2 primary mm, one is private for user3 and user1 member
end

test "should show a metamodel" do
mm = meta_models(:cicav_metamodel)
get api_v1_meta_model_url(mm), as: :json, headers: @auth_headers
assert_response :success
mminfos = JSON.parse(response.body)
assert_equal ["created_at", "id", "name", "owner_email"], mminfos.keys.sort
assert_equal ["created_at", "id", "name", "note", "owner_email"], mminfos.keys.sort
end

test "should create a metamodel" do
assert_difference("Analysis.count", 1) do
assert_difference("Operation.count", 2) do # doe copy + metamodel
assert_difference("MetaModel.count", 1) do
assert_difference("Surrogate.count", 1) do
post api_v1_operation_meta_models_url(@ope),
params: { meta_model: { kind: Surrogate::SMT_KPLS } },
as: :json, headers: @auth_headers
assert_difference("MetaModelPrototype.count", 1) do
assert_difference("Surrogate.count", 1) do
post api_v1_operation_meta_models_url(@ope),
params: { meta_model: { kind: Surrogate::SMT_KPLS } },
as: :json, headers: @auth_headers
end
end
end
end
Expand Down
11 changes: 2 additions & 9 deletions test/fixtures/analyses.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,11 @@ cicav_metamodel_analysis:

cicav_metamodel2_analysis:
name: CicavMetaModel2
public: false

singleton:
name: singleton

singleton_mm:
name: singleton_mm

mm_proto_cicav_metamodel:
name: CicavMetaModelProto
public: false

mm_proto_singleton_mm:
name: SingletonMMProto
name: SingletonMetaModel
public: false

23 changes: 0 additions & 23 deletions test/fixtures/disciplines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,5 @@ disc_singleton_mm:
type: metamodel
analysis: singleton_mm
position: 1

mm_proto_driver_cicav_metamodel:
name: __DRIVER__
type: null_driver
analysis: mm_proto_cicav_metamodel_analysis
position: 0

mm_proto_disc_cicav_metamodel:
name: MetaModelDisc
type: metamodel
analysis: mm_proto_cicav_metamodel_analysis
position: 1

mm_proto_driver_singleton_mm:
name: __DRIVER__
type: null_driver
analysis: mm_proto_singleton_mm
position: 0

mm_proto_disc_singleton_mm:
name: SingletonDisciplineMM
type: metamodel
analysis: mm_proto_singleton_mm
position: 1

10 changes: 1 addition & 9 deletions test/fixtures/meta_model_prototype.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
cicav_metamodel_prototype:
meta_model: cicav_metamodel
prototype: mm_proto_cicav_metamodel

cicav_metamodel2_prototype:
meta_model: cicav_metamodel2
prototype: mm_proto_cicav_metamodel

singleton_mm_prototype:
meta_model: singleton_mm_metamodel
prototype: mm_proto_singleton_mm
prototype: cicav_metamodel_analysis
10 changes: 10 additions & 0 deletions test/fixtures/meta_models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ cicav_metamodel2:
operation: mm_from_doe2
default_surrogate_kind: SMT_KRIGING

mm_proto_cicav_metamodel:
discipline: mm_proto_disc_cicav_metamodel
operation: mm_from_doe
default_surrogate_kind: SMT_KRIGING

singleton_mm_metamodel:
discipline: disc_singleton_mm
operation: doe_singleton_build_mm
default_surrogate_kind: OPENTURNS_PCE

mm_proto_singleton_mm_metamodel:
discipline: mm_proto_disc_singleton_mm
operation: doe_singleton_build_mm
default_surrogate_kind: OPENTURNS_PCE
14 changes: 3 additions & 11 deletions test/fixtures/roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ owner_cicav_mda_role:
owner_cicav_meta_model_role:
name: owner
resource: cicav_metamodel_analysis (Analysis)

owner_mm_proto_cicav_meta_model_role:
name: owner
resource: mm_proto_cicav_metamodel (Analysis)

owner_cicav_meta_model2_role:
name: owner
Expand All @@ -21,10 +17,6 @@ member_cicav_meta_model2_role:
name: member
resource: cicav_metamodel2_analysis (Analysis)

member_mm_proto_cicav_meta_model_role:
name: member
resource: mm_proto_cicav_metamodel (Analysis)

owner_outermda_role:
name: owner
resource: outermda (Analysis)
Expand Down Expand Up @@ -53,9 +45,9 @@ owner_singleton_mm_role:
name: owner
resource: singleton_mm (Analysis)

owner_mm_proto_singleton_mm_role:
name: owner
resource: mm_proto_singleton_mm (Analysis)
member_singleton_mm_role:
name: member
resource: singleton_mm (Analysis)

owner_optim_ackley2d_role:
name: owner
Expand Down
12 changes: 2 additions & 10 deletions test/fixtures/users_roles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,10 @@ users_roles7:
users_roles71:
user: user2
role: owner_cicav_meta_model2_role

users_roles711:
user: user2
role: owner_mm_proto_cicav_meta_model_role

users_roles72:
user: user1
role: member_cicav_meta_model2_role

users_roles72:
user: user1
role: member_mm_proto_cicav_meta_model_role

users_roles8:
user: user3
Expand All @@ -55,8 +47,8 @@ users_roles13:
role: owner_singleton_mm_role

users_roles14:
user: user3
role: owner_mm_proto_singleton_mm_role
user: user1
role: member_singleton_mm_role

users_roles_optim_ackley2d:
user: user1
Expand Down
Loading

0 comments on commit 92cfb74

Please sign in to comment.