<commit_before># -*- coding: utf-8 -*-
"""
    latexonhttp.api.caches
    ~~~~~~~~~~~~~~~~~~~~~
    Caches endpoints, used to get a view on caches usage
    and to allow smart-client optimization (server-cache use).

    :copyright: (c) 2019 <NAME>.
    :license: AGPL, see LICENSE for more details.
"""
import logging
from flask import Blueprint, request, jsonify, Response
from latexonhttp.caching.resources import get_cache_metadata_snapshot

from pprint import pformat

logger = logging.getLogger(__name__)

caches_app = Blueprint("caches", __name__)

# TODO Make this route private to the admin:
# this allows to get all cache file hashes
# and to actually get the file content,
# by dumping them in Latex output.
# Only provide the checker endpoint to the public.
@caches_app.route("/resources", methods=["GET"])
def resources_metadata():
    return (jsonify(get_cache_metadata_snapshot()), 200)


# TODO Check for a list of resource hashes to know if there are cached.
# POST:/caches/resources/check_cached<commit_msg>Do not let the cache entries hashes be public<commit_after># -*- coding: utf-8 -*-
"""
    latexonhttp.api.caches
    ~~~~~~~~~~~~~~~~~~~~~
    Caches endpoints, used to get a view on caches usage
    and to allow smart-client optimization (server-cache use).

    :copyright: (c) 2019 <NAME>.
    :license: AGPL, see LICENSE for more details.
"""
import logging
from flask import Blueprint, request, jsonify, Response
from latexonhttp.caching.resources import get_cache_metadata_snapshot

from pprint import pformat

logger = logging.getLogger(__name__)

caches_app = Blueprint("caches", __name__)


@caches_app.route("/resources", methods=["GET"])
def resources_metadata():
    return (jsonify(map_cache_metadata_for_public(get_cache_metadata_snapshot())), 200)


# TODO Check for a list of resource hashes to know if there are cached.
# POST:/caches/resources/check_cached

def map_cache_metadata_for_public(cache_metadata):
    # Filter out cache entries (or at least their hashes),
    # because they would potentially allow user to get
    # any cache file content (by dumping them in Latex output).
    return {
        **cache_metadata,
        "cached_resources": [
            {
                "size": cached_resource["size"]
            } for cached_resource in cache_metadata["cached_resources"].values()
        ]
    }
{'fraction_non_alphanumeric': 0.10814249363867684, 'fraction_numerical': 0.006785411365564037, 'mean_word_length': 4.0405982905982905, 'pattern_counts': {'":': 2, '<': 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': '1971781', 'n_tokens_mistral': 689, 'n_tokens_neox': 656, 'n_words': 234}