retrieve storage price from db

This commit is contained in:
Tobias Brunner 2025-06-04 17:06:32 +02:00
parent c8c224cfb8
commit 8bb8930361
No known key found for this signature in database
2 changed files with 45 additions and 4 deletions

View file

@ -6,7 +6,7 @@
class PriceCalculator {
constructor() {
this.pricingData = null;
this.storagePrice = 0.15; // CHF per GB per month
this.storagePrice = null;
this.currentOffering = null;
this.selectedConfiguration = null;
this.init();
@ -145,6 +145,10 @@ Please contact me with next steps for ordering this configuration.`;
}
this.pricingData = await response.json();
// Extract storage price from the first available plan
this.extractStoragePrice();
this.setupEventListeners();
this.populatePlanDropdown();
this.updatePricing();
@ -154,6 +158,23 @@ Please contact me with next steps for ordering this configuration.`;
}
}
// Extract storage price from pricing data
extractStoragePrice() {
if (!this.pricingData) return;
// Find the first plan with storage pricing data
for (const groupName of Object.keys(this.pricingData)) {
const group = this.pricingData[groupName];
for (const serviceLevel of Object.keys(group)) {
const plans = group[serviceLevel];
if (plans.length > 0 && plans[0].storage_price !== undefined) {
this.storagePrice = parseFloat(plans[0].storage_price);
return;
}
}
}
}
// Setup event listeners for calculator controls
setupEventListeners() {
if (!this.cpuRange || !this.memoryRange || !this.storageRange) return;
@ -437,11 +458,14 @@ Please contact me with next steps for ordering this configuration.`;
if (this.planMemory) this.planMemory.textContent = plan.ram + ' GB';
if (this.planServiceLevel) this.planServiceLevel.textContent = serviceLevel;
// Calculate pricing
// Calculate pricing using storage price from the plan data
const computePriceValue = parseFloat(plan.compute_plan_price);
const servicePriceValue = parseFloat(plan.sla_price);
const managedServicePrice = computePriceValue + servicePriceValue;
const storagePriceValue = storage * this.storagePrice;
// Use storage price from plan data or fallback to instance variable
const storageUnitPrice = plan.storage_price !== undefined ? parseFloat(plan.storage_price) : this.storagePrice;
const storagePriceValue = storage * storageUnitPrice;
const totalPriceValue = managedServicePrice + storagePriceValue;
// Update pricing display

View file

@ -1,6 +1,5 @@
import re
import yaml
import json
from decimal import Decimal
from django.shortcuts import render, get_object_or_404
@ -14,6 +13,7 @@ from hub.services.models import (
Service,
ComputePlan,
VSHNAppCatPrice,
StoragePlan,
)
from collections import defaultdict
from markdownify import markdownify
@ -239,6 +239,22 @@ def generate_pricing_data(offering):
),
)
# Fetch storage plans for this cloud provider
storage_plans = (
StoragePlan.objects.filter(cloud_provider=offering.cloud_provider)
.prefetch_related("prices")
.order_by("name")
)
# Get default storage pricing (use first available storage plan)
storage_price_data = {}
if storage_plans.exists():
default_storage_plan = storage_plans.first()
for currency in ["CHF", "EUR", "USD"]: # Add currencies as needed
price = default_storage_plan.get_price(currency)
if price is not None:
storage_price_data[currency] = price
# Fetch pricing for this specific service
try:
appcat_price = (
@ -352,6 +368,7 @@ def generate_pricing_data(offering):
"compute_plan_price": compute_plan_price,
"sla_price": sla_price,
"final_price": final_price,
"storage_price": storage_price_data.get(currency, 0),
}
)