-
Notifications
You must be signed in to change notification settings - Fork 46
/
app.py
executable file
·146 lines (105 loc) · 4.38 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
from __future__ import absolute_import
import os
from time import time, strftime
from flask import Flask, jsonify, request, render_template, Response
from prometheus_client import multiprocess, generate_latest, CollectorRegistry, CONTENT_TYPE_LATEST, Counter, Histogram
from api_common import get_device_builds, get_oems, get_device_data
from changelog.gerrit import GerritJSONProvider
from custom_exceptions import DeviceNotFoundException, UpstreamApiException
from config import Config
from api_v1 import api as api_v1
from api_v2 import api as api_v2
import extensions
app = Flask(__name__)
app.config.from_object('config.FlaskConfig')
app.register_blueprint(api_v1, url_prefix='/api/v1')
app.register_blueprint(api_v2, url_prefix='/api/v2')
app.json = GerritJSONProvider(app)
app.url_map.strict_slashes = False
extensions.setup(app)
##########################
# Jinja2 globals
##########################
def version():
return os.environ.get('VERSION', 'dev')[:6]
app.jinja_env.globals.update(version=version)
##########################
# Metrics
##########################
REQUEST_LATENCY = Histogram('flask_request_latency_seconds', 'Request Latency', ['method', 'endpoint'])
REQUEST_COUNT = Counter('flask_request_count', 'Request Count', ['method', 'endpoint', 'status'])
@app.before_request
def start_timer():
request.stats_start = time()
@app.after_request
def stop_timer(response):
delta = time() - request.stats_start
REQUEST_LATENCY.labels(request.method, request.endpoint).observe(delta)
REQUEST_COUNT.labels(request.method, request.endpoint, response.status_code).inc()
return response
@app.route('/metrics')
def metrics():
registry = CollectorRegistry()
multiprocess.MultiProcessCollector(registry)
return Response(generate_latest(registry), mimetype=CONTENT_TYPE_LATEST)
##########################
# Exception Handling
##########################
@app.errorhandler(DeviceNotFoundException)
def handle_unknown_device(error):
if request.path.startswith('/api/'):
return jsonify({'response': []})
oems = get_oems()
return render_template('error.html', header='Whoops - this page doesn\'t exist', message=error.message,
oems=oems), error.status_code
@app.errorhandler(UpstreamApiException)
def handle_upstream_exception(error):
if request.path.startswith('/api/'):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
return render_template('error.html', header='Something went wrong', message=error.message,
oems={}), error.status_code
##########################
# Web Views
##########################
@app.context_processor
def inject_year():
return dict(year=strftime('%Y'))
@app.route('/')
@extensions.cache.cached()
def show_index():
oems = get_oems()
return render_template('changes.html', oems=oems,
before=0, changelog=True)
@app.route('/<string:device>')
@extensions.cache.cached()
def web_device(device):
oems = get_oems()
device_data = get_device_data(device)
roms = get_device_builds(device)
for rom in roms:
if device_data.get('lineage_recovery', True):
# Pick recovery.img if exists, otherwise boot.img or None
if recovery := next((x for x in rom['files'] if x['filename'] == 'recovery.img'), None) or \
next((x for x in rom['files'] if x['filename'] == 'boot.img'), None):
rom['recovery'] = recovery
rom['filename'] = rom['files'][0]['filename']
rom['filepath'] = rom['files'][0]['filepath']
rom['size'] = rom['files'][0]['size']
has_recovery = any([True for rom in roms if 'recovery' in rom])
return render_template('device.html', oems=oems, active_device_data=device_data,
roms=roms, has_recovery=has_recovery,
wiki_info=Config.WIKI_INFO_URL, wiki_install=Config.WIKI_INSTALL_URL,
download_base_url=Config.DOWNLOAD_BASE_URL)
@app.route('/<string:device>/changes')
@extensions.cache.cached()
def show_changelog(device):
oems = get_oems()
device_data = get_device_data(device)
return render_template('changes.html', oems=oems, active_device_data=device_data,
before=0, changelog=True)
@app.route('/favicon.ico')
def favicon():
return ''