-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
44 lines (29 loc) · 1.35 KB
/
utils.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
import pandas as pd
import calendar
import logging
import prometheus_client
from prometheus_client import CollectorRegistry, Gauge, Counter, push_to_gateway
PUSHGATEWAY_URL = 'http://localhost:9092'
PROMETHEUS_JOB_NAME = 'autocheck_loan_dwh'
LOG_FILE_NAME = f"logs\\autocheck_{pd.to_datetime('today').strftime('%Y-%m-%d')}.log"
registry = CollectorRegistry()
log_counter = Counter('log_entries', 'Count of log entries by level', ['level'], registry=registry)
class PrometheusLoggingHandler(logging.Handler):
def emit(self, record):
if record.levelname == 'INFO':
log_counter.labels(level='info').inc()
elif record.levelname == 'WARNING':
log_counter.labels(level='warning').inc()
elif record.levelname == 'ERROR':
log_counter.labels(level='error').inc()
try:
push_to_gateway(PUSHGATEWAY_URL, job=PROMETHEUS_JOB_NAME, registry=registry)
except Exception as e:
logging.error(f'Prometheus: {e}')
def validate_and_cast_date(date_str):
month, day, year = map(int, date_str.split('/'))
last_valid_day = calendar.monthrange(year, month)[1]
if day <= last_valid_day:
return pd.to_datetime(date_str, format='%m/%d/%Y').date()
else:
return pd.to_datetime(f"{month}/{last_valid_day}/{year}", format='%m/%d/%Y').date()