<commit_before># Import PyGrid modules
from .. import main
from ..controller import processes
from ..cycles import cycle_manager
from ..models import model_manager
from ..workers import worker_manager
from ..processes import process_manager
from ..syft_assets import plans, protocols
from ..codes import RESPONSE_MSG, CYCLE, MSG_FIELD
from ..events.fl_events import report, cycle_request
from ..events.fl_events import authenticate as fl_events_auth
from ..exceptions import InvalidRequestKeyError, PyGridError

# General Imports
import io
import jwt
import json
import base64
import requests
from requests_toolbelt import MultipartEncoder
from flask import render_template, Response, request, current_app, send_file

# Dependencies used by req_join endpoint
# It's is a mockup endpoint and should be removed soon.
from scipy.stats import poisson
import numpy as np
from math import floor
import logging
from random import random


@main.route("/federated/cycle-request", methods=["POST"])
def worker_cycle_request():
    """" This endpoint is where the worker is attempting to join an active federated learning cycle. """
    response_body = {}
    status_code = None

    try:
        body = json.loads(request.data)
        response_body = cycle_request({MSG_FIELD.DATA: body}, None)
    except PyGridError or json.decoder.JSONDecodeError as e:
        status_code = 400  # Bad Request
        response_body[RESPONSE_MSG.ERROR] = str(e)
        response_body = json.dumps(response_body)
    except Exception as e:
        status_code = 500  # Internal Server Error
        response_body[RESPONSE_MSG.ERROR] = str(e)

    if isinstance(response_body, str):
        # Consider just data field as a response
        response_body = json.loads(response_body)[MSG_FIELD.DATA]

    response_body = json.dumps(response_body)
    return Response(response_body, status=status_code, mimetype="application/json")


@main.route("/federated/speed-test", methods=["GET", "POST"])
def connection_speed_test():
    """ Connection speed test. """
    response_body = {}
    status_code = None

    try:
        _worker_id = request.args.get("worker_id", None)
        _random = request.args.get("random", None)

        if not _worker_id or not _random:
            raise PyGridError

        # If GET method
        if request.method == "GET":
            # Download data sample (1MB)
            data_sample = b"x" * 67108864  # 64 Megabyte
            response = {"sample": data_sample}
            form = MultipartEncoder(response)
            return Response(form.to_string(), mimetype=form.content_type)
        elif request.method == "POST":  # Otherwise, it's POST method
            if request.file:
                status_code = 200  # Success
            else:
                raise PyGridError
    except PyGridError as e:
        status_code = 400  # Bad Request
        response_body[RESPONSE_MSG.ERROR] = str(e)
    except Exception as e:
        status_code = 500  # Internal Server Error
        response_body[RESPONSE_MSG.ERROR] = str(e)

    return Response(
        json.dumps(response_body), status_code=status_code, mimetype="application/json"
    )


@main.route("/federated/report", methods=["POST"])
def report_diff():
    """Allows reporting of (agg/non-agg) model diff after worker completes a cycle"""
    response_body = {}
    status_code = None

    try:
        body = json.loads(request.data)
        response_body = report({MSG_FIELD.DATA: body}, None)
    except PyGridError or json.decoder.JSONDecodeError as e:
        status_code = 400  # Bad Request
        response_body[RESPONSE_MSG.ERROR] = str(e)
        response_body = json.dumps(response_body)
    except Exception as e:<commit_msg>PyGrid speed test routes correction (#546)<commit_after># Import PyGrid modules
from .. import main
from ..controller import processes
from ..cycles import cycle_manager
from ..models import model_manager
from ..workers import worker_manager
from ..processes import process_manager
from ..syft_assets import plans, protocols
from ..codes import RESPONSE_MSG, CYCLE, MSG_FIELD
from ..events.fl_events import report, cycle_request
from ..events.fl_events import authenticate as fl_events_auth
from ..exceptions import InvalidRequestKeyError, PyGridError

# General Imports
import io
import jwt
import json
import base64
import requests
from requests_toolbelt import MultipartEncoder
from flask import render_template, Response, request, current_app, send_file

# Dependencies used by req_join endpoint
# It's is a mockup endpoint and should be removed soon.
from scipy.stats import poisson
import numpy as np
from math import floor
import logging
from random import random


@main.route("/federated/cycle-request", methods=["POST"])
def worker_cycle_request():
    """" This endpoint is where the worker is attempting to join an active federated learning cycle. """
    response_body = {}
    status_code = None

    try:
        body = json.loads(request.data)
        response_body = cycle_request({MSG_FIELD.DATA: body}, None)
    except PyGridError or json.decoder.JSONDecodeError as e:
        status_code = 400  # Bad Request
        response_body[RESPONSE_MSG.ERROR] = str(e)
        response_body = json.dumps(response_body)
    except Exception as e:
        status_code = 500  # Internal Server Error
        response_body[RESPONSE_MSG.ERROR] = str(e)

    if isinstance(response_body, str):
        # Consider just data field as a response
        response_body = json.loads(response_body)[MSG_FIELD.DATA]

    response_body = json.dumps(response_body)
    return Response(response_body, status=status_code, mimetype="application/json")


@main.route("/federated/speed-test", methods=["GET", "POST"])
def connection_speed_test():
    """ Connection speed test. """
    response_body = {}
    status_code = None

    try:
        _worker_id = request.args.get("worker_id", None)
        _random = request.args.get("random", None)

        if not _worker_id or not _random:
            raise PyGridError

        # If GET method
        if request.method == "GET":
            # Download data sample (1MB)
            data_sample = b"x" * 67108864  # 64 Megabyte
            response = {"sample": data_sample}
            form = MultipartEncoder(response)
            return Response(form.to_string(), mimetype=form.content_type)
        elif request.method == "POST":  # Otherwise, it's POST method
            if len(request.files) == 1:
                status_code = 200  # Success
            else:
                raise PyGridError
    except PyGridError as e:
        status_code = 400  # Bad Request
        response_body[RESPONSE_MSG.ERROR] = str(e)
    except Exception as e:
        status_code = 500  # Internal Server Error
        response_body[RESPONSE_MSG.ERROR] = str(e)

    return Response(
        json.dumps(response_body), status=status_code, mimetype="application/json"
    )


@main.route("/federated/report", methods=["POST"])
def report_diff():
    """Allows reporting of (agg/non-agg) model diff after worker completes a cycle"""
    response_body = {}
    status_code = None

    try:
        body = json.loads(request.data)
        response_body = report({MSG_FIELD.DATA: body}, None)
    except PyGridError or json.decoder.JSONDecodeError as e:
        status_code = 400  # Bad Request
        response_body[RESPONSE_MSG.ERROR] = str(e)
        response_body = json.dumps(response_body)
    except Exception as e:
{'fraction_non_alphanumeric': 0.08749832507034705, 'fraction_numerical': 0.00884362856760016, 'mean_word_length': 3.1838565022421523, 'pattern_counts': {'":': 6, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2946476', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2086, 'n_words': 637}