Python flask_sqlalchemy.get_debug_queries() Examples

The following are code examples for showing how to use flask_sqlalchemy.get_debug_queries(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: circleci-demo-python-flask   Author: CircleCI-Public   File: views.py    (license) View Source Project 5 votes vote down vote up
def after_request(response):
    for query in get_debug_queries():
        if query.duration >= current_app.config['CIRCULATE_SLOW_DB_QUERY_TIME']:
            current_app.logger.warning(
                'Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n'
                % (query.statement, query.parameters, query.duration,
                   query.context))
    return response 
Example 2
Project: scrobbler   Author: hatarist   File: manage.py    (license) View Source Project 5 votes vote down vote up
def make_shell_context():
    from pprint import pprint

    from flask_sqlalchemy import get_debug_queries
    from sqlalchemy import desc, func

    from scrobbler.models import Album, Artist, NowPlaying, Scrobble, Session, Token, User

    return dict(
        app=app, db=db, pprint=pprint, gq=get_debug_queries, func=func, desc=desc,
        User=User, Session=Session, Token=Token,
        Scrobble=Scrobble, NowPlaying=NowPlaying,
        Artist=Artist, Album=Album,
    ) 
Example 3
Project: web_develop   Author: dongweiming   File: logger_slow_query.py    (license) View Source Project 5 votes vote down vote up
def after_request(response):
    for query in get_debug_queries():
        if query.duration >= app.config['DATABASE_QUERY_TIMEOUT']:
            app.logger.warn(
                ('\nContext:{}\nSLOW QUERY: {}\nParameters: {}\n'
                 'Duration: {}\n').format(query.context, query.statement,
                                          query.parameters, query.duration))
    return response 
Example 4
Project: pyetje   Author: rorlika   File: views.py    (license) View Source Project 5 votes vote down vote up
def after_request(response):
    for query in get_debug_queries():
        if query.duration >= current_app.config['FLASKY_SLOW_DB_QUERY_TIME']:
            current_app.logger.warning(
                'Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n'
                % (query.statement, query.parameters, query.duration,
                   query.context))
    return response 
Example 5
Project: smart-iiot   Author: quanpower   File: views.py    (license) View Source Project 5 votes vote down vote up
def after_request(response):
    for query in get_debug_queries():
        if query.duration >= current_app.config['FLASKY_SLOW_DB_QUERY_TIME']:
            current_app.logger.warning(
                'Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n'
                % (query.statement, query.parameters, query.duration,
                   query.context))
    return response 
Example 6
Project: webapp   Author: superchilli   File: views.py    (license) View Source Project 5 votes vote down vote up
def after_request(response):
    for query in get_debug_queries():
        if query.duration >= current_app.config['SLOW_DB_QUERY_TIME']:
            current_app.logger.warning(
                'Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n'
                % (query.statement, query.parameters, query.duration,
                   query.context))
    return response 
Example 7
Project: Graduation_design   Author: mr-betterman   File: views.py    (license) View Source Project 5 votes vote down vote up
def after_request(response):
    for query in get_debug_queries():
        if query.duration >= current_app.config['FLASKY_SLOW_DB_QUERY_TIME']:
            current_app.logger.warning(
                'Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n'
                % (query.statement, query.parameters, query.duration,
                   query.context))
    return response 
Example 8
Project: flask_api   Author: nullcc   File: app.py    (license) View Source Project 5 votes vote down vote up
def configure_request_filter_handlers(app):
    """
    ???????
    :param app:  app??
    :return:
    """

    @app.before_request
    def before_request():
        print('before request handler')
        update_locale(app)
        login_manager.reload_user()
        # your before request code...

    @app.after_request
    def after_request(response):
        print('after request handler')
        # your after request code...
        # ?????????
        for query in get_debug_queries():
            if query.duration > app.config['DATABASE_QUERY_TIMEOUT']:
                app.logger.warn(
                    ('Context: {}\nSLOW QUERY: {}\nParameters: {}\n'
                     'Duration: {}\n').format(query.context, query.statement, query.parameters, query.duration)
                )

        return response

    @app.teardown_request
    def teardown_request(response):
        db.session.close()  # ??db session
        # your teardown request code...
        return response 
Example 9
Project: radar   Author: renalreg   File: debug.py    (license) View Source Project 5 votes vote down vote up
def debug_teardown_request(exception):
    """Log slow responses and queries."""

    url = request.url

    response_time = time.time() - g.start

    if response_time >= 0.1:
        msg = '{url} - slow response ({response_time:.2f} seconds)'
        print(colored(msg.format(url=url, response_time=response_time), 'yellow'))

    total_query_time = 0

    queries = defaultdict(lambda: [0, 0])

    for q in get_debug_queries():
        queries[q.statement][0] += 1  # number of time query run
        queries[q.statement][1] += q.duration  # total time for query
        total_query_time += q.duration  # total time for all queries

    # Queries taking longer than 0.1 seconds (slowest first)
    queries = [(k, v[0], v[1]) for k, v in queries.items()]
    queries = [x for x in queries if x[2] >= 0.1]
    queries.sort(key=lambda x: x[1], reverse=True)

    for query, times_run, query_time in queries:
        if times_run > 1:
            message = '{url} - slow query ({query_time:.2f} seconds over {times_run} calls) - {query}'
        else:
            message = '{url} - slow query ({query_time:.2f} seconds) - {query}'

        print(colored(message.format(url=url, query=query, times_run=times_run, query_time=query_time), 'yellow')) 
Example 10
Project: blog   Author: hukaixuan   File: views.py    (license) View Source Project 5 votes vote down vote up
def after_request(response):
	for query in get_debug_queries():
		if query.duration >= current_app.config['FLASKY2_SLOW_DB_QUERY_TIME']:
			current_app.logger.warning(
				'Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n'
				%(query.statement, query.parameters, query.duration, query.context)
				)
	return response 
Example 11
Project: Faiwong-s-blog   Author: Fai-Wong   File: views.py    (license) View Source Project 5 votes vote down vote up
def after_request(response):
    for query in get_debug_queries():
        if query.duration >= current_app.config['FLASKY_SLOW_DB_QUERY_TIME']:
            current_app.logger.warning(
                'Slow query: %s\nParameters: %s\nDuration: %s\nContext: %s\n'
                %(query.statement, query.parameters, query.duration, 
                    query.context))
    return response