refactor price calculator into multiple files
This commit is contained in:
parent
33e8f2152a
commit
67e1b4cab1
8 changed files with 1324 additions and 1877 deletions
104
hub/services/static/js/price-calculator/plan-manager.js
Normal file
104
hub/services/static/js/price-calculator/plan-manager.js
Normal file
|
@ -0,0 +1,104 @@
|
|||
/**
|
||||
* Plan Manager - Handles plan selection and matching logic
|
||||
*/
|
||||
class PlanManager {
|
||||
constructor(pricingDataManager) {
|
||||
this.pricingDataManager = pricingDataManager;
|
||||
}
|
||||
|
||||
// Find best matching plan based on requirements
|
||||
findBestMatchingPlan(cpus, memory, serviceLevel) {
|
||||
const pricingData = this.pricingDataManager.getPricingData();
|
||||
|
||||
if (!pricingData) return null;
|
||||
|
||||
let bestMatch = null;
|
||||
let bestScore = Infinity;
|
||||
|
||||
// Iterate through all groups and service levels
|
||||
Object.keys(pricingData).forEach(groupName => {
|
||||
const group = pricingData[groupName];
|
||||
|
||||
if (group[serviceLevel]) {
|
||||
group[serviceLevel].forEach(plan => {
|
||||
const planCpus = parseFloat(plan.vcpus);
|
||||
const planMemory = parseFloat(plan.ram);
|
||||
|
||||
// Check if plan meets minimum requirements
|
||||
if (planCpus >= cpus && planMemory >= memory) {
|
||||
// Calculate efficiency score (lower is better)
|
||||
const cpuOverhead = planCpus - cpus;
|
||||
const memoryOverhead = planMemory - memory;
|
||||
const score = cpuOverhead + memoryOverhead + plan.final_price * 0.1;
|
||||
|
||||
if (score < bestScore) {
|
||||
bestScore = score;
|
||||
bestMatch = {
|
||||
...plan,
|
||||
groupName: groupName
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return bestMatch;
|
||||
}
|
||||
|
||||
// Get current plan based on configuration
|
||||
getCurrentPlan(domManager) {
|
||||
const config = domManager.getCurrentConfiguration();
|
||||
const planSelect = domManager.get('planSelect');
|
||||
|
||||
if (planSelect?.value) {
|
||||
return JSON.parse(planSelect.value);
|
||||
}
|
||||
|
||||
return this.findBestMatchingPlan(config.cpus, config.memory, config.serviceLevel);
|
||||
}
|
||||
|
||||
// Populate plan dropdown based on selected service level
|
||||
populatePlanDropdown(domManager) {
|
||||
const planSelect = domManager.get('planSelect');
|
||||
if (!planSelect) return;
|
||||
|
||||
const serviceLevel = domManager.getSelectedServiceLevel();
|
||||
if (!serviceLevel) return;
|
||||
|
||||
// Clear existing options
|
||||
planSelect.innerHTML = '<option value="">Auto-select best matching plan</option>';
|
||||
|
||||
// Get plans for the selected service level
|
||||
const availablePlans = this.pricingDataManager.getPlansForServiceLevel(serviceLevel);
|
||||
|
||||
// Add plans to dropdown
|
||||
availablePlans.forEach(plan => {
|
||||
const option = document.createElement('option');
|
||||
option.value = JSON.stringify(plan);
|
||||
option.textContent = `${plan.compute_plan} - ${plan.vcpus} vCPUs, ${plan.ram} GB RAM`;
|
||||
planSelect.appendChild(option);
|
||||
});
|
||||
}
|
||||
|
||||
// Update sliders to match selected plan
|
||||
updateSlidersForPlan(plan, domManager) {
|
||||
const cpuRange = domManager.get('cpuRange');
|
||||
const memoryRange = domManager.get('memoryRange');
|
||||
const cpuValue = domManager.get('cpuValue');
|
||||
const memoryValue = domManager.get('memoryValue');
|
||||
|
||||
if (cpuRange && cpuValue) {
|
||||
cpuRange.value = plan.vcpus;
|
||||
cpuValue.textContent = plan.vcpus;
|
||||
}
|
||||
|
||||
if (memoryRange && memoryValue) {
|
||||
memoryRange.value = plan.ram;
|
||||
memoryValue.textContent = plan.ram;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Export for use in other modules
|
||||
window.PlanManager = PlanManager;
|
Loading…
Add table
Add a link
Reference in a new issue