import Chart from "../core.js";
import { DriveFiles, Followings, Users, Notes } from "../../../models/index.js";
import { toPuny } from "../../../misc/convert-host.js";
import { name, schema } from "./entities/instance.js";
/**
 * インスタンスごとのチャート
 */ export default class InstanceChart extends Chart {
    constructor(){
        super(name, schema, true);
    }
    async tickMajor(group) {
        const [notesCount, usersCount, followingCount, followersCount, driveFiles] = await Promise.all([
            Notes.countBy({
                userHost: group
            }),
            Users.countBy({
                host: group
            }),
            Followings.countBy({
                followerHost: group
            }),
            Followings.countBy({
                followeeHost: group
            }),
            DriveFiles.countBy({
                userHost: group
            })
        ]);
        return {
            "notes.total": notesCount,
            "users.total": usersCount,
            "following.total": followingCount,
            "followers.total": followersCount,
            "drive.totalFiles": driveFiles
        };
    }
    async tickMinor() {
        return {};
    }
    async requestReceived(host) {
        await this.commit({
            "requests.received": 1
        }, toPuny(host));
    }
    async requestSent(host, isSucceeded) {
        await this.commit({
            "requests.succeeded": isSucceeded ? 1 : 0,
            "requests.failed": isSucceeded ? 0 : 1
        }, toPuny(host));
    }
    async newUser(host) {
        await this.commit({
            "users.total": 1,
            "users.inc": 1
        }, toPuny(host));
    }
    async updateNote(host, note, isAdditional) {
        await this.commit({
            "notes.total": isAdditional ? 1 : -1,
            "notes.inc": isAdditional ? 1 : 0,
            "notes.dec": isAdditional ? 0 : 1,
            "notes.diffs.normal": note.replyId == null && note.renoteId == null ? isAdditional ? 1 : -1 : 0,
            "notes.diffs.renote": note.renoteId != null ? isAdditional ? 1 : -1 : 0,
            "notes.diffs.reply": note.replyId != null ? isAdditional ? 1 : -1 : 0,
            "notes.diffs.withFile": note.fileIds.length > 0 ? isAdditional ? 1 : -1 : 0
        }, toPuny(host));
    }
    async updateFollowing(host, isAdditional) {
        await this.commit({
            "following.total": isAdditional ? 1 : -1,
            "following.inc": isAdditional ? 1 : 0,
            "following.dec": isAdditional ? 0 : 1
        }, toPuny(host));
    }
    async updateFollowers(host, isAdditional) {
        await this.commit({
            "followers.total": isAdditional ? 1 : -1,
            "followers.inc": isAdditional ? 1 : 0,
            "followers.dec": isAdditional ? 0 : 1
        }, toPuny(host));
    }
    async updateDrive(file, isAdditional) {
        const fileSizeKb = file.size / 1000;
        await this.commit({
            "drive.totalFiles": isAdditional ? 1 : -1,
            "drive.incFiles": isAdditional ? 1 : 0,
            "drive.incUsage": isAdditional ? fileSizeKb : 0,
            "drive.decFiles": isAdditional ? 1 : 0,
            "drive.decUsage": isAdditional ? fileSizeKb : 0
        }, file.userHost);
    }
}
