-
Notifications
You must be signed in to change notification settings - Fork 0
/
Report.py
373 lines (358 loc) · 13.1 KB
/
Report.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
import boto3
import smtplib
import pandas as pd
from time import strftime
from extData.sele_v2 import dataExt
from em.ol.olProps import olProps_var
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from aws.meyita.props.pgprodConProps import pgprod_prop
class morningPending():
def __init__(self):
# connection string reference to rds
con = pgprod_prop()
# Query determines the hight of pending loans right before funding occurs
qry_Pend = """
select
pd,
cntformer,
sumformer,
cntnewdm,
sumnewdm,
cntnew,
sumnew,
cnttotal,
sumtotal
FROM <table>
WHERE sumtotal = (
SELECT DISTINCT
max(sumtotal)
FROM <table name>
WHERE pd = current_date
--WHERE pd = '2019-01-29'
AND ph in (4,5,6,7))
AND pd = current_date;
"""
# pandas dataframe
df_Pend = pd.read_sql_query(qry_Pend, con.pgprodcon)
df_Pend = df_Pend.drop_duplicates()
self.cntFormer = format(int(df_Pend['cntformer']), ',')
self.revFormerTotal = format(int(df_Pend['sumformer']), ',')
self.cnt_new_dm_Total = format(int(df_Pend['cntnewdm']), ',')
self.revNew_dm_Total = format(int(df_Pend['sumnewdm']), ',')
self.cntNewTotal = format(int(df_Pend['cntnew']), ',')
self.revNewTotal = format(int(df_Pend['sumnew']), ',')
self.cntTotal = format(int(df_Pend['cnttotal']), ',')
self.revFundedTotal = format(int(df_Pend['sumtotal']), ',')
self.calcPendingTotal = int(df_Pend['sumtotal'])
def mornPending_var():
return mornPending()
# Get, Set CVF from previous day
class mornCVF():
# if the value of CVF is null or an error then it will auto assign $50,000
def __init__(self):
# connection string reference to rds
con = pgprod_prop()
# Retrieves the cvf from the previous night
qry_mornCVF = """
select ulday, sumh from <table name> where ulday = current_date - 1;
"""
# pandas dataframe
df_mornCVF = pd.read_sql_query(qry_mornCVF, con=con.pgprodcon)
df_mornCVF = df_mornCVF.drop_duplicates()
try:
blankIndex = [''] * len(df_mornCVF)
df_mornCVF.index = blankIndex
self.sumH = df_mornCVF['sumh'].astype(float)
except Exception as e:
print("CVF calculation error: " + str(e))
self.sumH = float(50000)
def mornCVF_var():
return mornCVF()
# Get, Set 4 am added loans sumtotal from previous day
class morn4amloansTotal():
def __init__(self):
# Retrieves the 4 am added loans
con = pgprod_prop()
qry_4amloans = """select max(sumtotal) as sumtotal from <table name>s where pd = current_date"""
fourDF = pd.read_sql_query(sql=qry_4amloans, con=con.pgprodcon)
fourDF = fourDF.drop_duplicates()
try:
blankIndex = [''] * len(fourDF)
fourDF.index = blankIndex
self.fourAMAddedLoansSumtotal = fourDF['sumtotal']
except Exception as e:
print("4 am clac error: " + str(e))
#print(fourDF['sumtotal'])
def morn4amloansTotal_var():
return morn4amloansTotal()
# Get, Set 5 am added loans sumtotal from previous day
class morn5amloansTotal():
def __init__(self):
# Retrieves the 5 am added loans
con = pgprod_prop()
qry_5amloans = """select max(sumtotal) as sumtotal from <table name> where pd = current_date"""
fiveDF = pd.read_sql_query(sql=qry_5amloans, con=con.pgprodcon)
fiveDF = fiveDF.drop_duplicates()
try:
blankIndex = [''] * len(fiveDF)
fiveDF.index = blankIndex
self.fiveAMAddedLoansSumtotal = fiveDF['sumtotal']
except Exception as e:
print("5 am clac error: " + str(e))
def morn5amloansTotal_var():
return morn5amloansTotal()
# Get, Set 6 am added loans sumtotal from previous day
class morn6amloansTotal():
def __init__(self):
# Retrieves the 5 am added loans
con = pgprod_prop()
qry_6amloans = """select max(sumtotal) as sumtotal from <table name> where pd = current_date"""
sixDF = pd.read_sql_query(sql=qry_6amloans, con=con.pgprodcon)
sixDF = sixDF.drop_duplicates()
try:
blankIndex = [''] * len(sixDF)
sixDF.index = blankIndex
self.sixAMAddedLoansSumtotal = sixDF['sumtotal']
except Exception as e:
print("6 am clac error: " + str(e))
def morn6amloansTotal_var():
return morn6amloansTotal()
# download raw and upload aggrigates to rds
def morn4amLoansDLUL():
dataExt(turl=["Loan_Search2","exit"])
#print('Starting morning forecast')
var = pgprod_prop()
con = var.pgprodcon
cursor = con.cursor()
try:
cd = strftime("%m/%d/%Y")
tr = cd + ' 04'
lsRaw = "<file path>"
df = pd.read_csv("<file path>",
index_col='Loan#')
df['Loan Amount'] = df[df.columns[10]].replace('[\$,]', '', regex=True).astype(float)
newLoans = df[df['Loan Date/Time'].str.contains(tr)].sum().fillna(0)['Loan Amount']
lsCalc = newLoans
qryrf = """
INSERT INTO
<table name> (pd,sumtotal)
VALUES (current_date,""" + str(lsCalc) + """)
"""
cursor.execute(qryrf)
con.commit()
cursor.close()
con.close()
except Exception as e:
#print("4 am Loans failed: " + str(e))
try:
day = strftime("%d")
month = strftime("%m")
year = strftime("%Y")
date = year + "-" + month + "-" + day
lsCalce = int(0)
qryrf = """
INSERT INTO
<table name> (pd,sumtotal)
VALUES (current_date,""" + str(lsCalce) + """)
"""
cursor.execute(qryrf)
con.commit()
cursor.close()
con.close()
except Exception as e:
print("Back up 4 am Loans failed: " + str(e))
def morn5amLoansDLUL():
dataExt(turl=["Loan_Search2","exit"])
#print('Starting morning forecast')
var = pgprod_prop()
con = var.pgprodcon
cursor = con.cursor()
try:
cd = strftime("%m/%d/%Y")
tr = cd + ' 05'
lsRaw = "C<file path>"
df = pd.read_csv("<file path>",
index_col='Loan#')
df['Loan Amount'] = df[df.columns[10]].replace('[\$,]', '', regex=True).astype(float)
newLoans = df[df['Loan Date/Time'].str.contains(tr)].sum().fillna(0)['Loan Amount']
lsCalc = newLoans
#print(lsCalc)
qryrf = """
INSERT INTO
<table name> (pd,sumtotal)
VALUES (current_date,""" + str(lsCalc) + """)
"""
cursor.execute(qryrf)
con.commit()
cursor.close()
con.close()
except Exception as e:
print("5 am Loans failed: " + str(e))
try:
day = strftime("%d")
month = strftime("%m")
year = strftime("%Y")
date = year + "-" + month + "-" + day
lsCalce = int(0)
qryrf = """
INSERT INTO
<table name> (pd,sumtotal)
VALUES (current_date,""" + str(lsCalce) + """)
"""
cursor.execute(qryrf)
con.commit()
cursor.close()
con.close()
except Exception as e:
print("Back up 5 am Loans failed: " + str(e))
def morn6amLoansDLUL():
dataExt(turl=["Loan_Search2","exit"])
#print('Starting morning forecast')
var = pgprod_prop()
con = var.pgprodcon
cursor = con.cursor()
try:
cd = strftime("%m/%d/%Y")
tr = cd + ' 06'
lsRaw = "<csv file location>"
df = pd.read_csv("<csv file location>",
index_col='Loan#')
df['Loan Amount'] = df[df.columns[10]].replace('[\$,]', '', regex=True).astype(float)
newLoans = df[df['Loan Date/Time'].str.contains(tr)].sum().fillna(0)['Loan Amount']
lsCalc = newLoans
#print(lsCalc)
qryrf = """
INSERT INTO
<table name> (pd,sumtotal)
VALUES (current_date,""" + str(lsCalc) + """)
"""
cursor.execute(qryrf)
con.commit()
cursor.close()
con.close()
except Exception as e:
print("6 am Loans failed: " + str(e))
try:
day = strftime("%d")
month = strftime("%m")
year = strftime("%Y")
date = year + "-" + month + "-" + day
lsCalce = int(0)
qryrf = """
INSERT INTO
<table name> (pd,sumtotal)
VALUES (current_date,""" + str(lsCalce) + """)
"""
cursor.execute(qryrf)
con.commit()
cursor.close()
con.close()
except Exception as e:
print("Back up 6 am Loans failed: " + str(e))
# Morning Forecast Calculation
class mornForecastCalc():
def __init__(self):
#print("Start Morning Forecast")
if morn4amloansTotal_var().fourAMAddedLoansSumtotal is None :
addL4 = 0
else:
addL4 = morn4amloansTotal_var().fourAMAddedLoansSumtotal
if morn5amloansTotal_var().fiveAMAddedLoansSumtotal is None:
addL5 = 0
else:
addL5 = morn5amloansTotal_var().fiveAMAddedLoansSumtotal
if morn6amloansTotal_var().sixAMAddedLoansSumtotal is None:
addL6 = 0
else:
addL6 = morn6amloansTotal_var().sixAMAddedLoansSumtotal
cvf = mornCVF_var().sumH
mp = mornPending_var().calcPendingTotal
#print("4 am added loans: " + str(addL4))
#print("5 am added loans: " + str(addL5))
#print("6 am added loans: " + str(addL6))
#print("CVF total: " + str(cvf))
#print("Pending total: " + str(mp))
addedLoans = [addL4, addL5, addL6]
hrR = max(addedLoans, key=lambda item:item[0])
print('Loans Added per Hour')
print(hrR)
totalHrR = hrR * 8
print('Total loans per hour')
print(totalHrR)
print('cfv')
print(cvf)
#print("added by hour: " + str(hrR))
self.morningFundingForecast = ((hrR * 8) + mp) + cvf
print("morning forecast is: " + str(self.morningFundingForecast))
def mornForecastCalc_var():
return mornForecastCalc()
# Load Morning Forecast to rds table
def mornforecastUL():
var = pgprod_prop()
con = var.pgprodcon
cursor = con.cursor()
mf = int(mornForecastCalc_var().morningFundingForecast)
qryrf = """
INSERT INTO
<table name> (pd,mornforecast)
VALUES (current_date,""" + str(mf) + """)
"""
cursor.execute(qryrf)
con.commit()
cursor.close()
con.close()
# mornforecast DynamoDB UL
def mornforecastDynamoDBUL():
cd = strftime("%Y-%m-%d")
mf = int(mornForecastCalc_var().morningFundingForecast)
sesh = boto3.Session()
dynamodb = sesh.resource('dynamodb', region_name="us-east-2")
table = dynamodb.Table('<table name>')
table.put_item(
Item={
"uldate":cd,
"mornforecast": mf
}
)
print("Successfully updated mornforecast table in dynamoDB")
# risk forecast
class mornRiskCalc():
def __init__(self):
con = pgprod_prop()
print('initiating risk forecast')
qry_rf = """
SELECT
*
FROM <table name>
WHERE CAST(fdate as date) = current_date;
"""
# risk forecast
rfDF = pd.read_sql_query(qry_rf, con.pgprodcon)
self.rfTotal = format(int(rfDF['ftotal']), ',')
#print('rftotal')
print(self.rfTotal)
def mornRiskCalc_var():
return mornRiskCalc()
# email report
def morningFundingReportEM():
msg = MIMEMultipart()
recpSolo = ["<Your email>"]
recpTest = ["<your test distro>"]
recp = ["<your distro list>"]
sender = "<your email address>"
msg['To'] = ", ".join(recpSolo)
msg['From'] = sender
msg['Subject'] = "<your subject line>"
bodyORG = MIMEText("""
<html>You HTML for the email.</html>
""", 'html', 'utf-8')
msg.attach(bodyORG)
olp = olProps_var()
s = smtpserver = smtplib.SMTP("smtp-mail.outlook.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.login(olp.MSUS, olp.MSPS)
s.sendmail(sender, recpSolo, msg.as_string())
print('done!')
s.close()