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:

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:

Key Workflows

  1. 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.

  2. Cost Calculation per Increment
    The cost for each increment is computed as:

    cost = attendeeCount × (averageWage / SECONDS_PER_HOUR) × elapsedTime
    

    This formula prorates the hourly wage to a per-second rate and multiplies by the number of attendees and elapsed seconds.

  3. 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.

  4. 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:

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:

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.