<commit_before>from collections import defaultdict

from bson.code import Code
from bson.objectid import ObjectId

from voteit.core import votes, issues

REDUCE = Code("""
function(obj, prev) {
    if (!prev.votes.hasOwnProperty(obj.option)) {
        prev.votes[obj.option] = 0;
    }
    prev.votes[obj.option] += obj.weight;
};
""")


def get_options(filters):
    options = votes.find(filters).distinct('option')
    return options


def get_weight(mdata, option):
    weights = mdata.get('weights', {})
    weight = weights.get(option)
    if weight is None:
        if option == 'yes' and weights['no'] is not None:
            weight = -1 * weights['no']
        if option == 'no' and weights['yes'] is not None:
            weight = -1 * weights['yes']
    return weight or 0


def generate_stances(blocs=[], issue_ids=[], filters={}):
    keys = set(blocs)
    _filt = {}
    if len(issue_ids):
        _filt = {'_id': {'$in': [ObjectId(i) for i in issue_ids]}}
    issue_objs = list(issues.find(_filt))

    motion_issues = defaultdict(list)
    for issue in issue_objs:
        for motion in issue.get('motions', []):
            mid = motion.get('motion_id')
            motion_issues[mid].append((issue, motion))

    spec = dict(filters)
    spec['motion.motion_id'] = {'$in': motion_issues.keys()}
    keys.add('motion.motion_id')

    options = get_options(spec)

    data = {}
    # Aggregate on the server.
    for cell in votes.group(keys, spec, {"votes": {}}, REDUCE):

        # Aggregate by issue locally.
        for issue, mdata in motion_issues.get(cell.get('motion.motion_id')):

            # Output cell key.
            key = repr([issue.get('_id')] + [cell.get(k) for k in keys])
            if not key in data:
                data[key] = {
                    'issue': {'name': issue.get('name'), 'id': issue.get('_id')},
                    'stance': defaultdict(int),
                    'bloc': {},
                    'num_motions': 0
                }
            for option in options:
                v = cell.get('votes').get(option, 0) * get_weight(mdata, option)
                data[key]['stance'][option] += v
            
            for k, v in cell.items():<commit_msg>Show full issue. <commit_after>from collections import defaultdict

from bson.code import Code
from bson.objectid import ObjectId

from voteit.core import votes, issues

REDUCE = Code("""
function(obj, prev) {
    if (!prev.votes.hasOwnProperty(obj.option)) {
        prev.votes[obj.option] = 0;
    }
    prev.votes[obj.option] += obj.weight;
};
""")


def get_options(filters):
    options = votes.find(filters).distinct('option')
    return options


def get_weight(mdata, option):
    weights = mdata.get('weights', {})
    weight = weights.get(option)
    if weight is None:
        if option == 'yes' and weights['no'] is not None:
            weight = -1 * weights['no']
        if option == 'no' and weights['yes'] is not None:
            weight = -1 * weights['yes']
    return weight or 0


def generate_stances(blocs=[], issue_ids=[], filters={}):
    keys = set(blocs)
    _filt = {}
    if len(issue_ids):
        _filt = {'_id': {'$in': [ObjectId(i) for i in issue_ids]}}
    issue_objs = list(issues.find(_filt))

    motion_issues = defaultdict(list)
    for issue in issue_objs:
        for motion in issue.get('motions', []):
            mid = motion.get('motion_id')
            motion_issues[mid].append((issue, motion))

    spec = dict(filters)
    spec['motion.motion_id'] = {'$in': motion_issues.keys()}
    keys.add('motion.motion_id')

    options = get_options(spec)

    data = {}
    # Aggregate on the server.
    for cell in votes.group(keys, spec, {"votes": {}}, REDUCE):

        # Aggregate by issue locally.
        for issue, mdata in motion_issues.get(cell.get('motion.motion_id')):

            # Output cell key.
            key = repr([issue.get('_id')] + [cell.get(k) for k in keys])
            if not key in data:
                data[key] = {
                    'issue': issue,
                    'stance': defaultdict(int),
                    'bloc': {},
                    'num_motions': 0
                }
            for option in options:
                v = cell.get('votes').get(option, 0) * get_weight(mdata, option)
                data[key]['stance'][option] += v
            
            for k, v in cell.items():
{'fraction_non_alphanumeric': 0.14669703872437356, 'fraction_numerical': 0.002733485193621868, 'mean_word_length': 2.228676470588235, 'pattern_counts': {'":': 2, '<': 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': '4073730', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1361, 'n_words': 347}