<commit_before>import logging
from flask import jsonify, request
import flask_login
from server import app
from server.util.request import api_error_handler, json_error_response, form_fields_required
from server.views.topics.apicache import topic_story_count
from server.auth import user_mediacloud_key, user_mediacloud_client
from server.views.topics.foci import FOCAL_TECHNIQUE_BOOLEAN_QUERY
from server.util.tags import cached_tags_in_tag_set, TAG_SETS_ID_MEDIA_TYPE

logger = logging.getLogger(__name__)

DEFAULT_SAMPLE_SIZE = 5000


@app.route('/api/topics/<topics_id>/focal-sets/media-type/preview/story-counts', methods=['GET'])
@flask_login.login_required
@api_error_handler
def media_type_story_counts(topics_id):
    tag_story_counts = []
    media_type_tags = cached_tags_in_tag_set(TAG_SETS_ID_MEDIA_TYPE)
    # grab the total stories
    total_stories = topic_story_count(user_mediacloud_key(), topics_id)['count']
    # make a count for each tag based on media_id
    for tag in media_type_tags:
            query_clause = "tags_id_media:{}".format(tag['tags_id'])
        if len(tag['media_ids']) > 0:
            tagged_story_count = topic_story_count(user_mediacloud_key(), topics_id, q=query_clause)['count']
        else:
            tagged_story_count = 0
        tag_story_counts.append({
            'label': tag['label'],
            'tags_id': tag['tags_id'],
            'count': tagged_story_count,
            'pct': float(tagged_story_count)/float(total_stories)
        })

    return jsonify({'story_counts': tag_story_counts})


@app.route('/api/topics/<topics_id>/focal-sets/media-type/preview/coverage', methods=['GET'])
@flask_login.login_required
@api_error_handler
def media_type_coverage(topics_id):
    media_type_tags = cached_media_tags(TAG_SETS_ID_MEDIA_TYPE)
    # grab the total stories
    total_stories = topic_story_count(user_mediacloud_key(), topics_id)['count']
    # count the stories in any media in tagged as media_type
    tag_media_ids = " ".join(str(tag['tags_id']) for tag in media_type_tags)
    query_clause = "tags_id_media:({})".format(tag_media_ids)
    tagged_story_count = topic_story_count(user_mediacloud_key(), topics_id, q=query_clause)['count']
    return jsonify({'counts': {'count': tagged_story_count, 'total': total_stories}})


@app.route('/api/topics/<topics_id>/focal-sets/media-type/create', methods=['POST'])
@form_fields_required('focalSetName', 'focalSetDescription')
@flask_login.login_required<commit_msg>Remove bug in story_count logic (#807)<commit_after>import logging
from flask import jsonify, request
import flask_login
from server import app
from server.util.request import api_error_handler, json_error_response, form_fields_required
from server.views.topics.apicache import topic_story_count
from server.auth import user_mediacloud_key, user_mediacloud_client
from server.views.topics.foci import FOCAL_TECHNIQUE_BOOLEAN_QUERY
from server.util.tags import cached_tags_in_tag_set, TAG_SETS_ID_MEDIA_TYPE

logger = logging.getLogger(__name__)

DEFAULT_SAMPLE_SIZE = 5000


@app.route('/api/topics/<topics_id>/focal-sets/media-type/preview/story-counts', methods=['GET'])
@flask_login.login_required
@api_error_handler
def media_type_story_counts(topics_id):
    tag_story_counts = []
    media_type_tags = cached_tags_in_tag_set(TAG_SETS_ID_MEDIA_TYPE)
    # grab the total stories
    total_stories = topic_story_count(user_mediacloud_key(), topics_id)['count']
    # make a count for each tag based on media_id
    for tag in media_type_tags:
        query_clause = "tags_id_media:{}".format(tag['tags_id'])
        tagged_story_count = topic_story_count(user_mediacloud_key(), topics_id, q=query_clause)['count']
        tag_story_counts.append({
            'label': tag['label'],
            'tags_id': tag['tags_id'],
            'count': tagged_story_count,
            'pct': float(tagged_story_count)/float(total_stories)
        })

    return jsonify({'story_counts': tag_story_counts})


@app.route('/api/topics/<topics_id>/focal-sets/media-type/preview/coverage', methods=['GET'])
@flask_login.login_required
@api_error_handler
def media_type_coverage(topics_id):
    media_type_tags = cached_tags_in_tag_set(TAG_SETS_ID_MEDIA_TYPE)
    # grab the total stories
    total_stories = topic_story_count(user_mediacloud_key(), topics_id)['count']
    # count the stories in any media in tagged as media_type
    tags_ids = " ".join(str(tag['tags_id']) for tag in media_type_tags)
    query_clause = "tags_id_media:({})".format(tags_ids)
    tagged_story_count = topic_story_count(user_mediacloud_key(), topics_id, q=query_clause)['count']
    return jsonify({'counts': {'count': tagged_story_count, 'total': total_stories}})


@app.route('/api/topics/<topics_id>/focal-sets/media-type/create', methods=['POST'])
@form_fields_required('focalSetName', 'focalSetDescription')
@flask_login.login_required
{'fraction_non_alphanumeric': 0.11088421482540331, 'fraction_numerical': 0.0026546865427812946, 'mean_word_length': 5.478835978835979, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '7304471', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1661, 'n_words': 282}