From 56d28986af22151186bf3f0dac4fdfb16d648c1f Mon Sep 17 00:00:00 2001 From: Peter Pfeufer Date: Wed, 6 Nov 2024 01:50:10 +0100 Subject: [PATCH] [CHANGE] Move duplicate code to own functions --- afat/helper/views.py | 75 +++++++++++++++++++++++++++++++++++ afat/views/statistics.py | 84 +++++++--------------------------------- 2 files changed, 90 insertions(+), 69 deletions(-) diff --git a/afat/helper/views.py b/afat/helper/views.py index 585135c7..08ca376f 100644 --- a/afat/helper/views.py +++ b/afat/helper/views.py @@ -4,6 +4,7 @@ # Standard Library import random +from collections import OrderedDict from datetime import datetime # Django @@ -322,3 +323,77 @@ def current_month_and_year() -> tuple[int, int]: current_year = datetime.now().year return current_month, current_year + + +def get_fats_per_hour(fats) -> list: + """ + Get the FATs per hour from the fats queryset + + :param fats: + :type fats: + :return: + :rtype: + """ + + data_time = {i: fats.filter(fatlink__created__hour=i).count() for i in range(24)} + + return [ + list(data_time.keys()), + list(data_time.values()), + [get_random_rgba_color()], + ] + + +def get_fat_per_weekday(fats) -> list: + """ + Get the FATs per weekday from the fats queryset + + :param fats: + :type fats: + :return: + :rtype: + """ + + return [ + [ + _("Monday"), + _("Tuesday"), + _("Wednesday"), + _("Thursday"), + _("Friday"), + _("Saturday"), + _("Sunday"), + ], + [fats.filter(fatlink__created__iso_week_day=i).count() for i in range(1, 8)], + [get_random_rgba_color()], + ] + + +def get_average_fats_by_corporations(fats, corporations) -> list: + """ + Get the average FATs per corporation + + :param fats: + :type fats: + :param corporations: + :type corporations: + :return: + :rtype: + """ + + data_avgs = { + corp.corporation_name: round( + fats.filter(character__corporation_id=corp.corporation_id).count() + / corp.member_count, + 2, + ) + for corp in corporations + } + + data_avgs = OrderedDict(sorted(data_avgs.items(), key=lambda x: x[1], reverse=True)) + + return [ + list(data_avgs.keys()), + list(data_avgs.values()), + get_random_rgba_color(), + ] diff --git a/afat/views/statistics.py b/afat/views/statistics.py index a5d87d39..e178b71a 100644 --- a/afat/views/statistics.py +++ b/afat/views/statistics.py @@ -32,6 +32,9 @@ from afat.helper.views import ( characters_with_permission, current_month_and_year, + get_average_fats_by_corporations, + get_fat_per_weekday, + get_fats_per_hour, get_random_rgba_color, user_has_any_perms, ) @@ -214,7 +217,7 @@ def character( # pylint: disable=too-many-locals ): can_view_character = False - # Check if the user if by any chance in the same corporation as the character + # Check if the user is by any chance in the same corporation as the character # and can view own corporation statistics if ( eve_character not in valid @@ -277,14 +280,8 @@ def character( # pylint: disable=too-many-locals colors, ] - # Data for by Time Line Chart - data_time = {i: fats.filter(fatlink__created__hour=i).count() for i in range(24)} - - data_time = [ - list(data_time.keys()), - list(data_time.values()), - [get_random_rgba_color()], - ] + # Data for by timeline Chart + data_time = get_fats_per_hour(fats) context = { "character": eve_character, @@ -351,7 +348,7 @@ def corporation( # pylint: disable=too-many-statements too-many-branches too-ma current_month, current_year = current_month_and_year() - # Check character has permission to view other corp stats + # Check character has permission to view other corps stats if int(request.user.profile.main_character.corporation_id) != int(corpid): if not user_has_any_perms( user=request.user, @@ -444,10 +441,7 @@ def corporation( # pylint: disable=too-many-statements too-many-branches too-ma data_stacked = [] for key, value in data.items(): - stack = [] - stack.append(key) - stack.append(get_random_rgba_color()) - stack.append([]) + stack = [key, get_random_rgba_color(), []] data_ = stack[2] @@ -460,28 +454,10 @@ def corporation( # pylint: disable=too-many-statements too-many-branches too-ma data_stacked = [chars, data_stacked] # Data for By Time - data_time = {i: fats.filter(fatlink__created__hour=i).count() for i in range(24)} - - data_time = [ - list(data_time.keys()), - list(data_time.values()), - [get_random_rgba_color()], - ] + data_time = get_fats_per_hour(fats) # Data for By Weekday - data_weekday = [ - [ - gettext("Monday"), - gettext("Tuesday"), - gettext("Wednesday"), - gettext("Thursday"), - gettext("Friday"), - gettext("Saturday"), - gettext("Sunday"), - ], - [fats.filter(fatlink__created__iso_week_day=i).count() for i in range(1, 8)], - [get_random_rgba_color()], - ] + data_weekday = get_fat_per_weekday(fats) chars = {} @@ -647,45 +623,15 @@ def alliance( # pylint: disable=too-many-statements too-many-branches too-many- corporations_in_alliance = EveCorporationInfo.objects.filter(alliance=ally) # Avg fats by corp - data_avgs = { - corp.corporation_name: round( - fats.filter(character__corporation_id=corp.corporation_id).count() - / corp.member_count, - 2, - ) - for corp in corporations_in_alliance - } - - data_avgs = OrderedDict(sorted(data_avgs.items(), key=lambda x: x[1], reverse=True)) - data_avgs = [ - list(data_avgs.keys()), - list(data_avgs.values()), - get_random_rgba_color(), - ] + data_avgs = get_average_fats_by_corporations( + fats=fats, corporations=corporations_in_alliance + ) # Fats by Time - data_time = {i: fats.filter(fatlink__created__hour=i).count() for i in range(24)} - - data_time = [ - list(data_time.keys()), - list(data_time.values()), - [get_random_rgba_color()], - ] + data_time = get_fats_per_hour(fats=fats) # Fats by weekday - data_weekday = [ - [ - gettext("Monday"), - gettext("Tuesday"), - gettext("Wednesday"), - gettext("Thursday"), - gettext("Friday"), - gettext("Saturday"), - gettext("Sunday"), - ], - [fats.filter(fatlink__created__iso_week_day=i).count() for i in range(1, 8)], - [get_random_rgba_color()], - ] + data_weekday = get_fat_per_weekday(fats=fats) # Corp list corps = {}