"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");
function buildOneMonthAgo() {
    const monthAgo = new Date();
    monthAgo.setHours(0, 0, 0, 0);
    monthAgo.setDate(monthAgo.getDate() - 29);
    return monthAgo;
}
describe('Test views timeserie stats', function () {
    const availableMetrics = ['viewers'];
    let servers;
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            servers = yield (0, shared_1.prepareViewsServers)();
        });
    });
    describe('Common metric tests', function () {
        let vodVideoId;
        before(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                this.timeout(240000);
                ({ vodVideoId } = yield (0, shared_1.prepareViewsVideos)({ servers, live: false, vod: true }));
            });
        });
        it('Should display empty metric stats', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const metric of availableMetrics) {
                    const { data } = yield servers[0].videoStats.getTimeserieStats({ videoId: vodVideoId, metric });
                    (0, chai_1.expect)(data).to.have.length.at.least(1);
                    for (const d of data) {
                        (0, chai_1.expect)(d.value).to.equal(0);
                    }
                }
            });
        });
    });
    describe('Test viewer and watch time metrics on live and VOD', function () {
        let vodVideoId;
        let liveVideoId;
        let command;
        function expectTodayLastValue(result, lastValue) {
            const { data } = result;
            const last = data[data.length - 1];
            const today = new Date().getDate();
            (0, chai_1.expect)(new Date(last.date).getDate()).to.equal(today);
            if (lastValue)
                (0, chai_1.expect)(last.value).to.equal(lastValue);
        }
        function expectTimeserieData(result, lastValue) {
            const { data } = result;
            (0, chai_1.expect)(data).to.have.length.at.least(25);
            expectTodayLastValue(result, lastValue);
            for (let i = 0; i < data.length - 2; i++) {
                (0, chai_1.expect)(data[i].value).to.equal(0);
            }
        }
        function expectInterval(result, intervalMs) {
            const first = result.data[0];
            const second = result.data[1];
            (0, chai_1.expect)(new Date(second.date).getTime() - new Date(first.date).getTime()).to.equal(intervalMs);
        }
        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 appropriate viewers metrics', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const videoId of [vodVideoId, liveVideoId]) {
                    yield servers[0].views.simulateViewer({ id: videoId, currentTimes: [0, 3] });
                    yield servers[1].views.simulateViewer({ id: videoId, currentTimes: [0, 5] });
                }
                yield (0, shared_1.processViewersStats)(servers);
                for (const videoId of [vodVideoId, liveVideoId]) {
                    const result = yield servers[0].videoStats.getTimeserieStats({
                        videoId,
                        startDate: buildOneMonthAgo(),
                        endDate: new Date(),
                        metric: 'viewers'
                    });
                    expectTimeserieData(result, 2);
                }
            });
        });
        it('Should display appropriate watch time metrics', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                for (const videoId of [vodVideoId, liveVideoId]) {
                    const result = yield servers[0].videoStats.getTimeserieStats({
                        videoId,
                        startDate: buildOneMonthAgo(),
                        endDate: new Date(),
                        metric: 'aggregateWatchTime'
                    });
                    expectTimeserieData(result, 8);
                    yield servers[1].views.simulateViewer({ id: videoId, currentTimes: [0, 1] });
                }
                yield (0, shared_1.processViewersStats)(servers);
                for (const videoId of [vodVideoId, liveVideoId]) {
                    const result = yield servers[0].videoStats.getTimeserieStats({
                        videoId,
                        startDate: buildOneMonthAgo(),
                        endDate: new Date(),
                        metric: 'aggregateWatchTime'
                    });
                    expectTimeserieData(result, 9);
                }
            });
        });
        it('Should use a custom start/end date', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const now = new Date();
                const twentyDaysAgo = new Date();
                twentyDaysAgo.setDate(twentyDaysAgo.getDate() - 19);
                const result = yield servers[0].videoStats.getTimeserieStats({
                    videoId: vodVideoId,
                    metric: 'aggregateWatchTime',
                    startDate: twentyDaysAgo,
                    endDate: now
                });
                (0, chai_1.expect)(result.groupInterval).to.equal('1 day');
                (0, chai_1.expect)(result.data).to.have.lengthOf(20);
                const first = result.data[0];
                (0, chai_1.expect)(new Date(first.date).toLocaleDateString()).to.equal(twentyDaysAgo.toLocaleDateString());
                expectInterval(result, 24 * 3600 * 1000);
                expectTodayLastValue(result, 9);
            });
        });
        it('Should automatically group by months', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const now = new Date();
                const heightYearsAgo = new Date();
                heightYearsAgo.setFullYear(heightYearsAgo.getFullYear() - 7);
                const result = yield servers[0].videoStats.getTimeserieStats({
                    videoId: vodVideoId,
                    metric: 'aggregateWatchTime',
                    startDate: heightYearsAgo,
                    endDate: now
                });
                (0, chai_1.expect)(result.groupInterval).to.equal('6 months');
                (0, chai_1.expect)(result.data).to.have.length.above(10).and.below(200);
            });
        });
        it('Should automatically group by days', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const now = new Date();
                const threeMonthsAgo = new Date();
                threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
                const result = yield servers[0].videoStats.getTimeserieStats({
                    videoId: vodVideoId,
                    metric: 'aggregateWatchTime',
                    startDate: threeMonthsAgo,
                    endDate: now
                });
                (0, chai_1.expect)(result.groupInterval).to.equal('2 days');
                (0, chai_1.expect)(result.data).to.have.length.above(10).and.below(200);
            });
        });
        it('Should automatically group by hours', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const now = new Date();
                const twoDaysAgo = new Date();
                twoDaysAgo.setDate(twoDaysAgo.getDate() - 1);
                const result = yield servers[0].videoStats.getTimeserieStats({
                    videoId: vodVideoId,
                    metric: 'aggregateWatchTime',
                    startDate: twoDaysAgo,
                    endDate: now
                });
                (0, chai_1.expect)(result.groupInterval).to.equal('1 hour');
                (0, chai_1.expect)(result.data).to.have.length.above(24).and.below(50);
                expectInterval(result, 3600 * 1000);
                expectTodayLastValue(result, 9);
            });
        });
        it('Should automatically group by ten minutes', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const now = new Date();
                const twoHoursAgo = new Date();
                twoHoursAgo.setHours(twoHoursAgo.getHours() - 4);
                const result = yield servers[0].videoStats.getTimeserieStats({
                    videoId: vodVideoId,
                    metric: 'aggregateWatchTime',
                    startDate: twoHoursAgo,
                    endDate: now
                });
                (0, chai_1.expect)(result.groupInterval).to.equal('10 minutes');
                (0, chai_1.expect)(result.data).to.have.length.above(20).and.below(30);
                expectInterval(result, 60 * 10 * 1000);
                expectTodayLastValue(result);
            });
        });
        it('Should automatically group by one minute', function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                const now = new Date();
                const thirtyAgo = new Date();
                thirtyAgo.setMinutes(thirtyAgo.getMinutes() - 30);
                const result = yield servers[0].videoStats.getTimeserieStats({
                    videoId: vodVideoId,
                    metric: 'aggregateWatchTime',
                    startDate: thirtyAgo,
                    endDate: now
                });
                (0, chai_1.expect)(result.groupInterval).to.equal('1 minute');
                (0, chai_1.expect)(result.data).to.have.length.above(20).and.below(40);
                expectInterval(result, 60 * 1000);
                expectTodayLastValue(result);
            });
        });
        after(function () {
            return tslib_1.__awaiter(this, void 0, void 0, function* () {
                yield (0, server_commands_1.stopFfmpeg)(command);
            });
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=video-views-timeserie-stats.js.map