Commit 4a5093e8 by Ngô Trung Hưng

fix -part 5

parent cde1c3b4
Pipeline #733 failed with stages
in 0 seconds
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class City < ApplicationRecord class City < ApplicationRecord
has_many :city_jobs has_many :city_jobs
has_many :jobs, through: :city_jobs has_many :jobs, through: :city_jobs
end end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
default: &default default: &default
adapter: mysql2 adapter: mysql2
encoding: utf8 encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root username: root
password: '1' password: '1'
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateCompanies < ActiveRecord::Migration[5.2] class CreateCompanies < ActiveRecord::Migration[5.2]
def change def change
create_table :companies, options: 'COLLATE=utf8_general_ci' do |t| create_table :companies do |t|
t.string :name t.string :name
t.string :address t.string :address
t.text :short_description t.text :short_description
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateCities < ActiveRecord::Migration[5.2] class CreateCities < ActiveRecord::Migration[5.2]
def change def change
create_table :cities, options: 'COLLATE=utf8_general_ci' do |t| create_table :cities do |t|
t.string :name t.string :name
t.boolean :area t.boolean :area
t.timestamps t.timestamps
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateIndustries < ActiveRecord::Migration[5.2] class CreateIndustries < ActiveRecord::Migration[5.2]
def change def change
create_table :industries, options: 'COLLATE=utf8_general_ci' do |t| create_table :industries do |t|
t.string :name t.string :name
t.timestamps t.timestamps
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateJobs < ActiveRecord::Migration[5.2] class CreateJobs < ActiveRecord::Migration[5.2]
def change def change
create_table :jobs, options: 'COLLATE=utf8_general_ci' do |t| create_table :jobs do |t|
t.string :name t.string :name
t.integer :company_id t.integer :company_id
t.string :level t.string :level
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateHistories < ActiveRecord::Migration[5.2] class CreateHistories < ActiveRecord::Migration[5.2]
def change def change
create_table :histories, options: 'COLLATE=utf8_general_ci' do |t| create_table :histories do |t|
t.integer :user_id t.integer :user_id
t.integer :job_id t.integer :job_id
t.timestamps t.timestamps
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateFavorites < ActiveRecord::Migration[5.2] class CreateFavorites < ActiveRecord::Migration[5.2]
def change def change
create_table :favorites, options: 'COLLATE=utf8_general_ci' do |t| create_table :favorites do |t|
t.integer :user_id t.integer :user_id
t.integer :job_id t.integer :job_id
t.timestamps t.timestamps
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateUsers < ActiveRecord::Migration[5.2] class CreateUsers < ActiveRecord::Migration[5.2]
def change def change
create_table :users, options: 'COLLATE=utf8_general_ci' do |t| create_table :users do |t|
t.string :email t.string :email
t.string :name t.string :name
t.string :password_digest t.string :password_digest
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateCityJobs < ActiveRecord::Migration[5.2] class CreateCityJobs < ActiveRecord::Migration[5.2]
def change def change
create_table :city_jobs, options: 'COLLATE=utf8_general_ci' do |t| create_table :city_jobs do |t|
t.references :job t.references :job
t.references :city t.references :city
t.timestamps t.timestamps
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateIndustryJobs < ActiveRecord::Migration[5.2] class CreateIndustryJobs < ActiveRecord::Migration[5.2]
def change def change
create_table :industry_jobs, options: 'COLLATE=utf8_general_ci' do |t| create_table :industry_jobs do |t|
t.references :industry t.references :industry
t.references :job t.references :job
t.timestamps t.timestamps
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# Description/Explanation of Person class # Description/Explanation of Person class
class CreateAppliedJobs < ActiveRecord::Migration[5.2] class CreateAppliedJobs < ActiveRecord::Migration[5.2]
def change def change
create_table :applied_jobs, options: 'COLLATE=utf8_general_ci' do |t| create_table :applied_jobs do |t|
t.references :user t.references :user
t.references :job t.references :job
t.string :name t.string :name
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
ActiveRecord::Schema.define(version: 2020_07_28_021412) do ActiveRecord::Schema.define(version: 2020_07_28_021412) do
create_table "applied_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "applied_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.bigint "user_id" t.bigint "user_id"
t.bigint "job_id" t.bigint "job_id"
t.string "name" t.string "name"
...@@ -24,14 +24,14 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do ...@@ -24,14 +24,14 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.index ["user_id"], name: "index_applied_jobs_on_user_id" t.index ["user_id"], name: "index_applied_jobs_on_user_id"
end end
create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "cities", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "name" t.string "name"
t.boolean "area" t.boolean "area"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "city_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "city_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.bigint "job_id" t.bigint "job_id"
t.bigint "city_id" t.bigint "city_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
...@@ -40,7 +40,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do ...@@ -40,7 +40,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.index ["job_id"], name: "index_city_jobs_on_job_id" t.index ["job_id"], name: "index_city_jobs_on_job_id"
end end
create_table "companies", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "companies", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "name" t.string "name"
t.string "address" t.string "address"
t.text "short_description" t.text "short_description"
...@@ -48,27 +48,27 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do ...@@ -48,27 +48,27 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
t.integer "job_id" t.integer "job_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "histories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "histories", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.integer "user_id" t.integer "user_id"
t.integer "job_id" t.integer "job_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "industries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "industries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "name" t.string "name"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "industry_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "industry_jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.bigint "industry_id" t.bigint "industry_id"
t.bigint "job_id" t.bigint "job_id"
t.datetime "created_at", null: false t.datetime "created_at", null: false
...@@ -77,7 +77,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do ...@@ -77,7 +77,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.index ["job_id"], name: "index_industry_jobs_on_job_id" t.index ["job_id"], name: "index_industry_jobs_on_job_id"
end end
create_table "jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "jobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "name" t.string "name"
t.integer "company_id" t.integer "company_id"
t.string "level" t.string "level"
...@@ -90,7 +90,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do ...@@ -90,7 +90,7 @@ ActiveRecord::Schema.define(version: 2020_07_28_021412) do
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.string "email" t.string "email"
t.string "name" t.string "name"
t.string "password_digest" t.string "password_digest"
......
...@@ -13,8 +13,8 @@ class FtpSever ...@@ -13,8 +13,8 @@ class FtpSever
NAME_CSV = 'jobs.zip' NAME_CSV = 'jobs.zip'
def data_csv def data_csv
donwload_csv download_csv
CSV.parse(File.read('lib/csv/jobs.csv'), headers: true) CSV.parse(File.read(Rails.root.join('lib', 'csv', 'jobs.csv')), headers: true)
end end
def logger def logger
...@@ -23,12 +23,16 @@ class FtpSever ...@@ -23,12 +23,16 @@ class FtpSever
private private
def donwload_csv def jobs_csv_path
Rails.root.join(NAME_CSV)
end
def download_csv
Net::FTP.open(CONTENT_SERVER_DOMAIN_NAME, CONTENT_SERVER_USER_NAME, CONTENT_SERVER_USER_PASSWORD) do |ftp| Net::FTP.open(CONTENT_SERVER_DOMAIN_NAME, CONTENT_SERVER_USER_NAME, CONTENT_SERVER_USER_PASSWORD) do |ftp|
ftp.getbinaryfile(NAME_CSV) ftp.getbinaryfile(NAME_CSV)
Unzip.extract_zip(Rails.root.join(NAME_CSV), Rails.root.join('lib', 'csv')) Unzip.extract_zip((jobs_csv_path), Rails.root.join('lib', 'csv'))
File.delete(Rails.root.join(NAME_CSV)) if File.exist?(Rails.root.join(NAME_CSV)) File.delete(jobs_csv_path) if File.exist?(jobs_csv_path)
logger.info 'Donwload & extract success' logger.info 'Download & extract success'
end end
end end
end end
...@@ -15,10 +15,13 @@ class ImportData ...@@ -15,10 +15,13 @@ class ImportData
import_jobs_from(csv_data) import_jobs_from(csv_data)
end end
def logger
@logger ||= Logger.new(Rails.root.join('log', 'import_data_csv.log'))
end
private private
def import_industries_from(csv) def import_industries_from(csv)
puts 'Import data industries . . .'
industries = [] industries = []
industries += csv['category'].map(&:strip) industries += csv['category'].map(&:strip)
industries.each do |val| industries.each do |val|
...@@ -26,30 +29,30 @@ class ImportData ...@@ -26,30 +29,30 @@ class ImportData
val.gsub!('/', ' / ') val.gsub!('/', ' / ')
Industry.find_or_create_by(name: val) Industry.find_or_create_by(name: val)
end end
puts 'Done parse csv industries' rescue StandardError => e
logger.error "Import_industries: #{e}"
end end
def import_cities_from(csv) def import_cities_from(csv)
puts 'Import data cities . . .' city_names = csv['work place'].uniq.select(&:present?)
cities = csv['work place'].uniq.select(&:present?) cities = city_names.map { |val| val.delete('[]\"') }
cities = cities.map { |val| val.delete('[]\"') }
cities.each do |val| cities.each do |val|
next if val.blank? next if val.blank?
City.find_or_create_by(name: val) { |city| city.area = DOMESTIC } City.find_or_create_by(name: val) { |city| city.area = DOMESTIC }
end end
rescue StandardError => e
logger.error "Import_cities: #{e}"
end end
def import_companies_from(csv) def import_companies_from(csv)
puts 'Import data companies . . .'
csv['company name'].each_with_index do |name, index| csv['company name'].each_with_index do |name, index|
Company.find_or_create_by(name: name.strip) do |company| Company.find_or_create_by(name: name.strip) do |company|
company.address = csv['company address'][index] company.address = csv['company address'][index]
company.short_description = csv['benefit'][index] company.short_description = csv['benefit'][index]
end end
puts index
rescue StandardError => e rescue StandardError => e
puts e logger.error "Import_companies: #{e}"
end end
end end
...@@ -57,34 +60,25 @@ class ImportData ...@@ -57,34 +60,25 @@ class ImportData
csv['name'].each_with_index do |name, index| csv['name'].each_with_index do |name, index|
desc = "#{csv['requirement'][index]} #{(csv['description'][index])}" desc = "#{csv['requirement'][index]} #{(csv['description'][index])}"
company = Company.find_by name: csv['company name'][index].to_s.strip company = Company.find_by name: csv['company name'][index].to_s.strip
company_id = company.blank? ? COMPANY_SECURITY : company.id company_id = company.try(:id) || COMPANY_SECURITY
begin begin
job = Job.create!(name: name, job = Job.create(name: name,
company_id: company_id, company_id: company_id,
level: csv['level'][index], level: csv['level'][index],
salary: csv['salary'][index], salary: csv['salary'][index],
create_date: Time.now, create_date: Time.now,
description: desc) description: desc)
make_foreign_cities_table(csv['work place'][index], job.id) city_names = csv['work place'][index].to_s.delete('[]\"')
make_foreign_industries_table(csv['category'][index], job.id) city = City.find_or_create_by(name: city_names.strip) { |record| record.area = DOMESTIC }
job.cities << city
industry_name = csv['category'][index].to_s.gsub(',', '/').gsub('/', ' / ')
industry = Industry.find_or_create_by(name: industry_name.strip)
job.industries << industry
puts index puts index
rescue StandardError => e rescue StandardError => e
puts e logger.error "Import_jobs: #{e}"
end end
end end
end end
def make_foreign_cities_table(data, id_job)
data = data.to_s.delete('[]\"')
city = City.find_or_create_by(name: data.strip) { |record| record.area = DOMESTIC }
city_id = city.id
CityJob.create(job_id: id_job, city_id: city_id)
end
def make_foreign_industries_table(data, id_job)
industry_name = data.to_s.gsub(',', '/').gsub('/', ' / ')
industry = Industry.find_or_create_by(name: industry_name.strip)
industry_id = industry.id
IndustryJob.create(industry_id: industry_id, job_id: id_job)
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment