-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_records.rb
398 lines (375 loc) · 14.2 KB
/
load_records.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# frozen_string_literal: true
require 'csv'
require 'date'
# !/bin/env ruby
require "#{Dir.pwd}/config/environment.rb"
# Script for importing seeds data
class LoadRecords < ApplicationRecord
def software_records
file = "#{Dir.pwd}/public/uploads/#{$filename}"
csv = CSV.read(file, headers: true)
webapp_type = csv['Software Type']
vendor_records = csv['Vendor Record']
statuses = csv['Status']
hosting_environments = csv['Hosting Environment']
valid_hostings = []
invalid_hostings = []
hostings_invalid = false
valid_types = []
valid_vendors = []
valid_statuses = []
invalid_vendors = []
invalid_types = []
invalid_statuses = []
types_invalid = false
vendors_invalid = false
statuses_invalid = false
webapp_type.each do |type|
valid_types.push(SoftwareType.find_by(title: type).id)
rescue StandardError
types_invalid = true
invalid_types.push(type)
end
vendor_records.each do |record|
valid_vendors.push(VendorRecord.find_by(title: record).id)
rescue StandardError
vendors_invalid = true
invalid_vendors.push(record)
end
statuses.each do |status|
valid_statuses.push(Status.find_by(title: status).id)
rescue StandardError
statuses_invalid = true
invalid_statuses.push(status)
end
hosting_environments.each do |hosting_env|
if hosting_env.to_s.strip.empty?
valid_hostings.push(nil)
else
valid_hostings.push(HostingEnvironment.find_by(title: hosting_env).id)
end
rescue StandardError
hostings_invalid = true
invalid_hostings.push(hosting_env)
end
if types_invalid || vendors_invalid || statuses_invalid || hostings_invalid
puts '---------------------------------------------------------------------'
puts('No Software Records created due to following in-valid records/types/statuses/hosting environments..')
puts '---------------------------------------------------------------------'
puts('In-Valid Vendor Records')
puts('-----------------------------')
puts(invalid_vendors)
puts('-----------------------------')
puts('In-Valid Software Types')
puts('-----------------------------')
puts(invalid_types)
puts('-----------------------------')
puts('In-Valid Statuses')
puts('-----------------------------')
puts(invalid_statuses)
puts('-----------------------------')
puts('In-Valid Hosting Env.')
puts('-----------------------------')
puts(invalid_hostings)
else
count = 0
created = 0
csv.each do |row|
title = row['Software Record']
desc = row['Description']
software_type_id = valid_types[count]
vendor_record_id = valid_vendors[count]
status_id = valid_statuses[count]
hosting_environment_id = if valid_hostings[count].nil?
HostingEnvironment.find_by(title: 'None').id
else
valid_hostings[count]
end
departments = []
if row['Departments'].to_s.include?(',')
alldeps = row['Departments'].split(',')
alldeps.each do |dep|
dep = dep.to_s.strip
departments.push(dep)
end
else
departments.push(row['Departments'])
end
developers = []
if row['Developers'].to_s.empty?
developers.push('')
elsif row['Developers'].to_s.include?(',')
alldevs = row['Developers'].split(',')
alldevs.each do |dev|
dev = dev.to_s.strip
developers.push(dev)
end
else
developers.push(row['Developers'])
end
tech_leads = []
if row['Tech Leads'].to_s.empty?
tech_leads.push('')
elsif row['Tech Leads'].to_s.include?(',')
alltechleads = row['Tech Leads'].split(',')
alltechleads.each do |lead|
lead = lead.to_s.strip
tech_leads.push(lead)
end
else
tech_leads.push(row['Tech Leads'])
end
product_owners = []
if row['Product Owners'].to_s.empty?
product_owners.push('')
elsif row['Product Owners'].to_s.include?(',')
allproductowners = row['Product Owners'].split(',')
allproductowners.each do |owner|
owner = owner.to_s.strip
product_owners.push(owner)
end
else
product_owners.push(row['Product Owners'])
end
admin_users = []
if row['Admin Users'].to_s.empty?
admin_users.push('')
elsif row['Admin Users'].to_s.include?(',')
alladminusers = row['Admin Users'].split(',')
alladminusers.each do |admin|
admin = admin.to_s.strip
admin_users.push(admin)
end
else
admin_users.push(row['Admin Users'])
end
date_implemented = row['Date Implemented'].to_s.strip
lang = row['Languages Used'].to_s.strip
url = row['Production URL'].to_s.strip
source_url = row['Source Code URL'].to_s.strip
user_seats = row['User Seats'].to_s.strip
annual_fees = row['Annual Fees'].to_s.strip
support_contract = row['Support Contract'].to_s.strip
version = row['Current Version'].to_s.strip
notes = row['Notes'].to_s.strip
bvalue = row['Business value'].to_s.strip
itquality = row['IT quality'].to_s.strip
created_by = $user
sensitive_information = row['Sensitive Information'].to_s.strip
date_of_upgrade = row['Date of upgrade'].to_s.strip
authentication_type = row['Authentication Type'].to_s.strip
# change managmeent fields
requires_cm = row['Requires Chanage Management review?'].to_s.strip
last_security_scan = row['Last Security Scan'].to_s.strip
last_accessibility_scan = row['Last Accessibility Scan'].to_s.strip
last_ogc_review = row['Last OGC Review'].to_s.strip
last_info_sec_review = row['Last Infosec Review'].to_s.strip
cm_stakeholders = row['CM Stakeholders'].to_s.strip
cm_other_notes = row['CM Other Notes'].to_s.strip
# serven env fields
qa_url = row['QA URL'].to_s.strip
dev_url = row['Dev URL'].to_s.strip
prod_url = row['Prod URL'].to_s.strip
production_support_servers = row['Production Support Servers'].to_s.strip
last_record_change = row['Last Record Change'].to_s.strip
track_uptime = row['Track Uptime'].to_s.strip
monitor_health = row['Monitor Health'].to_s.strip
monitor_errors = row['Monitor Errors'].to_s.strip
if !SoftwareRecord.find_by(title:).nil? && SoftwareRecord.find_by(title:).title.to_s == title && SoftwareRecord.find_by(title:).software_type_id == software_type_id && SoftwareRecord.find_by(title:).vendor_record_id == vendor_record_id
puts("Software Record '#{title}' is found in the db and hence suppressing it.")
else
SoftwareRecord.new(title:,
description: desc,
status_id:,
software_type_id:,
vendor_record_id:,
departments:,
date_implemented:,
developers:,
tech_leads:,
product_owners:,
admin_users:,
languages_used: lang,
production_url: url,
source_code_url: source_url,
user_seats:,
annual_fees:,
support_contract:,
hosting_environment_id:,
current_version: version,
notes:,
business_value: bvalue,
it_quality: itquality,
created_by:,
sensitive_information:,
authentication_type:,
requires_cm:,
last_security_scan:,
last_accessibility_scan:,
last_ogc_review:,
last_info_sec_review:,
cm_stakeholders:,
cm_other_notes:,
date_of_upgrade:,
qa_url:,
dev_url:,
prod_url:,
production_support_servers:,
last_record_change:,
track_uptime:,
monitor_health:,
monitor_errors:).save
created += 1
puts("Created Software Record '#{row['Software Record']}'...")
end
count += 1
end
puts '---------------------------------------------------------------------'
puts "Total Software Records created -> #{created}"
puts '---------------------------------------------------------------------'
end
end
def vendor_records
file = "#{Dir.pwd}/public/uploads/#{$filename}"
csv = CSV.read(file, headers: true)
vendor_records = csv['Vendor Record']
duplicate_vendors = []
vendors_exists = false
valid_vendors = []
count = 1
vendor_records.each do |record|
VendorRecord.find_by(title: record).id
rescue StandardError
vendors_exists = false
valid_vendors.push(record)
else
vendors_exists = true
duplicate_vendors.push(record)
end
duplicate_vendors = duplicate_vendors.uniq
valid_vendors = valid_vendors.uniq
puts 'Duplicate Vendor Records exists and suppressing them...' if vendors_exists
valid_vendors.each do |eachrecord|
count += 1
puts "Creating Vendor Record '#{eachrecord}'"
VendorRecord.new(title: eachrecord, description: '-').save
end
puts '---------------------------------------------------------------------'
puts "Total Vendor Records created -> #{count - 1}"
puts '---------------------------------------------------------------------'
end
def software_types
file = "#{Dir.pwd}/public/uploads/#{$filename}"
csv = CSV.read(file, headers: true)
software_types = csv['Software Type']
duplicate_types = []
types_exists = false
valid_types = []
count = 1
software_types.each do |type|
SoftwareType.find_by(title: type).id
rescue StandardError
types_exists = false
valid_types.push(type)
else
types_exists = true
duplicate_types.push(type)
end
duplicate_types = duplicate_types.uniq
valid_types = valid_types.uniq
puts 'Duplicate Software Types exists and suppressing them...' if types_exists
valid_types.each do |eachtype|
count += 1
puts "Creating Software Type '#{eachtype}'"
SoftwareType.new(title: eachtype, description: '-').save
end
puts '---------------------------------------------------------------------'
puts "Total Software Types created -> #{count - 1}"
puts '---------------------------------------------------------------------'
end
def statuses
file = "#{Dir.pwd}/public/uploads/#{$filename}"
csv = CSV.read(file, headers: true)
all_statuses = csv['Status']
duplicate_statuses = []
statuses_exists = false
valid_statuses = []
count = 1
all_statuses.each do |status|
Status.find_by(title: status).id
rescue StandardError
statuses_exists = false
valid_statuses.push(status)
else
statuses_exists = true
duplicate_statuses.push(status)
end
duplicate_statuses = duplicate_statuses.uniq
valid_statuses = valid_statuses.uniq
puts 'Duplicate Statuses exists and suppressing them...' if statuses_exists
valid_statuses.each do |eachstatus|
count += 1
puts "Creating Status '#{eachstatus}'"
Status.new(title: eachstatus).save
end
puts '---------------------------------------------------------------------'
puts "Total Statuses created -> #{count - 1}"
puts '---------------------------------------------------------------------'
end
def hosting_envs
file = "#{Dir.pwd}/public/uploads/#{$filename}"
csv = CSV.read(file, headers: true)
all_hostings = csv['Hosting Environment']
duplicate_hostings = []
hostings_exists = false
valid_hostings = []
count = 1
all_hostings.each do |hosting|
HostingEnvironment.find_by(title: hosting).id
rescue StandardError
hostings_exists = false
valid_hostings.push(hosting)
else
hostings_exists = true
duplicate_hostings.push(hosting)
end
duplicate_hostings = duplicate_hostings.uniq
valid_hostings = valid_hostings.uniq
puts 'Duplicate Statuses exists and suppressing them...' if hostings_exists
valid_hostings.each do |eachenv|
count += 1
puts "Creating Hosting Environment '#{eachenv}'"
HostingEnvironment.new(title: eachenv).save
end
puts '---------------------------------------------------------------------'
puts "Total Hosting Environments created -> #{count - 1}"
puts '---------------------------------------------------------------------'
end
end
args = ARGV[0]
$filename = ARGV[1]
$user = ARGV[2]
case args
when 'vendor'
LoadRecords.table_name = 'vendor_records'
l = LoadRecords.new
l.vendor_records
when 'software'
LoadRecords.table_name = 'software_records'
l = LoadRecords.new
l.software_records
when 'type'
LoadRecords.table_name = 'software_types'
l = LoadRecords.new
l.software_types
when 'status'
LoadRecords.table_name = 'statuses'
l = LoadRecords.new
l.statuses
when 'hosting_env'
LoadRecords.table_name = 'hosting_environments'
l = LoadRecords.new
l.hosting_envs
else
puts "Invalid arguments...\nTry passing...\n 1) `vendor` to import vendor_records data, \n 2) `software` to import software_records data, \n 3) `type` to import software_types data, \n 4) `status` to import status data, \n 5) `hosting_env` to import hosting_environment data"
end