def get_chat_stats(self, update, users_in_conversation): msg = update.message all_messages = Messages.objects(from_chat=msg.chat_id).only('from_user', 'number_of_words') total_messages = all_messages.count() user_stats = [] total_words = 0 for user in users_in_conversation: messages = Messages.objects( from_chat=msg.chat_id, from_user=user ).only('from_user', 'number_of_words') messages_per_user = messages.count() words_per_user = self.count_words(messages) total_words += words_per_user conv_percentage = (messages_per_user / total_messages) * 100 user_stats.append({ 'user': user, 'number_of_messages': messages_per_user, 'conversation_percentage': "{0:.2f}".format(conv_percentage), 'number_of_words': words_per_user }) return { 'total_words': total_words, 'total_messages': total_messages, 'user_stats': user_stats }
def count(self, update, args): msg = update.message if len(args) == 0: messages = Messages.objects(from_chat=msg.chat.id) username = msg.chat.title else: user = Users.objects(first_name__iexact=' '.join(args), chats__contains=msg.chat_id).first() username = user.username if user.username else user.first_name messages = Messages.objects(from_user=user.id).only('number_of_words') return username, messages.count(), self.count_words(messages)
def top_messagers_in_chat(self, chat_id, top_num): users_in_chat = Users.objects(chats__contains=chat_id).only('id') user_ids = map(lambda n: n.id, users_in_chat) count_dict = {} for user_id in user_ids: count_dict[self.get_user_name(user_id)] = Messages.objects(from_chat=chat_id, from_user=user_id).count() return form_list(count_dict,top_num)