-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (74 loc) · 2.56 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
import csv
import os
import boto3
import time
from datetime import date
from dotenv import load_dotenv
from flask import Flask, render_template
from app.Register.register import register
from app.Enter.enter import enter
from app.extensions import mongo
REGISTER_IMAGES_FOLDER = 'app\\Register\\images'
ENTER_IMAGES_FOLDER = 'app\\Enter\\images'
METRICS_FOLDER = 'metrics'
app = Flask(__name__, static_folder = "metrics")
app.secret_key = "base1234"
app.config['REGISTER_IMAGES_FOLDER'] = REGISTER_IMAGES_FOLDER
app.config['ENTER_IMAGES_FOLDER'] = ENTER_IMAGES_FOLDER
app.config['METRICS_FOLDER'] = METRICS_FOLDER
app.config.from_object('settings')
app.register_blueprint(register, url_prefix="/register")
app.register_blueprint(enter, url_prefix="/enter")
mongo.init_app(app)
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route("/")
def home():
return render_template("home.html")
@app.route("/metrics")
def metrics():
# AWS Cloudwatch
metric_json = '{'\
'"view": "timeSeries",'\
'"stacked": true,'\
'"metrics": ['\
' [ "AWS/Rekognition", "SuccessfulRequestCount" ],'\
' [ ".", "ResponseTime", "Operation", "CompareFaces", { "accountId": "058981865517" } ]'\
'],'\
'"legend": {'\
' "position": "bottom"'\
'},'\
'"yAxis": {'\
' "left": {'\
' "showUnits": true'\
' }'\
'},'\
'"setPeriodToTimeRange": true,'\
'"width": 1263,'\
'"height": 250,'\
'"start": "-P1D",'\
'"end": "P0D"'\
'}'
load_dotenv()
client = boto3.client('cloudwatch',
aws_access_key_id = os.getenv("ACCESS_KEY_ID"),
aws_secret_access_key = os.getenv("SECRET_ACCESS_KEY"),
region_name='us-east-2')
response = client.get_metric_widget_image(
MetricWidget=metric_json
)
filename = "metrics_graph_"+str(time.time())+".jpg"
with open(os.path.join(app.config['METRICS_FOLDER'], filename), "wb") as fh:
fh.write(response['MetricWidgetImage'])
return render_template("metrics.html", timestamp=date.today().strftime("%B %d, %Y"), filename=filename)
if __name__ == "__main__":
app.run(debug=True)