import { ApiCall } from "tsrpc";
import { Reqhealthz, Reshealthz } from "../monopoly/protocols/Ptlhealthz";

export default async function (call: ApiCall<Reqhealthz, Reshealthz>) {
    let _chkMDB = await G.mongodb.indexInformation("user")
    let _chkRedis = await G.redis.chkRedis()

    let _dateTime = new Date();
    let _newTime = _dateTime.format('YYYY-MM-DDTHH:MM+01:00');

    // 构造返回值
    let _data: Reshealthz = {
        code: 0,
        data: {
            dependencies: []
        }
    }

    let _names = ["mongodb", "redis"]
    for (let index = 0; index < [_chkMDB, _chkRedis].length; index++) {
        const element = [_chkMDB, _chkRedis][index];

        let _tmp = {
            dependency: _names[index],
            status: (_names[index] == "mongodb" ? _chkMDB : _chkRedis) ? 0 : 102,
            reason: '',
            severity: _names[index] == "mongodb" ? "critical" : "warning",
            lastcheck: _newTime
        }

        _data.code = _tmp.status
        _data.data.dependencies.push(_tmp)

    }

    call.succ(_data);
}