Add disk usage statistics
- add model CustomerPackageDiskUsage for hosting package disk usage statistics - add REST API endpoint for submittings statistics for disk, mysql and pgsql usage - add disk usage information to hosting package detail view - add separate hosting package disk usage statistic view
This commit is contained in:
parent
affb49a971
commit
cb62bd63e2
10 changed files with 379 additions and 16 deletions
|
@ -4,6 +4,9 @@ This module defines views related to hosting packages.
|
|||
"""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import http
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import get_user_model
|
||||
|
@ -13,6 +16,12 @@ from django.shortcuts import get_object_or_404, redirect
|
|||
from django.utils.translation import gettext as _
|
||||
from django.views.generic import DetailView, ListView
|
||||
from django.views.generic.edit import CreateView, FormView
|
||||
|
||||
import rest_framework.request
|
||||
from rest_framework.permissions import BasePermission
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
|
||||
|
||||
from .forms import (
|
||||
|
@ -24,10 +33,14 @@ from .forms import (
|
|||
)
|
||||
from .models import (
|
||||
CustomerHostingPackage,
|
||||
CustomerPackageDiskUsage,
|
||||
DiskSpaceOption,
|
||||
MailboxOption,
|
||||
UserDatabaseOption,
|
||||
)
|
||||
from .serializers import DiskUsageSerializer
|
||||
|
||||
logger = logging.getLogger("gnuviechadmin.hostingpackages")
|
||||
|
||||
|
||||
class CreateHostingPackage(PermissionRequiredMixin, CreateView):
|
||||
|
@ -259,3 +272,85 @@ class AddHostingOption(StaffUserRequiredMixin, FormView):
|
|||
).format(option=option, package=hosting_package.name),
|
||||
)
|
||||
return redirect(hosting_package)
|
||||
|
||||
|
||||
class HasDiskUsageUploadPermission(BasePermission):
|
||||
def has_permission(self, request, view):
|
||||
return (
|
||||
request.user.has_perm("hostingpackages.add_customerpackagediskusage")
|
||||
and request.method == "POST"
|
||||
)
|
||||
|
||||
|
||||
class UploadCustomerPackageDiskUsage(APIView):
|
||||
permission_classes = [HasDiskUsageUploadPermission]
|
||||
allowed_methods = ("POST",)
|
||||
serializer = DiskUsageSerializer(many=True)
|
||||
|
||||
def post(self, request: rest_framework.request.Request, format=None):
|
||||
if request.content_type != "application/json":
|
||||
return Response("Unacceptable", status=http.HTTPStatus.BAD_REQUEST)
|
||||
for row in request.data:
|
||||
user = row["user"]
|
||||
for key in row:
|
||||
if key == "user":
|
||||
continue
|
||||
else:
|
||||
for item, size in row[key].items():
|
||||
try:
|
||||
package = CustomerHostingPackage.objects.get(
|
||||
osuser__username=user
|
||||
)
|
||||
(
|
||||
metric,
|
||||
created,
|
||||
) = CustomerPackageDiskUsage.objects.get_or_create(
|
||||
package=package,
|
||||
source=key,
|
||||
item=item,
|
||||
)
|
||||
metric.used_kb = size
|
||||
metric.save()
|
||||
except CustomerHostingPackage.DoesNotExist:
|
||||
logger.warning(
|
||||
"hosting package for user %s does not exist", user
|
||||
)
|
||||
|
||||
logger.info("usage date submitted by %s", request.user)
|
||||
|
||||
return Response("Accepted", status=http.HTTPStatus.ACCEPTED)
|
||||
|
||||
|
||||
class CustomerHostingPackageDiskUsageDetails(DetailView):
|
||||
template_name_suffix = "_disk_usage_details"
|
||||
model = CustomerHostingPackage
|
||||
pk_url_kwarg = "package"
|
||||
context_object_name = "hostingpackage"
|
||||
|
||||
def get_queryset(self, queryset=None):
|
||||
return super().get_queryset().prefetch_related("customerpackagediskusage_set")
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context_data = super().get_context_data(**kwargs)
|
||||
|
||||
disk_usage, mysql_usage, pgsql_usage = [], [], []
|
||||
|
||||
for usage in self.get_object().customerpackagediskusage_set.order_by(
|
||||
"-used_kb"
|
||||
):
|
||||
if usage.source == "disk":
|
||||
disk_usage.append(usage)
|
||||
elif usage.source == "mysql":
|
||||
mysql_usage.append(usage)
|
||||
elif usage.source == "pgsql":
|
||||
pgsql_usage.append(usage)
|
||||
|
||||
context_data.update(
|
||||
{
|
||||
"disk_usage": disk_usage,
|
||||
"mysql_usage": mysql_usage,
|
||||
"pgsql_usage": pgsql_usage,
|
||||
}
|
||||
)
|
||||
|
||||
return context_data
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue