Ejemplo n.º 1
0
def vote_history(group_id, member, nick, msg):
    '''
    View vote history
    '''
    tip = ''
    vote_tasks = wechatdb.get_vote_tasks({'group_id': group_id, 'group_users': member, 'status': Status.FINISHED})
    if len(vote_tasks) > 0:
        task_ids = [task['id'] for task in vote_tasks]
        vote_records = wechatdb.get_vote_records({'vote_id': {'$in': task_ids}, 'voter': member, 'valid': True})
        record_dict = {}
        for record in vote_records:
            record_dict[record['vote_id']] = record
        strong = 0
        weak = 0
        not_vote = 0
        diff_strong = 0
        diff_weak = 0
        for task in vote_tasks:
            vote_id = task['id']
            if vote_id in record_dict:
                vote_values = record_dict[vote_id]['vote']
                item_results = task['item_results']
                _LOGGER.info(vote_values)
                _LOGGER.info(item_results)
                for vote, item_result in zip(vote_values, item_results):
                    if vote == STRONG:
                        strong += 1
                        if not item_result['passed']:
                            diff_strong += 1
                    else:
                        weak += 1
                        if item_result['passed']:
                            diff_weak += 1
            else:
                not_vote += 1
        _LOGGER.info(diff_weak)
        sum_votes = strong + weak
        strong_rate = 0
        weak_rate = 0
        different_rate = 0
        diff_strong_rate = 0
        diff_weak_rate = 0
        if sum_votes > 0:
            strong_rate = strong * 100.0 / sum_votes
            weak_rate = weak * 100.0 / sum_votes
            different_rate = (diff_strong + diff_weak) * 100.0 / sum_votes
            diff_strong_rate = diff_strong * 100.0 / sum_votes
            diff_weak_rate = diff_weak * 100.0 / sum_votes
        tip = u'查询%s的结果\n顶次数: %d, 占比: %.2f%%\n踩次数: %d, 占比: %.2f%%\n跟结果不同次数: %d, 占比: %.2f%%\n其中顶的次数: %d, 占比: %.2f%%\n踩的次数: %d, 占比: %.2f%%\n未投票次数: %d' % \
                (nick, strong, strong_rate, weak, weak_rate, diff_strong + diff_weak, different_rate, diff_strong, diff_strong_rate, \
                diff_weak, diff_weak_rate, not_vote)
    else:
        tip = u'暂无投票历史'
    return {'tip': tip}
Ejemplo n.º 2
0
def vote_history(group_id, member, nick, msg):
    """
    View vote history
    """
    tip = ""
    vote_tasks = wechatdb.get_vote_tasks({"group_id": group_id, "group_users": member, "status": Status.FINISHED})
    if len(vote_tasks) > 0:
        task_ids = [task["id"] for task in vote_tasks]
        vote_records = wechatdb.get_vote_records({"vote_id": {"$in": task_ids}, "voter": member, "valid": True})
        record_dict = {}
        for record in vote_records:
            record_dict[record["vote_id"]] = record
        strong = 0
        weak = 0
        not_vote = 0
        diff_strong = 0
        diff_weak = 0
        for task in vote_tasks:
            vote_id = task["id"]
            if vote_id in record_dict:
                vote_values = record_dict[vote_id]["vote"]
                item_results = task["item_results"]
                _LOGGER.info(vote_values)
                _LOGGER.info(item_results)
                for vote, item_result in zip(vote_values, item_results):
                    if vote == STRONG:
                        strong += 1
                        if not item_result["passed"]:
                            diff_strong += 1
                    else:
                        weak += 1
                        if item_result["passed"]:
                            diff_weak += 1
            else:
                not_vote += 1
        _LOGGER.info(diff_weak)
        sum_votes = strong + weak
        strong_rate = 0
        weak_rate = 0
        different_rate = 0
        diff_strong_rate = 0
        diff_weak_rate = 0
        if sum_votes > 0:
            strong_rate = strong * 100.0 / sum_votes
            weak_rate = weak * 100.0 / sum_votes
            different_rate = (diff_strong + diff_weak) * 100.0 / sum_votes
            diff_strong_rate = diff_strong * 100.0 / sum_votes
            diff_weak_rate = diff_weak * 100.0 / sum_votes
        tip = (
            u"查询%s的结果\n顶次数: %d, 占比: %.2f%%\n踩次数: %d, 占比: %.2f%%\n跟结果不同次数: %d, 占比: %.2f%%\n其中顶的次数: %d, 占比: %.2f%%\n踩的次数: %d, 占比: %.2f%%\n未投票次数: %d"
            % (
                nick,
                strong,
                strong_rate,
                weak,
                weak_rate,
                diff_strong + diff_weak,
                different_rate,
                diff_strong,
                diff_strong_rate,
                diff_weak,
                diff_weak_rate,
                not_vote,
            )
        )
    else:
        tip = u"暂无投票历史"
    return {"tip": tip}
Ejemplo n.º 3
0
def vote_finish(group_id, sender, nick, msg, group_users):
    """
    vote finished
    """
    tip = ""
    group_users = simplejson.loads(group_users)
    user_nick = {}
    for user in group_users:
        if user["name"] != ROBOT and user["id"] not in ROBOT_IDS:
            user_nick[user["id"]] = user["name"]
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        vote_task = wechatdb.get_vote_task({"id": vote_id})
        items = vote_task["items"]
        records = wechatdb.get_vote_records({"vote_id": vote_id})
        voter_set = set([record["voter"] for record in records])
        not_vote_users = list(set(user_nick.keys()) - voter_set)
        not_vote_nicks = [user_nick[user] for user in not_vote_users]
        item_results = []
        valid_records = [record for record in records if record["valid"]]
        vote_number = len(records)
        vote_valid_number = len(valid_records)
        vote_invalid_number = vote_number - vote_valid_number
        tip = u"总票数: %d\n有效票数: %d\n无效票数: %d" % (vote_number, vote_valid_number, vote_invalid_number)
        if vote_task["voted"]:
            tip = u"%s\n%s" % (tip, vote_task["voted"])
        item_tip_list = []
        passed = True
        for i, item in enumerate(items):
            strong_number = len([record for record in valid_records if record["vote"][i] == STRONG])
            weak_number = len([record for record in valid_records if record["vote"][i] == WEAK])
            weight = vote_task["weight"]
            passed = passed and (strong_number > (strong_number + weak_number) * weight)
            strong_rate = (
                strong_number * 1.0 / (strong_number + weak_number) if (strong_number + weak_number) > 0 else 0.0
            )
            item_str = u"%s:\n%d%%顶" % (item, strong_rate * 100)
            item_tip_list.append(item_str)

            item_result = {
                "name": item,
                "strong": strong_number,
                "strong_rate": strong_rate,
                "weak": weak_number,
                "passed": passed,
            }
            item_results.append(item_result)
        item_tip = "\n".join(item_tip_list)
        tip = u"%s\n%s\n投票%s\n最后投票人员: %s\n" % (tip, item_tip, PASSED_MAP[passed], vote_task.get("last_voter", ""))
        if vote_task["not_vote"]:
            tip = u"%s未投票人员: %s" % (tip, ",".join(not_vote_nicks) or u"无")

        update = {
            "$set": {
                "not_vote": not_vote_users,
                "finished_time": timestamp_now(),
                "item_results": item_results,
                "status": Status.FINISHED,
                "group_users": user_nick.keys(),
            }
        }
        wechatdb.update_vote_task({"id": vote_id}, update)
        wechatdb.remove_vote_group(group_id)
    else:
        tip = u"投票还未开始"
    return {"tip": tip}
Ejemplo n.º 4
0
def vote_finish(group_id, sender, nick, msg, group_users):
    '''
    vote finished
    '''
    tip = ''
    group_users = simplejson.loads(group_users)
    user_nick = {}
    for user in group_users:
        if user['name'] != ROBOT and user['id'] not in ROBOT_IDS:
            user_nick[user['id']] = user['name']
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        vote_task = wechatdb.get_vote_task({'id': vote_id})
        items = vote_task['items']
        records = wechatdb.get_vote_records({'vote_id': vote_id})
        voter_set = set([record['voter'] for record in records])
        not_vote_users = list(set(user_nick.keys()) - voter_set)
        not_vote_nicks = [user_nick[user] for user in not_vote_users]
        item_results = []
        valid_records = [record for record in records if record['valid']]
        vote_number = len(records)
        vote_valid_number = len(valid_records)
        vote_invalid_number = vote_number - vote_valid_number
        tip = u'总票数: %d\n有效票数: %d\n无效票数: %d' % (vote_number, vote_valid_number, vote_invalid_number)
        if vote_task['voted']:
            tip = u'%s\n%s' % (tip, vote_task['voted'])
        item_tip_list = []
        passed = True
        for i, item in enumerate(items):
            strong_number = len([record for record in valid_records if record['vote'][i] == STRONG])
            weak_number = len([record for record in valid_records if record['vote'][i] == WEAK])
            weight = vote_task['weight']
            passed = passed and (strong_number > (strong_number + weak_number) * weight)
            strong_rate = strong_number * 1.0 / (strong_number + weak_number) if (strong_number + weak_number) > 0 else 0.0
            item_str = u'%s:\n%d%%顶' % (item, strong_rate * 100)
            item_tip_list.append(item_str)

            item_result = {
                'name': item,
                'strong': strong_number,
                'strong_rate': strong_rate,
                'weak': weak_number,
                'passed': passed,
            }
            item_results.append(item_result)
        item_tip = '\n'.join(item_tip_list)
        tip = u'%s\n%s\n投票%s\n最后投票人员: %s\n' % (tip, item_tip, PASSED_MAP[passed], vote_task.get('last_voter', ''))
        if vote_task['not_vote']:
            tip = u'%s未投票人员: %s' % (tip, ','.join(not_vote_nicks) or u'无')

        update = {
            '$set': {
                'not_vote': not_vote_users,
                'finished_time': timestamp_now(),
                'item_results': item_results,
                'status': Status.FINISHED,
                'group_users': user_nick.keys(),
            }
        }
        wechatdb.update_vote_task({'id': vote_id}, update)
        wechatdb.remove_vote_group(group_id)
    else:
        tip = u'投票还未开始'
    return {'tip': tip}
Ejemplo n.º 5
0
def vote_history(group_id, member, nick, msg):
    '''
    View vote history
    '''
    tip = ''
    vote_tasks = wechatdb.get_vote_tasks({
        'group_id': group_id,
        'group_users': member,
        'status': Status.FINISHED
    })
    if len(vote_tasks) > 0:
        task_ids = [task['id'] for task in vote_tasks]
        vote_records = wechatdb.get_vote_records({
            'vote_id': {
                '$in': task_ids
            },
            'voter': member,
            'valid': True
        })
        record_dict = {}
        for record in vote_records:
            record_dict[record['vote_id']] = record
        strong = 0
        weak = 0
        not_vote = 0
        diff_strong = 0
        diff_weak = 0
        for task in vote_tasks:
            vote_id = task['id']
            if vote_id in record_dict:
                vote_values = record_dict[vote_id]['vote']
                item_results = task['item_results']
                _LOGGER.info(vote_values)
                _LOGGER.info(item_results)
                for vote, item_result in zip(vote_values, item_results):
                    if vote == STRONG:
                        strong += 1
                        if not item_result['passed']:
                            diff_strong += 1
                    else:
                        weak += 1
                        if item_result['passed']:
                            diff_weak += 1
            else:
                not_vote += 1
        _LOGGER.info(diff_weak)
        sum_votes = strong + weak
        strong_rate = 0
        weak_rate = 0
        different_rate = 0
        diff_strong_rate = 0
        diff_weak_rate = 0
        if sum_votes > 0:
            strong_rate = strong * 100.0 / sum_votes
            weak_rate = weak * 100.0 / sum_votes
            different_rate = (diff_strong + diff_weak) * 100.0 / sum_votes
            diff_strong_rate = diff_strong * 100.0 / sum_votes
            diff_weak_rate = diff_weak * 100.0 / sum_votes
        tip = u'查询%s的结果\n顶次数: %d, 占比: %.2f%%\n踩次数: %d, 占比: %.2f%%\n跟结果不同次数: %d, 占比: %.2f%%\n其中顶的次数: %d, 占比: %.2f%%\n踩的次数: %d, 占比: %.2f%%\n未投票次数: %d' % \
                (nick, strong, strong_rate, weak, weak_rate, diff_strong + diff_weak, different_rate, diff_strong, diff_strong_rate, \
                diff_weak, diff_weak_rate, not_vote)
    else:
        tip = u'暂无投票历史'
    return {'tip': tip}
Ejemplo n.º 6
0
def vote_finish(group_id, sender, nick, msg, group_users):
    '''
    vote finished
    '''
    tip = ''
    group_users = simplejson.loads(group_users)
    user_nick = {}
    for user in group_users:
        if user['name'] != ROBOT and user['id'] not in ROBOT_IDS:
            user_nick[user['id']] = user['name']
    vote_id = wechatdb.get_vote_by_group(group_id)
    if vote_id:
        vote_task = wechatdb.get_vote_task({'id': vote_id})
        items = vote_task['items']
        records = wechatdb.get_vote_records({'vote_id': vote_id})
        voter_set = set([record['voter'] for record in records])
        not_vote_users = list(set(user_nick.keys()) - voter_set)
        not_vote_nicks = [user_nick[user] for user in not_vote_users]
        item_results = []
        valid_records = [record for record in records if record['valid']]
        vote_number = len(records)
        vote_valid_number = len(valid_records)
        vote_invalid_number = vote_number - vote_valid_number
        tip = u'总票数: %d\n有效票数: %d\n无效票数: %d' % (vote_number, vote_valid_number,
                                                vote_invalid_number)
        if vote_task['voted']:
            tip = u'%s\n%s' % (tip, vote_task['voted'])
        item_tip_list = []
        passed = True
        for i, item in enumerate(items):
            strong_number = len([
                record for record in valid_records
                if record['vote'][i] == STRONG
            ])
            weak_number = len([
                record for record in valid_records if record['vote'][i] == WEAK
            ])
            weight = vote_task['weight']
            passed = passed and (strong_number >
                                 (strong_number + weak_number) * weight)
            strong_rate = strong_number * 1.0 / (
                strong_number + weak_number) if (strong_number +
                                                 weak_number) > 0 else 0.0
            item_str = u'%s:\n%d%%顶' % (item, strong_rate * 100)
            item_tip_list.append(item_str)

            item_result = {
                'name': item,
                'strong': strong_number,
                'strong_rate': strong_rate,
                'weak': weak_number,
                'passed': passed,
            }
            item_results.append(item_result)
        item_tip = '\n'.join(item_tip_list)
        tip = u'%s\n%s\n投票%s\n最后投票人员: %s\n' % (
            tip, item_tip, PASSED_MAP[passed], vote_task.get('last_voter', ''))
        if vote_task['not_vote']:
            tip = u'%s未投票人员: %s' % (tip, ','.join(not_vote_nicks) or u'无')

        update = {
            '$set': {
                'not_vote': not_vote_users,
                'finished_time': timestamp_now(),
                'item_results': item_results,
                'status': Status.FINISHED,
                'group_users': user_nick.keys(),
            }
        }
        wechatdb.update_vote_task({'id': vote_id}, update)
        wechatdb.remove_vote_group(group_id)
    else:
        tip = u'投票还未开始'
    return {'tip': tip}