forked from SilexOne/sse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
128 lines (108 loc) · 4.33 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
import os
import sys
import json
import sqlite3
from subprocess import Popen
from flatten_json import flatten_json, unflatten
from flask import Flask, render_template, jsonify, request, redirect, url_for
from settings import (CONFIG_LOCATION, PYTHON_ENV, SCORING_ENGINE_LOCATION,
DATABASE_LOCATION)
import logging
logging.getLogger("werkzeug").setLevel(logging.WARNING) # Stop the flood of messages
app = Flask(__name__)
@app.route('/')
def scoreboard():
return render_template("index.html")
@app.route('/config')
def config_display():
try:
config = read_config().json
except AttributeError as e:
# TODO: Use a page to collect errors
return "scoring_engine/main.json ran into an error when being convert to JSON. Check " \
"to see if main.json exist and if it has the correct syntax. Error message: " \
"{}".format(e)
except Exception as e:
return str(e)
return render_template('config.html', result=config)
@app.route('/api/config', methods=['POST', 'GET'])
def read_config():
if request.method == 'GET':
try:
with open(CONFIG_LOCATION, 'r') as f:
config = json.load(f)
except Exception as e:
return str(e)
flat_json = [{'name': k, 'value': v} for k, v in flatten_json(config).items()]
sorted_flat_json = sorted(flat_json, key=lambda k: k['name'])
return jsonify(sorted_flat_json)
elif request.method == 'POST':
result = request.form
result = unflatten(result)
with open(CONFIG_LOCATION, 'w') as f:
json.dump(result, f, indent=4)
return redirect(url_for('scoreboard'))
@app.route('/api/engine', methods=['POST', 'GET'])
def start_scoring_engine():
if request.method == 'POST':
scoring_engine = Popen([PYTHON_ENV, SCORING_ENGINE_LOCATION],
stdout=sys.stdout, stderr=sys.stderr,
env=os.environ)
return redirect(url_for('scoreboard'))
elif request.method == 'GET':
return ''
@app.route('/api/services/status')
def score_board():
services_last_status = {}
with sqlite3.connect(DATABASE_LOCATION) as connection:
try:
cursor = connection.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
table_name_response = cursor.fetchall()
for table in table_name_response:
table_name = table[0]
cursor.execute("SELECT * FROM {} ORDER BY id DESC LIMIT 1".format(table_name))
last_entry_response = cursor.fetchone()
last_entry_status = last_entry_response[2]
services_last_status[table_name] = last_entry_status
return jsonify(services_last_status)
except Exception as e:
return str(e)
@app.route('/api/tables')
def list_tables():
tables = []
with sqlite3.connect(DATABASE_LOCATION) as connection:
cursor = connection.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
response = cursor.fetchall()
for table in response:
tables.append(table[0])
return str(tables)
@app.route('/api/tables/<tablename>')
def query_table(tablename):
response = ''
with sqlite3.connect(DATABASE_LOCATION) as connection:
try:
cursor = connection.cursor()
cursor.execute("SELECT * FROM {}".format(tablename))
all_rows = cursor.fetchall()
for row in all_rows:
response += '{0} | {1} | {2}<br/>'.format(row[0], row[1], row[2])
return str(response)
except Exception as e:
return str(e)
@app.route('/api/tables/last/<tablename>')
def query_table_last_entry(tablename):
with sqlite3.connect(DATABASE_LOCATION) as connection:
try:
cursor = connection.cursor()
cursor.execute("SELECT * FROM {}".format(tablename))
all_rows = cursor.fetchall()
last_entry = '{0} | {1} | {2}'.format(all_rows[-1][0],
all_rows[-1][1],
all_rows[-1][2])
return str(last_entry)
except Exception as e:
return str(e)
if __name__ == '__main__':
app.run()