From e233b5ea23e79bf74726e577f623069aa26bee59 Mon Sep 17 00:00:00 2001 From: Piao Date: Fri, 16 Aug 2024 10:03:23 +0100 Subject: [PATCH] update to run everyday --- server/cronjob/prediction-cron | 2 +- server/cronjob/prediction.py | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/server/cronjob/prediction-cron b/server/cronjob/prediction-cron index 7c8c649..89d28e5 100644 --- a/server/cronjob/prediction-cron +++ b/server/cronjob/prediction-cron @@ -1 +1 @@ -*/10 * * * * root /usr/bin/python /usr/src/app/cronjob/prediction.py >> /var/log/cron.log 2>&1 +0 0 * * * root /usr/bin/python /usr/src/app/cronjob/prediction.py >> /var/log/cron.log 2>&1 diff --git a/server/cronjob/prediction.py b/server/cronjob/prediction.py index 25b49e9..80943e6 100644 --- a/server/cronjob/prediction.py +++ b/server/cronjob/prediction.py @@ -35,19 +35,24 @@ def main(): """ Run prediction based on given input + Implemented based on daily prediction """ + cur_time = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.000Z') + parser = argparse.ArgumentParser('Prediction service') parser.add_argument( '--prometheus_query', type=str, help='API endpoint to get the relevant metric', - default=f'{PROMETHEUS}' + '/api/v1/query_range?query=sum(increase(kepler_container_joules_total{job=%27kepler%27}[10m]))&start=2024-08-11T00:00:00.000Z&end=2024-08-13T00:00:00.000Z&step=600s' + default='sum(increase(kepler_container_joules_total{job=%27kepler%27}[1d]))&start=2024-08-11T00:00:00.000Z&'+f'end={cur_time}&step=86400s' ) parser.add_argument('--forecasting_method', help='Choose forecasting method [ARIMA, HBNN]', default='ARIMA') args = parser.parse_args() + # Get full query + prometheus_query = f'{PROMETHEUS}/api/v1/query_range?query={args.prometheus_query}' # Get the metrics - res = requests.get(args.prometheus_query) + res = requests.get(prometheus_query) print(res.content) if (res.ok): res_json = json.loads(res.content)['data']['result'][0]['values'] @@ -56,18 +61,18 @@ def main(): times = data_dict.keys() vals = data_dict.values() vals = np.array(list(vals)).astype(float) - print('times', times) - print('vals', vals) + print('\ntimes', times) + print('\nvals', vals) # Get the precition - pred = AutoARIMA(vals, evaluate=10) + pred = AutoARIMA(vals, evaluate=1) print(pred.prediction, pred.upper_bound, pred.lower_bound) # Construct JSON format data to be posted data = { - "aggregation_interval": 600, + "aggregation_interval": 86400, "forecasting_lower_bounds": pred.lower_bound if isinstance(pred.lower_bound, list) else [pred.lower_bound], - "forecasting_model": "ARIMA", + "forecasting_model": args.forecasting_method, "forecasting_period": 1, "forecasting_upper_bounds": pred.upper_bound if isinstance(pred.upper_bound, list) else [pred.upper_bound], "forecasting_values": pred.prediction if isinstance(pred.prediction, list) else [pred.prediction], @@ -78,11 +83,11 @@ def main(): store(ENDPOINT, data) else: - print(f'Response: {res}') + print(f'\nResponse: {res}') def store( - endpoint: str = 'http://data-storage.integration/prediction', + endpoint: str = ENDPOINT, data: dict = TEST_DATA, ): """ Post data to REST API endpoint