<commit_before>"""Main WSGI application."""

import json
import werkzeug.routing
import werkzeug.exceptions

from jacquard.users import get_settings
from jacquard.users.settings import branch_hash
from jacquard.experiments.constraints import meets_constraints
from jacquard.experiments.experiment import Experiment


def on_root(config):
    """
    The / endpoint.

    Returns a small number of URLs to the rest of the system. Probably not
    hugely useful for production, provided primarily to make the API more
    discoverable without probing into the source.

    If you're here probing into the source anyway, this goal has evidently
    failed. Please accept my apologies.
    """
    return {'user': '/users/<user>', 'experiments': '/experiment'}


def on_user(config, user):
    """
    The settings-for-user endpoint.

    This is the primary endpoint for normal interactive use. It just looks up
    the current settings for a given user ID and returns them.
    """
    settings = get_settings(user, config.storage, config.directory)
    return {**settings, 'user': user}


def on_experiments(config):
    """
    The experiments index endpoint.

    This is for use in reporting tooling: it provides a brief summary of all
    experiments known to the system, as well as whether or not they are
    active.
    """
    with config.storage.transaction() as store:
        active_experiments = store.get('active-experiments', ())
        experiments = []

        for key in store:
            if not key.startswith('experiments/'):
                continue
            definition = store[key]
            experiments.append(definition)

    return [
        {
            'id': experiment['id'],
            'url': '/experiment/%s' % experiment['id'],
            'state':
                'active'
                if experiment['id'] in active_experiments
                else 'inactive',
            'name': experiment.get('name', experiment['id']),
        }
        for experiment in experiments
    ]


def on_experiment(config, experiment):
    """
    The experiment lookup endpoint.

    This provides detailed data on a specific experiment, including lists
    of all user IDs in each of its branches.

    As a result this is quite expensive to hit.

    Provided for reporting tooling which runs statistics.
    """
    with config.storage.transaction() as store:
        experiment_config = Experiment.from_store(store, experiment)

    branch_ids = [branch['id'] for branch in experiment_config.branches]
    branches = {x: [] for x in branch_ids}

    for user_entry in config.directory.all_users():
        if not meets_constraints(experiment_config.constraints, user_entry):<commit_msg>Refactor experiments list to use Experiment object<commit_after>"""Main WSGI application."""

import json
import werkzeug.routing
import werkzeug.exceptions

from jacquard.users import get_settings
from jacquard.users.settings import branch_hash
from jacquard.experiments.constraints import meets_constraints
from jacquard.experiments.experiment import Experiment


def on_root(config):
    """
    The / endpoint.

    Returns a small number of URLs to the rest of the system. Probably not
    hugely useful for production, provided primarily to make the API more
    discoverable without probing into the source.

    If you're here probing into the source anyway, this goal has evidently
    failed. Please accept my apologies.
    """
    return {'user': '/users/<user>', 'experiments': '/experiment'}


def on_user(config, user):
    """
    The settings-for-user endpoint.

    This is the primary endpoint for normal interactive use. It just looks up
    the current settings for a given user ID and returns them.
    """
    settings = get_settings(user, config.storage, config.directory)
    return {**settings, 'user': user}


def on_experiments(config):
    """
    The experiments index endpoint.

    This is for use in reporting tooling: it provides a brief summary of all
    experiments known to the system, as well as whether or not they are
    active.
    """
    with config.storage.transaction() as store:
        active_experiments = store.get('active-experiments', ())
        experiments = []

        for key in store:
            if not key.startswith('experiments/'):
                continue
            experiment_id = key[len('experiments/'):]
            definition = Experiment.from_store(store, experiment_id)
            experiments.append(definition)

    return [
        {
            'id': experiment.id,
            'url': '/experiment/%s' % experiment.id,
            'state':
                'active'
                if experiment.id in active_experiments
                else 'inactive',
            'name': experiment.name,
        }
        for experiment in experiments
    ]


def on_experiment(config, experiment):
    """
    The experiment lookup endpoint.

    This provides detailed data on a specific experiment, including lists
    of all user IDs in each of its branches.

    As a result this is quite expensive to hit.

    Provided for reporting tooling which runs statistics.
    """
    with config.storage.transaction() as store:
        experiment_config = Experiment.from_store(store, experiment)

    branch_ids = [branch['id'] for branch in experiment_config.branches]
    branches = {x: [] for x in branch_ids}

    for user_entry in config.directory.all_users():
        if not meets_constraints(experiment_config.constraints, user_entry):
{'fraction_non_alphanumeric': 0.08242753623188406, 'fraction_numerical': 0.0, 'mean_word_length': 2.8102139406487234, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '322830', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1417, 'n_words': 531}