-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
83 lines (71 loc) · 2.15 KB
/
Copy pathutils.py
File metadata and controls
83 lines (71 loc) · 2.15 KB
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
import logging
import time
import warnings
from datetime import datetime
import pandas as pd
from sqlalchemy.exc import SAWarning
from connections.snowflake import connect_snowflake
def init_logging():
logging.basicConfig(
filename="app.log",
filemode="w",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
encoding="utf-8",
)
logging.getLogger("snowflake.connector").setLevel(logging.WARNING)
logging.getLogger("azure").setLevel(logging.WARNING)
warnings.filterwarnings(
"ignore",
category=SAWarning,
message=(
"The GenericFunction 'flatten' is already registered and is going "
"to be overridden."),
)
logging.info("### START ###")
def execution_time(start_time: float):
elapsed = time.time() - start_time
minutes = int(elapsed // 60)
seconds = int(elapsed % 60)
return round(minutes + (seconds / 100), 2)
def save_logs(
programa: str,
tabela: str,
duracao: float,
error: str,
categoria: str,
grupo: str,
finalizado: bool,
periodicidade: str,
):
with open("app.log", encoding="utf-8") as log_file:
logs = log_file.read()
if finalizado and error == "S":
finalizado = False
dataframe = pd.DataFrame([{
"processo": f"{programa.upper()}.{tabela.upper()}",
"duracao": duracao,
"alerta": error == "S",
"categoria": categoria.upper(),
"dthr_processo": datetime.now(),
"logs": logs,
"grupo": grupo.upper(),
"dt_particao": datetime.now(),
"finalizado": finalizado,
"origem": "JOB_LAKE",
"periodicidade": periodicidade.upper(),
}])
engine = connect_snowflake()
try:
with engine.begin() as connection:
dataframe.to_sql(
"logs_processos",
connection,
schema="stg_mkt",
if_exists="append",
index=False,
method="multi",
chunksize=1,)
logging.info("Log de execução salvo no banco de dados.")
finally:
engine.dispose()