function splitIntervalByDays(startTimestamp, endTimestamp) { const intervals = []; let currentStart = new Date(startTimestamp); const end = new Date(endTimestamp); while (currentStart < end) { const currentEnd = new Date(currentStart); currentEnd.setUTCHours(23, 59, 59, 999); // Set to end of the UTC day // If currentEnd exceeds the actual end, adjust to match the actual end timestamp if (currentEnd > end) { currentEnd.setTime(end.getTime()); } // Add the interval to the list intervals.push({ startTimestamp: currentStart.getTime(), endTimestamp: currentEnd.getTime() }); // Move to the start of the next day currentStart = new Date(currentEnd); currentStart.setUTCHours(0, 0, 0, 0); currentStart.setUTCDate(currentStart.getUTCDate() + 1); } return intervals; } function processCallRecords(callRecords) { const result = []; callRecords.forEach(record => { const { customerId, callId, startTimestamp, endTimestamp } = record; const intervals = splitIntervalByDays(startTimestamp, endTimestamp); // Create new records for each interval and add to result intervals.forEach(interval => { result.push({ customerId, callId, startTimestamp: interval.startTimestamp, endTimestamp: interval.endTimestamp }); }); }); return result; } function findMaxConcurrentIntervalsByDay(processedRecords) { const customerDailyIntervals = {}; // Group intervals by customerId and date processedRecords.forEach(record => { const { customerId, callId, startTimestamp, endTimestamp } = record; const startDate = new Date(startTimestamp).toISOString().split('T')[0]; if (!customerDailyIntervals[customerId]) { customerDailyIntervals[customerId] = {}; } if (!customerDailyIntervals[customerId][startDate]) { customerDailyIntervals[customerId][startDate] = []; } customerDailyIntervals[customerId][startDate].push({ start: startTimestamp, end: endTimestamp, callId }); }); const maxConcurrentPerCustomerPerDay = {}; // Calculate max concurrent intervals for each customer on each day Object.keys(customerDailyIntervals).forEach(customerId => { maxConcurrentPerCustomerPerDay[customerId] = {}; Object.keys(customerDailyIntervals[customerId]).forEach(date => { const intervals = customerDailyIntervals[customerId][date]; const events = []; intervals.forEach(interval => { events.push({ time: interval.start, type: 'start', callId: interval.callId }); events.push({ time: interval.end, type: 'end', callId: interval.callId }); }); // Sort events by time, with 'end' events before 'start' events if times are equal events.sort((a, b) => a.time - b.time || (a.type === 'end' ? -1 : 1)); let maxConcurrent = 0; let currentConcurrent = 0; const currentCalls = new Set(); const maxConcurrentCalls = new Set(); events.forEach(event => { if (event.type === 'start') { currentConcurrent++; currentCalls.add(event.callId); if (currentConcurrent > maxConcurrent) { maxConcurrent = currentConcurrent; maxConcurrentCalls.clear(); currentCalls.forEach(callId => maxConcurrentCalls.add(callId)); } else if (currentConcurrent === maxConcurrent) { currentCalls.forEach(callId => maxConcurrentCalls.add(callId)); } } else { currentConcurrent--; currentCalls.delete(event.callId); } }); maxConcurrentPerCustomerPerDay[customerId][date] = { maxConcurrent, callIds: Array.from(maxConcurrentCalls) }; }); }); return maxConcurrentPerCustomerPerDay; } // Example call records const callRecords = [ { customerId: 123, callId: 'Jan1st_11:30pm_to_Jan1st_11:40pm_Call', startTimestamp: 1704151800000, endTimestamp: 1704152400000 }, { customerId: 123, callId: 'Jan2nd_11:50pm_to_Jan3rd_12:20am_Call', startTimestamp: 1704239400000, endTimestamp: 1704241200000 }, { customerId: 123, callId: 'Jan3rd_12:10am_to_Jan3rd_1:00am_Call', startTimestamp: 1704240600000, endTimestamp: 1704243600000 }, { customerId: 123, callId: 'Jan4th_11:00pm_to_Jan5th_12:00am_Call', startTimestamp: 1704409200000, endTimestamp: 1704412800000 }, { customerId: 123, callId: 'Jan3rd_11:00pm_to_Jan5th_1:00am_Call', startTimestamp: 1704238800000, endTimestamp: 1704325200000 }, { customerId: 123, callId: 'Dec31st_11:00pm_to_Jan2nd_1:00am_Call', startTimestamp: 1704075600000, endTimestamp: 1704237600000 }, { customerId: 123, callId: 'Feb28th_11:00pm_to_Mar1st_1:00am_Call', startTimestamp: 1706838000000, endTimestamp: 1706924400000 }, { customerId: 123, callId: 'Dec30th_11:00pm_to_Jan2nd_1:00am_Call', startTimestamp: 1703992800000, endTimestamp: 1704237600000 }, { customerId: 123, callId: 'Dec30th_11:00pm_to_Feb1st_1:00am_Call', startTimestamp: 1703992800000, endTimestamp: 1706816400000 }, { customerId: 456, callId: 'Mar1st_10:00am_to_Mar1st_11:00am_Call', startTimestamp: 1706925600000, endTimestamp: 1706929200000 }, { customerId: 456, callId: 'Mar1st_10:30am_to_Mar1st_11:30am_Call', startTimestamp: 1706927400000, endTimestamp: 1706931000000 }, { customerId: 456, callId: 'Mar1st_11:00am_to_Mar1st_12:00pm_Call', startTimestamp: 1706929200000, endTimestamp: 1706932800000 } ]; const processedRecords = processCallRecords(callRecords); const maxConcurrentIntervalsByDay = findMaxConcurrentIntervalsByDay(processedRecords); console.log(maxConcurrentIntervalsByDay);
Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json
.
Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.
Google chrome's javascript engine V8
and is pretty fast.Asynchronous
, event-driven
and works on single-thread model
thus eliminating the dis-advantages of multi-thread model.Express is one of the most popular web application framework in the NodeJS echosystem.
let moment = require('moment');
console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));
const _ = require("lodash");
let colors = ['blue', 'green', 'yellow', 'red'];
let firstElement = _.first(colors);
let lastElement = _.last(colors);
console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);
Following are the libraries supported by OneCompiler's NodeJS compiler.