using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace JiShe.CollectBusEPO.Extensions.HealthCheck
{
    public static class Extensions
    {
        /// <summary>
        ///     添加监控检查,支持配置文件配置
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static IServiceCollection AddHealthChecks(
            this IServiceCollection services, IConfiguration configuration)
        {
            if (Convert.ToBoolean(configuration["HealthCheck:IsEnable"]))
            {
                var healthChecksService = services
                    .AddHealthChecks();

                //添加对mysql的监控检查
                //if (Convert.ToBoolean(configuration["HealthCheck:MySql:IsEnable"]))
                //{
                //    var connectionString = configuration["HealthCheck:MySql:Connection"];
                //    if (connectionString.IsNullOrWhiteSpace())
                //        connectionString = configuration.GetConnectionString("Default");
                //    healthChecksService.AddMySql(
                //        connectionString,
                //        "Mysql",
                //        HealthStatus.Degraded,
                //        new string[] { "db", "sql", "mysql" }
                //    );
                //}
                //添加对Pings的监控检查
                //if (Convert.ToBoolean(configuration["HealthCheck:Pings:IsEnable"]))
                //    healthChecksService.AddPingHealthCheck(setup =>
                //    {
                //        setup.AddHost(configuration["HealthCheck:Pings:Host"], Convert.ToInt32(configuration["HealthCheck:Pings:TimeOut"]));
                //    }, tags: new string[] { "ping" });

                ////添加对postgresql的监控检查
                //if (Convert.ToBoolean(configuration["HealthCheck:PostgreSql:IsEnable"]))
                //    healthChecksService.AddNpgSql(
                //        configuration["ConnectionStrings:Default"],
                //        name: configuration["HealthCheck:PostgreSql:Name"],
                //        failureStatus: HealthStatus.Degraded,
                //        tags: new string[] { "db", "sql", "postgresql" }
                //        );

                ////添加对Redis的监控检查
                //if (Convert.ToBoolean(configuration["HealthCheck:Redis:IsEnable"]))
                //    healthChecksService.AddRedis(
                //        configuration["HealthCheck:Redis:ConnectionString"],
                //        tags: new string[] { "redis" }
                //    );

                ////添加对RabbitMq的监控检查
                //if (Convert.ToBoolean(configuration["HealthCheck:RabbitMq:IsEnable"]))
                //    healthChecksService.AddRabbitMQ(
                //        rabbitConnectionString: configuration["HealthCheck:RabbitMq:ConnectionString"],
                //        tags: new string[] { "redis" }
                //    );

                ////添加对sql server的监控检查
                //if (Convert.ToBoolean(configuration["HealthCheck:SqlServer:IsEnable"]))
                //    healthChecksService.AddSqlServer(
                //        configuration["ConnectionStrings:Default"],
                //        "SELECT 1;",
                //        configuration["HealthCheck:SqlServer:Name"],
                //        HealthStatus.Degraded,
                //        new string[] { "db", "sql", "sqlserver" }

                ////添加对Hangfire的监控检查
                //if (Convert.ToBoolean(configuration["HealthCheck:Hangfire:IsEnable"]))
                //    healthChecksService.AddHangfire(options =>
                //    {
                //        options.MaximumJobsFailed = Convert.ToInt32(configuration["HealthCheck:Hangfire:MaximumJobsFailed"]);
                //        options.MinimumAvailableServers = Convert.ToInt32(configuration["HealthCheck:Hangfire:MinimumAvailableServers"]);
                //    }, tags: new string[] { "hangfire" });

                ////添加对SignalR的监控检查
                //if (Convert.ToBoolean(configuration["HealthCheck:SignalR:IsEnable"]))
                //    healthChecksService.AddSignalRHub(configuration["HealthCheck:SignalR:Url"], tags: new string[] { "signalr" });
            }
            return services;
        }
    }
}