forked from Akkudoktor-EOS/EOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_load_profile.py
58 lines (47 loc) · 1.94 KB
/
server_load_profile.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
from flask import Flask, jsonify, request
import numpy as np
from datetime import datetime
import modules.class_load as cl
from pprint import pprint
app = Flask(__name__)
# Constants
DATE_FORMAT = '%Y-%m-%d'
EXPECTED_ARRAY_SHAPE = (2, 24)
FILEPATH = r'.\load_profiles.npz'
def get_load_forecast(year_energy):
"""Initialize LoadForecast with the given year_energy."""
return cl.LoadForecast(filepath=FILEPATH, year_energy=float(year_energy))
def validate_date(date_str):
"""Validate the date string and return a datetime object."""
try:
return datetime.strptime(date_str, DATE_FORMAT)
except ValueError:
raise ValueError("Date is not in the correct format. Expected format: YYYY-MM-DD.")
@app.route('/getdata', methods=['GET'])
def get_data():
# Retrieve the date and year_energy from query parameters
date_str = request.args.get('date')
year_energy = request.args.get('year_energy')
if not date_str or not year_energy:
return jsonify({"error": "Missing 'date' or 'year_energy' query parameter."}), 400
try:
# Validate and convert the date
date_obj = validate_date(date_str)
lf = get_load_forecast(year_energy)
# Get daily statistics for the requested date
array_list = lf.get_daily_stats(date_str)
pprint(array_list)
pprint(array_list.shape)
# Check if the shape of the array is valid
if array_list.shape == EXPECTED_ARRAY_SHAPE:
return jsonify({date_str: array_list.tolist()})
else:
return jsonify({"error": "Data not found for the given date."}), 404
except ValueError as e:
# Return a descriptive error message for date validation issues
return jsonify({"error": str(e)}), 400
except Exception:
# Return a generic error message for unexpected errors
return jsonify({"error": "An unexpected error occurred."}), 500
if __name__ == '__main__':
app.run(debug=True)