<commit_before>import datetime
from django.utils.importlib import import_module
from django.template.defaultfilters import slugify
from django.conf import settings
from bigbrother.models import ModuleStat
from bigbrother.warnings import send_warning


def get_module_list():
    default_modules = (
        'bigbrother.core.UserCount',
        'bigbrother.core.NewUsersTodayCount',
        'bigbrother.core.FreeRamCount',
        'bigbrother.core.FreeDiskCount',
    )
    return getattr(settings, 'BIGBROTHER_MODULES', default_modules)


def get_module_classes():
    """
    Returns all the module classes defined in the settings
    """
    clslist = []
    for m in get_module_list():
        modulename, attr = m.rsplit('.', 1)
        try:
            module = import_module(modulename)
        except ImportError:
            continue
        cls = getattr(module, attr, None)
        if not cls:
            continue
        clslist.append(cls)
    return clslist


def get_module_by_slug(slug):
    """
    Searches for a module by slug
    """
    for cls in get_module_classes():
        if cls().get_slug() == slug:
            return cls


def update_modules(logger=None):
    """
    Process all module updates
    """
    for cls in get_module_classes():
        instance = cls()
        if not instance.check_compatible():
            continue
        if instance.write_to_db:
            if logger:
                logger.debug('Saving %s - Value: %.2f' % (instance.get_slug(), instance.get_val()))
            ModuleStat.objects.create(modulename=instance.get_slug(), value=instance.get_val())


class BigBrotherModule():
    name = 'Unnamed Module'
    write_to_db = True
    prepend_text = ''
    add_text = ''
    warning_low = None
    warning_high = None
    link_url = None
    aggregate_function = None

    def check_compatible(self):
        return True

    def get_aggregate_function(self):
        return self.aggregate_function

    def get_val(self):
        """
        Returns the current value
        """
        raise NotImplementedError('get_val not implemented.')
        
    def get_text(self):
        """
        Returns the current value as formatted text
        """
        return '%s%g%s' % (self.prepend_text, self.get_val(), self.add_text)
        
    def get_slug(self):
        """
        Returns the URL friendly slug for the module
        """
        return slugify(self.name)

    def check_warning(self):
        """
        Check if a warning level has been breached
        """
        if self.warning_high and self.get_val() >= self.warning_high:
            self.warn(warningtype='high')
            return True
        if self.warning_low and self.get_val() <= self.warning_low:
            self.warn(warningtype='low')
            return True
        return False
    
    def warn(self, warningtype):
        send_warning(module=self.__class__, warningtype=warningtype)


class UserCount(BigBrotherModule):
    name = 'Total Users'
    
    def get_val(self):
        try:
            from django.contrib.auth import get_user_model
            USER_MODEL = get_user_model()
        except ImportError:
            from django.contrib.auth.models import User as USER_MODEL
        users = USER_MODEL.objects.all()
        return users.count()

        
class NewUsersTodayCount(BigBrotherModule):
    name = 'New Users Today'
    
    def get_val(self):
        try:
            from django.contrib.auth import get_user_model
            USER_MODEL = get_user_model()
        except ImportError:
            from django.contrib.auth.models import User as USER_MODEL
        curday = datetime.datetime.today()
        users = USER_MODEL.objects.filter(date_joined__year=curday.year, date_joined__month=curday.month, date_joined__day=curday.day)
        return users.count()


class FreeRamCount(BigBrotherModule):
    name = 'Free RAM'
    add_text = ' MB'
    warning_low = 16

    def check_compatible(self):
        try:
            import psutil
        except ImportError:
            return False
        return True

    def get_val(self):
        import psutil
        return psutil.phymem_usage()[2] / (1024 * 1024)


class SwapUsage(BigBrotherModule):
    name = 'Swap Usage'
    add_text = ' MB'
    warning_high = 1

    def check_compatible(self):
        try:
            import psutil
        except ImportError:
            return False
        return True

    def get_val(self):
        import psutil
        return psutil.virtmem_usage()[1] / (1024 * 1024)


class FreeDiskCount(BigBrotherModule):
    name = 'Free Disk Space'
    add_text = ' GB'

    def check_compatible(self):
        import platform
        if platform.system() == 'Windows':
            return False
        return True

    def get_val(self):
        import os
        s = os.statvfs(os.path.split(os.getcwd())[0])
        return round((s.f_bavail * s.f_frsize) / (1024 * 1024 * 1024.0), 1)
<commit_msg>Add get prefix/suffix allowing for more flexibility when defining the visible output of a module.<commit_after>import datetime
from django.utils.importlib import import_module
from django.template.defaultfilters import slugify
from django.conf import settings
from bigbrother.models import ModuleStat
from bigbrother.warnings import send_warning


def get_module_list():
    default_modules = (
        'bigbrother.core.UserCount',
        'bigbrother.core.NewUsersTodayCount',
        'bigbrother.core.FreeRamCount',
        'bigbrother.core.FreeDiskCount',
    )
    return getattr(settings, 'BIGBROTHER_MODULES', default_modules)


def get_module_classes():
    """
    Returns all the module classes defined in the settings
    """
    clslist = []
    for m in get_module_list():
        modulename, attr = m.rsplit('.', 1)
        try:
            module = import_module(modulename)
        except ImportError:
            continue
        cls = getattr(module, attr, None)
        if not cls:
            continue
        clslist.append(cls)
    return clslist


def get_module_by_slug(slug):
    """
    Searches for a module by slug
    """
    for cls in get_module_classes():
        if cls().get_slug() == slug:
            return cls


def update_modules(logger=None):
    """
    Process all module updates
    """
    for cls in get_module_classes():
        instance = cls()
        if not instance.check_compatible():
            continue
        if instance.write_to_db:
            if logger:
                logger.debug('Saving %s - Value: %.2f' % (instance.get_slug(), instance.get_val()))
            ModuleStat.objects.create(modulename=instance.get_slug(), value=instance.get_val())


class BigBrotherModule():
    name = 'Unnamed Module'
    write_to_db = True
    prefix_text = ''
    suffix_text = ''
    warning_low = None
    warning_high = None
    link_url = None
    aggregate_function = None

    def check_compatible(self):
        return True

    def get_aggregate_function(self):
        """
        Return the Django aggregation function this module uses for the aggregated graph views.
        """
        return self.aggregate_function

    def get_val(self):
        """
        Returns the current value
        """
        raise NotImplementedError('get_val not implemented.')

    def get_prefix_text(self):
        """
        Get the text to prefix the value with, for example $ for monetary values.
        """
        return self.prefix_text

    def get_suffix_text(self):
        """
        Get the suffix for the value, for example "Users" for a user count.
        """
        return self.suffix_text

    def get_text(self):
        """
        Returns the current value as formatted text
        """
        return '%s%g%s' % (self.get_prefix_text(), self.get_val(), self.get_suffix_text())
        
    def get_slug(self):
        """
        Returns the URL friendly slug for the module
        """
        return slugify(self.name)

    def check_warning(self):
        """
        Check if a warning level has been breached
        """
        if self.warning_high and self.get_val() >= self.warning_high:
            self.warn(warningtype='high')
            return True
        if self.warning_low and self.get_val() <= self.warning_low:
            self.warn(warningtype='low')
            return True
        return False
    
    def warn(self, warningtype):
        send_warning(module=self.__class__, warningtype=warningtype)


class UserCount(BigBrotherModule):
    name = 'Total Users'
    
    def get_val(self):
        try:
            from django.contrib.auth import get_user_model
            USER_MODEL = get_user_model()
        except ImportError:
            from django.contrib.auth.models import User as USER_MODEL
        users = USER_MODEL.objects.all()
        return users.count()

        
class NewUsersTodayCount(BigBrotherModule):
    name = 'New Users Today'
    
    def get_val(self):
        try:
            from django.contrib.auth import get_user_model
            USER_MODEL = get_user_model()
        except ImportError:
            from django.contrib.auth.models import User as USER_MODEL
        curday = datetime.datetime.today()
        users = USER_MODEL.objects.filter(date_joined__year=curday.year, date_joined__month=curday.month, date_joined__day=curday.day)
        return users.count()


class FreeRamCount(BigBrotherModule):
    name = 'Free RAM'
    suffix_text = ' MB'
    warning_low = 16

    def check_compatible(self):
        try:
            import psutil
        except ImportError:
            return False
        return True

    def get_val(self):
        import psutil
        return psutil.phymem_usage()[2] / (1024 * 1024)


class SwapUsage(BigBrotherModule):
    name = 'Swap Usage'
    suffix_text = ' MB'
    warning_high = 1

    def check_compatible(self):
        try:
            import psutil
        except ImportError:
            return False
        return True

    def get_val(self):
        import psutil
        return psutil.virtmem_usage()[1] / (1024 * 1024)


class FreeDiskCount(BigBrotherModule):
    name = 'Free Disk Space'
    suffix_text = ' GB'

    def check_compatible(self):
        import platform
        if platform.system() == 'Windows':
            return False
        return True

    def get_val(self):
        import os
        s = os.statvfs(os.path.split(os.getcwd())[0])
        return round((s.f_bavail * s.f_frsize) / (1024 * 1024 * 1024.0), 1)

{'fraction_non_alphanumeric': 0.0893726725866514, 'fraction_numerical': 0.007256755466437506, 'mean_word_length': 2.244733581164808, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4288431', 'n_tokens_mistral': 3123, 'n_tokens_neox': 2981, 'n_words': 769}