-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
363 lines (339 loc) · 16.4 KB
/
main.py
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
import argparse
import datetime
from random_data import education
import csv
from faker import Faker
import random
from employee_dto import EmployeeFactory
from client_dto import ClientFactory
from survey_dto import ConductedSurveyFactory, SurveyFactory
import logging
# import pyodbc
import psycopg2
import os
NUMBER_OF_RECORDS: int = 10
CHANGE_PERCENT = 5
GENDER_PERCENT = 50
KIDS_PERCENT = 50
LAST_CALLED = 1
DISMISSAL_RATE = 20
COMPLETED_PERCENT = 60
PHONE_PERCENT = 80
MINIMUM_SALARY = 2000
MAXIMUM_SALARY = 5000
def change_history_data(str_data_type):
faker = Faker()
if str_data_type == 'employee':
with open('employee_file.csv', newline='', mode='r') as csvfile:
logging.basicConfig(filename="changes.log",
level=logging.INFO,
format="Change info: employee's %(message)s")
possible_changes = ['first_name', 'last_name', 'gender', 'education', 'salary']
reader = csv.DictReader(csvfile)
employee_data = []
for row in reader:
employee_data.append(row)
nr_of_records = len(employee_data)
for i in range(int(float(CHANGE_PERCENT) / 100.0 * float(nr_of_records))):
changed_employee = random.choice(employee_data)
value_to_change = random.choice(list(changed_employee.keys()))
while value_to_change not in possible_changes:
value_to_change = random.choice(list(changed_employee.keys()))
if value_to_change == 'first_name':
old_name = changed_employee['first_name']
if changed_employee['gender'] == 1:
changed_employee['first_name'] = faker.first_name_male()
else:
changed_employee['first_name'] = faker.first_name_female()
logging.info(f"(id: {changed_employee['employee_id']}) first name changed: {old_name} => {changed_employee['first_name']}")
elif value_to_change == 'last_name':
old_last = changed_employee['last_name']
changed_employee['last_name'] = faker.last_name()
logging.info(f"(id: {changed_employee['employee_id']}) last name changed: {old_last} => {changed_employee['last_name']}")
elif value_to_change == 'gender':
changed_employee['gender'] = int(changed_employee['gender']).__xor__(1)
logging.info(f"(id: {changed_employee['employee_id']}) gender changed: {int(changed_employee['gender']).__xor__(1)} => {changed_employee['gender']}")
elif value_to_change == 'education':
old_education = changed_employee['education']
changed_employee['education'] = random.choice(education)
logging.info(f"(id: {changed_employee['employee_id']}) education degree changed: {old_education} => {changed_employee['education']}")
elif value_to_change == 'salary':
old_salary = changed_employee['salary']
changed_employee['salary'] = random.randrange(2000, 5000, 100)
logging.info(f"(id: {changed_employee['employee_id']}) salary changed: {old_salary} => {changed_employee['salary']}")
with open('employee_file.csv', newline='', mode='w') as csvfile:
writer = csv.writer(
csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
writer.writerow(
[
"employee_id",
"first_name",
"last_name",
"dob",
"gender",
"employment_date",
"dismissal_date",
"education",
"salary",
]
)
for employee in employee_data:
writer.writerow(employee.values())
if str_data_type == 'client':
logging.basicConfig(filename="changes.log",
level=logging.INFO,
format="Change info: client's %(message)s")
with open('client_file.csv', newline='', mode='r') as csvfile:
possible_changes = ['first_name', 'last_name', 'gender', 'profession', 'has_kids', 'education', 'is_married']
reader = csv.DictReader(csvfile)
client_data = []
for row in reader:
client_data.append(row)
nr_of_records = len(client_data)
for i in range(int(float(CHANGE_PERCENT) / 100.0 * float(nr_of_records))):
changed_client = random.choice(client_data)
value_to_change = random.choice(list(changed_client.keys()))
while value_to_change not in possible_changes:
value_to_change = random.choice(list(changed_client.keys()))
if value_to_change == 'first_name':
old_name = changed_client['first_name']
if changed_client['gender'] == 1:
changed_client['first_name'] = faker.first_name_male()
else:
changed_client['first_name'] = faker.first_name_female()
logging.info(
f"(id: {changed_client['client_id']}) first name changed: {old_name} => {changed_client['first_name']}")
elif value_to_change == 'last_name':
old_last = changed_client['last_name']
changed_client['last_name'] = faker.last_name()
logging.info(
f"(id: {changed_client['client_id']}) last name changed: {old_last} => {changed_client['last_name']}")
elif value_to_change == 'gender':
changed_client['gender'] = int(changed_client['gender']).__xor__(1)
logging.info(
f"(id: {changed_client['client_id']}) gender changed: {int(changed_client['gender']).__xor__(1)} => {changed_client['gender']}")
elif value_to_change == 'education':
old_education = changed_client['education']
changed_client['education'] = random.choice(education)
logging.info(
f"(id: {changed_client['client_id']}) education degree changed: {old_education} => {changed_client['education']}")
elif value_to_change == 'profession':
old_job = changed_client['profession']
changed_client['profession'] = faker.job()
logging.info(
f"(id: {changed_client['client_id']}) job changed: {old_job} => {changed_client['profession']}")
elif value_to_change == 'has_kids':
changed_client['has_kids'] = int(changed_client['has_kids']).__xor__(1)
logging.info(
f"(id: {changed_client['client_id']}) kids status changed: {int(changed_client['has_kids']).__xor__(1)} => {changed_client['has_kids']}")
elif value_to_change == 'is_married':
changed_client['is_married'] = int(changed_client['is_married']).__xor__(1)
logging.info(
f"(id: {changed_client['client_id']}) marriage status changed: {int(changed_client['is_married']).__xor__(1)} => {changed_client['is_married']}")
with open('clients_file.csv', newline='', mode='w') as csvfile:
writer = csv.writer(
csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
writer.writerow(
[
"client_id",
"first_name",
"last_name",
"dob",
"gender",
"profession",
"has_kids",
"education",
"email",
"phone",
"last_called",
"is_married"
]
)
for client in client_data:
writer.writerow(client.values())
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate information for Employees, Clients, Survey to insert into database"
)
parser.add_argument('type', help="employee, client, conductedsurvey or survey")
parser.add_argument('-n', help="number of records", default=10)
parser.add_argument('--change', help='change percent (default = 0)', default=0)
parser.add_argument('--gender', help='percent of males (default = 50)', default=50)
parser.add_argument('--kids', help='percent of peoples having kids (default = 50)',default=50)
parser.add_argument('--last-called', help='when was the last call maximum in years (default = 1)',default=1)
parser.add_argument('--dismissal', help='dismissal percent (default = 20)',default=20)
parser.add_argument('--completed', help='surveys completed percent (default = 60',default=60)
parser.add_argument('--phone', help='what is the percent of surveys conducted by calls',default=80)
parser.add_argument('--min-salary', help='minimum salary of the employees', default=2000)
parser.add_argument('--max-salary', help='maximum salary of the employees', default=5000)
args=(parser.parse_args())
NUMBER_OF_RECORDS = args.n
CHANGE_PERCENT = args.change
GENDER_PERCENT = args.gender
KIDS_PERCENT = args.kids
LAST_CALLED = args.last_called
DISMISSAL_RATE = args.dismissal
COMPLETED_PERCENT = args.completed
PHONE_PERCENT = args.phone
MINIMUM_SALARY = args.min_salary
MAXIMUM_SALARY = args.max_salary
if args.type == 'employee':
print('generating employees...')
if int(CHANGE_PERCENT) > 0:
change_history_data('employee')
employees = [EmployeeFactory.generate_employee(GENDER_PERCENT,DISMISSAL_RATE, MINIMUM_SALARY, MAXIMUM_SALARY) for i in range(int(NUMBER_OF_RECORDS))]
with open("employee_file.csv", mode="a") as employee_file:
employee_writer = csv.writer(
employee_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
if int(CHANGE_PERCENT) == 0:
employee_writer.writerow(
[
"employee_id",
"pesel",
"first_name",
"last_name",
"dob",
"gender",
"employment_date",
"dismissal_date",
"education",
"salary",
]
)
for employee in employees:
employee_writer.writerow(vars(employee).values())
elif args.type == 'client':
print('generating clients...')
if int(CHANGE_PERCENT) > 0:
change_history_data('client')
clients = [ClientFactory.generate_client(GENDER_PERCENT,KIDS_PERCENT) for i in range(int(NUMBER_OF_RECORDS))]
with open("client_file.csv", mode="a", newline="") as clients_file:
clients_writer = csv.writer(
clients_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
if int(CHANGE_PERCENT) == 0:
clients_writer.writerow(
[
"client_id",
"pesel",
"first_name",
"last_name",
"dob",
"gender",
"profession",
"has_kids",
"education",
"email",
"phone",
"last_called",
"is_married"
]
)
for client in clients:
clients_writer.writerow(vars(client).values())
elif args.type == 'conductedsurvey':
print('generating conduted surveys')
with open("employee_file.csv", mode="r") as employee_file:
csv_reader = csv.reader(employee_file, delimiter=",", quotechar='"')
csv_headings = next(csv_reader)
employees_ids = []
for line in csv_reader:
employees_ids.append(line[0])
with open("client_file.csv", mode="r") as clients_file:
csv_reader = csv.reader(clients_file, delimiter=",", quotechar='"')
csv_headings = next(csv_reader)
clients_ids = []
for line in csv_reader:
clients_ids.append(line[0])
conducted_surveys = [ConductedSurveyFactory.generate_conducted_survey(employees_ids, clients_ids) for i in range(int(NUMBER_OF_RECORDS))]
with open("conductedsurvey_file.csv", mode="w",newline="") as survey_file:
survey_writer = csv.writer(
survey_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
survey_writer.writerow(
[
"conducted_survey_id",
"employees_id",
"clients_id",
"survey_id",
"answers",
"datetime",
"email_or_phone",
"is_completed"
# "survey_html",
]
)
for survey in conducted_surveys:
survey_writer.writerow(vars(survey).values())
elif args.type == 'survey':
print('generating surveys...')
surveys = [SurveyFactory.generate_survey() for i in range(int(NUMBER_OF_RECORDS))]
with open("survey_file.csv", mode="w",newline="") as survey_file:
survey_writer = csv.writer(
survey_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL
)
survey_writer.writerow(
[
"survey_id",
"survey_content",
"title",
"company_name",
# "survey_html",
]
)
for survey in surveys:
survey_writer.writerow(vars(survey).values())
else:
print('Wrong option type --help for usage')
# try:
# connection = pyodbc.connect(driver='{SQL Server}',
# server='DESKTOP-R4RQFGR\MSSQLSERVER02',
# database='datawarehouses',
# )
# # (1) Execution Example with MySQL ODBC
# sql = """
# BULK INSERT datawarehouses.dbo.Employee
# FROM 'C:\\Users\\adrgo\\generator\\employee_file.csv' WITH (
# FIELDTERMINATOR='\\t',
# ROWTERMINATOR='\\n'
# );
# """
# with connection.cursor() as cursor:
# base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# fixture_path = os.path.join(base, "generator/employee_file.csv")
# with open(fixture_path) as f:
# try:
# next(f)
# cursor.execute(sql)
# # cursor.copy_from(f, "employee", sep=",")
# connection.commit()
# except Exception as e:
# print("Error while copying to MSSQL", e)
# print("copied")
# except (Exception) as e:
# print("Error while connecting to MSSQL", e)
# try:
# connection = psycopg2.connect(
# user="datawarehouses",
# password="datawarehouses",
# host="127.0.0.1",
# port="5432",
# database="datawarehouses",
# )
# name = args.type
# with connection.cursor() as cursor:
# base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# fixture_path = os.path.join(base, f"generator/{name}_file.csv")
# with open(fixture_path) as f:
# try:
# next(f)
# cursor.copy_from(f, f"{name}", sep=",")
# connection.commit()
# except Exception as e:
# print(e)
# print("copied")
# except (Exception, psycopg2.Error) as error:
# print("Error while connecting to PostgreSQL", error)