"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const shared_1 = require("../../shared");
const server_commands_1 = require("../../../../shared/server-commands");
const core_utils_1 = require("../../../../shared/core-utils");
function simulateComplexViewers(servers, videoUUID) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const user0 = '8.8.8.8,127.0.0.1';
        const user1 = '8.8.8.8,127.0.0.1';
        const user2 = '8.8.8.9,127.0.0.1';
        const user3 = '8.8.8.10,127.0.0.1';
        const user4 = '8.8.8.11,127.0.0.1';
        yield servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user0 });
        yield (0, core_utils_1.wait)(500);
        yield servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user1 });
        yield servers[0].views.view({ id: videoUUID, currentTime: 2, xForwardedFor: user0 });
        yield (0, core_utils_1.wait)(500);
        const startDate = new Date().toISOString();
        yield servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user2 });
        yield (0, core_utils_1.wait)(500);
        yield servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user3 });
        yield (0, core_utils_1.wait)(500);
        yield servers[0].views.view({ id: videoUUID, currentTime: 4, xForwardedFor: user1 });
        yield (0, core_utils_1.wait)(500);
        yield servers[0].views.view({ id: videoUUID, currentTime: 3, xForwardedFor: user3 });
        yield (0, core_utils_1.wait)(500);
        const endDate = new Date().toISOString();
        yield servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user4 });
        yield servers[0].views.view({ id: videoUUID, currentTime: 5, xForwardedFor: user2 });
        yield (0, core_utils_1.wait)(500);
        yield servers[0].views.view({ id: videoUUID, currentTime: 1, xForwardedFor: user4 });
        yield (0, shared_1.processViewersStats)(servers);
        return { startDate, endDate };
    });
}
describe('Test views overall stats', function () {
    let servers;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            servers = yield (0, shared_1.prepareViewsServers)();
        });
    });
    describe('Test watch time stats of local videos on live and VOD', function () {
        let vodVideoId;
        let liveVideoId;
        let command;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                ({ vodVideoId, liveVideoId, ffmpegCommand: command } = yield (0, shared_1.prepareViewsVideos)({ servers, live: true, vod: true }));
            });
        });
        it('Should display overall stats of a video with no viewers', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const videoId of [liveVideoId, vodVideoId]) {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId });
                    const video = yield servers[0].videos.get({ id: videoId });
                    (0, chai_1.expect)(video.views).to.equal(0);
                    (0, chai_1.expect)(stats.averageWatchTime).to.equal(0);
                    (0, chai_1.expect)(stats.totalWatchTime).to.equal(0);
                    (0, chai_1.expect)(stats.totalViewers).to.equal(0);
                }
            });
        });
        it('Should display overall stats with 1 viewer below the watch time limit', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                for (const videoId of [liveVideoId, vodVideoId]) {
                    yield servers[0].views.simulateViewer({ id: videoId, currentTimes: [0, 1] });
                }
                yield (0, shared_1.processViewersStats)(servers);
                for (const videoId of [liveVideoId, vodVideoId]) {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId });
                    const video = yield servers[0].videos.get({ id: videoId });
                    (0, chai_1.expect)(video.views).to.equal(0);
                    (0, chai_1.expect)(stats.averageWatchTime).to.equal(1);
                    (0, chai_1.expect)(stats.totalWatchTime).to.equal(1);
                    (0, chai_1.expect)(stats.totalViewers).to.equal(1);
                }
            });
        });
        it('Should display overall stats with 2 viewers', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                {
                    yield servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [0, 3] });
                    yield servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [0, 35, 40] });
                    yield (0, shared_1.processViewersStats)(servers);
                    {
                        const stats = yield servers[0].videoStats.getOverallStats({ videoId: vodVideoId });
                        const video = yield servers[0].videos.get({ id: vodVideoId });
                        (0, chai_1.expect)(video.views).to.equal(1);
                        (0, chai_1.expect)(stats.averageWatchTime).to.equal(2);
                        (0, chai_1.expect)(stats.totalWatchTime).to.equal(4);
                        (0, chai_1.expect)(stats.totalViewers).to.equal(2);
                    }
                    {
                        const stats = yield servers[0].videoStats.getOverallStats({ videoId: liveVideoId });
                        const video = yield servers[0].videos.get({ id: liveVideoId });
                        (0, chai_1.expect)(video.views).to.equal(1);
                        (0, chai_1.expect)(stats.averageWatchTime).to.equal(21);
                        (0, chai_1.expect)(stats.totalWatchTime).to.equal(41);
                        (0, chai_1.expect)(stats.totalViewers).to.equal(2);
                    }
                }
            });
        });
        it('Should display overall stats with a remote viewer below the watch time limit', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                for (const videoId of [liveVideoId, vodVideoId]) {
                    yield servers[1].views.simulateViewer({ id: videoId, currentTimes: [0, 2] });
                }
                yield (0, shared_1.processViewersStats)(servers);
                {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId: vodVideoId });
                    const video = yield servers[0].videos.get({ id: vodVideoId });
                    (0, chai_1.expect)(video.views).to.equal(1);
                    (0, chai_1.expect)(stats.averageWatchTime).to.equal(2);
                    (0, chai_1.expect)(stats.totalWatchTime).to.equal(6);
                    (0, chai_1.expect)(stats.totalViewers).to.equal(3);
                }
                {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId: liveVideoId });
                    const video = yield servers[0].videos.get({ id: liveVideoId });
                    (0, chai_1.expect)(video.views).to.equal(1);
                    (0, chai_1.expect)(stats.averageWatchTime).to.equal(14);
                    (0, chai_1.expect)(stats.totalWatchTime).to.equal(43);
                    (0, chai_1.expect)(stats.totalViewers).to.equal(3);
                }
            });
        });
        it('Should display overall stats with a remote viewer above the watch time limit', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                yield servers[1].views.simulateViewer({ id: vodVideoId, currentTimes: [0, 5] });
                yield servers[1].views.simulateViewer({ id: liveVideoId, currentTimes: [0, 45] });
                yield (0, shared_1.processViewersStats)(servers);
                {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId: vodVideoId });
                    const video = yield servers[0].videos.get({ id: vodVideoId });
                    (0, chai_1.expect)(video.views).to.equal(2);
                    (0, chai_1.expect)(stats.averageWatchTime).to.equal(3);
                    (0, chai_1.expect)(stats.totalWatchTime).to.equal(11);
                    (0, chai_1.expect)(stats.totalViewers).to.equal(4);
                }
                {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId: liveVideoId });
                    const video = yield servers[0].videos.get({ id: liveVideoId });
                    (0, chai_1.expect)(video.views).to.equal(2);
                    (0, chai_1.expect)(stats.averageWatchTime).to.equal(22);
                    (0, chai_1.expect)(stats.totalWatchTime).to.equal(88);
                    (0, chai_1.expect)(stats.totalViewers).to.equal(4);
                }
            });
        });
        it('Should filter overall stats by date', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                const beforeView = new Date();
                yield servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [0, 3] });
                yield (0, shared_1.processViewersStats)(servers);
                {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId: vodVideoId, startDate: beforeView.toISOString() });
                    (0, chai_1.expect)(stats.averageWatchTime).to.equal(3);
                    (0, chai_1.expect)(stats.totalWatchTime).to.equal(3);
                    (0, chai_1.expect)(stats.totalViewers).to.equal(1);
                }
                {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId: liveVideoId, endDate: beforeView.toISOString() });
                    (0, chai_1.expect)(stats.averageWatchTime).to.equal(22);
                    (0, chai_1.expect)(stats.totalWatchTime).to.equal(88);
                    (0, chai_1.expect)(stats.totalViewers).to.equal(4);
                }
            });
        });
        after(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.stopFfmpeg)(command);
            });
        });
    });
    describe('Test watchers peak stats of local videos on VOD', function () {
        let videoUUID;
        let before2Watchers;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                ({ vodVideoId: videoUUID } = yield (0, shared_1.prepareViewsVideos)({ servers, live: true, vod: true }));
            });
        });
        it('Should not have watchers peak', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const stats = yield servers[0].videoStats.getOverallStats({ videoId: videoUUID });
                (0, chai_1.expect)(stats.viewersPeak).to.equal(0);
                (0, chai_1.expect)(stats.viewersPeakDate).to.be.null;
            });
        });
        it('Should have watcher peak with 1 watcher', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                const before = new Date();
                yield servers[0].views.simulateViewer({ id: videoUUID, currentTimes: [0, 2] });
                const after = new Date();
                yield (0, shared_1.processViewersStats)(servers);
                const stats = yield servers[0].videoStats.getOverallStats({ videoId: videoUUID });
                (0, chai_1.expect)(stats.viewersPeak).to.equal(1);
                (0, chai_1.expect)(new Date(stats.viewersPeakDate)).to.be.above(before).and.below(after);
            });
        });
        it('Should have watcher peak with 2 watchers', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                before2Watchers = new Date();
                yield servers[0].views.view({ id: videoUUID, currentTime: 0 });
                yield servers[1].views.view({ id: videoUUID, currentTime: 0 });
                yield servers[0].views.view({ id: videoUUID, currentTime: 2 });
                yield servers[1].views.view({ id: videoUUID, currentTime: 2 });
                const after = new Date();
                yield (0, shared_1.processViewersStats)(servers);
                const stats = yield servers[0].videoStats.getOverallStats({ videoId: videoUUID });
                (0, chai_1.expect)(stats.viewersPeak).to.equal(2);
                (0, chai_1.expect)(new Date(stats.viewersPeakDate)).to.be.above(before2Watchers).and.below(after);
            });
        });
        it('Should filter peak viewers stats by date', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() });
                    (0, chai_1.expect)(stats.viewersPeak).to.equal(0);
                    (0, chai_1.expect)(stats.viewersPeakDate).to.not.exist;
                }
                {
                    const stats = yield servers[0].videoStats.getOverallStats({ videoId: videoUUID, endDate: before2Watchers.toISOString() });
                    (0, chai_1.expect)(stats.viewersPeak).to.equal(1);
                    (0, chai_1.expect)(new Date(stats.viewersPeakDate)).to.be.below(before2Watchers);
                }
            });
        });
        it('Should complex filter peak viewers by date', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(60000);
                const { startDate, endDate } = yield simulateComplexViewers(servers, videoUUID);
                const expectCorrect = (stats) => {
                    (0, chai_1.expect)(stats.viewersPeak).to.equal(3);
                    (0, chai_1.expect)(new Date(stats.viewersPeakDate)).to.be.above(new Date(startDate)).and.below(new Date(endDate));
                };
                expectCorrect(yield servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate, endDate }));
                expectCorrect(yield servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate }));
                expectCorrect(yield servers[0].videoStats.getOverallStats({ videoId: videoUUID, endDate }));
                expectCorrect(yield servers[0].videoStats.getOverallStats({ videoId: videoUUID }));
            });
        });
    });
    describe('Test countries', function () {
        let videoUUID;
        it('Should not report countries if geoip is disabled', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(120000);
                const { uuid } = yield servers[0].videos.quickUpload({ name: 'video' });
                yield (0, server_commands_1.waitJobs)(servers);
                yield servers[1].views.view({ id: uuid, xForwardedFor: '8.8.8.8,127.0.0.1', currentTime: 1 });
                yield (0, shared_1.processViewersStats)(servers);
                const stats = yield servers[0].videoStats.getOverallStats({ videoId: uuid });
                (0, chai_1.expect)(stats.countries).to.have.lengthOf(0);
            });
        });
        it('Should report countries if geoip is enabled', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                const { uuid } = yield servers[0].videos.quickUpload({ name: 'video' });
                videoUUID = uuid;
                yield (0, server_commands_1.waitJobs)(servers);
                yield Promise.all([
                    servers[0].kill(),
                    servers[1].kill()
                ]);
                const config = { geo_ip: { enabled: true } };
                yield Promise.all([
                    servers[0].run(config),
                    servers[1].run(config)
                ]);
                yield servers[0].views.view({ id: uuid, xForwardedFor: '8.8.8.8,127.0.0.1', currentTime: 1 });
                yield servers[1].views.view({ id: uuid, xForwardedFor: '8.8.8.4,127.0.0.1', currentTime: 3 });
                yield servers[1].views.view({ id: uuid, xForwardedFor: '80.67.169.12,127.0.0.1', currentTime: 2 });
                yield (0, shared_1.processViewersStats)(servers);
                const stats = yield servers[0].videoStats.getOverallStats({ videoId: uuid });
                (0, chai_1.expect)(stats.countries).to.have.lengthOf(2);
                (0, chai_1.expect)(stats.countries[0].isoCode).to.equal('US');
                (0, chai_1.expect)(stats.countries[0].viewers).to.equal(2);
                (0, chai_1.expect)(stats.countries[1].isoCode).to.equal('FR');
                (0, chai_1.expect)(stats.countries[1].viewers).to.equal(1);
            });
        });
        it('Should filter countries stats by date', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const stats = yield servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() });
                (0, chai_1.expect)(stats.countries).to.have.lengthOf(0);
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=video-views-overall-stats.js.map