OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add conversion job and service
Oliver Günthere4d847b83627
diff --git a/modules/ifc_models/app/models/ifc_models/ifc_model.rb b/modules/ifc_models/app/models/ifc_models/ifc_model.rbindex fee25318289..6e75886828b 100644--- a/modules/ifc_models/app/models/ifc_models/ifc_model.rb+++ b/modules/ifc_models/app/models/ifc_models/ifc_model.rb@@ -17,7 +17,7 @@ module IFCModelsdelete_attachment :metadataend- delete_attachment :ifc+ delete_attachment nameattach_files('first' => { 'file' => file, 'description' => name })endenddiff --git a/modules/ifc_models/app/services/ifc_models/view_converter_service.rb b/modules/ifc_models/app/services/ifc_models/view_converter_service.rbindex 6b805c5a2e7..0e2b2148e34 100644--- a/modules/ifc_models/app/services/ifc_models/view_converter_service.rb+++ b/modules/ifc_models/app/services/ifc_models/view_converter_service.rb@@ -29,56 +29,36 @@ require 'open3'module IFCModelsclass ViewConverterService+ attr_reader :ifc_model, :errors- ##- # Check availability of the pipeline- def self.available?- available_commands.length == pipeline_commands.length+ PIPELINE_COMMANDS ||= %w[IfcConvert COLLADA2GLTF gltf2xkt xeokit-metadata].freeze+++ def initialize(ifc_model)+ @errors = ActiveModel::Errors.new(self)+ @ifc_model = ifc_modelend- def self.pipeline_commands- %w[IfcConvert COLLADA2GLTF gltf2xkt xeokit-metadata]+ ##+ # Check availability of the pipeline+ def available?+ available_commands.length == PIPELINE_COMMANDS.lengthend- def self.available_commands+ def available_commands@available ||= begin- pipeline_commands.select do |command|+ PIPELINE_COMMANDS.select do |command|_, status = Open3.capture2e('which', command)status.exitstatus == 0endendend- attr_reader :ifc_model, :errors-- def initialize(ifc_model)- @errors = ActiveModel::Errors.new(self)- @ifc_model = ifc_model- end-def callvalidate!Dir.mktmpdir do |dir|- # Step 1: IfcConvert- Rails.logger.debug { "Converting #{ifc_model.inspect} to DAE"}- ifc_file = ifc_model.ifc_attachment.diskfile.path- collada_file = convert_to_collada(ifc_file, dir)-- # Step 2: Collada2GLTF- Rails.logger.debug { "Converting #{ifc_model.inspect} to GLTF"}- gltf_file = convert_to_gltf(collada_file, dir)-- # Step 3: Convert to XKT- Rails.logger.debug { "Converting #{ifc_model.inspect} to XKT"}- xkt_file = convert_to_xkt(gltf_file, dir)- ifc_model.xkt_attachment = File.new xkt_file-- # Convert metadata- Rails.logger.debug { "Retrieving metadata of #{ifc_model.inspect}"}- metadata_file = convert_metadata(ifc_file, dir)- ifc_model.metadata_attachment = File.new metadata_file-+ perform_conversion!(dir)if ifc_model.saveServiceResult.new(success: true, result: ifc_model)else@@ -90,6 +70,27 @@ module IFCModelsServiceResult.new(success: false).tap { |r| r.errors.add(:base, e.message) }end+ def perform_conversion!(dir)+ # Step 1: IfcConvert+ Rails.logger.debug { "Converting #{ifc_model.inspect} to DAE"}+ ifc_file = ifc_model.ifc_attachment.diskfile.path+ collada_file = convert_to_collada(ifc_file, dir)++ # Step 2: Collada2GLTF+ Rails.logger.debug { "Converting #{ifc_model.inspect} to GLTF"}+ gltf_file = convert_to_gltf(collada_file, dir)++ # Step 3: Convert to XKT+ Rails.logger.debug { "Converting #{ifc_model.inspect} to XKT"}+ xkt_file = convert_to_xkt(gltf_file, dir)+ ifc_model.xkt_attachment = File.new xkt_file++ # Convert metadata+ Rails.logger.debug { "Retrieving metadata of #{ifc_model.inspect}"}+ metadata_file = convert_metadata(ifc_file, dir)+ ifc_model.metadata_attachment = File.new metadata_file+ end+### Call IfcConvert with an IFC file to output an identically-named# DAE collada file.@@ -152,8 +153,8 @@ module IFCModelsenddef validate!- unless self.class.available?- missing = self.class.pipeline_commands - self.class.available_commands+ unless available?+ missing = PIPELINE_COMMANDS - available_commandsraise I18n.t('ifc_models.conversion.missing_commands', names: missing.join(", "))enddiff --git a/modules/ifc_models/app/workers/ifc_models/ifc_conversion_job.rb b/modules/ifc_models/app/workers/ifc_models/ifc_conversion_job.rbindex 34955be523d..43a9eb6cf01 100644--- a/modules/ifc_models/app/workers/ifc_models/ifc_conversion_job.rb+++ b/modules/ifc_models/app/workers/ifc_models/ifc_conversion_job.rb@@ -1,11 +1,16 @@module IFCModelsclass IFCConversionJob < ::ApplicationJob- queue+ queue_as :ifc_conversion### Run the conversion of IFC todef perform(ifc_model)+ result = ViewConverterService.new(ifc_model).call+ unless result.success?+ errors = result.errors.full_messages.join(". ")+ Rails.logger.error "Failed to convert IFC model #{ifc_model.inspect}: #{errors}"+ endendendenddiff --git a/modules/ifc_models/spec/factories/ifc_model_factory.rb b/modules/ifc_models/spec/factories/ifc_model_factory.rbnew file mode 100644index 00000000000..19d921d77eb--- /dev/null+++ b/modules/ifc_models/spec/factories/ifc_model_factory.rb@@ -0,0 +1,46 @@+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++FactoryBot.define do+ factory :ifc_model, class: ::IFCModels::IFCModel do+ title { 'My IFC model' }+ uploader factory: :user+ ifc_attachment {+ FileHelpers.mock_uploaded_file name: "model.ifc", content_type: 'application/binary', binary: true+ }++ factory :ifc_model_converted do+ xkt_attachment {+ FileHelpers.mock_uploaded_file name: "model.xkt", content_type: 'application/binary', binary: true+ }+ metadata_attachment {+ FileHelpers.mock_uploaded_file name: "model.json", content_type: 'application/json', binary: true+ }+ end+ end+enddiff --git a/modules/ifc_models/spec/services/view_conversion_service_spec.rb b/modules/ifc_models/spec/services/view_conversion_service_spec.rbnew file mode 100644index 00000000000..61404f83c8b--- /dev/null+++ b/modules/ifc_models/spec/services/view_conversion_service_spec.rb@@ -0,0 +1,86 @@+require 'spec_helper'++describe IFCModels::ViewConverterService do+ let(:model) { FactoryBot.build :ifc_model }+ subject { described_class.new(model) }++ shared_context 'available pipeline commands' do |available|+ before do+ # Mock the call to Open3 to test available commands+ allow(Open3)+ .to(receive(:capture2e))+ .with('which', any_args)+ .and_wrap_original do |_, *args|++ matches =+ if available.is_a?(Array)+ available.include?(args[1])+ else+ available+ end++ result = OpenStruct.new(exitstatus: matches ? 0 : 1)+ ["irrelevant output", result]+ end+ end+ end++ describe '#available_commands' do+ context 'with only one available command' do+ include_context 'available pipeline commands', %w[IfcConvert]++ it 'is not available' do+ expect(subject).not_to be_available+ end+ end++ context 'with all available commands' do+ include_context 'available pipeline commands', described_class::PIPELINE_COMMANDS++ it 'is available' do+ expect(subject).to be_available+ end+ end+ end++ describe '#call' do++ context 'if not available?' do+ include_context 'available pipeline commands', false++ it 'returns an error' do+ expect(subject).not_to be_available+ result = subject.call+ expect(result.errors[:base].first).to include 'The following IFC converter commands are missing'+ end+ end++ context 'if available' do+ before do+ allow(subject).to receive(:available?).and_return true+ end++ it 'calls the conversion and returns save result' do+ expect(subject)+ .to(receive(:perform_conversion!))++ expect(model)+ .to(receive(:save))+ .and_return(true)++ expect(subject.call).to be_success+ end++ it 'calls the conversion and returns error' do+ expect(subject)+ .to(receive(:perform_conversion!))++ expect(model)+ .to(receive(:save))+ .and_return(false)++ expect(subject.call).not_to be_success+ end+ end+ end+end\ No newline at end of filediff --git a/modules/ifc_models/spec/workers/ifc_conversion_job_spec.rb b/modules/ifc_models/spec/workers/ifc_conversion_job_spec.rbnew file mode 100644index 00000000000..128d596f38e--- /dev/null+++ b/modules/ifc_models/spec/workers/ifc_conversion_job_spec.rb@@ -0,0 +1,14 @@+require 'spec_helper'++describe IFCModels::IFCConversionJob, type: :job do+ let(:model) { FactoryBot.build :ifc_model }+ subject { described_class.perform_now(model) }++ it 'calls the conversion service' do+ expect(::IFCModels::ViewConverterService)+ .to receive_message_chain(:new, :call)+ .and_return ServiceResult.new success: true++ expect { subject }.not_to raise_error+ end+end\ No newline at end of file