-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhardware.py
48 lines (38 loc) · 1.27 KB
/
hardware.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
from flask import Flask, request, jsonify
import sqlite3 as sql
import time
import random
import os
application = Flask(__name__)
#Setup mysql connection using flask-mysql
from flaskext.mysql import MySQL
mysql = MySQL()
#Set variables using ENV vars or use defaults
application.config['MYSQL_DATABASE_HOST'] = os.environ.get('MYSQL_HOST', 'mysql')
application.config['MYSQL_DATABASE_USER'] = os.environ.get('MYSQL_USER', 'mysql')
application.config['MYSQL_DATABASE_PASSWORD'] = os.environ.get('MYSQL_PASSWORD', 'mysql')
application.config['MYSQL_DATABASE_DB'] = os.environ.get('MYSQL_DATABASE', 'hardware')
mysql.init_app(application)
def slow_process_to_calculate_availability(provider, name):
time.sleep(5)
return random.choice(['HIGH', 'MEDIUM', 'LOW'])
@application.route('/hardware/')
def hardware():
con = mysql.connect()
c = con.cursor()
c.execute('SELECT * from hardware')
statuses = [
{
'provider': row[1],
'name': row[2],
'availability': slow_process_to_calculate_availability(
row[1],
row[2]
),
}
for row in c.fetchall()
]
con.close()
return jsonify(statuses)
if __name__ == "__main__":
application.run(host='0.0.0.0', port=5001)