Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chizkiyahu committed Mar 30, 2024
1 parent 8265453 commit b2d7eb0
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 36 deletions.
24 changes: 12 additions & 12 deletions docs/plans.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,84 @@
{
"company_name": "אלקטרה פאוור",
"plan_description": "מסלול לילה",
"days_of_week": [1, 2, 3, 4, 5, 6, 7],
"days_of_week": [0, 1, 2, 3, 4, 5, 6],
"applicable_hours": [23, 0, 1, 2, 3, 4, 5, 6],
"discount": [20]
},
{
"company_name": "סלקום אנרג'י",
"plan_description": "חוסכים בלילה",
"days_of_week": [1, 2, 3, 4, 5],
"days_of_week": [0, 1, 2, 3, 4],
"applicable_hours": [22, 23, 0, 1, 2, 3, 4, 5, 6],
"discount": [20]
},
{
"company_name": "פרטנר חשמל",
"plan_description": "חיות לילה",
"days_of_week": [1, 2, 3, 4, 5],
"days_of_week": [0, 1, 2, 3, 4],
"applicable_hours": [0, 1, 2, 3, 4, 5],
"discount": [20]
},
{
"company_name": "סלקום אנרג'י",
"plan_description": "עובדים מהבית",
"days_of_week": [1, 2, 3, 4, 5],
"days_of_week": [0, 1, 2, 3, 4],
"applicable_hours": [7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
"discount": [15]
},
{
"company_name": "פרטנר חשמל",
"plan_description": "עובדים מהבית",
"days_of_week": [1, 2, 3, 4, 5],
"days_of_week": [0, 1, 2, 3, 4],
"applicable_hours": [8, 9, 10, 11, 12, 13, 14, 15, 16],
"discount": [15]
},
{
"company_name": "פזגז",
"plan_description": "הנחת סופ\"ש",
"days_of_week": [5, 6, 7],
"days_of_week": [5, 6],
"applicable_hours": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
"discount": [10]
},
{
"company_name": "אלקטרה פאוור",
"plan_description": "מסלול הייטק",
"days_of_week": [1, 2, 3, 4, 5, 6, 7],
"days_of_week": [0, 1, 2, 3, 4, 5, 6],
"applicable_hours": [23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
"discount": [8, 9, 10]
},
{
"company_name": "פזגז",
"plan_description": "הנחה 24/7",
"days_of_week": [1, 2, 3, 4, 5, 6, 7],
"days_of_week": [0, 1, 2, 3, 4, 5, 6],
"applicable_hours": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
"discount": [7]
},
{
"company_name": "אמישראגז חשמל",
"plan_description": "חיסכון קבוע",
"days_of_week": [1, 2, 3, 4, 5, 6, 7],
"days_of_week": [0, 1, 2, 3, 4, 5, 6],
"applicable_hours": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
"discount": [6.5]
},
{
"company_name": "אלקטרה פאוור",
"plan_description": "מסלול POWER",
"days_of_week": [1, 2, 3, 4, 5, 6, 7],
"days_of_week": [0, 1, 2, 3, 4, 5, 6],
"applicable_hours": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
"discount": [5, 6, 7]
},
{
"company_name": "סלקום אנרג'י",
"plan_description": "חוסכים קבוע",
"days_of_week": [1, 2, 3, 4, 5, 6, 7],
"days_of_week": [0, 1, 2, 3, 4, 5, 6],
"applicable_hours": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
"discount": [5, 6, 7]
},
{
"company_name": "פרטנר חשמל",
"plan_description": "הנחה קבועה כל היום",
"days_of_week": [1, 2, 3, 4, 5, 6, 7],
"days_of_week": [0, 1, 2, 3, 4, 5, 6],
"applicable_hours": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
"discount": [5, 6, 7]
}
Expand Down
111 changes: 87 additions & 24 deletions docs/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ document.querySelectorAll('#dayFilters input').forEach(input => input.addEventLi
function processData(csvData, isInitialLoad) {
const lines = csvData.split('\n').slice(12); // Adjust if header lines count changes
let hourlyData = {};
let hourlyDataPerDay = {}; // New object to store hourly data per day
let dates = [];

lines.forEach(line => {
const [date, time, value] = line.split(',').map(part => part.replace(/"/g, ''));
if (!date || !time || !value) return; // Skip incomplete lines
Expand All @@ -43,27 +43,66 @@ function processData(csvData, isInitialLoad) {
const currentDate = new Date(year, month - 1, day);
dates.push(currentDate); // Collect all dates for initial load processing

// Filter application
const startDateFilter = document.getElementById('startDate').value;
const endDateFilter = document.getElementById('endDate').value;
let startDateFilter = document.getElementById('startDate').valueAsDate;
if (startDateFilter) {
startDateFilter.setHours(0, 0, 0, 0);
}

let endDateFilter = document.getElementById('endDate').valueAsDate;
if (endDateFilter) {
endDateFilter.setHours(23, 59, 59, 999);
}

const dayFilters = [...document.querySelectorAll('#dayFilters input[type="checkbox"]:checked')].map(el => parseInt(el.value));

if ((startDateFilter && currentDate < new Date(startDateFilter)) || (endDateFilter && currentDate > new Date(endDateFilter)) || !dayFilters.includes(currentDate.getDay())) {
return; // Skip this entry if it doesn't match the filters


// Reset current date to start of the day


if (startDateFilter && currentDate < startDateFilter) {
return;
}

if (endDateFilter && currentDate > endDateFilter) {
return; // Skip this entry if it doesn't match the end date filter
}

if (!dayFilters.includes(currentDate.getDay())) {
return; // Skip this entry if it doesn't match the selected days filter
}



const hour = time.split(':')[0];
const dayOfWeek = currentDate.getDay(); // Get day of the week

hourlyData[hour] = (hourlyData[hour] || 0) + parseFloat(value);

// Initialize hourlyDataPerDay for the current day if not present
if (!hourlyDataPerDay[dayOfWeek]) {
hourlyDataPerDay[dayOfWeek] = {};
}

// Store hourly data for the current day
hourlyDataPerDay[dayOfWeek][hour] = (hourlyDataPerDay[dayOfWeek][hour] || 0) + parseFloat(value);
});

// Set start and end dates from data on initial load
if (isInitialLoad && dates.length > 0) {
dates.sort((a, b) => a - b); // Sort dates
document.getElementById('startDate').valueAsDate = dates[0];
document.getElementById('endDate').valueAsDate = dates[dates.length - 1];
const startDate = new Date(dates[0]);
startDate.setUTCHours(0, 0, 0, 0); // Set hours, minutes, seconds, and milliseconds to 0 in UTC
// Adjust start date based on local time zone offset
startDate.setDate(startDate.getDate() + 1);
document.getElementById('startDate').valueAsDate = startDate;

const lastDate = dates[dates.length - 1];
document.getElementById('endDate').valueAsDate = new Date(lastDate.getFullYear(), lastDate.getMonth(), lastDate.getDate(), 23, 59, 59, 999);
}

displayGraph(hourlyData);
calculateBestPlan(hourlyData);
calculateBestPlan(hourlyDataPerDay); // Pass hourlyDataPerDay to calculateBestPlan
}

function displayGraph(hourlyData) {
Expand Down Expand Up @@ -115,31 +154,56 @@ function displayGraph(hourlyData) {
});
chart.render();
}
let cachedPlans = null; // Cached plans

async function fetchPlans() {
const response = await fetch('plans.json');
return await response.json();
if (cachedPlans === null) {
const response = await fetch('plans.json');
cachedPlans = await response.json();
}
return cachedPlans;
}
window.onload = async function() {
try {
await fetchPlans();
} catch (error) {
console.error("Error fetching plans:", error);
}
};

async function calculateBestPlan(hourlyData) {
async function calculateBestPlan(hourlyDataPerDay) {
const plans = await fetchPlans(); // Fetch plans from server

let totalKwhFreePerPlan = plans.map(plan => ({...plan, totalKwhFree: 0}));

Object.entries(hourlyData).forEach(([hour, kwh]) => {
const hourInt = parseInt(hour); // Ensure hour is an integer

totalKwhFreePerPlan.forEach(plan => {
plan.applicable_hours.forEach(planHour => {
if (hourInt === planHour) {
plan.totalKwhFree += kwh * (plan.discount[0] / 100); // Assuming single discount for simplicity
}
// Iterate over each day of the week
for (let dayOfWeek in hourlyDataPerDay) {
if (hourlyDataPerDay.hasOwnProperty(dayOfWeek)) {
// Convert dayOfWeek to a number for comparison
const dayOfWeekInt = parseInt(dayOfWeek);

// Iterate over hourly data for the current day
Object.entries(hourlyDataPerDay[dayOfWeek]).forEach(([hour, kwh]) => {
const hourInt = parseInt(hour); // Ensure hour is an integer

// Update totalKwhFreePerPlan only if the plan's day of the week matches the current day
totalKwhFreePerPlan.forEach(plan => {
if (plan.days_of_week.includes(dayOfWeekInt)) {
plan.applicable_hours.forEach(planHour => {
if (hourInt === planHour) {
plan.totalKwhFree += kwh * (plan.discount[0] / 100); // Assuming single discount for simplicity
}
});
}
});
});
});
});
}
}

displayPlanResults(totalKwhFreePerPlan);
}


function displayPlanResults(totalKwhFreePerPlan) {
// Sort plans by the highest savings
totalKwhFreePerPlan.sort((a, b) => b.totalKwhFree - a.totalKwhFree);
Expand Down Expand Up @@ -220,8 +284,7 @@ function formatDaysOfWeek(days) {
if (days.length === 7) {
return 'All';
} else {
// Adjust for one-based indexing of days
return days.map(day => dayNames[(day - 1) % 7]).join(', ');
return days.map(day => dayNames[day % 7]).join(', ');
}
}

Expand Down

0 comments on commit b2d7eb0

Please sign in to comment.