Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
venjob_nth
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ngô Trung Hưng
venjob_nth
Commits
0f57f980
Commit
0f57f980
authored
Jul 28, 2020
by
Ngô Trung Hưng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix import data from csv
parent
12437310
Pipeline
#729
failed with stages
in 0 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
46 deletions
+48
-46
lib/service/ftp.rb
+7
-4
lib/service/import_data.rb
+32
-35
lib/service/unzip.rb
+8
-6
lib/tasks/csv.rake
+1
-1
No files found.
lib/service/ftp.rb
View file @
0f57f980
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
require
'service/unzip'
require
'service/unzip'
require
'net/ftp'
require
'net/ftp'
require
'logger'
require
'csv'
require
'csv'
# Access Ftp Server get file & extract file
# Access Ftp Server get file & extract file
...
@@ -9,15 +10,17 @@ class FtpSever
...
@@ -9,15 +10,17 @@ class FtpSever
CONTENT_SERVER_DOMAIN_NAME
=
'192.168.1.156'
CONTENT_SERVER_DOMAIN_NAME
=
'192.168.1.156'
CONTENT_SERVER_USER_NAME
=
'training'
CONTENT_SERVER_USER_NAME
=
'training'
CONTENT_SERVER_USER_PASSWORD
=
'training'
CONTENT_SERVER_USER_PASSWORD
=
'training'
@logger
||=
Logger
.
new
(
"
#{
Rails
.
root
}
/log/csv.log"
)
def
self
.
donwload_csv
def
self
.
donwload_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
|
begin
begin
ftp
.
getbinaryfile
(
'jobs.zip'
)
ftp
.
getbinaryfile
(
'jobs.zip'
)
extract_zip
(
'./jobs.zip'
,
'lib/csv'
)
Unzip
.
extract_zip
(
'./jobs.zip'
,
'lib/csv'
)
File
.
delete
(
'./jobs.zip'
)
if
File
.
exist?
(
'./jobs.zip'
)
File
.
delete
(
'./jobs.zip'
)
if
File
.
exist?
(
'./jobs.zip'
)
puts
'Extract file done
'
@logger
.
info
'Donwload & extract success
'
rescue
StandardError
=>
e
rescue
FileNotFound
=>
e
puts
"
#{
e
}
File not found
"
@logger
.
error
"
#{
e
.
message
}
"
end
end
end
end
end
end
...
...
lib/service/import_data.rb
View file @
0f57f980
...
@@ -3,24 +3,24 @@
...
@@ -3,24 +3,24 @@
require
'service/ftp'
require
'service/ftp'
# Import data from CSV
# Import data from CSV
class
ImportData
CSV
class
ImportData
DOMESTIC
=
1
DOMESTIC
=
1
COMPANY_SECURITY
=
1
COMPANY_SECURITY
=
1
def
import_data
def
import_data
csv_data
=
FtpSever
.
data_csv
csv_data
=
FtpSever
.
data_csv
parse_csv_industries
(
csv_data
)
import_industries_from
(
csv_data
)
parse_csv_cities
(
csv_data
)
import_cities_from
(
csv_data
)
parse_csv_companies
(
csv_data
)
import_companies_from
(
csv_data
)
parse_csv_jobs
(
csv_data
)
import_jobs_from
(
csv_data
)
end
end
private
private
def
parse_csv_industries
(
data
)
def
import_industries_from
(
csv
)
puts
'Import data industries . . .'
puts
'Import data industries . . .'
industries
=
[]
industries
=
[]
industries
+=
data
[
'category'
].
map
(
&
:strip
)
industries
+=
csv
[
'category'
].
map
(
&
:strip
)
industries
.
each
do
|
val
|
industries
.
each
do
|
val
|
val
.
gsub!
(
','
,
'/'
)
if
val
.
include?
(
','
)
val
.
gsub!
(
','
,
'/'
)
if
val
.
include?
(
','
)
val
.
gsub!
(
'/'
,
' / '
)
val
.
gsub!
(
'/'
,
' / '
)
...
@@ -29,9 +29,9 @@ class ImportDataCSV
...
@@ -29,9 +29,9 @@ class ImportDataCSV
puts
'Done parse csv industries'
puts
'Done parse csv industries'
end
end
def
parse_csv_cities
(
data
)
def
import_cities_from
(
csv
)
puts
'Import data cities . . .'
puts
'Import data cities . . .'
cities
=
data
[
'work place'
].
uniq
.
select
(
&
:present?
)
cities
=
csv
[
'work place'
].
uniq
.
select
(
&
:present?
)
cities
=
cities
.
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?
...
@@ -40,12 +40,12 @@ class ImportDataCSV
...
@@ -40,12 +40,12 @@ class ImportDataCSV
end
end
end
end
def
parse_csv_companies
(
data
)
def
import_companies_from
(
csv
)
puts
'Import data companies . . .'
puts
'Import data companies . . .'
data
[
'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
=
data
[
'company address'
][
index
]
company
.
address
=
csv
[
'company address'
][
index
]
company
.
short_description
=
data
[
'benefit'
][
index
]
company
.
short_description
=
csv
[
'benefit'
][
index
]
end
end
puts
index
puts
index
rescue
StandardError
=>
e
rescue
StandardError
=>
e
...
@@ -53,22 +53,20 @@ class ImportDataCSV
...
@@ -53,22 +53,20 @@ class ImportDataCSV
end
end
end
end
def
parse_csv_jobs
(
data
)
def
import_jobs_from
(
csv
)
data
[
'name'
].
each_with_index
do
|
name
,
index
|
csv
[
'name'
].
each_with_index
do
|
name
,
index
|
desc
=
(
data
[
'requirement'
][
index
]).
to_s
<<
(
data
[
'description'
][
index
]).
to_s
desc
=
"
#{
csv
[
'requirement'
][
index
]
}
#{
(
csv
[
'description'
][
index
])
}
"
id_company
=
Company
.
find_by
name:
data
[
'company name'
][
index
].
to_s
.
strip
company
=
Company
.
find_by
name:
csv
[
'company name'
][
index
].
to_s
.
strip
id_company
=
id_company
.
blank?
?
COMPANY_SECURITY
:
id_
company
.
id
id_company
=
company
.
blank?
?
COMPANY_SECURITY
:
company
.
id
begin
begin
id_job
=
Job
.
create!
(
name:
name
,
job
=
Job
.
create!
(
name:
name
,
company_id:
id_company
,
company_id:
id_company
,
level:
data
[
'level'
][
index
],
level:
csv
[
'level'
][
index
],
experience:
''
,
salary:
csv
[
'salary'
][
index
],
salary:
data
[
'salary'
][
index
],
create_date:
Time
.
now
,
create_date:
Time
.
now
,
description:
desc
)
expiration_date:
''
,
make_foreign_cities_table
(
csv
[
'work place'
][
index
],
job
.
id
)
description:
desc
)
make_foreign_industries_table
(
csv
[
'category'
][
index
],
job
.
id
)
make_foreign_cities_table
(
data
[
'work place'
][
index
],
id_job
.
id
)
make_foreign_industries_table
(
data
[
'category'
][
index
],
id_job
.
id
)
puts
index
puts
index
rescue
StandardError
=>
e
rescue
StandardError
=>
e
puts
e
puts
e
...
@@ -78,16 +76,15 @@ class ImportDataCSV
...
@@ -78,16 +76,15 @@ class ImportDataCSV
def
make_foreign_cities_table
(
data
,
id_job
)
def
make_foreign_cities_table
(
data
,
id_job
)
data
=
data
.
to_s
.
delete
(
'[]\"'
)
data
=
data
.
to_s
.
delete
(
'[]\"'
)
id_city
=
City
.
find_by
name:
data
.
strip
city
=
City
.
find_or_create_by
(
name:
data
.
strip
)
{
|
record
|
record
.
area
=
DOMESTIC
}
id_city
=
id_city
.
blank?
?
City
.
create!
(
name:
data
.
strip
,
area:
DOMESTIC
).
id
:
id_
city
.
id
city_id
=
city
.
id
CityJob
.
create!
(
job_id:
id_job
,
city_id:
id_city
)
CityJob
.
create!
(
job_id:
id_job
,
city_id:
city_id
)
end
end
def
make_foreign_industries_table
(
data
,
id_job
)
def
make_foreign_industries_table
(
data
,
id_job
)
data
=
data
.
to_s
.
gsub
(
','
,
'/'
).
gsub
(
'/'
,
' / '
)
data
=
data
.
to_s
.
gsub
(
','
,
'/'
).
gsub
(
'/'
,
' / '
)
data
=
data
.
strip
industry
=
Industry
.
find_or_create_by
(
name:
data
.
strip
)
id_industry
=
Industry
.
find_by
name:
data
industry_id
=
industry
.
id
id_industry
=
id_industry
.
blank?
?
Industry
.
create!
(
name:
data
.
strip
).
id
:
id_industry
.
id
IndustryJob
.
create!
(
industry_id:
industry_id
,
job_id:
id_job
)
IndustryJob
.
create!
(
industry_id:
id_industry
,
job_id:
id_job
)
end
end
end
end
lib/service/unzip.rb
View file @
0f57f980
...
@@ -3,12 +3,14 @@
...
@@ -3,12 +3,14 @@
require
'zip'
require
'zip'
# Description/Explanation of Person class
# Description/Explanation of Person class
def
extract_zip
(
file
,
destination
)
module
Unzip
FileUtils
.
mkdir_p
(
destination
)
def
self
.
extract_zip
(
file
,
destination
)
Zip
::
File
.
open
(
file
)
do
|
zip_file
|
FileUtils
.
mkdir_p
(
destination
)
zip_file
.
each
do
|
f
|
Zip
::
File
.
open
(
file
)
do
|
zip_file
|
fpath
=
File
.
join
(
destination
,
f
.
name
)
zip_file
.
each
do
|
f
|
zip_file
.
extract
(
f
,
fpath
)
unless
File
.
exist?
(
fpath
)
fpath
=
File
.
join
(
destination
,
f
.
name
)
zip_file
.
extract
(
f
,
fpath
)
unless
File
.
exist?
(
fpath
)
end
end
end
end
end
end
end
lib/tasks/csv.rake
View file @
0f57f980
...
@@ -5,7 +5,7 @@ require 'service/import_data'
...
@@ -5,7 +5,7 @@ require 'service/import_data'
# Description/Explanation of Person class
# Description/Explanation of Person class
namespace
:csv
do
namespace
:csv
do
task
import_csv: :environment
do
task
import_csv: :environment
do
csv
=
ImportData
CSV
.
new
csv
=
ImportData
.
new
csv
.
import_data
csv
.
import_data
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment