Skip to content

Commit

Permalink
update to run everyday
Browse files Browse the repository at this point in the history
  • Loading branch information
Piao committed Aug 16, 2024
1 parent 4c18ac5 commit e233b5e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion server/cronjob/prediction-cron
Original file line number Diff line number Diff line change
@@ -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
23 changes: 14 additions & 9 deletions server/cronjob/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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],
Expand All @@ -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
Expand Down

0 comments on commit e233b5e

Please sign in to comment.