Display query/page generation time in debug mode

This commit is contained in:
RunasSudo 2023-01-02 16:59:52 +11:00
parent d8caedd2dd
commit dd96e7721b
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 30 additions and 5 deletions

View File

@ -40,6 +40,12 @@
</nav>
<div class="container">
{% block content %}{% endblock %}
{% if config['DEBUG'] %}
<footer class="border-top pt-4 mt-4">
<p class="text-muted">Queries executed in {{ dbtime() }} msec. Page generated in __EXECUTION_TIME__ msec.</p>
</footer>
{% endif %}
</div>
{% endblock %}

View File

@ -14,16 +14,19 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask import Flask
from flask import Flask, g
from flask_sqlalchemy.record_queries import get_recorded_queries
from .database import db
from .general_journal.models import GeneralJournalTransaction
from .statements.models import StatementLine, StatementLineTransaction
import time
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///drcr.db'
app.config['SQLALCHEMY_RECORD_QUERIES'] = True
app.config['SQLALCHEMY_RECORD_QUERIES'] = app.debug
db.init_app(app)
def all_transactions():
@ -42,6 +45,22 @@ from .statements import views
def initdb():
db.create_all()
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
if app.debug:
@app.before_request
def before_request():
g.start = time.time()
@app.after_request
def after_request(response):
diff = time.time() - g.start
if response.response and response.status_code == 200 and response.content_type.startswith('text/html'):
response.set_data(response.get_data().replace(b'__EXECUTION_TIME__', bytes(format(diff * 1000, '.1f'), 'utf-8')))
return response
@app.context_processor
def add_dbtime():
def dbtime():
queries = get_recorded_queries()
total_duration = sum(q.duration for q in queries)
return format(total_duration * 1000, '.1f')
return dict(dbtime=dbtime)