-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
473 lines (402 loc) · 18.4 KB
/
main.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import threading
import warnings
from fastapi import (
FastAPI,
status
)
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from loguru import logger
from helpers.database_interactions import connect_to_sqlite_db
from helpers.log_setting import (
remove_logfile_handler,
set_logfile_handler,
set_stdout_logger
)
from helpers.main_helpers import (
generate_order_id,
lem_prices_return_structure,
milp_return_structure,
offers_return_structure
)
from schemas.enums import (
LemOrganization,
PricingMechanism
)
from schemas.input_schemas import (
BaseUserParams,
UserParams
)
from schemas.output_schemas import (
AcceptedResponse,
BilateralMILPOutputs,
MeterIDNotFound,
OrderNotFound,
OrderNotProcessed,
PoolMILPOutputs,
TimeseriesDataNotFound,
VanillaOutputs
)
from threads.vanilla_thread import run_vanilla_thread
from threads.dual_thread import run_dual_thread
from threads.loop_thread import run_loop_thread
# Silence deprecation warning for startup and shutdown events
warnings.filterwarnings("ignore", category=DeprecationWarning)
# Initialize the app
app = FastAPI(
title='Enershare - REC Operation and LEM pricing API',
description='A REST API designed to calculate the prices in a Local Energy Market (LEM) and '
'to determine the operational schedule of Battery Energy Storage System (BESS) assets '
'within a Renewable Energy Community (REC).',
version='0.2.1'
)
# To avoid CORS problem, where the UI makes an initial OPTIONS request
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173",],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Set up logging
set_stdout_logger()
app.state.handler = set_logfile_handler('logs')
# Runs when the API is started: set loggers and create / connect to SQLite database ####################################
@app.on_event('startup')
def startup_event():
# Get cursor and connection to SQLite database
app.state.conn, app.state.cursor = connect_to_sqlite_db()
# Runs when the API is closed: remove logger handlers and disconnect SQLite database
@app.on_event('shutdown')
def shutdown_event():
# Remove all handlers associated with the logger object
remove_logfile_handler(app.state.handler)
# Get cursor and connection to SQLite database
app.state.conn.close()
# VANILLA ENDPOINTS ####################################################################################################
@app.post('/vanilla/{pricing_mechanism}',
description='Calculate an array of LEM prices using the selected pricing mechanism. <br />'
'No Mixed Integer Linear Programming (MILP) is solved. '
'The LEM offers are formulated solely on the basis of the meters’ historical or '
'projected net consumption and their corresponding opportunity costs',
status_code=status.HTTP_202_ACCEPTED,
tags=['Calculate LEM Prices'])
def vanilla(pricing_mechanism: PricingMechanism, user_params: UserParams) -> AcceptedResponse:
# generate an order ID for the user to fetch the results when ready
logger.info('Generating unique order ID.')
id_order = generate_order_id()
# get the type of mechanism select for price computation
pm = pricing_mechanism.value
# update the database with the new order ID
logger.info('Creating registry in database for new order ID.')
app.state.cursor.execute('''
INSERT INTO Orders (order_id, processed, error, message, request_type, lem_organization, pricing_mechanism)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (id_order, False, '', '', 'vanilla', 'pool', pm))
app.state.conn.commit()
# initiate a parallel process (thread) to start computing the prices
# while a message is immediately sent to the user
logger.info('Launching thread.')
threading.Thread(target=run_vanilla_thread,
args=(pricing_mechanism, user_params, id_order, app.state.conn, app.state.cursor)).start()
logger.info('Returning confirmation message with order ID.')
return JSONResponse(content={'message': 'Processing has started. Use the order ID for status updates.',
'order_id': id_order},
status_code=status.HTTP_202_ACCEPTED)
@app.get('/vanilla/{order_id}',
description='An endpoint for fetching the results of a "vanilla" request, given the order ID.',
responses={
202: {'model': OrderNotProcessed, 'description': 'Order found but not yet processed.'},
404: {'model': OrderNotFound, 'description': 'Order not found.'},
412: {'model': MeterIDNotFound, 'description': 'One or more meter IDs not found.'},
422: {'model': TimeseriesDataNotFound,
'description': 'One or more data points for one or more meter IDs not found.'}
},
status_code=status.HTTP_200_OK,
tags=['Retrieve LEM Prices'])
def vanilla(order_id: str) -> VanillaOutputs:
# Check if the order_id exists in the database
logger.info('Searching for order ID in local database.')
app.state.cursor.execute('''
SELECT * FROM Orders WHERE order_id = ?
''', (order_id,))
# Fetch one row
order = app.state.cursor.fetchone()
order_type = order[4] if order is not None else None
if order is not None and order_type == 'vanilla':
logger.info('Order ID found. Checking if order has already been processed.')
processed = bool(order[1])
error = order[2]
message = order[3]
# Check if the order is processed
if processed:
logger.info('Order ID processed. Checking if process raised error.')
if error == '412':
# If the order is found but was met with missing meter ID(s)
return JSONResponse(content={'message': message,
'order_id': order_id},
status_code=status.HTTP_412_PRECONDITION_FAILED)
elif error == '422':
# If the order is found but was met with missing data point(s)
return JSONResponse(content={'message': message,
'order_id': order_id},
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
else:
logger.info('Order ID correctly processed. Fetching outputs.')
# If the order resulted from a request to a "vanilla" endpoint,
# prepare the response message accordingly
lem_prices = lem_prices_return_structure(app.state.cursor, order_id)
offers = offers_return_structure(app.state.cursor, order_id)
return JSONResponse(content={'order_id': order_id,
'lem_prices': lem_prices,
'offers': offers},
status_code=status.HTTP_200_OK)
else:
# If the order is found but not processed, return 202 Accepted
return JSONResponse(content={'message': 'Order found but not yet processed.',
'order_id': order_id},
status_code=status.HTTP_202_ACCEPTED)
else:
# If the order is not found, return 404 Not Found
return JSONResponse(content={'message': 'Order not found.',
'order_id': order_id},
status_code=status.HTTP_404_NOT_FOUND)
# MILP ENDPOINTS #######################################################################################################
@app.post('/dual',
description='Calculate an array of LEM prices and the operational schedule of the BESS assets '
'by executing a purely collective MILP. <br />'
'In this process, the shadow prices of a LEM equilibrium constraint are returned as the optimal '
'LEM prices.',
status_code=status.HTTP_202_ACCEPTED,
tags=['Schedule operation and calculate LEM Prices'])
def dual(user_params: BaseUserParams) -> AcceptedResponse:
# generate an order ID for the user to fetch the results when ready
logger.info('Generating unique order ID.')
id_order = generate_order_id()
# update the database with the new order ID
logger.info('Creating registry in database for new order ID.')
app.state.cursor.execute('''
INSERT INTO Orders (order_id, processed, error, message, request_type, lem_organization, pricing_mechanism)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (id_order, False, '', '', 'dual', 'pool', ''))
app.state.conn.commit()
# initiate a parallel process (thread) to start computing the prices
# while a message is immediately sent to the user
logger.info('Launching thread.')
threading.Thread(target=run_dual_thread,
args=(user_params, id_order, app.state.conn, app.state.cursor)).start()
return JSONResponse(content={'message': 'Processing has started. Use the order ID for status updates.',
'order_id': id_order},
status_code=status.HTTP_202_ACCEPTED)
@app.post('/loop/{lem_organization}/{pricing_mechanism}',
description='Calculate an array of LEM prices and the operational schedule of the BESS assets '
'by implementing an iterative algorithm. <br />'
'In this process, successive two-stage MILP are solved until a specified stopping criterion '
'is achieved. Each two-stage MILP procedure is executed with LEM prices that are calculated '
'based on the user-defined pricing mechanism. '
'The offers are formulated based on the net loads that result from the previously solved MILP.',
status_code=status.HTTP_202_ACCEPTED,
tags=['Schedule operation and calculate LEM Prices'])
def loop(pricing_mechanism: PricingMechanism,
lem_organization: LemOrganization,
user_params: UserParams) -> AcceptedResponse:
# generate an order ID for the user to fetch the results when ready
logger.info('Generating unique order ID.')
id_order = generate_order_id()
# get the type of mechanism select for price computation
pm = pricing_mechanism.value
# get the LEM organization
lo = lem_organization.value
# update the database with the new order ID
logger.info('Creating registry in database for new order ID.')
app.state.cursor.execute('''
INSERT INTO Orders (order_id, processed, error, message, request_type, lem_organization, pricing_mechanism)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (id_order, False, '', '', 'loop', lo, pm))
app.state.conn.commit()
# initiate a parallel process (thread) to start computing the prices
# while a message is immediately sent to the user
logger.info('Launching thread.')
threading.Thread(target=run_loop_thread,
args=(pm, lo, user_params, id_order, app.state.conn, app.state.cursor)).start()
logger.info('Returning confirmation message with order ID.')
return JSONResponse(content={'message': 'Processing has started. Use the order ID for status updates.',
'order_id': id_order},
status_code=status.HTTP_202_ACCEPTED)
@app.get('/dual/{order_id}',
description='An endpoint for fetching the results of a request that involves solving one or several MILP, '
'given the order ID.',
responses={
202: {'model': OrderNotProcessed, 'description': 'Order found but not yet processed.'},
404: {'model': OrderNotFound, 'description': 'Order not found.'},
412: {'model': MeterIDNotFound, 'description': 'One or more meter IDs not found.'},
422: {'model': TimeseriesDataNotFound,
'description': 'One or more data point for one or more meter IDs not found.'}
},
status_code=status.HTTP_200_OK,
tags=['Retrieve operation and LEM prices'])
def dual(order_id: str) -> PoolMILPOutputs:
# Check if the order_id exists in the database
logger.info('Searching for order ID in local database.')
app.state.cursor.execute('''
SELECT * FROM Orders WHERE order_id = ?
''', (order_id,))
# Fetch one row
order = app.state.cursor.fetchone()
order_type = order[4] if order is not None else None
lem_organization = order[5] if order is not None else None
if order is not None and order_type == 'dual' and lem_organization == 'pool':
logger.info('Order ID found. Checking if order has already been processed.')
processed = bool(order[1])
error = order[2]
message = order[3]
# Check if the order is processed
if processed:
logger.info('Order ID processed. Checking if process raised error.')
if error == '412':
# If the order is found but was met with missing meter ID(s)
return JSONResponse(content={'message': message,
'order_id': order_id},
status_code=status.HTTP_412_PRECONDITION_FAILED)
elif error == '422':
# If the order is found but was met with missing data point(s)
return JSONResponse(content={'message': message,
'order_id': order_id},
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
else:
logger.info('Order ID correctly processed. Fetching outputs.')
# If the order resulted from a request to a "vanilla" endpoint,
# prepare the response message accordingly
milp_return = milp_return_structure(app.state.cursor, order_id, 'pool')
return JSONResponse(content=milp_return,
status_code=status.HTTP_200_OK)
else:
# If the order is found but not processed, return 202 Accepted
return JSONResponse(content={'message': 'Order found but not yet processed.',
'order_id': order_id},
status_code=status.HTTP_202_ACCEPTED)
else:
# If the order is not found, return 404 Not Found
return JSONResponse(content={'message': 'Order not found.',
'order_id': order_id},
status_code=status.HTTP_404_NOT_FOUND)
@app.get('/loop/pool/{order_id}',
description='An endpoint for fetching the results of a request that involves solving one or several MILP, '
'given the order ID.',
responses={
202: {'model': OrderNotProcessed, 'description': 'Order found but not yet processed.'},
404: {'model': OrderNotFound, 'description': 'Order not found.'},
412: {'model': MeterIDNotFound, 'description': 'One or more meter IDs not found.'},
422: {'model': TimeseriesDataNotFound,
'description': 'One or more data point for one or more meter IDs not found.'}
},
status_code=status.HTTP_200_OK,
tags=['Retrieve operation and LEM prices'])
def loop_pool(order_id: str) -> PoolMILPOutputs:
# Check if the order_id exists in the database
logger.info('Searching for order ID in local database.')
app.state.cursor.execute('''
SELECT * FROM Orders WHERE order_id = ?
''', (order_id,))
# Fetch one row
order = app.state.cursor.fetchone()
order_type = order[4] if order is not None else None
lem_organization = order[5] if order is not None else None
if order is not None and order_type == 'loop' and lem_organization == 'pool':
logger.info('Order ID found. Checking if order has already been processed.')
processed = bool(order[1])
error = order[2]
message = order[3]
# Check if the order is processed
if processed:
logger.info('Order ID processed. Checking if process raised error.')
if error == '412':
# If the order is found but was met with missing meter ID(s)
return JSONResponse(content={'message': message,
'order_id': order_id},
status_code=status.HTTP_412_PRECONDITION_FAILED)
elif error == '422':
# If the order is found but was met with missing data point(s)
return JSONResponse(content={'message': message,
'order_id': order_id},
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
else:
logger.info('Order ID correctly processed. Fetching outputs.')
# If the order resulted from a request to a "vanilla" endpoint,
# prepare the response message accordingly
milp_return = milp_return_structure(app.state.cursor, order_id, 'pool')
return JSONResponse(content=milp_return,
status_code=status.HTTP_200_OK)
else:
# If the order is found but not processed, return 202 Accepted
return JSONResponse(content={'message': 'Order found but not yet processed.',
'order_id': order_id},
status_code=status.HTTP_202_ACCEPTED)
else:
# If the order is not found, return 404 Not Found
return JSONResponse(content={'message': 'Order not found.',
'order_id': order_id},
status_code=status.HTTP_404_NOT_FOUND)
@app.get('/loop/bilateral/{order_id}',
description='An endpoint for fetching the results of a request that involves solving one or several MILP, '
'given the order ID.',
responses={
202: {'model': OrderNotProcessed, 'description': 'Order found but not yet processed.'},
404: {'model': OrderNotFound, 'description': 'Order not found.'},
412: {'model': MeterIDNotFound, 'description': 'One or more meter IDs not found.'},
422: {'model': TimeseriesDataNotFound,
'description': 'One or more data point for one or more meter IDs not found.'}
},
status_code=status.HTTP_200_OK,
tags=['Retrieve operation and LEM prices'])
def loop_bilateral(order_id: str) -> BilateralMILPOutputs:
# Check if the order_id exists in the database
logger.info('Searching for order ID in local database.')
app.state.cursor.execute('''
SELECT * FROM Orders WHERE order_id = ?
''', (order_id,))
# Fetch one row
order = app.state.cursor.fetchone()
order_type = order[4] if order is not None else None
lem_organization = order[5] if order is not None else None
if order is not None and order_type == 'loop' and lem_organization == 'bilateral':
logger.info('Order ID found. Checking if order has already been processed.')
processed = bool(order[1])
error = order[2]
message = order[3]
# Check if the order is processed
if processed:
logger.info('Order ID processed. Checking if process raised error.')
if error == '412':
# If the order is found but was met with missing meter ID(s)
return JSONResponse(content={'message': message,
'order_id': order_id},
status_code=status.HTTP_412_PRECONDITION_FAILED)
elif error == '422':
# If the order is found but was met with missing data point(s)
return JSONResponse(content={'message': message,
'order_id': order_id},
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
else:
logger.info('Order ID correctly processed. Fetching outputs.')
# If the order resulted from a request to a "vanilla" endpoint,
# prepare the response message accordingly
milp_return = milp_return_structure(app.state.cursor, order_id, 'bilateral')
return JSONResponse(content=milp_return,
status_code=status.HTTP_200_OK)
else:
# If the order is found but not processed, return 202 Accepted
return JSONResponse(content={'message': 'Order found but not yet processed.',
'order_id': order_id},
status_code=status.HTTP_202_ACCEPTED)
else:
# If the order is not found, return 404 Not Found
return JSONResponse(content={'message': 'Order not found.',
'order_id': order_id},
status_code=status.HTTP_404_NOT_FOUND)
if __name__ == '__main__':
import uvicorn
import os
host = os.getenv("PRICING_HOST", "127.0.0.1")
port = int(os.getenv("PRICING_PORT", 8000))
uvicorn.run(app, host=host, port=port)