mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 21:12:23 +01:00
metrics: Do not fail when tables not migrated
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
from django.db.models import Count, Q
|
||||
from django.http import HttpResponse
|
||||
from django.db.utils import OperationalError
|
||||
|
||||
from prometheus_client import generate_latest, Gauge
|
||||
|
||||
from queues.models import Queue, PlaylistSong
|
||||
@ -11,29 +13,32 @@ queue_duration_gauge = Gauge('marietje_queue_duration', 'Marietje queue duration
|
||||
# Export the amount of different users in the queue
|
||||
queue_users_gauge = Gauge('marietje_queue_users', 'Users holding a queue at some point', ['name'])
|
||||
|
||||
try:
|
||||
for queue in Queue.objects.all():
|
||||
def _get_queue_length():
|
||||
return (PlaylistSong.objects.filter(playlist=queue.playlist_id, state=0)
|
||||
.count())
|
||||
|
||||
for queue in Queue.objects.all():
|
||||
def _get_queue_length():
|
||||
return (PlaylistSong.objects.filter(playlist=queue.playlist_id, state=0)
|
||||
.count())
|
||||
def _get_queue_duration():
|
||||
playlist_songs = (PlaylistSong.objects.filter(playlist=queue.playlist_id, state=0)
|
||||
.select_related('song')
|
||||
.all())
|
||||
return sum(ps.song.duration for ps in playlist_songs)
|
||||
|
||||
def _get_queue_duration():
|
||||
playlist_songs = (PlaylistSong.objects.filter(playlist=queue.playlist_id, state=0)
|
||||
.select_related('song')
|
||||
.all())
|
||||
return sum(ps.song.duration for ps in playlist_songs)
|
||||
def _get_queue_distinct_users():
|
||||
# Ideally, we would use .distinct() here, but it is not supported on
|
||||
# sqlite3. This makes testing a bit tedious. Also, it does not give me
|
||||
# that much of confidence that it would also work on mysql.
|
||||
# Furtermore, we do not expect the queue to be very long at any moment.
|
||||
return len(set(PlaylistSong.objects.filter(playlist=queue.playlist_id, state=0)
|
||||
.values_list('user')))
|
||||
|
||||
def _get_queue_distinct_users():
|
||||
# Ideally, we would use .distinct() here, but it is not supported on
|
||||
# sqlite3. This makes testing a bit tedious. Also, it does not give me
|
||||
# that much of confidence that it would also work on mysql.
|
||||
# Furtermore, we do not expect the queue to be very long at any moment.
|
||||
return len(set(PlaylistSong.objects.filter(playlist=queue.playlist_id, state=0)
|
||||
.values_list('user')))
|
||||
queue_length_gauge.labels(name=queue).set_function(_get_queue_length)
|
||||
queue_duration_gauge.labels(name=queue).set_function(_get_queue_duration)
|
||||
queue_users_gauge.labels(name=queue).set_function(_get_queue_distinct_users)
|
||||
|
||||
queue_length_gauge.labels(name=queue).set_function(_get_queue_length)
|
||||
queue_duration_gauge.labels(name=queue).set_function(_get_queue_duration)
|
||||
queue_users_gauge.labels(name=queue).set_function(_get_queue_distinct_users)
|
||||
except OperationalError:
|
||||
pass
|
||||
|
||||
|
||||
def metrics(request):
|
||||
|
||||
Reference in New Issue
Block a user