aircox-radiocampus/aircox/management/commands/archiver.py
Thomas Kairos 0e183099ed #88 #89 : use pytest + reorganise settings (#92)
- !88 pytest on existing tests
- !89 reorganise settings (! see notes for deployment)

Co-authored-by: bkfox <thomas bkfox net>
Reviewed-on: rc/aircox#92
2023-03-28 14:40:49 +02:00

49 lines
1.4 KiB
Python

"""Handle archiving of logs in order to keep database light and fast.
The logs are archived in gzip files, per day.
"""
import datetime
import logging
from argparse import RawTextHelpFormatter
from django.core.management.base import BaseCommand
from django.utils import timezone as tz
from aircox.conf import settings
from aircox.models import Log
from aircox.models.log import LogArchiver
logger = logging.getLogger("aircox.commands")
__all__ = ("Command",)
class Command(BaseCommand):
help = __doc__
def add_arguments(self, parser):
parser.formatter_class = RawTextHelpFormatter
group = parser.add_argument_group("actions")
group.add_argument(
"-a",
"--age",
type=int,
default=settings.LOGS_ARCHIVES_AGE,
help="minimal age in days of logs to archive. Default is "
"settings.LOGS_ARCHIVES_AGE",
)
group.add_argument(
"-k",
"--keep",
action="store_true",
help="keep logs in database instead of deleting them",
)
def handle(self, *args, age, keep, **options):
date = datetime.date.today() - tz.timedelta(days=age)
# FIXME: mysql support?
logger.info("archive logs for %s and earlier", date)
count = LogArchiver().archive(Log.objects.filter(date__date__lte=date))
logger.info("total log archived %d", count)