-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
321 lines (221 loc) · 10.7 KB
/
application.py
File metadata and controls
321 lines (221 loc) · 10.7 KB
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
# main backend entry point for chevron challenge
from flask import Flask, jsonify, request, send_file
from models import *
from helpers import get_dict, get_dict_array
import algo
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URL")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
@app.route("/")
def index():
return send_file("templates/index.html")
# API Endpoints, JSON request/response, TODO: validate post data
@app.route("/api/certification", methods=["GET", "POST"])
def certification_root():
if request.method == "POST":
# expected data [equipment_type_id, worker_id]
data = request.get_json()
# sanitization
if not data:
return jsonify({"success": False, "message": "Missing body."}), 400
if not 'equipment_type_id' in data or not 'worker_id' in data:
return jsonify({"success": False, "message": "Missing body fields."}), 400
if not isinstance(data['equipment_type_id'], int) or not isinstance(data['worker_id'], int):
return jsonify({"success": False, "message": "Invalid body fields."}), 400
if not EquipmentType.query.get(data['equipment_type_id']):
return jsonify({"success": False, "message": "Equipment Type key not found."}), 404
if not Worker.query.get(data['worker_id']):
return jsonify({"success": False, "message": "Worker key not found."}), 404
# add to db
certification = Certification(data['equipment_type_id'], data['worker_id'])
db.session.add(certification)
db.session.commit()
return jsonify(get_dict(certification))
else:
# get facilities
certifications = Certification.query.all()
return jsonify(get_dict_array(certifications))
@app.route("/api/equipment", methods=["GET", "POST"])
def equipment_root():
if request.method == "POST":
# expected data [equipment_type_id, facility_id]
data = request.get_json()
# sanitization
if not data:
return jsonify({"success": False, "message": "Missing body."}), 400
if not 'equipment_type_id' in data or not 'facility_id' in data:
return jsonify({"success": False, "message": "Missing body fields."}), 400
if not isinstance(data['equipment_type_id'], int) or not isinstance(data['facility_id'], int):
return jsonify({"success": False, "message": "Invalid body fields."}), 400
if not EquipmentType.query.get(data['equipment_type_id']):
return jsonify({"success": False, "message": "Equipment Type key not found."}), 404
if not Facility.query.get(data['facility_id']):
return jsonify({"success": False, "message": "Facility key not found."}), 404
# add to db
equipment = Equipment(data['equipment_type_id'], data['facility_id'])
db.session.add(equipment)
db.session.commit()
return jsonify(get_dict(equipment))
else:
# get equipments
equipments = Equipment.query.all()
return jsonify(get_dict_array(equipments))
@app.route("/api/equipment_type", methods=["GET", "POST"])
def equipment_type_root():
if request.method == "POST":
# expected data [name, prob, hour_min, hour_max]
data = request.get_json()
# sanitization
if not data:
return jsonify({"success": False, "message": "Missing body."}), 400
if not 'name' in data or not 'prob' in data or not 'hour_min' in data or not 'hour_max' in data:
return jsonify({"success": False, "message": "Missing body fields."}), 400
if not isinstance(data['name'], str) or not isinstance(data['prob'], (int, float)) or not isinstance(data['hour_min'], int) or not isinstance(data['hour_max'], int):
return jsonify({"success": False, "message": "Invalid body fields."}), 400
if len(data['name'].strip()) < 1 or len(data['name']) > 25:
return jsonify({"success": False, "message": "Name length out of range."}), 400
if data['prob'] < 0 or data['prob'] > 1:
return jsonify({"success": False, "message": "Probability out of range."}), 400
if data['hour_min'] < 1:
return jsonify({"success": False, "message": "Minimum hour out of range."}), 400
if data['hour_max'] < 1 or data['hour_max'] < data['hour_min']:
return jsonify({"success": False, "message": "Maximum hour out of range."}), 400
# add to db
e_type = EquipmentType(data['name'], data['prob'], data['hour_min'], data['hour_max'])
db.session.add(e_type)
db.session.commit()
# main algorithm run
algo.main()
return jsonify(get_dict(e_type))
else:
# get equipments
equipment_types = EquipmentType.query.all()
return jsonify(get_dict_array(equipment_types))
@app.route("/api/facility", methods=["GET", "POST"])
def facility_root():
if request.method == "POST":
# expected data [lat, lon]
data = request.get_json()
# sanitization
if not data:
return jsonify({"success": False, "message": "Missing body."}), 400
if not 'lat' in data or not 'lon' in data:
return jsonify({"success": False, "message": "Missing body fields."}), 400
if not isinstance(data['lat'], (int, float)) or not isinstance(data['lon'], (int, float)):
return jsonify({"success": False, "message": "Invalid body fields."}), 400
if data['lat'] < -90.0 or data['lat'] > 90.0:
return jsonify({"success": False, "message": "Latitude out of range."}), 400
if data['lon'] < -180.0 or data['lon'] > 180.0:
return jsonify({"success": False, "message": "Longitude out of range."}), 400
# add to db
facility = Facility(data['lat'], data['lon'])
db.session.add(facility)
db.session.commit()
return jsonify(get_dict(facility))
else:
# get facilities
facilities = Facility.query.all()
return jsonify(get_dict_array(facilities))
@app.route("/api/facility/<int:fid>")
def facility(fid):
# get response
facility = Facility.query.get(fid)
if facility is None:
return jsonify({"success": False, "message": "Facility with key not found!"}), 404
res = get_dict(facility)
# add relations
res['equipments'] = []
for e in facility.equipments:
res['equipments'].append({"id": e.id, "name": e.equipment_type.name, "equipment_type_id": e.equipment_type_id})
res['orders'] = get_dict_array(facility.orders)
return jsonify(res)
@app.route("/api/order", methods=["GET", "POST"])
def order_root():
if request.method == "POST":
# expected data [priority, time_to_completion, facility_id, equipment_id]
data = request.get_json()
# sanitization
if not data:
return jsonify({"success": False, "message": "Missing body."}), 400
if not 'priority' in data or not 'time_to_completion' in data or not 'facility_id' in data or not 'equipment_id' in data:
return jsonify({"success": False, "message": "Missing body fields."}), 400
if not isinstance(data['priority'], int) or not isinstance(data['time_to_completion'], int) or not isinstance(data['facility_id'], int) or not isinstance(data['equipment_id'], int):
return jsonify({"success": False, "message": "Invalid body fields."}), 400
if data['priority'] < 1 or data['priority'] > 5:
return jsonify({"success": False, "message": "Priority out of range."}), 400
if data['time_to_completion'] < 1:
return jsonify({"success": False, "message": "Time to completion out of range."}), 400
if not Equipment.query.get(data['equipment_id']):
return jsonify({"success": False, "message": "Equipment key not found."}), 404
if not Facility.query.get(data['facility_id']):
return jsonify({"success": False, "message": "Facility key not found."}), 404
# add to db
order = Order(data['priority'], data['time_to_completion'], data['facility_id'], data['equipment_id'])
db.session.add(order)
db.session.commit()
# after each call
algo.main()
return jsonify(get_dict(order))
else:
# get orders
orders = Order.query.order_by(Order.created_at.desc()).all()
return jsonify(get_dict_array(orders))
@app.route("/api/order/<int:order_id>")
def order(order_id):
# get response
order = Order.query.get(order_id)
if order is None:
return jsonify({"success": False, "message": "Order with key not found!"}), 404
# build response
res = get_dict(order)
# add relations
res['worker_name'] = order.worker.name if order.worker else None
return jsonify(res)
@app.route("/api/worker", methods=["GET", "POST"])
def worker_root():
if request.method == "POST":
# expected data [name, shift]
data = request.get_json()
# sanitization
if not data:
return jsonify({"success": False, "message": "Missing body."}), 400
if not 'name' in data or not 'shift' in data:
return jsonify({"success": False, "message": "Missing body fields."}), 400
if not isinstance(data['name'], str) or not isinstance(data['shift'], str):
return jsonify({"success": False, "message": "Invalid body fields."}), 400
if len(data['name'].strip()) < 1 or len(data['name']) > 100:
return jsonify({"success": False, "message": "Name length out of range."}), 400
if len(data['shift'].strip()) < 1 or len(data['shift']) > 15:
return jsonify({"success": False, "message": "Shift length out of range."}), 400
# add to db
worker = Worker(data['name'], data['shift'])
db.session.add(worker)
db.session.commit()
return jsonify(get_dict(worker))
else:
# get request
workers = Worker.query.all()
return jsonify(get_dict_array(workers))
@app.route("/api/worker/<int:worker_id>")
def worker(worker_id):
# get response
worker = Worker.query.get(worker_id)
if worker is None:
return jsonify({"success": False, "message": "Worker with key not found!"}), 404
# build response
res = get_dict(worker)
# add relations
res['certifications'] = []
for cert in worker.certifications:
res['certifications'].append({"name": cert.equipment_type.name, "id": cert.id})
res['orders'] = get_dict_array(worker.orders)
return jsonify(res)
@app.route("/debug")
def debug_algo():
algo.main()
return jsonify({"success": True})
if __name__ == "__main__":
with app.app_context():
db.create_all()
app.run(debug=False, use_reloader=True)