Remove stale disk usage stats

This commit is contained in:
Jan Dittberner 2023-07-23 11:24:10 +02:00
parent 4b74f5d04b
commit 3b6d50a62a
2 changed files with 19 additions and 4 deletions

View file

@ -1,6 +1,8 @@
Changelog Changelog
========= =========
* :bug:`-` remove stale disk usage stats older than 30 minutes
* :release:`0.15.0 <2023-07-23> * :release:`0.15.0 <2023-07-23>
* :feature:`10` add disk usage details for mail and web * :feature:`10` add disk usage details for mail and web

View file

@ -6,6 +6,7 @@ from __future__ import absolute_import
import http import http
import logging import logging
from datetime import timedelta
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
@ -13,6 +14,7 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import PermissionRequiredMixin, UserPassesTestMixin from django.contrib.auth.mixins import PermissionRequiredMixin, UserPassesTestMixin
from django.http import Http404 from django.http import Http404
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.utils import timezone
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, FormView from django.views.generic.edit import CreateView, FormView
@ -278,8 +280,8 @@ class AddHostingOption(StaffUserRequiredMixin, FormView):
class HasDiskUsageUploadPermission(BasePermission): class HasDiskUsageUploadPermission(BasePermission):
def has_permission(self, request, view): def has_permission(self, request, view):
return ( return (
request.user.has_perm("hostingpackages.add_customerpackagediskusage") request.user.has_perm("hostingpackages.add_customerpackagediskusage")
and request.method == "POST" and request.method == "POST"
) )
@ -292,12 +294,15 @@ class UploadCustomerPackageDiskUsage(APIView):
if request.content_type != "application/json": if request.content_type != "application/json":
return Response("Unacceptable", status=http.HTTPStatus.BAD_REQUEST) return Response("Unacceptable", status=http.HTTPStatus.BAD_REQUEST)
submitted_sources = set()
for row in request.data: for row in request.data:
user = row["user"] user = row["user"]
for key in row: for key in row:
if key == "user": if key == "user":
continue continue
else: else:
submitted_sources.add(key)
for item, size in row[key].items(): for item, size in row[key].items():
try: try:
package = CustomerHostingPackage.objects.get( package = CustomerHostingPackage.objects.get(
@ -315,7 +320,9 @@ class UploadCustomerPackageDiskUsage(APIView):
if key == "mail": if key == "mail":
try: try:
ma_mb = package.mailboxes.get(username=item).mailaddressmailbox_set.first() ma_mb = package.mailboxes.get(
username=item
).mailaddressmailbox_set.first()
if ma_mb: if ma_mb:
metric.email_address_id = ma_mb.mailaddress_id metric.email_address_id = ma_mb.mailaddress_id
except Mailbox.DoesNotExist: except Mailbox.DoesNotExist:
@ -327,6 +334,12 @@ class UploadCustomerPackageDiskUsage(APIView):
"hosting package for user %s does not exist", user "hosting package for user %s does not exist", user
) )
if submitted_sources:
CustomerPackageDiskUsage.objects.filter(
source__in=submitted_sources,
modified__lt=timezone.now() - timedelta(minutes=30),
).delete()
logger.info("usage data submitted by %s", request.user) logger.info("usage data submitted by %s", request.user)
return Response("Accepted", status=http.HTTPStatus.ACCEPTED) return Response("Accepted", status=http.HTTPStatus.ACCEPTED)
@ -356,7 +369,7 @@ class CustomerHostingPackageDiskUsageDetails(StaffOrSelfLoginRequiredMixin, Deta
mail_usage, web_usage, mysql_usage, pgsql_usage = [], [], [], [] mail_usage, web_usage, mysql_usage, pgsql_usage = [], [], [], []
for usage in self.get_object().customerpackagediskusage_set.order_by( for usage in self.get_object().customerpackagediskusage_set.order_by(
"-used_kb" "-used_kb"
): ):
if usage.used_kb <= 0: if usage.used_kb <= 0:
continue continue