-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
155 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Management command to handle deletion of previous mongolog records. | ||
Usage Examples: | ||
# Delete all records alder than 54 day's AFTER backing up the current collection 'bulkupload' | ||
./manage.py mongolog_purge -d 54 -b -c bulkupload | ||
# Delete all entries | ||
./manage.py mongolog_purge -p -c bulkupload | ||
""" | ||
from __future__ import print_function | ||
import sys | ||
import logging | ||
from datetime import timedelta | ||
import subprocess | ||
from pymongo import MongoClient | ||
|
||
from mongolog.handlers import get_mongolog_handler | ||
|
||
from django.utils import timezone | ||
from django.core.management.base import BaseCommand | ||
|
||
console = logging.getLogger('mongolog-int') | ||
|
||
handler = get_mongolog_handler() | ||
client = MongoClient(handler.connection) | ||
db = client.mongolog | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'-p', '--purge', default=False, action='store_true', dest='purge', | ||
help='Remove all old results', | ||
) | ||
parser.add_argument( | ||
'-d', '--delete', default=14, type=int, action='store', dest='delete', | ||
help='Delete documents more than -d={n} days old', | ||
) | ||
parser.add_argument( | ||
'-f', '--force', default=False, action='store_true', dest='force', | ||
help='Do not prompt before removing documents', | ||
) | ||
parser.add_argument( | ||
'-b', '--backup', default=False, action='store_true', dest='backup', | ||
help='Backup collection before deleting', | ||
) | ||
parser.add_argument( | ||
'-c', '--collection', default='mongolog', type=str, action='store', dest='collection', | ||
help='Which mongolog collection to use', | ||
) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(Command, self).__init__(*args, **kwargs) | ||
|
||
def confirm(self, **options): | ||
if not options['force']: | ||
while 1: | ||
console.warn("Would you like to proceed? Y/N") | ||
ans = input().strip().lower() | ||
|
||
if ans not in ['y', 'yes', 'n', 'no']: | ||
continue | ||
elif ans[0] == 'n': | ||
console.info("You chose not to continue. Bye!") | ||
sys.exit(1) | ||
elif ans[0] == 'y': | ||
break | ||
|
||
return True | ||
|
||
def purge(self, **options): | ||
""" Purge all records from the collection """ | ||
console.warn("You are about to delete all mongolog documents!!!") | ||
|
||
if self.confirm(**options): | ||
total = self.collection.find(query).count() | ||
self.collection.delete_many({}) | ||
console.warn("Total docs to remove: %s", total) | ||
|
||
def delete(self, **options): | ||
""" Delete all records older than --days={n} """ | ||
days = options['delete'] | ||
console.warn("Looking for documents older than %s day's", days) | ||
|
||
query = { | ||
'created': { | ||
'$lte': timezone.now() - timedelta(days=days) | ||
} | ||
} | ||
|
||
total = self.collection.find(query).count() | ||
console.warn("Total docs to remove: %s", total) | ||
|
||
if self.confirm(**options): | ||
self.collection.delete_many(query) | ||
console.info("Total docs removed: %s", total) | ||
|
||
def backup(self): | ||
""" Backup the collection before deleting """ | ||
console.info("Backing up your documents...") | ||
cmd = 'mongodump --db mongolog' | ||
subprocess.check_call([cmd], shell=True) | ||
|
||
def handle(self, *args, **options): | ||
""" Main processing handle """ | ||
self.collection = getattr(db, options['collection']) | ||
if options['backup']: | ||
self.backup() | ||
|
||
if options['purge']: | ||
self.purge(**options) | ||
elif isinstance(options['delete'], int): | ||
self.delete(**options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters