forked from corndeladmin/DevOps-Unit-10-Workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
89 lines (69 loc) · 2.77 KB
/
app.py
File metadata and controls
89 lines (69 loc) · 2.77 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
from azure.monitor.opentelemetry import configure_azure_monitor
import logging
logging.basicConfig(level=logging.INFO)
# Enable telemetry export to Application Insights
configure_azure_monitor(enable_live_metrics=True)
# Optional (later): configure_azure_monitor(enable_live_metrics=True
from flask import Flask, render_template, request
from datetime import datetime, timezone
from werkzeug.utils import redirect
from flask_config import Config
from data.database import initialise_database, add_order, clear_orders, count_orders, get_orders_to_display, get_queued_count, get_recently_placed_count, get_recently_processed_count
from scheduled_jobs import initialise_scheduled_jobs
from products import create_product_download
import requests
import logging
logging.basicConfig(level=logging.INFO)
app = Flask(__name__)
app.config.from_object(Config)
initialise_database(app)
initialise_scheduled_jobs(app)
@app.route("/")
def index():
orders = get_orders_to_display()
queue_count = get_queued_count()
recently_placed_count = get_recently_placed_count()
recently_processed_count = get_recently_processed_count()
scenarios = [
{ 'display': 'Add broken order', 'value': 'BrokenOrder' },
{ 'display': 'Monitoring Load', 'value': 'HighLoad' },
{ 'display': 'Queue Reliability', 'value': 'UnreliableProcessing' },
{ 'display': 'System Monitoring', 'value': 'VeryHighLoad' },
{ 'display': 'Reset to initial', 'value': 'Reset' }
]
return render_template(
"layout.html", orders=orders, queue_count=queue_count, recently_placed_count=recently_placed_count,
recently_processed_count=recently_processed_count, scenarios=scenarios
)
@app.route("/count")
def count():
return { 'count': count_orders() }
@app.route("/new", methods=["POST"])
def new_order():
product = request.json["product"]
customer = request.json["customer"]
date_placed = request.json["date_placed"] or datetime.now(tz=timezone.utc)
download = create_product_download(product)
try:
order = add_order(product, customer, date_placed, None, download)
except Exception as e:
return str(e)
return f"Added: {order}"
@app.route("/scenario", methods=["POST"])
def set_scenario():
scenario = request.form["scenario"]
if scenario == 'BrokenOrder':
product = 'Product from the future'
download = create_product_download(product)
add_order('Product from the future', 'Me', '3000-01-01T12:00:00Z', None, download)
return redirect('/')
if scenario == 'Reset':
clear_orders()
response = requests.post(
app.config["FINANCE_PACKAGE_URL"] + "/scenario",
json=scenario
)
response.raise_for_status()
return redirect('/')
if __name__ == "__main__":
app.run()