"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const chai_1 = require("chai");
const core_utils_1 = require("../../../../shared/core-utils");
const server_commands_1 = require("../../../../shared/server-commands");
describe('Test stats (excluding redundancy)', function () {
    let servers = [];
    let channelId;
    const user = {
        username: 'user1',
        password: 'super_password'
    };
    before(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            servers = yield (0, server_commands_1.createMultipleServers)(3);
            yield (0, server_commands_1.setAccessTokensToServers)(servers);
            yield (0, server_commands_1.setDefaultChannelAvatar)(servers);
            yield (0, server_commands_1.setDefaultAccountAvatar)(servers);
            yield (0, server_commands_1.doubleFollow)(servers[0], servers[1]);
            yield servers[0].users.create({ username: user.username, password: user.password });
            const { uuid } = yield servers[0].videos.upload({ attributes: { fixture: 'video_short.webm' } });
            yield servers[0].comments.createThread({ videoId: uuid, text: 'comment' });
            yield servers[0].views.simulateView({ id: uuid });
            yield (0, core_utils_1.wait)(8000);
            yield servers[2].follows.follow({ hosts: [servers[0].url] });
            yield (0, server_commands_1.waitJobs)(servers);
        });
    });
    it('Should have the correct stats on instance 1', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const data = yield servers[0].stats.get();
            (0, chai_1.expect)(data.totalLocalVideoComments).to.equal(1);
            (0, chai_1.expect)(data.totalLocalVideos).to.equal(1);
            (0, chai_1.expect)(data.totalLocalVideoViews).to.equal(1);
            (0, chai_1.expect)(data.totalLocalVideoFilesSize).to.equal(218910);
            (0, chai_1.expect)(data.totalUsers).to.equal(2);
            (0, chai_1.expect)(data.totalVideoComments).to.equal(1);
            (0, chai_1.expect)(data.totalVideos).to.equal(1);
            (0, chai_1.expect)(data.totalInstanceFollowers).to.equal(2);
            (0, chai_1.expect)(data.totalInstanceFollowing).to.equal(1);
            (0, chai_1.expect)(data.totalLocalPlaylists).to.equal(0);
        });
    });
    it('Should have the correct stats on instance 2', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const data = yield servers[1].stats.get();
            (0, chai_1.expect)(data.totalLocalVideoComments).to.equal(0);
            (0, chai_1.expect)(data.totalLocalVideos).to.equal(0);
            (0, chai_1.expect)(data.totalLocalVideoViews).to.equal(0);
            (0, chai_1.expect)(data.totalLocalVideoFilesSize).to.equal(0);
            (0, chai_1.expect)(data.totalUsers).to.equal(1);
            (0, chai_1.expect)(data.totalVideoComments).to.equal(1);
            (0, chai_1.expect)(data.totalVideos).to.equal(1);
            (0, chai_1.expect)(data.totalInstanceFollowers).to.equal(1);
            (0, chai_1.expect)(data.totalInstanceFollowing).to.equal(1);
            (0, chai_1.expect)(data.totalLocalPlaylists).to.equal(0);
        });
    });
    it('Should have the correct stats on instance 3', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const data = yield servers[2].stats.get();
            (0, chai_1.expect)(data.totalLocalVideoComments).to.equal(0);
            (0, chai_1.expect)(data.totalLocalVideos).to.equal(0);
            (0, chai_1.expect)(data.totalLocalVideoViews).to.equal(0);
            (0, chai_1.expect)(data.totalUsers).to.equal(1);
            (0, chai_1.expect)(data.totalVideoComments).to.equal(1);
            (0, chai_1.expect)(data.totalVideos).to.equal(1);
            (0, chai_1.expect)(data.totalInstanceFollowing).to.equal(1);
            (0, chai_1.expect)(data.totalInstanceFollowers).to.equal(0);
            (0, chai_1.expect)(data.totalLocalPlaylists).to.equal(0);
        });
    });
    it('Should have the correct total videos stats after an unfollow', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(15000);
            yield servers[2].follows.unfollow({ target: servers[0] });
            yield (0, server_commands_1.waitJobs)(servers);
            const data = yield servers[2].stats.get();
            (0, chai_1.expect)(data.totalVideos).to.equal(0);
        });
    });
    it('Should have the correct active user stats', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const server = servers[0];
            {
                const data = yield server.stats.get();
                (0, chai_1.expect)(data.totalDailyActiveUsers).to.equal(1);
                (0, chai_1.expect)(data.totalWeeklyActiveUsers).to.equal(1);
                (0, chai_1.expect)(data.totalMonthlyActiveUsers).to.equal(1);
            }
            {
                yield server.login.getAccessToken(user);
                const data = yield server.stats.get();
                (0, chai_1.expect)(data.totalDailyActiveUsers).to.equal(2);
                (0, chai_1.expect)(data.totalWeeklyActiveUsers).to.equal(2);
                (0, chai_1.expect)(data.totalMonthlyActiveUsers).to.equal(2);
            }
        });
    });
    it('Should have the correct active channel stats', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const server = servers[0];
            {
                const data = yield server.stats.get();
                (0, chai_1.expect)(data.totalLocalVideoChannels).to.equal(2);
                (0, chai_1.expect)(data.totalLocalDailyActiveVideoChannels).to.equal(1);
                (0, chai_1.expect)(data.totalLocalWeeklyActiveVideoChannels).to.equal(1);
                (0, chai_1.expect)(data.totalLocalMonthlyActiveVideoChannels).to.equal(1);
            }
            {
                const attributes = {
                    name: 'stats_channel',
                    displayName: 'My stats channel'
                };
                const created = yield server.channels.create({ attributes });
                channelId = created.id;
                const data = yield server.stats.get();
                (0, chai_1.expect)(data.totalLocalVideoChannels).to.equal(3);
                (0, chai_1.expect)(data.totalLocalDailyActiveVideoChannels).to.equal(1);
                (0, chai_1.expect)(data.totalLocalWeeklyActiveVideoChannels).to.equal(1);
                (0, chai_1.expect)(data.totalLocalMonthlyActiveVideoChannels).to.equal(1);
            }
            {
                yield server.videos.upload({ attributes: { fixture: 'video_short.webm', channelId } });
                const data = yield server.stats.get();
                (0, chai_1.expect)(data.totalLocalVideoChannels).to.equal(3);
                (0, chai_1.expect)(data.totalLocalDailyActiveVideoChannels).to.equal(2);
                (0, chai_1.expect)(data.totalLocalWeeklyActiveVideoChannels).to.equal(2);
                (0, chai_1.expect)(data.totalLocalMonthlyActiveVideoChannels).to.equal(2);
            }
        });
    });
    it('Should have the correct playlist stats', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const server = servers[0];
            {
                const data = yield server.stats.get();
                (0, chai_1.expect)(data.totalLocalPlaylists).to.equal(0);
            }
            {
                yield server.playlists.create({
                    attributes: {
                        displayName: 'playlist for count',
                        privacy: 1,
                        videoChannelId: channelId
                    }
                });
                const data = yield server.stats.get();
                (0, chai_1.expect)(data.totalLocalPlaylists).to.equal(1);
            }
        });
    });
    it('Should correctly count video file sizes if transcoding is enabled', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(120000);
            yield servers[0].config.updateCustomSubConfig({
                newConfig: {
                    transcoding: {
                        enabled: true,
                        webtorrent: {
                            enabled: true
                        },
                        hls: {
                            enabled: true
                        },
                        resolutions: {
                            '0p': false,
                            '144p': false,
                            '240p': false,
                            '360p': false,
                            '480p': false,
                            '720p': false,
                            '1080p': false,
                            '1440p': false,
                            '2160p': false
                        }
                    }
                }
            });
            yield servers[0].videos.upload({ attributes: { name: 'video', fixture: 'video_short.webm' } });
            yield (0, server_commands_1.waitJobs)(servers);
            {
                const data = yield servers[1].stats.get();
                (0, chai_1.expect)(data.totalLocalVideoFilesSize).to.equal(0);
            }
            {
                const data = yield servers[0].stats.get();
                (0, chai_1.expect)(data.totalLocalVideoFilesSize).to.be.greaterThan(500000);
                (0, chai_1.expect)(data.totalLocalVideoFilesSize).to.be.lessThan(600000);
            }
        });
    });
    it('Should have the correct AP stats', function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.timeout(60000);
            yield servers[0].config.disableTranscoding();
            const first = yield servers[1].stats.get();
            for (let i = 0; i < 10; i++) {
                yield servers[0].videos.upload({ attributes: { name: 'video' } });
            }
            yield (0, server_commands_1.waitJobs)(servers);
            yield (0, core_utils_1.wait)(6000);
            const second = yield servers[1].stats.get();
            (0, chai_1.expect)(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed);
            const apTypes = [
                'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
            ];
            const processed = apTypes.reduce((previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'], 0);
            (0, chai_1.expect)(second.totalActivityPubMessagesProcessed).to.equal(processed);
            (0, chai_1.expect)(second.totalActivityPubMessagesSuccesses).to.equal(processed);
            (0, chai_1.expect)(second.totalActivityPubMessagesErrors).to.equal(0);
            for (const apType of apTypes) {
                (0, chai_1.expect)(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0);
            }
            yield (0, core_utils_1.wait)(6000);
            const third = yield servers[1].stats.get();
            (0, chai_1.expect)(third.totalActivityPubMessagesWaiting).to.equal(0);
            (0, chai_1.expect)(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond);
        });
    });
    after(function () {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield (0, server_commands_1.cleanupTests)(servers);
        });
    });
});
//# sourceMappingURL=stats.js.map