mirror of
				https://github.com/ae-utbm/sith.git
				synced 2025-11-04 11:03:04 +00:00 
			
		
		
		
	galaxy: improve logging and performance reporting
This commit is contained in:
		@@ -24,6 +24,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import math
 | 
					import math
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
 | 
					import time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from typing import Tuple
 | 
					from typing import Tuple
 | 
				
			||||||
from django.db import models
 | 
					from django.db import models
 | 
				
			||||||
@@ -313,9 +314,12 @@ class Galaxy(models.Model):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def rule(cls) -> None:
 | 
					    def rule(cls) -> None:
 | 
				
			||||||
 | 
					        cls.logger.info("Eradicating previous Galaxy.")
 | 
				
			||||||
        GalaxyStar.objects.all().delete()
 | 
					        GalaxyStar.objects.all().delete()
 | 
				
			||||||
        # The following is a no-op thanks to cascading, but in case that changes in the future, better keep it anyway.
 | 
					        # The following is a no-op thanks to cascading, but in case that changes in the future, better keep it anyway.
 | 
				
			||||||
        GalaxyLane.objects.all().delete()
 | 
					        GalaxyLane.objects.all().delete()
 | 
				
			||||||
 | 
					        cls.logger.info("Galaxy has been purged.")
 | 
				
			||||||
 | 
					        cls.logger.info("Listing rulable citizen.")
 | 
				
			||||||
        rulable_users = (
 | 
					        rulable_users = (
 | 
				
			||||||
            User.objects.filter(subscriptions__isnull=False)
 | 
					            User.objects.filter(subscriptions__isnull=False)
 | 
				
			||||||
            .filter(
 | 
					            .filter(
 | 
				
			||||||
@@ -326,17 +330,34 @@ class Galaxy(models.Model):
 | 
				
			|||||||
            )
 | 
					            )
 | 
				
			||||||
            .distinct()
 | 
					            .distinct()
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # force fetch of the whole query to make sure there won't
 | 
					        # force fetch of the whole query to make sure there won't
 | 
				
			||||||
        # be any more db hits
 | 
					        # be any more db hits
 | 
				
			||||||
        # this is memory expensive but prevents a lot of db hits, therefore
 | 
					        # this is memory expensive but prevents a lot of db hits, therefore
 | 
				
			||||||
        # is far more time efficient
 | 
					        # is far more time efficient
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        rulable_users = list(rulable_users)
 | 
					        rulable_users = list(rulable_users)
 | 
				
			||||||
 | 
					        rulable_users_count = len(rulable_users)
 | 
				
			||||||
 | 
					        user1_count = 0
 | 
				
			||||||
 | 
					        cls.logger.info(
 | 
				
			||||||
 | 
					            f"{rulable_users_count} citizen have been listed. Starting to rule."
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Display current speed every $speed_count_frequency users
 | 
				
			||||||
 | 
					        speed_count_frequency = 100
 | 
				
			||||||
 | 
					        avg_speed = 0
 | 
				
			||||||
 | 
					        avg_speed_count = 0
 | 
				
			||||||
        while len(rulable_users) > 0:
 | 
					        while len(rulable_users) > 0:
 | 
				
			||||||
            user1 = rulable_users.pop()
 | 
					            user1 = rulable_users.pop()
 | 
				
			||||||
            for user2 in rulable_users:
 | 
					            user1_count += 1
 | 
				
			||||||
 | 
					            rulable_users_count2 = len(rulable_users)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            tstart = time.time()
 | 
				
			||||||
 | 
					            for user2_count, user2 in enumerate(rulable_users, start=1):
 | 
				
			||||||
                cls.logger.debug("")
 | 
					                cls.logger.debug("")
 | 
				
			||||||
                cls.logger.debug(f"\t> Ruling '{user1}' against '{user2}'")
 | 
					                cls.logger.debug(
 | 
				
			||||||
                star1, _ = GalaxyStar.objects.get_or_create(owner=user1)
 | 
					                    f"\t> Examining '{user1}' ({user1_count}/{rulable_users_count}) with '{user2}' ({user2_count}/{rulable_users_count2})"
 | 
				
			||||||
 | 
					                )
 | 
				
			||||||
                star2, _ = GalaxyStar.objects.get_or_create(owner=user2)
 | 
					                star2, _ = GalaxyStar.objects.get_or_create(owner=user2)
 | 
				
			||||||
                if star1.mass == 0:
 | 
					                if star1.mass == 0:
 | 
				
			||||||
                    star1.mass = cls.compute_user_score(user1)
 | 
					                    star1.mass = cls.compute_user_score(user1)
 | 
				
			||||||
@@ -357,6 +378,29 @@ class Galaxy(models.Model):
 | 
				
			|||||||
                        clubs=clubs,
 | 
					                        clubs=clubs,
 | 
				
			||||||
                    ).save()
 | 
					                    ).save()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if user2_count % speed_count_frequency == 0:
 | 
				
			||||||
 | 
					                    tend = time.time()
 | 
				
			||||||
 | 
					                    delta = tend - tstart
 | 
				
			||||||
 | 
					                    speed = float(speed_count_frequency) / delta
 | 
				
			||||||
 | 
					                    avg_speed += speed
 | 
				
			||||||
 | 
					                    avg_speed_count += 1
 | 
				
			||||||
 | 
					                    cls.logger.info("")
 | 
				
			||||||
 | 
					                    cls.logger.info(
 | 
				
			||||||
 | 
					                        f"\t> Ruling citizen {user1_count}/{rulable_users_count} against citizen {user2_count}/{rulable_users_count2}"
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					                    cls.logger.info(
 | 
				
			||||||
 | 
					                        f"Speed: {speed:.2f} users per second (delta: {delta:.2f})"
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					                    eta = (rulable_users_count * rulable_users_count2) / speed / 3600
 | 
				
			||||||
 | 
					                    cls.logger.info(
 | 
				
			||||||
 | 
					                        "Estimated remaining time: {0:.2f} hours ({1:.2f} days)".format(
 | 
				
			||||||
 | 
					                            eta, eta / 24
 | 
				
			||||||
 | 
					                        )
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					                    tstart = time.time()
 | 
				
			||||||
 | 
					        avg_speed /= avg_speed_count
 | 
				
			||||||
 | 
					        cls.logger.info(f"Average speed: {avg_speed:.2f} users per second")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @classmethod
 | 
					    @classmethod
 | 
				
			||||||
    def scale_distance(cls, value) -> int:
 | 
					    def scale_distance(cls, value) -> int:
 | 
				
			||||||
        # TODO: this will need adjustements with the real, typical data on Taiste
 | 
					        # TODO: this will need adjustements with the real, typical data on Taiste
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user