示例#1
0
def log_request(response):
    lang_code = getattr(flask.request, 'lang_code', None)
    id = flask.request.args.get('id')
    cat = flask.request.args.get('cat')
    url = flask.request.url
    prefetch = (flask.request.headers.get('purpose') == 'prefetch' or
                flask.request.headers.get('X-Moz') == 'prefetch')
    user_agent = flask.request.headers.get('User-Agent', 'NULL')
    referrer = flask.request.referrer
    status_code = response.status_code

    with get_stats_db() as cursor, chdb.ignore_warnings():
        cursor.execute('INSERT INTO requests VALUES '
            '(NOW(), %s, %s, %s, %s, %s, %s, %s, %s)',
            (lang_code, id, cat, url, prefetch, user_agent,
             status_code, referrer))
    return response
示例#2
0
 def insert_intersection(cursor):
     with chdb.ignore_warnings():
         # Add the intersection if needed then set its expiration in a
         # separate statement, because we want to bump the expiration of the
         # intersection if it already exists. We could use REPLACE instead of
         # INSERT IGNORE/UPDATE but that's a MySQL extension.
         cursor.execute('''
             INSERT IGNORE INTO intersections VALUES (%s, 0)
         ''', (inter_id,))
         cursor.execute('''
             UPDATE intersections
             SET expiration = DATE_ADD(NOW(), INTERVAL %s DAY)
             WHERE id = %s
         ''', (expiration_days, inter_id))
         cursor.executemany('''
             INSERT IGNORE INTO articles_intersections VALUES (%s, %s)
         ''', [(page_id, inter_id) for page_id in page_ids])
         populate_snippets_links(cursor, intersection_ids = [inter_id])
     return inter_id, page_ids
示例#3
0
def log_request(response):
    user_agent = flask.request.headers.get('User-Agent', None)
    referrer = flask.request.referrer or None
    if is_spam(user_agent, referrer):
        return response
    lang_code = getattr(flask.g, '_lang_code', None)
    id = flask.request.args.get('id')
    cat = flask.request.args.get('cat')
    inter_id = flask.request.args.get('custom')
    url = flask.request.url
    prefetch = (flask.request.headers.get('purpose') == 'prefetch'
                or flask.request.headers.get('X-Moz') == 'prefetch')
    status_code = response.status_code

    with get_stats_db().cursor() as cursor, chdb.ignore_warnings():
        cursor.execute(
            'INSERT INTO requests VALUES '
            '(NOW(), %s, %s, %s, %s, %s, %s, %s, %s)',
            (lang_code, id, cat, url, prefetch, status_code, referrer,
             inter_id))
    return response
def expire_stats(cfg):
    stats_db = chdb.init_stats_db()
    with chdb.init_stats_db().cursor() as cursor, chdb.ignore_warnings():
        cursor.execute('DELETE FROM requests WHERE DATEDIFF(NOW(), ts) > %s',
                (cfg.stats_max_age_days,))
def expire_stats(cfg):
    stats_db = chdb.init_stats_db()
    with chdb.init_stats_db() as cursor, chdb.ignore_warnings():
        cursor.execute('DELETE FROM requests WHERE DATEDIFF(NOW(), ts) > %s',
                (cfg.stats_max_age_days,))