-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
530 lines (477 loc) · 13.6 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# initial my course projectcd
# import packajes
import sqlite3
import pandas as pd
import shutil
import os
conn = sqlite3.connect('bank.db')
cursor = conn.cursor()
# init raw data from sql_scripts and transform table names
def init_source_data():
print('Creating source data tables...')
with open('./sql_scripts/ddl_dml.sql', 'r', encoding='utf-8') as f:
table = f.read()
conn.executescript(table)
cursor.execute('ALTER TABLE cards RENAME TO STG_cards')
cursor.execute('ALTER TABLE accounts RENAME TO STG_accounts')
cursor.execute('ALTER TABLE clients RENAME TO STG_clients')
print("Done successfully!")
# import csv raw data rename import files and move to backup
def csv2sql(path, table_name, separatop=','):
print('Import data from file '+ path)
df = pd.read_csv(path, sep=separatop)
df.to_sql(table_name, conn, if_exists = 'replace')
new_path = './archive/' + path +'.backup'
shutil.move(path, new_path)
print('Import successfully!')
# import excel raw data rename import files and move to backup
def xlsx2sql(path, table_name):
print('Import data from file '+ path)
df = pd.read_excel(path)
df.to_sql(table_name, conn, if_exists = 'replace')
new_path = './archive/' + path +'.backup'
shutil.move(path, new_path)
print('Import successfully!')
def init_transactions():
cursor.execute('''
CREATE TABLE if not exists DWH_FACT_transactions(
trans_id varchar(128),
trans_date date,
card_num varchar(128),
open_type varchar(128),
amt decimal,
open_result varchar(128),
terminal varchar(128)
);
''')
def init_passport_blacklist():
cursor.execute('''
CREATE TABLE if not exists DWH_FACT_passport_blacklist(
passport_num varchar(128),
entry_dt date
);
''')
def init_terminals_hist():
cursor.execute('''
CREATE TABLE if not exists DWH_DIM_terminals_HIST(
terminal_id varchar(128),
terminal_type varchar(128),
terminal_city varchar(128),
terminal_address varchar(128),
deleted_flg integer default 0,
effective_from datetime default current_timestamp,
effective_to default (datetime('2999-12-31 23:59:59'))
);
''')
cursor.execute('''
CREATE VIEW if not exists v_terminals as
select
terminal_id,
terminal_type,
terminal_city,
terminal_address
from DWH_DIM_terminals_HIST
where current_timestamp between effective_from and effective_to and deleted_flg = 0
''')
def createnewRows():
cursor.execute('''
CREATE TABLE if not exists STG_new_rows as
select
t1.terminal_id,
t1.terminal_type,
t1.terminal_city,
t1.terminal_address
from STG_terminals t1
left join v_terminals t2
on t1.terminal_id = t2.terminal_id
where t2.terminal_id is null
''')
def createDeletedRows():
cursor.execute('''
CREATE TABLE if not exists STG_deleted_rows as
select
t1.terminal_id,
t1.terminal_type,
t1.terminal_city,
t1.terminal_address
from v_terminals t1
left join STG_terminals t2
on t1.terminal_id = t2.terminal_id
where t2.terminal_id is null
''')
def createChangedRows():
cursor.execute('''
CREATE TABLE if not exists STG_changed_rows as
select
t1.terminal_id,
t1.terminal_type,
t1.terminal_city,
t1.terminal_address
from STG_terminals t1
inner join v_terminals t2
on t1.terminal_id = t2.terminal_id
and( t1.terminal_id <> t2.terminal_id
or t1.terminal_type <> t2.terminal_type
or t1.terminal_city <> t2.terminal_city
or t1.terminal_address <> t2.terminal_address )
''')
def update_terminals_hist():
cursor.execute('''
INSERT INTO DWH_DIM_terminals_HIST(
terminal_id,
terminal_type,
terminal_city,
terminal_address
) select
terminal_id,
terminal_type,
terminal_city,
terminal_address
from STG_new_rows
''')
cursor.execute('''
UPDATE DWH_DIM_terminals_HIST
set effective_to = datetime('now', '-1 second')
where terminal_id in (select terminal_id from STG_changed_rows)
and effective_to = datetime('2999-12-31 23:59:59')
''')
cursor.execute('''
INSERT INTO DWH_DIM_terminals_HIST(
terminal_id,
terminal_type,
terminal_city,
terminal_address
) select
terminal_id,
terminal_type,
terminal_city,
terminal_address
from STG_changed_rows
''')
cursor.execute('''
UPDATE DWH_DIM_terminals_HIST
set effective_to = datetime('now', '-1 second')
where terminal_id in (select terminal_id from STG_deleted_rows)
and effective_to = datetime('2999-12-31 23:59:59')
''')
cursor.execute('''
INSERT INTO DWH_DIM_terminals_HIST(
terminal_id,
terminal_type,
terminal_city,
terminal_address,
deleted_flg
) select
terminal_id,
terminal_type,
terminal_city,
terminal_address,
1
from STG_deleted_rows
''')
conn.commit()
def showTable(table_name):
cursor.execute('select * from ' + table_name)
for row in cursor.fetchall():
print(row)
def make_meta_db():
cursor.execute('''
CREATE TABLE if not exists META_bank_db(
table_name varchar(128),
columm_name varchar(128),
type varchar(128)
);
''')
cursor.execute('''
INSERT INTO META_bank_db(
table_name,
columm_name,
type
) SELECT
t1.name as table_name,
t2.name as column_name,
t1.type as type
FROM
sqlite_master AS t1
JOIN
pragma_table_info(t1.name) AS t2
ORDER BY
t1.name,
t2.cid
''')
conn.commit()
def init_reports():
cursor.execute('''
CREATE TABLE if not exists REP_FRAUD (
event_dt date,
passport_num varchar(128),
FIO varchar(128),
phone varchar(128),
event_type varchar(128),
report_dt date
);
''')
def scam_catcher_type_1_1():
cursor.execute('''
INSERT INTO REP_FRAUD (
event_dt,
passport_num,
FIO,
phone,
event_type,
report_dt)
select
transaction_date as event_dt,
passport_num,
last_name ||' '||first_name||' '||patronymic as FIO,
phone,
'Overdue passport' as event_type,
datetime(current_timestamp) as report_dt
from STG_clients t1
join STG_accounts t2
on t1.client_id = t2.client
join STG_cards t3
on t3.account = t2.account
join DWH_FACT_transactions t4
on t4.card_num = t3.card_num
left join DWH_FACT_passport_blacklist t5
on t5.passport = t1.passport_num
where t4.transaction_date > t1.passport_valid_to
;
''')
conn.commit()
def scam_catcher_type_1_2():
cursor.execute('''
INSERT INTO REP_FRAUD(
event_dt,
passport_num,
FIO,
phone,
event_type,
report_dt)
select
transaction_date as event_dt,
passport_num,
last_name ||' '||first_name||' '||patronymic as FIO,
phone,
'Blocked passport' as event_type,
datetime(current_timestamp) as report_dt
from STG_clients t1
join STG_accounts t2
on t1.client_id = t2.client
join STG_cards t3
on t3.account = t2.account
join DWH_FACT_transactions t4
on t4.card_num = t3.card_num
left join DWH_FACT_passport_blacklist t5
on t5.passport = t1.passport_num
where t4.transaction_date > t5.date
;
''')
conn.commit()
def scam_catcher_type_2():
cursor.execute('''
INSERT INTO REP_FRAUD(
event_dt,
passport_num,
FIO,
phone,
event_type,
report_dt
) select
transaction_date as event_dt,
passport_num as passport,
last_name ||' '||first_name||' '||patronymic as FIO,
phone,
'Bank agreement not valid' as event_type,
datetime(current_timestamp) as report_dt
from STG_clients t1
join STG_accounts t2
on t1.client_id = t2.client
join STG_cards t3
on t3.account = t2.account
join DWH_FACT_transactions t4
on t4.card_num = t3.card_num
left join DWH_FACT_passport_blacklist t5
on t5.passport = t1.passport_num
where t4.transaction_date > t2.valid_to
;
''')
conn.commit()
def scam_catcher_type_3():
cursor.execute('''
INSERT INTO REP_FRAUD(
event_dt,
passport_num,
FIO,
phone,
event_type,
report_dt
) with cte_pre as (
select
transaction_date as event_dt,
passport_num as passport,
last_name ||' '||first_name||' '||patronymic as FIO,
phone,
'Transactions in different cities in less than an hour' as event_type,
t4.transaction_date,
case
when lag(terminal_city,1,null) over (partition by t3.card_num order by t4.transaction_date )!= terminal_city
and (julianday(t4.transaction_date) - julianday(lag(t4.transaction_date,1,null) over (partition by t3.card_num order by t4.transaction_date )) ) * 24 < 1
then 1
else 0 end IS_DIFF_CITY
from STG_clients t1
join STG_accounts t2
on t1.client_id = t2.client
join STG_cards t3
on t3.account = t2.account
join DWH_FACT_transactions t4
on t4.card_num = t3.card_num
join DWH_DIM_terminals_HIST t6
on t6.terminal_id = t4.terminal
order by 2,1
)
select
event_dt,
passport,
FIO,
phone,
'Transactions in different cities in less than an hour' as event_type,
datetime(current_timestamp) as report_dt
from cte_pre
where IS_DIFF_CITY = 1
;
''')
conn.commit()
def scam_catcher_type_4():
cursor.execute('''
INSERT INTO REP_FRAUD(
event_dt,
passport_num,
FIO,
phone,
event_type,
report_dt
) with cte_pre as(
select
transaction_date as event_dt,
passport_num as passport,
last_name ||' '||first_name||' '||patronymic as FIO,
phone,
'Selection of the amount with success in 20 minutes' as event_type,
t4.transaction_date,
t4.amount,
t4.oper_result,
lag(t4.oper_result ,1,null) over (partition by t3.card_num order by t4.transaction_date ) oper_result_prev,
lag(t4.oper_result ,2,null) over (partition by t3.card_num order by t4.transaction_date ) oper_result_prev2,
case
when
lag(t4.oper_result ,1,null) over (partition by t3.card_num order by t4.transaction_date ) = 'REJECT'
and lag(t4.oper_result ,2,null) over (partition by t3.card_num order by t4.transaction_date ) = 'REJECT'
and lag(t4.oper_result ,3,null) over (partition by t3.card_num order by t4.transaction_date ) = 'REJECT'
and t4.oper_result = 'SUCCESS'
and lag(t4.amount ,1,null) over (partition by t3.card_num order by t4.transaction_date ) - t4.amount>0
and lag(t4.amount ,2,null) over (partition by t3.card_num order by t4.transaction_date ) - lag(t4.amount ,1,null) over (partition by t3.card_num order by t4.transaction_date ) > 0
and lag(t4.amount ,3,null) over (partition by t3.card_num order by t4.transaction_date ) - lag(t4.amount ,2,null) over (partition by t3.card_num order by t4.transaction_date ) > 0
and (julianday(t4.transaction_date) - julianday(lag(t4.transaction_date,3,null) over (partition by t3.card_num order by t4.transaction_date )) ) * 24*60 <20
then 1
else 0 end IS_20_MIN
from STG_clients t1
join STG_accounts t2
on t1.client_id = t2.client
join STG_cards t3
on t3.account = t2.account
join DWH_FACT_transactions t4
on t4.card_num = t3.card_num
join DWH_DIM_terminals_HIST t6
on t6.terminal_id = t4.terminal
order by 2,1)
select
event_dt,
passport,
FIO,
phone,
'Selection of the amount with success in 20 minutes' as event_type,
datetime(current_timestamp) as report_dt
from cte_pre
where IS_20_MIN = 1
;
''')
conn.commit()
def delete_tmp_tables():
cursor.execute('DROP TABLE if exists STG_terminals')
cursor.execute('DROP TABLE if exists STG_new_rows')
cursor.execute('DROP TABLE if exists STG_deleted_rows')
cursor.execute('DROP TABLE if exists STG_changed_rows')
conn.commit()
# start scripts and check area
init_source_data()
init_transactions()
init_terminals_hist()
init_passport_blacklist()
init_reports()
csv2sql('transactions_01032021.txt','DWH_FACT_transactions',';')
xlsx2sql('passport_blacklist_01032021.xlsx', 'DWH_FACT_passport_blacklist')
xlsx2sql('terminals_01032021.xlsx','STG_terminals')
print(' ')
createnewRows()
createDeletedRows()
createChangedRows()
update_terminals_hist()
make_meta_db()
scam_catcher_type_1_1()
scam_catcher_type_1_2()
scam_catcher_type_2()
scam_catcher_type_3()
scam_catcher_type_4()
delete_tmp_tables()
showTable('REP_FRAUD')
#add next day data
csv2sql('transactions_02032021.txt','DWH_FACT_transactions',';')
xlsx2sql('passport_blacklist_02032021.xlsx', 'DWH_FACT_passport_blacklist')
xlsx2sql('terminals_02032021.xlsx','STG_terminals')
print(' ')
createnewRows()
createDeletedRows()
createChangedRows()
update_terminals_hist()
make_meta_db()
scam_catcher_type_1_1()
scam_catcher_type_1_2()
scam_catcher_type_2()
scam_catcher_type_3()
scam_catcher_type_4()
delete_tmp_tables()
showTable('REP_FRAUD')
# add another day data
csv2sql('transactions_03032021.txt','DWH_FACT_transactions',';')
xlsx2sql('passport_blacklist_03032021.xlsx', 'DWH_FACT_passport_blacklist')
xlsx2sql('terminals_03032021.xlsx','STG_terminals')
print(' ')
createnewRows()
createDeletedRows()
createChangedRows()
update_terminals_hist()
make_meta_db()
scam_catcher_type_1_1()
scam_catcher_type_1_2()
scam_catcher_type_2()
scam_catcher_type_3()
scam_catcher_type_4()
delete_tmp_tables()
showTable('REP_FRAUD')
## any test def
# showTable('STG_cards')
# showTable('STG_accounts')
# showTable('STG_clients')
# showTable('DWH_FACT_transactions')
# showTable('DWH_FACT_passport_blacklist')
# showTable('DWH_DIM_terminals_HIST')
# showTable('REP_FRAUD')
#showTable('META_bank_db')
# print('_-new-_'*5)
# showTable('STG_new_rows')
# print('_-deleted-_'*5)
# showTable('STG_deleted_rows')
# print('_-changed-_'*5)
# showTable('STG_changed_rows')