# Path: apps/core/models.py
# class UserLog(models.Model):
#     """
#     denotes single log for an user or global event
#     - user:  user object
#     - level: level of log; python log level
#     - time:  event time
#     - ip:    event ip
#     - hide:  hide log in user log page
#     - text:  detail log message
#     """
#     user = models.ForeignKey(User, on_delete=models.CASCADE,
#                              related_name='user_logs', blank=True, null=True)
#     level = models.IntegerField()
#     time = models.DateTimeField(auto_now=True)
#     ip = models.GenericIPAddressField()
#     hide = models.BooleanField(default=False)
#     text = models.CharField(max_length=500)
# 
#     def pretty(self):
#         time = localtime(self.time).isoformat()
#         username = self.user.username if self.user else 'unknown'
#         level, ip, text = self.level, self.ip, self.text
#         return f'{time}/{level} ({ip}, {username}) {text}'
# 
#     def __str__(self):
#         time_str = localtime(self.time).isoformat()
#         return f'{time_str}/{self.level} ({self.user}) {self.text}'


import os
import re
from datetime import timedelta
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils import timezone
from apps.core.models import UserLog




class Command(BaseCommand):
    help = 'Merge log files into one file'

    def get_full_path(self, log_file):
        return os.path.join(settings.LOG_BUFFER_DIR, log_file)

    def handle(self, *args, **options):
        # merge multiple log files into one file
        log_files = list(filter(
            lambda x: re.match(r'(\d{8})\.(\d+)\.log', x),
            os.listdir(settings.LOG_BUFFER_DIR),
        ))
        log_entities = {}
        for log_file in log_files:
            date = log_file[:8]
            if date not in log_entities:

# Path: apps/core/models.py
# class UserLog(models.Model):
#     """
#     denotes single log for an user or global event
#     - user:  user object
#     - level: level of log; python log level
#     - time:  event time
#     - ip:    event ip
#     - hide:  hide log in user log page
#     - text:  detail log message
#     """
#     user = models.ForeignKey(User, on_delete=models.CASCADE,
#                              related_name='user_logs', blank=True, null=True)
#     level = models.IntegerField()
#     time = models.DateTimeField(auto_now=True)
#     ip = models.GenericIPAddressField()
#     hide = models.BooleanField(default=False)
#     text = models.CharField(max_length=500)
# 
#     def pretty(self):
#         time = localtime(self.time).isoformat()
#         username = self.user.username if self.user else 'unknown'
#         level, ip, text = self.level, self.ip, self.text
#         return f'{time}/{level} ({ip}, {username}) {text}'
# 
#     def __str__(self):
#         time_str = localtime(self.time).isoformat()
#         return f'{time_str}/{self.level} ({self.user}) {self.text}'


# Path: apps/core/management/commands/collect-logs.py
import os
import re
from datetime import timedelta
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils import timezone
from apps.core.models import UserLog





class Command(BaseCommand):
    help = 'Merge log files into one file'

    def get_full_path(self, log_file):
        return os.path.join(settings.LOG_BUFFER_DIR, log_file)

    def handle(self, *args, **options):
        # merge multiple log files into one file
        log_files = list(filter(
            lambda x: re.match(r'(\d{8})\.(\d+)\.log', x),
            os.listdir(settings.LOG_BUFFER_DIR),
        ))
        log_entities = {}
        for log_file in log_files:
            date = log_file[:8]
            if date not in log_entities:

                log_entities[date] = []