<commit_before>from flask import jsonify, request

from . import api
from ..models import db
from ..models.log import Event, Log
from ..schemas.log import event_schema, events_schema
from ..utils import create_hash


@api.route('/logs/', methods=['GET'])
def get_logs():
    all_events = Event.select()
    result = events_schema.dump(all_events)
    return jsonify(result.data)


@api.route('/logs/', methods=['POST'])<commit_msg>api: implement log time interval summary response<commit_after>from flask import jsonify, request

from . import api
from ..models import db
from ..models.log import Event, Log
from ..schemas.log import event_schema, events_schema
from ..utils import create_hash


@api.route('/logs/', methods=['GET'])
def get_logs():
    fmt = request.args.get('format')
    ival = request.args.get('interval')

    data = {}
    if fmt == 'summary' and ival:
        # This query would be convoluted to write with the ORM, and it uses
        # SQLite-specific functions anyway, hence the raw SQL.
        res = db.execute_sql("""
            -- Aggregate log events into time intervals
            SELECT
                datetime((strftime('%s', date) / ?) * ?, 'unixepoch') interval,
                count(*) cnt
            FROM event
            GROUP BY interval
            ORDER BY interval
        """, (ival, ival)).fetchall()
        data['result'] = dict(res)
    else:
        all_events = Event.select()
        data = events_schema.dump(all_events).data

    return jsonify(data)


@api.route('/logs/', methods=['POST'])
{'fraction_non_alphanumeric': 0.12572533849129594, 'fraction_numerical': 0.0, 'mean_word_length': 2.8132678132678133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5479079', 'n_tokens_mistral': 456, 'n_tokens_neox': 440, 'n_words': 139}