mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 22:12:22 +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.db.models import Count, Q
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
from django.db.utils import OperationalError
|
||||||
|
|
||||||
from prometheus_client import generate_latest, Gauge
|
from prometheus_client import generate_latest, Gauge
|
||||||
|
|
||||||
from queues.models import Queue, PlaylistSong
|
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
|
# Export the amount of different users in the queue
|
||||||
queue_users_gauge = Gauge('marietje_queue_users', 'Users holding a queue at some point', ['name'])
|
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_duration():
|
||||||
def _get_queue_length():
|
playlist_songs = (PlaylistSong.objects.filter(playlist=queue.playlist_id, state=0)
|
||||||
return (PlaylistSong.objects.filter(playlist=queue.playlist_id, state=0)
|
.select_related('song')
|
||||||
.count())
|
.all())
|
||||||
|
return sum(ps.song.duration for ps in playlist_songs)
|
||||||
|
|
||||||
def _get_queue_duration():
|
def _get_queue_distinct_users():
|
||||||
playlist_songs = (PlaylistSong.objects.filter(playlist=queue.playlist_id, state=0)
|
# Ideally, we would use .distinct() here, but it is not supported on
|
||||||
.select_related('song')
|
# sqlite3. This makes testing a bit tedious. Also, it does not give me
|
||||||
.all())
|
# that much of confidence that it would also work on mysql.
|
||||||
return sum(ps.song.duration for ps in playlist_songs)
|
# 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():
|
queue_length_gauge.labels(name=queue).set_function(_get_queue_length)
|
||||||
# Ideally, we would use .distinct() here, but it is not supported on
|
queue_duration_gauge.labels(name=queue).set_function(_get_queue_duration)
|
||||||
# sqlite3. This makes testing a bit tedious. Also, it does not give me
|
queue_users_gauge.labels(name=queue).set_function(_get_queue_distinct_users)
|
||||||
# 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)
|
except OperationalError:
|
||||||
queue_duration_gauge.labels(name=queue).set_function(_get_queue_duration)
|
pass
|
||||||
queue_users_gauge.labels(name=queue).set_function(_get_queue_distinct_users)
|
|
||||||
|
|
||||||
|
|
||||||
def metrics(request):
|
def metrics(request):
|
||||||
|
|||||||
Reference in New Issue
Block a user