journal: use EntryManager

This commit is contained in:
Christophe Siraut 2020-08-25 12:38:11 +02:00
parent b2ed0b5466
commit 74aaaab261
2 changed files with 7 additions and 1 deletions

View File

@ -19,7 +19,7 @@ class Command(BaseCommand):
def handle(self, *args, **options):
_, columns = os.popen("stty size", "r").read().split()
for entry in reversed(Entry.objects.order_by('-timestamp')[:int(options['lines'])]):
for entry in Entry.objects.print(lines=options['lines']):
line = "%s %s %s" % (
entry.timestamp.strftime("%c"),
entry.host,

View File

@ -3,10 +3,16 @@ from django.contrib.postgres.fields import JSONField
from django.contrib.postgres.indexes import GinIndex
class EntryManager(models.Manager):
def print(self, lines=100):
return self.model.objects.order_by('-timestamp')[:int(lines)].reverse()
class Entry(models.Model):
timestamp = models.DateTimeField(auto_now_add=True, db_index=True)
host = models.CharField(max_length=128, db_index=True)
data = JSONField()
objects = EntryManager()
def __str__(self):
return '%s %s %s' % (self.timestamp, self.host, self.data)