/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { performanceNow } from "@fluid-internal/client-utils";
import { assert } from "@fluidframework/core-utils/internal";
import { createChildLogger, } from "@fluidframework/telemetry-utils/internal";
export class BatchTracker {
    constructor(batchEventEmitter, logger, batchLengthThreshold, batchCountSamplingRate, dateTimeProvider = () => performanceNow()) {
        this.batchEventEmitter = batchEventEmitter;
        this.trackedBatchCount = 0;
        this.logger = createChildLogger({ logger, namespace: "Batching" });
        this.batchEventEmitter.on("batchBegin", (message) => {
            this.startBatchSequenceNumber = message.sequenceNumber;
            this.batchProcessingStartTimeStamp = dateTimeProvider();
            this.trackedBatchCount++;
        });
        this.batchEventEmitter.on("batchEnd", (error, message) => {
            assert(this.startBatchSequenceNumber !== undefined &&
                this.batchProcessingStartTimeStamp !== undefined, 0x2ba /* "batchBegin must fire before batchEnd" */);
            const length = message.sequenceNumber - this.startBatchSequenceNumber + 1;
            if (length >= batchLengthThreshold) {
                this.logger.sendPerformanceEvent({
                    eventName: "LengthTooBig",
                    length,
                    threshold: batchLengthThreshold,
                    batchEndSequenceNumber: message.sequenceNumber,
                    duration: dateTimeProvider() - this.batchProcessingStartTimeStamp,
                    batchError: error !== undefined,
                });
            }
            if (this.trackedBatchCount % batchCountSamplingRate === 0) {
                this.logger.sendPerformanceEvent({
                    eventName: "Length",
                    length,
                    samplingRate: batchCountSamplingRate,
                    batchEndSequenceNumber: message.sequenceNumber,
                    duration: dateTimeProvider() - this.batchProcessingStartTimeStamp,
                });
            }
            this.startBatchSequenceNumber = undefined;
            this.batchProcessingStartTimeStamp = undefined;
        });
    }
}
/**
 * Track batch sizes in terms of op counts and processing times
 *
 * @param batchEventEmitter - event emitter which tracks the lifecycle of batch operations
 * @param logger - See {@link @fluidframework/core-interfaces#ITelemetryLoggerExt}
 * @param batchLengthThreshold - threshold for the length of a batch when to send an error event
 * @param batchCountSamplingRate - rate for batches for which to send an event with its characteristics
 */
export const BindBatchTracker = (batchEventEmitter, logger, batchLengthThreshold = 1000, batchCountSamplingRate = 1000) => new BatchTracker(batchEventEmitter, logger, batchLengthThreshold, batchCountSamplingRate);
//# sourceMappingURL=batchTracker.js.map