aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-03-07 08:38:38 +0100
committerZog2018-03-12 12:00:14 +0100
commit0b9269780989dbd69a9be9d63670aab12050cf60 (patch)
tree1c9d3c4c5c9a881b1ffb5402705941cf2d9318e0
parent565641838e020f992e58f05d94ab0097b74a27b2 (diff)
downloadchouette-core-0b9269780989dbd69a9be9d63670aab12050cf60.tar.bz2
Refs #6133; Exports naive implementation
-rw-r--r--app/models/concerns/iev_interfaces/task.rb43
-rw-r--r--app/models/export/base.rb25
-rw-r--r--app/models/export/message.rb8
-rw-r--r--app/models/export/resource.rb8
-rw-r--r--app/models/import/base.rb37
-rw-r--r--config/initializers/apartment.rb3
-rw-r--r--db/migrate/20180306152953_update_imports_names.rb7
-rw-r--r--db/migrate/20180307071448_create_new_exports.rb56
-rw-r--r--db/schema.rb61
9 files changed, 209 insertions, 39 deletions
diff --git a/app/models/concerns/iev_interfaces/task.rb b/app/models/concerns/iev_interfaces/task.rb
index 2eb0c532f..b142850fb 100644
--- a/app/models/concerns/iev_interfaces/task.rb
+++ b/app/models/concerns/iev_interfaces/task.rb
@@ -6,6 +6,11 @@ module IevInterfaces::Task
belongs_to :workbench, class_name: "::Workbench"
belongs_to :referential
+ mount_uploader :file, ImportUploader
+ validates :file, presence: true
+
+ has_many :children, foreign_key: :parent_id, class_name: self.name, dependent: :destroy
+
extend Enumerize
enumerize :status, in: %w(new pending successful warning failed running aborted canceled), scope: true, default: :new
@@ -20,6 +25,8 @@ module IevInterfaces::Task
end
scope :blocked, -> { where('created_at < ? AND status = ?', 4.hours.ago, 'running') }
+
+ before_create :initialize_fields
end
module ClassMethods
@@ -48,4 +55,40 @@ module IevInterfaces::Task
parent.child_change
update(notified_parent_at: DateTime.now)
end
+
+ def children_succeedeed
+ children.with_status(:successful, :warning).count
+ end
+
+ def update_status
+ status =
+ if children.where(status: self.class.failed_statuses).count > 0
+ 'failed'
+ elsif children.where(status: "warning").count > 0
+ 'warning'
+ elsif children.where(status: "successful").count == children.count
+ 'successful'
+ end
+
+ attributes = {
+ current_step: children.count,
+ status: status
+ }
+
+ if self.class.finished_statuses.include?(status)
+ attributes[:ended_at] = Time.now
+ end
+
+ update attributes
+ end
+
+ def child_change
+ return if self.class.finished_statuses.include?(status)
+
+ update_status
+ end
+
+ private
+ def initialize_fields
+ end
end
diff --git a/app/models/export/base.rb b/app/models/export/base.rb
new file mode 100644
index 000000000..d809055db
--- /dev/null
+++ b/app/models/export/base.rb
@@ -0,0 +1,25 @@
+class Export::Base < ActiveRecord::Base
+ self.table_name = "exports"
+
+ def self.messages_class_name
+ "Export::Message"
+ end
+
+ def self.resources_class_name
+ "Export::Resource"
+ end
+
+ include IevInterfaces::Task
+
+ def self.model_name
+ ActiveModel::Name.new Export::Base, Export::Base, "Export::Base"
+ end
+
+ private
+
+ def initialize_fields
+ super
+ self.token_upload = SecureRandom.urlsafe_base64
+ end
+
+end
diff --git a/app/models/export/message.rb b/app/models/export/message.rb
new file mode 100644
index 000000000..b64b524ac
--- /dev/null
+++ b/app/models/export/message.rb
@@ -0,0 +1,8 @@
+class Export::Message < ActiveRecord::Base
+ self.table_name = :export_messages
+
+ include IevInterfaces::Message
+
+ belongs_to :export, class_name: Export::Base
+ belongs_to :resource, class_name: Export::Resource
+end
diff --git a/app/models/export/resource.rb b/app/models/export/resource.rb
new file mode 100644
index 000000000..98f103be4
--- /dev/null
+++ b/app/models/export/resource.rb
@@ -0,0 +1,8 @@
+class Export::Resource < ActiveRecord::Base
+ self.table_name = :export_resources
+
+ include IevInterfaces::Resource
+
+ belongs_to :export, class_name: Export::Base
+ has_many :messages, class_name: "ExportMessage", foreign_key: :resource_id
+end
diff --git a/app/models/import/base.rb b/app/models/import/base.rb
index 0e1eae2c0..e2ae129be 100644
--- a/app/models/import/base.rb
+++ b/app/models/import/base.rb
@@ -11,51 +11,17 @@ class Import::Base < ActiveRecord::Base
include IevInterfaces::Task
- mount_uploader :file, ImportUploader
-
- has_many :children, foreign_key: :parent_id, class_name: "Import::Base", dependent: :destroy
-
- validates :file, presence: true
-
- before_create :initialize_fields
-
def self.model_name
ActiveModel::Name.new Import::Base, Import::Base, "Import::Base"
end
- def children_succeedeed
- children.with_status(:successful, :warning).count
- end
-
def child_change
return if self.class.finished_statuses.include?(status)
- update_status
+ super
update_referentials
end
- def update_status
- status =
- if children.where(status: self.class.failed_statuses).count > 0
- 'failed'
- elsif children.where(status: "warning").count > 0
- 'warning'
- elsif children.where(status: "successful").count == children.count
- 'successful'
- end
-
- attributes = {
- current_step: children.count,
- status: status
- }
-
- if self.class.finished_statuses.include?(status)
- attributes[:ended_at] = Time.now
- end
-
- update attributes
- end
-
def update_referentials
return unless self.class.finished_statuses.include?(status)
@@ -67,6 +33,7 @@ class Import::Base < ActiveRecord::Base
private
def initialize_fields
+ super
self.token_download = SecureRandom.urlsafe_base64
end
diff --git a/config/initializers/apartment.rb b/config/initializers/apartment.rb
index 53b27006a..6b817caed 100644
--- a/config/initializers/apartment.rb
+++ b/config/initializers/apartment.rb
@@ -36,6 +36,9 @@ Apartment.configure do |config|
'ComplianceControlBlock',
'ComplianceControlSet',
'CustomField',
+ 'Export::Base',
+ 'Export::Message',
+ 'Export::Resource',
'GenericAttributeControl::MinMax',
'GenericAttributeControl::Pattern',
'GenericAttributeControl::Uniqueness',
diff --git a/db/migrate/20180306152953_update_imports_names.rb b/db/migrate/20180306152953_update_imports_names.rb
index 086d5ce5a..42129a580 100644
--- a/db/migrate/20180306152953_update_imports_names.rb
+++ b/db/migrate/20180306152953_update_imports_names.rb
@@ -1,8 +1,13 @@
class UpdateImportsNames < ActiveRecord::Migration
def change
Import::Base.all.pluck(:type).uniq.each do |type|
- next if type =~ /^Imports/
+ next if type =~ /^Import/
Import::Base.where(type: type).update_all type: "Import::#{type.gsub 'Import', ''}"
end
+
+ Import::Base.all.pluck(:parent_type).uniq.each do |type|
+ next if type =~ /^Import/
+ Import::Base.where(parent_type: type).update_all parent_type: "Import::#{type.gsub 'Import', ''}"
+ end
end
end
diff --git a/db/migrate/20180307071448_create_new_exports.rb b/db/migrate/20180307071448_create_new_exports.rb
new file mode 100644
index 000000000..74921d108
--- /dev/null
+++ b/db/migrate/20180307071448_create_new_exports.rb
@@ -0,0 +1,56 @@
+class CreateNewExports < ActiveRecord::Migration
+ def change
+ create_table :exports do |t|
+ t.string "status"
+ t.string "current_step_id"
+ t.float "current_step_progress"
+ t.integer "workbench_id", limit: 8
+ t.integer "referential_id", limit: 8
+ t.string "name"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.string "file"
+ t.datetime "started_at"
+ t.datetime "ended_at"
+ t.string "token_upload"
+ t.string "type"
+ t.integer "parent_id", limit: 8
+ t.string "parent_type"
+ t.datetime "notified_parent_at"
+ t.integer "current_step", default: 0
+ t.integer "total_steps", default: 0
+ t.string "creator"
+ end
+
+ add_index "exports", ["referential_id"], name: "index_exports_on_referential_id", using: :btree
+ add_index "exports", ["workbench_id"], name: "index_exports_on_workbench_id", using: :btree
+
+ create_table "export_messages", id: :bigserial, force: :cascade do |t|
+ t.string "criticity"
+ t.string "message_key"
+ t.hstore "message_attributes"
+ t.integer "export_id", limit: 8
+ t.integer "resource_id", limit: 8
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.hstore "resource_attributes"
+ end
+
+ add_index "export_messages", ["export_id"], name: "index_export_messages_on_export_id", using: :btree
+ add_index "export_messages", ["resource_id"], name: "index_export_messages_on_resource_id", using: :btree
+
+ create_table "export_resources", id: :bigserial, force: :cascade do |t|
+ t.integer "export_id", limit: 8
+ t.string "status"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.string "resource_type"
+ t.string "reference"
+ t.string "name"
+ t.hstore "metrics"
+ end
+
+ add_index "export_resources", ["export_id"], name: "index_export_resources_on_export_id", using: :btree
+
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 53dbaceb2..094d2cb02 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,13 +11,14 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20180306152953) do
+ActiveRecord::Schema.define(version: 20180307071448) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
- enable_extension "postgis"
enable_extension "hstore"
+ enable_extension "postgis"
enable_extension "unaccent"
+ enable_extension "objectid"
create_table "access_links", id: :bigserial, force: :cascade do |t|
t.integer "access_point_id", limit: 8
@@ -90,9 +91,9 @@ ActiveRecord::Schema.define(version: 20180306152953) do
t.integer "organisation_id", limit: 8
t.datetime "created_at"
t.datetime "updated_at"
- t.integer "workgroup_id", limit: 8
t.integer "int_day_types"
t.date "excluded_dates", array: true
+ t.integer "workgroup_id", limit: 8
end
add_index "calendars", ["organisation_id"], name: "index_calendars_on_organisation_id", using: :btree
@@ -119,6 +120,7 @@ ActiveRecord::Schema.define(version: 20180306152953) do
t.datetime "updated_at"
t.date "end_date"
t.string "date_type"
+ t.string "mode"
end
add_index "clean_ups", ["referential_id"], name: "index_clean_ups_on_referential_id", using: :btree
@@ -299,6 +301,58 @@ ActiveRecord::Schema.define(version: 20180306152953) do
add_index "custom_fields", ["resource_type"], name: "index_custom_fields_on_resource_type", using: :btree
+ create_table "export_messages", id: :bigserial, force: :cascade do |t|
+ t.string "criticity"
+ t.string "message_key"
+ t.hstore "message_attributes"
+ t.integer "export_id", limit: 8
+ t.integer "resource_id", limit: 8
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.hstore "resource_attributes"
+ end
+
+ add_index "export_messages", ["export_id"], name: "index_export_messages_on_export_id", using: :btree
+ add_index "export_messages", ["resource_id"], name: "index_export_messages_on_resource_id", using: :btree
+
+ create_table "export_resources", id: :bigserial, force: :cascade do |t|
+ t.integer "export_id", limit: 8
+ t.string "status"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.string "resource_type"
+ t.string "reference"
+ t.string "name"
+ t.hstore "metrics"
+ end
+
+ add_index "export_resources", ["export_id"], name: "index_export_resources_on_export_id", using: :btree
+
+ create_table "exports", id: :bigserial, force: :cascade do |t|
+ t.string "status"
+ t.string "current_step_id"
+ t.float "current_step_progress"
+ t.integer "workbench_id", limit: 8
+ t.integer "referential_id", limit: 8
+ t.string "name"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.string "file"
+ t.datetime "started_at"
+ t.datetime "ended_at"
+ t.string "token_upload"
+ t.string "type"
+ t.integer "parent_id", limit: 8
+ t.string "parent_type"
+ t.datetime "notified_parent_at"
+ t.integer "current_step", default: 0
+ t.integer "total_steps", default: 0
+ t.string "creator"
+ end
+
+ add_index "exports", ["referential_id"], name: "index_exports_on_referential_id", using: :btree
+ add_index "exports", ["workbench_id"], name: "index_exports_on_workbench_id", using: :btree
+
create_table "facilities", id: :bigserial, force: :cascade do |t|
t.integer "stop_area_id", limit: 8
t.integer "line_id", limit: 8
@@ -753,6 +807,7 @@ ActiveRecord::Schema.define(version: 20180306152953) do
t.datetime "created_at"
t.datetime "updated_at"
t.string "objectid_format"
+ t.string "registration_number_format"
end
create_table "stop_areas", id: :bigserial, force: :cascade do |t|