-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2_forecast_manager.py
executable file
·89 lines (71 loc) · 3.29 KB
/
v2_forecast_manager.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
import json
import logging
import os
import sys
import argparse
import time
import datetime
import urllib3
from influxdb import DataFrameClient, InfluxDBClient
urllib3.disable_warnings()
from classes.forecaster_v2 import ForecasterV2
if __name__ == "__main__":
# --------------------------------------------------------------------------- #
# Configuration file
# --------------------------------------------------------------------------- #
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("-c", help="configuration file")
arg_parser.add_argument("-l", help="log file (optional, if empty log redirected on stdout)")
args = arg_parser.parse_args()
# Load the main parameters
config_file = args.c
if os.path.isfile(config_file) is False:
print('\nATTENTION! Unable to open configuration file %s\n' % config_file)
sys.exit(1)
cfg = json.loads(open(args.c).read())
# Load the connections parameters and update the config dict with the related values
cfg_conns = json.loads(open(cfg['connectionsFile']).read())
cfg.update(cfg_conns)
# --------------------------------------------------------------------------- #
# Set logging object
# --------------------------------------------------------------------------- #
if not args.l:
log_file = None
else:
log_file = args.l
logger = logging.getLogger()
logging.basicConfig(format='%(asctime)-15s::%(levelname)s::%(funcName)s::%(message)s', level=logging.INFO,
filename=log_file)
logger.info('Starting program')
logger.info('Connection to InfluxDb server on socket [%s:%s]' % (cfg['influxDB']['host'], cfg['influxDB']['port']))
try:
ifc_df = DataFrameClient(host=cfg['influxDB']['host'], port=cfg['influxDB']['port'],
password=cfg['influxDB']['password'], username=cfg['influxDB']['user'],
database=cfg['influxDB']['database'], ssl=cfg['influxDB']['ssl'])
ifc = InfluxDBClient(host=cfg['influxDB']['host'], port=cfg['influxDB']['port'],
password=cfg['influxDB']['password'], username=cfg['influxDB']['user'],
database=cfg['influxDB']['database'], ssl=cfg['influxDB']['ssl'])
except Exception as e:
logger.error('EXCEPTION: %s' % str(e))
sys.exit(3)
logger.info('Connection successful')
forecaster = ForecasterV2(ifc_df, ifc, cfg, logger)
forecaster.retrieve_predictors()
if cfg['forecastPeriod']['case'] == 'current':
forecaster.perform_forecast(cfg['forecastPeriod']['case'])
else:
start_day = cfg['forecastPeriod']['startDate']
end_day = cfg['forecastPeriod']['endDate']
curr_day = start_day
end_dt = datetime.datetime.strptime(end_day, '%Y-%m-%d')
while True:
# Perform the prediction
forecaster.perform_forecast(curr_day)
time.sleep(1)
# add a day
curr_dt = datetime.datetime.strptime(curr_day, '%Y-%m-%d')
curr_day = datetime.datetime.strftime(curr_dt + datetime.timedelta(days=1), '%Y-%m-%d')
# Last day-1d checking
if curr_dt.timestamp() >= end_dt.timestamp():
break
logger.info('Ending program')