Incremental Cost Calculation
Purpose
Within the broader scope of Real-Time Meeting Cost Tracking (79640), the Incremental Cost Calculation subtopic addresses the need to accurately compute meeting costs in discrete segments rather than as a continuous stream. This approach manages changes in attendee count and wage rates dynamically by segmenting the meeting into time increments, each with its own cost parameters.
By breaking the meeting duration into distinct increments, it becomes possible to:
Record precise cost impact for each interval where attendee or wage data remains constant.
Accurately sum partial costs to provide a running and total meeting cost.
Handle mid-meeting updates to attendees or wages without losing track of previous cost segments.
This granularity supports reliable cost tracking and forms the foundation for the live updates and persistence features handled in other subtopics like Meeting Lifecycle Management and Dynamic Cost Visualization.
Functionality
The core functionality centers on tracking discrete time intervals (increments) for which attendee count and average wage are fixed. Each increment encapsulates:
Start and stop time
Duration elapsed in seconds
Attendee count during that interval
Average wage during that interval
Calculated cost for that interval
Cumulative total cost up to that increment
Meeting purpose (optional descriptive metadata)
Key Workflows
Increment Creation
When a meeting starts or when attendee/wage data changes, the current increment is "closed" by recording its stop time and calculating cost. A new increment begins immediately after with updated parameters.Cost Calculation per Increment
The cost for each increment is computed as:cost = attendeeCount × (averageWage / SECONDS_PER_HOUR) × elapsedTimeThis formula prorates the hourly wage to a per-second rate and multiplies by the number of attendees and elapsed seconds.
Accumulation of Costs
Each new increment stores the sum of all prior increments' total costs plus its own cost, enabling efficient retrieval of total meeting cost by looking at the last increment.Dynamic Updates
Upon user changes (e.g., attendee count or wage), the system pushes the current increment to finalize it, then starts a new increment with updated values, ensuring historical accuracy.
Integration with Meeting Model
The Meeting class orchestrates the incremental calculation by:
Maintaining an array of
incrementsto record all segments.Managing the current increment’s start time, attendee count, and wage.
Providing methods to push increments (
pushIncrement) when stopping the meeting or changing parameters.Calculating total cost on-demand by summing increments and adding the cost of the current running increment if the meeting is active.
A snippet illustrating the push and calculation logic:
pushIncrement() {
if (this.currentIncrementStartTime === '') return;
const now = new Date();
const lastTotalCost = this.increments.length ? this.increments.slice(-1)[0].totalCost : 0.00;
this.increments.push(new Increment(
this.currentIncrementStartTime,
now,
this.currentIncrementAttendeeCount,
this.currentIncrementAverageWage,
lastTotalCost,
this.purpose
));
this.currentIncrementStartTime = now;
}
getTotalCost() {
const lastIncrement = this.increments.slice(-1)[0];
const totalCostSoFar = lastIncrement ? lastIncrement.totalCost : 0.00;
if (this.clockRunning) {
const currentCost = new Increment(
this.currentIncrementStartTime,
new Date(),
this.currentIncrementAttendeeCount,
this.currentIncrementAverageWage
).cost;
return totalCostSoFar + currentCost;
}
return totalCostSoFar;
}
Data Model: Increment Class
The Increment class encapsulates the interval data and cost calculation logic, ensuring immutability of each segment once created.
export default class Increment {
constructor(startTime, stopTime, attendeeCount, averageWage, previousTotalCost=0, purpose='') {
this.startTime = startTime;
this.stopTime = stopTime;
this.elapsedTime = (stopTime - startTime) / 1000; // milliseconds to seconds
this.attendeeCount = attendeeCount;
this.averageWage = averageWage;
this.cost = this.attendeeCount * (this.averageWage / 3600) * this.elapsedTime;
this.totalCost = previousTotalCost + this.cost;
this.purpose = purpose;
}
}
Integration
Incremental Cost Calculation is tightly integrated with the overall meeting lifecycle and UI updates:
It supports Meeting Lifecycle Management by providing the mechanism to finalize increments when a meeting is started, stopped, or updated.
It feeds cost data to Dynamic Cost Visualization for rendering up-to-date cost and elapsed time displays.
The increments array is saved to persistent storage via the
Warehousemodel when the meeting ends, linking to Persistent Meeting History.
This modular separation allows the parent topic to manage meeting state transitions and user interactions while delegating precise cost computation to this subtopic.
sequenceDiagram
participant User
participant MeetingModel
participant IncrementModel
participant Warehouse
participant UI
User->>MeetingModel: Start meeting
MeetingModel->>IncrementModel: Initialize first increment
MeetingModel->>UI: Update cost display (initial)
User->>MeetingModel: Change attendees/wage
MeetingModel->>IncrementModel: Push current increment (close interval)
MeetingModel->>IncrementModel: Start new increment (new params)
MeetingModel->>UI: Update cost display (updated)
MeetingModel->>IncrementModel: Periodic cost calc (timer ticks)
MeetingModel->>UI: Update cost display (running cost)
User->>MeetingModel: Stop meeting
MeetingModel->>IncrementModel: Push final increment
MeetingModel->>Warehouse: Save increments (persistent storage)
MeetingModel->>UI: Update final cost display
This sequence diagram illustrates how increments are created and closed in response to user actions, and how cost data flows from the incremental calculation subtopic to UI and storage components.