Skip to content

Commit

Permalink
Merge 552b910 into 5dbad27
Browse files Browse the repository at this point in the history
  • Loading branch information
gnulnx authored Sep 25, 2018
2 parents 5dbad27 + 552b910 commit 2cf482b
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7-dev"
# - "3.5-dev" # 3.5 development branch
# - "nightly" # currently points to 3.6-dev
env:
Expand Down
4 changes: 4 additions & 0 deletions mongolog/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,22 @@ def get_mongolog_handler(logger_name=None, show_logger_names=False):
Return the first MongoLogHander found in the list of defined loggers.
NOTE: If more than one is defined, only the first one is used.
"""
console.warn(logger_name)
if logger_name:
logger_names = [logger_name]
else:
logger_names = [''] + list(logging.Logger.manager.loggerDict)

console.warn(logger_names)
if show_logger_names:
console.info("get_mongolog_handler(): Logger_names: %s", json.dumps(logger_names, indent=4, sort_keys=True, default=str))

for name in logger_names:
logger = logging.getLogger(name)
console.warn(logger)
handler = None
for _handler in logger.handlers:
console.error("%s, %s, %s, %s", isinstance(_handler, BaseMongoLogHandler), _handler, type(_handler), _handler.name)
if isinstance(_handler, BaseMongoLogHandler):
handler = _handler
break
Expand Down
29 changes: 14 additions & 15 deletions mongolog/management/commands/analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import print_function
import logging
import logging.config
import django
import json

import pymongo
Expand All @@ -19,6 +18,7 @@


class Command(BaseCommand):
"""
if django.VERSION[1] <= 7:
from optparse import make_option
option_list = BaseCommand.option_list + (
Expand All @@ -32,25 +32,24 @@ class Command(BaseCommand):
'-q', '--query', default=None, action='store', dest='query',
help='Pass in a search query to mongo.'),
)

"""
def __init__(self, *args, **kwargs):
super(Command, self).__init__(*args, **kwargs)
self.prev_object_id = None

def add_arguments(self, parser):
if django.VERSION[1] >= 7:
parser.add_argument(
'-l', '--limit', default=10, type=int, action='store', dest='limit',
help='Limit Results',
)
parser.add_argument(
'-t', '--tail', default=False, action='store_true', dest='tail',
help='Tail the log file. By default it will limit to 10 results. Use --limit to change',
)
parser.add_argument(
'-q', '--query', default=None, type=str, action='store', dest='query',
help='Pass in a search query to mongo.',
)
parser.add_argument(
'-l', '--limit', default=10, type=int, action='store', dest='limit',
help='Limit Results',
)
parser.add_argument(
'-t', '--tail', default=False, action='store_true', dest='tail',
help='Tail the log file. By default it will limit to 10 results. Use --limit to change',
)
parser.add_argument(
'-q', '--query', default=None, type=str, action='store', dest='query',
help='Pass in a search query to mongo.',
)

def print_results(self, results):
# older versions of pymongo didn't use a CommandCursor object to iterate over the results.
Expand Down
117 changes: 117 additions & 0 deletions mongolog/management/commands/mongolog_purge.py
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)
10 changes: 9 additions & 1 deletion mongolog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def find(cls, logger=None, query=None, project=None, uuid=None, level=None, limi
client = MongoClient(handler.connection)
db = client.mongolog

if logger:
collection = getattr(db, handler.collection)
else:
collection = db.mongolog

print("collection: %s" % collection)
print("type(handler): %s" % type(handler))
print("handler.collection: %s" % handler.collection)
aggregate_commands = []

if not query:
Expand All @@ -72,7 +80,7 @@ def find(cls, logger=None, query=None, project=None, uuid=None, level=None, limi
if limit:
aggregate_commands.append({"$limit": limit})

results = db.mongolog.aggregate(aggregate_commands)
results = collection.aggregate(aggregate_commands)
return results['result'] if isinstance(results, dict) else results


Expand Down
10 changes: 9 additions & 1 deletion mongolog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@
)
from mongolog.models import Mongolog

import django
django_version = django.VERSION[0]

from django.core.management import call_command
from django.test import TestCase
from django.test import Client
from django.core.urlresolvers import reverse

if django_version < 2:
from django.core.urlresolvers import reverse
else:
from django.urls import reverse

from django.conf import settings
LOGGING = settings.LOGGING
console = logging.getLogger("console")
Expand Down

0 comments on commit 2cf482b

Please sign in to comment.