initial work on comparison

This commit is contained in:
Tobias Brunner 2025-05-27 17:07:55 +02:00
parent 06b4cba4bc
commit 4cffe5a9e3
No known key found for this signature in database
6 changed files with 358 additions and 2 deletions

View file

@ -1,8 +1,10 @@
from django.shortcuts import render
import re
from django.shortcuts import render
from collections import defaultdict
from hub.services.models import ComputePlan, VSHNAppCatPrice
from hub.services.models import ComputePlan, VSHNAppCatPrice, ExternalPricePlans
from django.contrib.admin.views.decorators import staff_member_required
from django.db import models
def natural_sort_key(name):
@ -11,11 +13,32 @@ def natural_sort_key(name):
return int(match.group(1)) if match else 0
def get_external_price_comparisons(plan, appcat_price, currency, service_level):
"""Get external price comparisons for a specific compute plan and service"""
try:
# Filter by service level if external price has one set
external_prices = ExternalPricePlans.objects.filter(
compare_to=plan, service=appcat_price.service, currency=currency
).select_related("cloud_provider")
# Filter by service level if the external price has it configured
if service_level:
external_prices = external_prices.filter(
models.Q(service_level=service_level)
| models.Q(service_level__isnull=True)
)
return external_prices
except Exception:
return []
@staff_member_required
def pricelist(request):
"""Generate comprehensive price list grouped by compute plan groups and service levels"""
# Get filter parameters from request
show_discount_details = request.GET.get("discount_details", "").lower() == "true"
show_price_comparison = request.GET.get("price_comparison", "").lower() == "true"
filter_cloud_provider = request.GET.get("cloud_provider", "")
filter_service = request.GET.get("service", "")
filter_compute_plan_group = request.GET.get("compute_plan_group", "")
@ -179,6 +202,38 @@ def pricelist(request):
service_level
]
# Get external price comparisons if enabled
external_comparisons = []
if show_price_comparison:
external_prices = get_external_price_comparisons(
plan, appcat_price, currency, service_level
)
for ext_price in external_prices:
price_difference = float(ext_price.amount) - float(
final_price
)
price_ratio = (
float(ext_price.amount) / float(final_price)
if final_price > 0
else None
)
external_comparisons.append(
{
"provider": ext_price.cloud_provider.name,
"plan_name": ext_price.plan_name,
"amount": ext_price.amount,
"difference": price_difference,
"ratio": price_ratio,
"description": ext_price.description,
"vcpus": ext_price.vcpus,
"ram": ext_price.ram,
"storage": ext_price.storage,
"replicas": ext_price.replicas,
}
)
group_name = plan.group.name if plan.group else "No Group"
# Add pricing data to the grouped structure
@ -230,6 +285,7 @@ def pricelist(request):
appcat_price.discount_model
and appcat_price.discount_model.active
),
"external_comparisons": external_comparisons,
}
)
@ -278,6 +334,7 @@ def pricelist(request):
context = {
"pricing_data_by_group_and_service_level": final_context_data,
"show_discount_details": show_discount_details,
"show_price_comparison": show_price_comparison,
"filter_cloud_provider": filter_cloud_provider,
"filter_service": filter_service,
"filter_compute_plan_group": filter_compute_plan_group,