-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
284 lines (244 loc) · 10.4 KB
/
run.py
File metadata and controls
284 lines (244 loc) · 10.4 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
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
"""
TelcoTemp -- Temperature Monitoring and Visualization System
Supports both CML (microwave links), Meteo (weather stations), and Combined modes
"""
from telcotemp.core.config import AppConfig
from telcotemp.core.log import LoggerManager
from telcotemp.core.initialization import (
initialize_database,
initialize_geographical_processing,
)
from telcotemp.processing.calculation_engine import (
CalculationEngine,
CombinedCalculationEngine,
)
import argparse
import datetime
import sys
class Application:
"""Unified CLI application for CML, Meteo, and Combined modes."""
def __init__(self, mode="combined"):
"""
Initialize application.
:param mode: "cml", "meteo", or "combined"
"""
try:
self.config = AppConfig(mode=mode)
self.logger_manager = LoggerManager(self.config)
self.logger = self.logger_manager.get_logger("backend_logger")
self.logger.info(f"Initializing TelcoTemp in {mode.upper()} mode")
# Initialize database and metadata provider(s)
self.engine, self.metadata_provider = initialize_database(
self.config, self.logger
)
# Initialize geographical processing
self.geo_components = initialize_geographical_processing(self.config)
# Initialize calculation engine(s)
if mode == "combined":
cml_provider, meteo_provider = self.metadata_provider
self.calculation_engine = CombinedCalculationEngine(
self.config,
self.logger_manager,
cml_provider,
meteo_provider,
self.geo_components,
)
else:
self.calculation_engine = CalculationEngine(
self.config,
self.logger_manager,
self.metadata_provider,
self.geo_components,
)
self.logger.info("Application initialized successfully")
except Exception as e:
print(f"Error initializing application: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
def parse_arguments(self):
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="TelcoTemp - Unified temperature mapping from CML, weather stations, or both",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Combined mode (default) - continuous processing
python run.py
# Combined mode - process last week
python run.py --first_run
# Combined mode - specific time range
python run.py --start_time "2024-01-01 00:00" --end_time "2024-01-02 00:00"
# CML mode only
python marunin.py --mode cml --start_time "2024-01-01 00:00" --end_time "2024-01-02 00:00"
# Meteo mode only
python run.py --mode meteo --start_time "2024-01-01 00:00" --end_time "2024-01-02 00:00"
# Combined mode - filter both sources
python run.py --cml_filter_ids "123,456" --meteo_filter_ids "LKCB,LKKV"
""",
)
parser.add_argument(
"--mode",
choices=["cml", "meteo", "combined"],
default="combined",
help="Data source mode: cml, meteo, or combined (default: combined)",
)
parser.add_argument(
"--first_run",
action="store_true",
help="Process hourly maps for the last week",
)
parser.add_argument(
"--start_time",
type=str,
help="Start time in format YYYY-MM-DD HH:MM",
)
parser.add_argument(
"--end_time",
type=str,
help="End time in format YYYY-MM-DD HH:MM",
)
parser.add_argument(
"--filter_ids",
type=str,
help="Comma-separated list of Link IDs (CML mode) or Station IDs (Meteo mode)",
)
parser.add_argument(
"--cml_filter_ids",
type=str,
help="Comma-separated list of Link IDs (Combined mode)",
)
parser.add_argument(
"--meteo_filter_ids",
type=str,
help="Comma-separated list of Station IDs (Combined mode)",
)
parser.add_argument(
"--continuous",
action="store_true",
help="Run in continuous mode (process every hour)",
)
return parser.parse_args()
def run(self):
"""Run the application."""
try:
args = self.parse_arguments()
# Update mode if specified and different
if args.mode != self.config.mode:
self.logger.info(f"Switching to {args.mode.upper()} mode")
self.__init__(mode=args.mode)
self.logger.info(f"Starting in {args.mode.upper()} mode")
# Handle filter IDs based on mode
if args.mode == "combined":
cml_filter_ids = None
meteo_filter_ids = None
if args.cml_filter_ids:
cml_filter_ids = [
fid.strip() for fid in args.cml_filter_ids.split(",")
]
self.logger.info(f"CML filter: {cml_filter_ids}")
if args.meteo_filter_ids:
meteo_filter_ids = [
fid.strip() for fid in args.meteo_filter_ids.split(",")
]
self.logger.info(f"Meteo filter: {meteo_filter_ids}")
# Process based on arguments
if args.first_run:
# Process last week
end_time = datetime.datetime.now(datetime.timezone.utc).replace(
minute=0, second=0, microsecond=0
)
start_time = end_time - datetime.timedelta(days=7)
self.logger.info(
f"Processing last week: {start_time} to {end_time}"
)
self.calculation_engine.process_time_range(
start_time, end_time, cml_filter_ids, meteo_filter_ids
)
elif args.start_time and args.end_time:
# Process specific time range
try:
start_time = datetime.datetime.strptime(
args.start_time, "%Y-%m-%d %H:%M"
).replace(tzinfo=datetime.timezone.utc)
end_time = datetime.datetime.strptime(
args.end_time, "%Y-%m-%d %H:%M"
).replace(tzinfo=datetime.timezone.utc)
self.logger.info(
f"Processing time range: {start_time} to {end_time}"
)
self.calculation_engine.process_time_range(
start_time, end_time, cml_filter_ids, meteo_filter_ids
)
except ValueError as e:
self.logger.error(f"Invalid time format: {e}")
self.logger.error("Use format: YYYY-MM-DD HH:MM")
sys.exit(1)
else:
# Default: Continuous processing
self.logger.info("Starting continuous processing (default mode)")
self.calculation_engine.data_processing_loop(
first_run=False,
cml_filter_ids=cml_filter_ids,
meteo_filter_ids=meteo_filter_ids,
)
else:
# Single mode (CML or Meteo)
filter_ids = None
if args.filter_ids:
filter_ids = [fid.strip() for fid in args.filter_ids.split(",")]
id_type = "link_ids" if args.mode == "cml" else "station_ids"
self.logger.info(f"Filtering by {id_type}: {filter_ids}")
# Determine execution mode
if args.continuous:
# Continuous processing loop
self.logger.info("Starting continuous processing loop")
self.calculation_engine.data_processing_loop(
first_run=args.first_run, filter_ids=filter_ids
)
elif args.first_run:
# Process last week
end_time = datetime.datetime.now(datetime.timezone.utc).replace(
minute=0, second=0, microsecond=0
)
start_time = end_time - datetime.timedelta(days=7)
self.logger.info(
f"Processing last week: {start_time} to {end_time}"
)
self.calculation_engine.process_time_range(
start_time, end_time, filter_ids
)
elif args.start_time and args.end_time:
# Process specific time range
try:
start_time = datetime.datetime.strptime(
args.start_time, "%Y-%m-%d %H:%M"
).replace(tzinfo=datetime.timezone.utc)
end_time = datetime.datetime.strptime(
args.end_time, "%Y-%m-%d %H:%M"
).replace(tzinfo=datetime.timezone.utc)
self.logger.info(
f"Processing time range: {start_time} to {end_time}"
)
self.calculation_engine.process_time_range(
start_time, end_time, filter_ids
)
except ValueError as e:
self.logger.error(f"Invalid time format: {e}")
self.logger.error("Use format: YYYY-MM-DD HH:MM")
sys.exit(1)
else:
self.logger.error(
"Specify --first_run, --continuous, or both --start_time and --end_time"
)
sys.exit(1)
self.logger.info("Processing completed")
except KeyboardInterrupt:
self.logger.info("Manual shutdown of the program...")
sys.exit(0)
def main():
"""Main entry point."""
app = Application()
app.run()
if __name__ == "__main__":
main()