-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (61 loc) · 2.14 KB
/
app.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
from flask import Flask, request, redirect, send_from_directory, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
import os
app = Flask(__name__)
from views import flash_ssd_simulation
from views import cell_simulator
from views import about
from views import advance_ssd_simulator
from views.api import trace_file_reader
from views.api import trace_file_converter
from views import trace_file_converter
from views import documentation
from views import json_upload_to_regenerate
from views import presentation
# from views import sitemap_xml
# Configure the SQLite database URI
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
# # Initialize SQLAlchemy instance
db = SQLAlchemy(app)
# create a random secret_key
secret_key = os.urandom(24)
app.secret_key = secret_key
# # Define a model for the database table
class Data(db.Model):
id = db.Column(db.Integer, primary_key=True)
value = db.Column(db.Float)
# # Create the database tables
@app.route('/create_db')
def create_db():
db.create_all()
return 'Database created'
@app.route('/insert_data' , methods=['POST'])
def insert_data():
value = request.json.get('value')
if not value:
return jsonify({'message': 'No value provided.'}), 400
sum_value = db.session.query(func.sum(Data.value)).scalar()
num_rows = db.session.query(func.count(Data.id)).scalar()
print(sum_value, num_rows)
if num_rows == 0:
average_waf = value
else:
average_waf = (sum_value + value)/(num_rows + 1)
new_data = Data(value=average_waf)
db.session.add(new_data)
db.session.commit()
return jsonify({'message': 'Data inserted successfully.'})
@app.route('/get_data' , methods=['GET'])
def get_data():
data = Data.query.all()
data = [{'y': d.value} for d in data]
return jsonify(data)
@app.before_request
def redirect_to_domain():
if request.host.startswith('3.89.197.36') and request.host.endswith(':3000'):
return redirect('https://ssd.qbithabib.com', code=301)
@app.route('/robots.txt')
@app.route('/sitemap.xml')
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])