<commit_before># -*- coding: utf8 -*-
"""API module for libcrowds-analyst."""

import json
from rq import Queue
from redis import Redis
from flask import Blueprint, request, current_app, abort, make_response
from libcrowds_analyst import analysis


BP = Blueprint('api', __name__)
QUEUE = Queue('libcrowds_analyst', connection=Redis())
MINUTE = 60


def process_payload():
    """Check that a payload is valid and complete it."""
    payload = request.json or {}
    if request.method != 'POST':
        abort(400)

    if payload.get('event') != 'task_completed':
        abort(400)

    if not request.args.get('api_key'):
        abort(400)

    payload['api_key'] = request.args.get('api_key')
    payload['endpoint'] = current_app.config['ENDPOINT']
    payload['doi'] = current_app.config['DOI']
    payload['path'] = request.path
    return payload


def analyse(func):
    """Analyse a webhook."""
    payload = process_payload()
    QUEUE.enqueue_call(func=func, kwargs=payload, timeout=10*MINUTE)
    return ok_response()


def analyse_all(func):
    """Analyse all results for a project."""
    payload = process_payload()
    payload['project_short_name'] = request.args.get('project_short_name')
    QUEUE.enqueue_call(func=func, kwargs=payload, timeout=30*MINUTE)
    return ok_response()


def ok_response():
    """Return a basic HTTP 200 response."""
    response = make_response(
        json.dumps({
            "message": "OK",
            "status": 200,
        })
    )<commit_msg>Only look for task_completed when not analysing all<commit_after># -*- coding: utf8 -*-
"""API module for libcrowds-analyst."""

import json
from rq import Queue
from redis import Redis
from flask import Blueprint, request, current_app, abort, make_response
from libcrowds_analyst import analysis


BP = Blueprint('api', __name__)
QUEUE = Queue('libcrowds_analyst', connection=Redis())
MINUTE = 60


def process_payload():
    """Check that a payload is valid and complete it."""
    payload = request.json or {}
    if request.method != 'POST':
        abort(400)

    if not request.args.get('api_key'):
        abort(400)

    payload['api_key'] = request.args.get('api_key')
    payload['endpoint'] = current_app.config['ENDPOINT']
    payload['doi'] = current_app.config['DOI']
    payload['path'] = request.path
    return payload


def analyse(func):
    """Analyse a webhook."""
    payload = process_payload()
    if payload.get('event') != 'task_completed':
        abort(400)

    QUEUE.enqueue_call(func=func, kwargs=payload, timeout=10*MINUTE)
    return ok_response()


def analyse_all(func):
    """Analyse all results for a project."""
    payload = process_payload()
    payload['project_short_name'] = request.args.get('project_short_name')
    QUEUE.enqueue_call(func=func, kwargs=payload, timeout=30*MINUTE)
    return ok_response()


def ok_response():
    """Return a basic HTTP 200 response."""
    response = make_response(
        json.dumps({
            "message": "OK",
            "status": 200,
        })
    )
{'fraction_non_alphanumeric': 0.13534822601839686, 'fraction_numerical': 0.01445466491458607, 'mean_word_length': 3.6136363636363638, 'pattern_counts': {'":': 4, '<': 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': '779489', 'n_tokens_mistral': 984, 'n_tokens_neox': 923, 'n_words': 241}