From 57a292a9f3bd131e777a294af5d51697f817631c Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Tue, 28 May 2024 01:41:42 +0300 Subject: [PATCH] revert data api and fix repeating availabilities --- pages/api/index.ts | 29 +++++++++++++++++++++++++---- pages/api/shiftgenerate.ts | 28 +++++++++++++++++++++------- src/helpers/common.js | 3 +++ 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/pages/api/index.ts b/pages/api/index.ts index b3c44c3..9e7c3a3 100644 --- a/pages/api/index.ts +++ b/pages/api/index.ts @@ -6,7 +6,7 @@ import { DayOfWeek, AvailabilityType, UserRole, EventLogType } from '@prisma/cli const common = require('../../src/helpers/common'); const dataHelper = require('../../src/helpers/data'); const subq = require('../../prisma/bl/subqueries'); -import { addMinutes } from 'date-fns'; +import { set, format, addMinutes, addDays } from 'date-fns'; import fs from 'fs'; import path from 'path'; @@ -147,9 +147,30 @@ export default async function handler(req, res) { res.status(200).json(events); case "getPublisherInfo": - //let pubs = await filterPublishers("id,firstName,lastName,email".split(","), "", null, req.query.assignments || true, req.query.availabilities || true, false, req.query.id); - let pubs = await dataHelper.filterPublishersNew("id,firstName,lastName,email,isActive,assignments,availabilities", day, false, true, false, true, false, req.query.id); - res.status(200).json(pubs[0] || {}); + let pubs = await filterPublishers("id,firstName,lastName,email".split(","), "", null, req.query.assignments || true, req.query.availabilities || true, false, req.query.id); + let pub = pubs[0] || {}; + if (pub) { + let dayOfWeekQuery = common.getDayOfWeek(day); + + pub.availabilities = pub.availabilities.map(avail => { + if (avail.dayOfMonth == null) { + let dayOfWeek = common.getDayOfWeek(avail.startTime); + let newStart = new Date(day); + newStart = addDays(newStart, dayOfWeek - dayOfWeekQuery); + newStart.setHours(avail.startTime.getHours(), avail.startTime.getMinutes(), 0, 0); + let newEnd = new Date(newStart); + newEnd.setHours(avail.endTime.getHours(), avail.endTime.getMinutes(), 0, 0); + return { + ...avail, + startTime: newStart, + endTime: newEnd + } + } + return avail; + }); + } + //let pubs = await dataHelper.filterPublishersNew("id,firstName,lastName,email,isActive,assignments,availabilities", day, false, true, false, true, false, req.query.id); + res.status(200).json(pub); break; case "filterPublishersNew": let includeOldAvailabilities = common.parseBool(req.query.includeOldAvailabilities); diff --git a/pages/api/shiftgenerate.ts b/pages/api/shiftgenerate.ts index dc55004..bf694b8 100644 --- a/pages/api/shiftgenerate.ts +++ b/pages/api/shiftgenerate.ts @@ -956,13 +956,15 @@ async function RankPublishersForShiftWeighted(publishers, scheduledPubsPerDayAnd p.desiredCompletion = p.currentMonthAssignments / p.desiredShiftsPerMonth; }); - const calculateScore = (p) => { + const calculateScoreAndPenalties = (p) => { let score = (p.isMale ? weights.gender : 0) - (p.desiredCompletion * weights.desiredCompletion) + ((1 - p.currentMonthAvailabilityHoursCount / 24) * weights.availability) + (p.currentMonthAssignments * weights.lastMonthCompletion) - (p.currentMonthAssignments * weights.currentAssignments); + let penalties = []; + // Apply penalties based on proximity to current day for (let i = 1; i <= 6; i++) { const previousDayKey = common.getISODateOnly(addDays(currentDay, -i)); @@ -972,21 +974,33 @@ async function RankPublishersForShiftWeighted(publishers, scheduledPubsPerDayAnd if (flattenRegistry(previousDayKey).includes(p.id)) { score *= penalty; + penalties.push({ day: previousDayKey, penalty }); } if (flattenRegistry(nextDayKey).includes(p.id)) { score *= penalty; + penalties.push({ day: nextDayKey, penalty }); } } - return score; + return { score, penalties }; }; - let ranked = publishers.sort((a, b) => { - const scoreA = calculateScore(a); - const scoreB = calculateScore(b); - - return scoreB - scoreA; // Sort descending by score + // Calculate scores and penalties for each publisher + publishers.forEach(p => { + const result = calculateScoreAndPenalties(p); + p.score = result.score; + p.penalties = result.penalties; }); + // Sort publishers based on score + let ranked = publishers.sort((a, b) => b.score - a.score); + + // Log the scores and penalties of the top publisher + if (ranked.length > 0) { + console.log(`Top Publisher: ${ranked[0].name}`); + console.log(`Score: ${ranked[0].score}`); + console.log(`Penalties:`, ranked[0].penalties); + } + return ranked; } diff --git a/src/helpers/common.js b/src/helpers/common.js index dce0164..4675c0b 100644 --- a/src/helpers/common.js +++ b/src/helpers/common.js @@ -110,6 +110,9 @@ Date.prototype.getDayEuropean = function () { return (day === 0) ? 6 : day - 1; // Convert 0 (Sunday) to 6, and decrement other days by 1 }; +exports.getDayOfWeek = function (date) { + return date.getDayEuropean(); +}; // Helper function to convert month name to 0-based index exports.getMonthNames = function () { return ["януари", "февруари", "март", "април", "май", "юни", "юли", "август", "септември", "октомври", "ноември", "декември"];