-
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
10 changed files
with
257 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
If you contribute to mongolog please add your name to the list | ||
|
||
* John Furr | ||
* John Dowling |
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,73 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Management command to handle deletion of previous mongolog records. | ||
Usage Examples: | ||
# Used to migrate monoglog data from one collection to another | ||
./manage.py ml_migrate {from_collection} {to_collectioj} | ||
""" | ||
from __future__ import print_function | ||
import sys | ||
import logging | ||
from pymongo import MongoClient | ||
|
||
from mongolog.handlers import get_mongolog_handler | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
console = logging.getLogger('mongolog-int') | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'-f', '--from', default='', type=str, action='store', dest='from', | ||
help='From Collection', | ||
) | ||
parser.add_argument( | ||
'-t', '--to', default='', type=str, action='store', dest='to', | ||
help='To Collection', | ||
) | ||
parser.add_argument( | ||
'-d', '--delete', default=False, action='store_true', dest='delete', | ||
help='Delete the from collection', | ||
) | ||
parser.add_argument( | ||
'--force', default=False, action='store_true', dest='force', | ||
help='Force delete "from" collection without prompt', | ||
) | ||
|
||
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 handle(self, *args, **options): | ||
""" Main processing handle """ | ||
handler = get_mongolog_handler() | ||
client = MongoClient(handler.connection) | ||
db = client.mongolog | ||
|
||
from_collection = getattr(db, options['from']) | ||
from_collection.copyTo(options['to']) | ||
|
||
if options['delete']: | ||
console.warn("Deleting from collection") | ||
if self.confirm(): | ||
form_collection.delete_many({}) |
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 ml_purge -d 54 -b -c bulkupload | ||
# Delete all entries | ||
./manage.py ml_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') | ||
|
||
|
||
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( | ||
'-l', '--logger', default='mongolog', type=str, action='store', dest='logger', | ||
help='Which mongolog logger to use. The collection defined in the log handler will be used.', | ||
) | ||
|
||
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({}).count() | ||
self.collection.delete_many({}) | ||
console.warn("Total docs to remove: %s", total) | ||
|
||
def delete(self, **options): | ||
""" Delete all records older than --delete={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 """ | ||
handler = get_mongolog_handler(logger_name=options['logger']) | ||
client = MongoClient(handler.connection) | ||
db = client.mongolog | ||
self.collection = getattr(db, handler.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
Oops, something went wrong.