"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "SocketIoTask", {
    enumerable: true,
    get: function() {
        return SocketIoTask;
    }
});
const _socketiogateway = require("../state/socket-io.gateway");
const _eventconstants = require("../constants/event.constants");
const _metricutils = require("../utils/metric.utils");
class SocketIoTask {
    socketIoGateway;
    printerSocketStore;
    printerEventsCache;
    floorStore;
    fileUploadTrackerCache;
    eventEmitter2;
    printerCache;
    logger;
    settingsStore;
    aggregateSizeCounter = 0;
    aggregateWindowLength = 100;
    aggregateSizes = [];
    rounding = 2;
    constructor({ socketIoGateway, floorStore, printerSocketStore, printerEventsCache, printerCache, loggerFactory, fileUploadTrackerCache, settingsStore, eventEmitter2 }){
        this.socketIoGateway = socketIoGateway;
        this.printerSocketStore = printerSocketStore;
        this.printerEventsCache = printerEventsCache;
        this.fileUploadTrackerCache = fileUploadTrackerCache;
        this.floorStore = floorStore;
        this.logger = loggerFactory(SocketIoTask.name);
        this.eventEmitter2 = eventEmitter2;
        this.printerCache = printerCache;
        this.settingsStore = settingsStore;
        this.eventEmitter2.on(_eventconstants.socketIoConnectedEvent, async ()=>{
            await this.sendUpdate();
        });
    }
    async run() {
        await this.sendUpdate();
    }
    async sendUpdate() {
        const floors = await this.floorStore.listCache();
        const printers = await this.printerCache.listCachedPrinters(true);
        const socketStates = this.printerSocketStore.getSocketStatesById();
        const printerEvents = await this.printerEventsCache.getAllKeyValues();
        const trackedUploads = this.fileUploadTrackerCache.getUploads(true);
        const socketIoData = {
            printers,
            floors,
            socketStates,
            printerEvents,
            trackedUploads
        };
        if (this.settingsStore.getDebugSettingsSensitive()?.debugSocketIoBandwidth) {
            const kbDataString = Object.entries(socketIoData).map(([id, state])=>{
                return `${id} ${(0, _metricutils.formatKB)(state)}`;
            }).join(" ");
            this.logger.log(kbDataString);
        }
        const serializedData = JSON.stringify(socketIoData);
        const transportDataSize = (0, _metricutils.sizeKB)(serializedData);
        this.updateAggregator(transportDataSize);
        this.socketIoGateway.send(_socketiogateway.IO_MESSAGES.LegacyUpdate, serializedData);
    }
    updateAggregator(transportDataLength) {
        if (this.aggregateSizeCounter >= this.aggregateWindowLength) {
            const summedPayloadSize = this.aggregateSizes.reduce((t, n)=>t += n);
            const averagePayloadSize = summedPayloadSize / this.aggregateWindowLength;
            this.logger.log(`Printer SocketIO metrics ${averagePayloadSize.toFixed(this.rounding)}kB [${this.aggregateWindowLength} TX avg].`);
            this.aggregateSizeCounter = 0;
            this.aggregateSizes = [];
        }
        this.aggregateSizes.push(transportDataLength);
        ++this.aggregateSizeCounter;
    }
}

//# sourceMappingURL=socketio.task.js.map