"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const bluebird_1 = tslib_1.__importDefault(require("bluebird"));
const core_utils_1 = require("../shared/core-utils");
const server_commands_1 = require("../shared/server-commands");
let servers;
const viewers = [];
let videoId;
run()
    .then(() => process.exit(0))
    .catch(err => console.error(err))
    .finally(() => (0, server_commands_1.killallServers)(servers));
function run() {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        yield prepare();
        while (true) {
            yield runViewers();
        }
    });
}
function prepare() {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        console.log('Preparing servers...');
        const config = {
            log: {
                level: 'info'
            },
            rates_limit: {
                api: {
                    max: 5000000
                }
            },
            views: {
                videos: {
                    local_buffer_update_interval: '30 minutes',
                    ip_view_expiration: '1 hour'
                }
            }
        };
        const env = { PRODUCTION_CONSTANTS: 'true' };
        servers = yield Promise.all([
            (0, server_commands_1.createSingleServer)(1, config, { env, nodeArgs: ['--inspect'] }),
            (0, server_commands_1.createSingleServer)(2, config, { env }),
            (0, server_commands_1.createSingleServer)(3, config, { env })
        ]);
        yield (0, server_commands_1.setAccessTokensToServers)(servers);
        yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
        yield (0, server_commands_1.doubleFollow)(servers[0], servers[2]);
        const { uuid } = yield servers[0].videos.quickUpload({ name: 'video' });
        videoId = uuid;
        yield (0, server_commands_1.waitJobs)(servers);
        const THOUSAND_VIEWERS = 2;
        for (let i = 2; i < 252; i++) {
            for (let j = 2; j < 6; j++) {
                for (let k = 2; k < THOUSAND_VIEWERS + 2; k++) {
                    viewers.push({ xForwardedFor: `0.${k}.${j}.${i},127.0.0.1` });
                }
            }
        }
        console.log('Servers preparation finished.');
    });
}
function runViewers() {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        console.log('Will run views of %d viewers.', viewers.length);
        const before = new Date().getTime();
        yield bluebird_1.default.map(viewers, viewer => {
            return servers[0].views.simulateView({ id: videoId, xForwardedFor: viewer.xForwardedFor });
        }, { concurrency: 500 });
        console.log('Finished to run views in %d seconds.', (new Date().getTime() - before) / 1000);
        yield (0, core_utils_1.wait)(5000);
    });
}
//# sourceMappingURL=simulate-many-viewers.js.map