minecraft_manager/overview.py

66 lines
2.4 KiB
Python
Raw Normal View History

import os
import json
2018-12-06 04:38:36 +00:00
from datetime import datetime, timedelta
from django.conf import settings
from minecraft_manager.models import Application, Player, Ticket, Warning, IP
def overview_data():
data = {}
# Setup
with open(os.path.join(settings.MINECRAFT_BASE_DIR, 'banned-players.json'), encoding='utf-8') as f:
bans = json.load(f)
# Totals
data['total'] = {
'application': {
'accepted': Application.objects.filter(accepted=True).count(),
'denied': Application.objects.filter(accepted=False).count(),
'all': Application.objects.count()
},
'player': {
'banned': len(bans),
'unbanned': Player.objects.count() - len(bans),
'all': Player.objects.count()
},
'ticket': {
'claimed': Ticket.objects.filter(staff__isnull=False).count(),
'unclaimed': Ticket.objects.filter(staff__isnull=True).count(),
'resolved': Ticket.objects.filter(resolved=True).count(),
'unresolved': Ticket.objects.filter(resolved=False).count(),
'all': Ticket.objects.count()
},
'warning': Warning.objects.count(),
'ip': IP.objects.count()
}
# Averages
data['average'] = {
2018-12-05 23:06:26 +00:00
'age': round(sum([application.age for application in Application.objects.all()]) /
data['total']['application']['all'] if data['total']['application']['all'] != 0 else 1, 2)
}
2018-12-06 04:38:36 +00:00
# Percentage
data['percentage'] = {
2018-12-05 23:05:32 +00:00
'accepted': round((data['total']['application']['accepted'] /
2018-12-06 04:38:36 +00:00
data['total']['application']['all'] if data['total']['application']['all'] != 0 else 1) * 100, 2),
2018-12-05 23:05:32 +00:00
'banned': round((data['total']['player']['banned'] /
2018-12-06 04:38:36 +00:00
data['total']['player']['all'] if data['total']['player']['all'] != 0 else 1) * 100, 2),
'applied': round((data['total']['application']['all'] / data['total']['player']['all']) * 100, 2)
}
# Unique logins
now = datetime.now()
day = now - timedelta(days=1)
week = now - timedelta(weeks=1)
month = now - timedelta(weeks=4)
data['unique'] = {
'day': Player.objects.filter(last_seen__range=[day, now]).count(),
'week': Player.objects.filter(last_seen__range=[week, now]).count(),
'month': Player.objects.filter(last_seen__range=[month, now]).count()
}
return data