-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
138 lines (104 loc) · 4.35 KB
/
config.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
import logging
import cProfile
import pstats
from datetime import datetime, timedelta
import os
from io import StringIO
from constants import Constants as ct
LOGS_DIR = ct.LOGS
def obscure(secret: str) -> str:
"""
Replaces all but the first and last 3 chars in a string
with asterisks in order to keep it partially private.
If the length is less than 6, only keeps the first and last chars and obscures the rest.
If the length is 3 or less, replaces all chars with asterisks.
"""
length = len(secret)
if length <= 3:
return '*' * length
elif length <= 6:
return secret[0] + '*' * (length - 2) + secret[-1]
else:
return secret[:3] + '*' * (length - 6) + secret[-3:]
def setup_logging(log_name: str, logging_level=logging.DEBUG,
logs_dir=LOGS_DIR):
"""Setup logging with two logs: one for normal logs, and one for warnings and errors.
Prints everything to the terminal as well as saving it to files."""
if not os.path.exists(logs_dir):
os.makedirs(logs_dir)
print(f"Log dir created: `{logs_dir}`")
if not os.path.exists(logs_dir):
raise Exception(f"Failed to create logs directory: {logs_dir}")
logger = logging.getLogger(log_name)
logger.setLevel(logging_level)
console_handler = logging.StreamHandler()
file_handler = logging.FileHandler(f'{logs_dir}/{log_name}.log')
error_file_handler = logging.FileHandler(f'{logs_dir}/{log_name}_issues.log')
console_handler.setLevel(logging_level)
file_handler.setLevel(logging_level)
error_file_handler.setLevel(logging.WARNING)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
error_file_handler.setFormatter(formatter)
if not logger.handlers:
logger.addHandler(console_handler)
logger.addHandler(file_handler)
logger.addHandler(error_file_handler)
return logger
def setup_subtle_logging(log_name, logging_level=logging.DEBUG, logs_dir=LOGS_DIR):
"""setup logging with only one log file and without printing
to the terminal - useful for perfomrance logging"""
logger = logging.getLogger(log_name)
logger.setLevel(logging_level)
file_handler = logging.FileHandler(f'{logs_dir}/{log_name}.log')
file_handler.setLevel(logging.DEBUG)
logger.propagate = False
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
if not logger.handlers:
logger.addHandler(file_handler)
return logger
def start_monitor() -> cProfile.Profile:
"""Launches cProfile so it can log performance"""
profiler = cProfile.Profile()
profiler.enable()
return profiler
def stop_monitor(script_name: str, profiler: cProfile.Profile, logger,
logs_dir=LOGS_DIR) -> None:
"""Terminates performance tracking, saves both a .prof file and a
human-readable text file."""
profiler.disable()
binary_profile = f"{logs_dir}/{script_name}_performance.prof"
profiler.dump_stats(binary_profile)
s = StringIO()
ps = pstats.Stats(profiler, stream=s).sort_stats('cumulative')
ps.print_stats()
human_readable_stats = s.getvalue()
first_line = human_readable_stats.splitlines()[0]
print(first_line) # Print only the first line to the terminal
text_profile = os.path.join(logs_dir, f"{script_name}_performance.txt")
with open(text_profile, 'w') as f:
f.write(human_readable_stats)
def get_settlement_period(current_time=None, previous_period=False):
"""
Get the current settlement period for energy providers, where
2300:2330 is the first period.
"""
if current_time is None:
current_time = datetime.now()
start_of_day = current_time.replace(hour=23, minute=0, second=0, microsecond=0)
if current_time.hour < 23:
start_of_day -= timedelta(days=1)
time_difference = current_time - start_of_day
minutes_since_start = time_difference.total_seconds() // 60
settlement_period = int(minutes_since_start // 30) + 1
if previous_period:
settlement_period -= 1
if settlement_period < 1:
settlement_period = 48
elif settlement_period > 48:
settlement_period = 48
return settlement_period