work on logging

This commit is contained in:
bkfox 2015-11-23 11:21:05 +01:00
parent 4eb5e4a25c
commit 201353367d
4 changed files with 95 additions and 40 deletions

View File

@ -47,7 +47,7 @@ class Command (BaseCommand):
delay = options.get('delay') / 1000
while True:
for controller in self.monitor.controllers.values():
controller.dealer.monitor()
controller.monitor()
time.sleep(delay)

View File

@ -241,13 +241,6 @@ class Dealer (Source):
def stream_info (self):
pass
def get_next_diffusion (self):
diffusions = models.Diffusion.get_next(self.station)
if not diffusions.count():
return
diffusion = diffusions[0]
return diffusion
@property
def on (self):
r = self.connector.send('var.get ', self.id, '_on')
@ -314,11 +307,18 @@ class Dealer (Source):
# run the diff
if self.playlist == diff.playlist and diff.date <= now:
# FIXME: log
self.on = True
for source in self.controller.source:
source.skip()
self.controller.log(
source = self.id,
diffusion = diff,
date = now,
comment = 'trigger the scheduled diffusion to liquidsoap; '
'skip all other sources',
)
class Controller:
connector = None
@ -369,11 +369,13 @@ class Controller:
return self.dealer
return self.streams.get(source_id)
def next_diffusions (self, count = 5):
def log (self, **kwargs):
"""
Return a list of the count next diffusions
Create a log using **kwargs, and print info
"""
return models.Diffusion.get_next(self.station)[:count]
log = models.Log(**kwargs)
log.save()
log.print()
def update_all (self):
"""
@ -384,6 +386,31 @@ class Controller:
for source in self.streams.values():
source.update()
def __change_log (self, source):
last_log = models.Log.objects.filter(
source = source.id,
).prefetch_related('sound').order('-date')
on_air = source.current_sound
if on_air == last_log.sound.path:
return
self.log(
source = source.id,
sound = models.Sound.objects.get(path = on_air),
start = tz.make_aware(tz.datetime.now()),
comment = 'sound has changed'
)
def monitor (self):
"""
Log changes in the sources, and call dealer.monitor.
"""
self.dealer.monitor()
self.__change_log(self.dealer)
for source in self.sources:
self.__change_log(source)
class Monitor:
"""

View File

@ -90,6 +90,20 @@ class DiffusionAdmin (admin.ModelAdmin):
list_filter = ('type', 'date', 'program')
list_editable = ('type', 'date')
fields = ['type', 'date', 'initial', 'sounds', 'program']
readonly_fields = ('duration',)
def get_form(self, request, obj=None, **kwargs):
if obj:
if obj.date < tz.make_aware(tz.datetime.now()):
self.readonly_fields = list(self.fields)
self.readonly_fields.remove('type')
elif obj.initial:
self.readonly_fields = ['program', 'sounds']
else:
self.readonly_fields = []
return super().get_form(request, obj, **kwargs)
def get_queryset(self, request):
qs = super(DiffusionAdmin, self).get_queryset(request)
if '_changelist_filters' in request.GET or \

View File

@ -396,32 +396,6 @@ class Schedule (models.Model):
verbose_name_plural = _('Schedules')
class Log (models.Model):
"""
Log a played sound start and stop, or a single message
"""
sound = models.ForeignKey(
'Sound',
help_text = 'Played sound',
blank = True, null = True,
)
stream = models.ForeignKey(
'Stream',
blank = True, null = True,
)
start = models.DateTimeField(
'start',
)
stop = models.DateTimeField(
'stop',
blank = True, null = True,
)
comment = models.CharField(
max_length = 512,
blank = True, null = True,
)
class Station (Nameable):
"""
A Station regroup one or more programs (stream and normal), and is the top
@ -607,11 +581,51 @@ class Diffusion (models.Model):
super(Diffusion, self).save(*args, **kwargs)
def __str__ (self):
return self.program.name + ' on ' + str(self.date) \
+ str(self.type)
return self.program.name + ', ' + \
self.date.strftime('%Y-%m-%d %H:%M') +\
'' # FIXME str(self.type_display)
class Meta:
verbose_name = _('Diffusion')
verbose_name_plural = _('Diffusions')
class Log (models.Model):
"""
Log a played sound start and stop, or a single message
"""
source = models.CharField(
_('source'),
max_length = 64,
help_text = 'source information',
blank = True, null = True,
)
diffusion = models.ForeignKey(
'Diffusion',
help_text = _('related diffusion'),
blank = True, null = True,
)
sound = models.ForeignKey(
'Sound',
help_text = _('played sound'),
blank = True, null = True,
)
date = models.DateTimeField(
'date',
)
comment = models.CharField(
max_length = 512,
blank = True, null = True,
)
def print (self):
print(str(self), ':', self.comment or '')
if self.diffusion:
print(' - diffusion #' + str(self.diffusion.id))
if self.sound:
print(' - sound #' + str(self.sound.id), self.sound.path)
def __str__ (self):
return self.date.strftime('%Y-%m-%d %H:%M') + ', ' + self.source