$(function () {
  function init() {
    // $.get('http://192.168.10.51:8088/test.txt', function (res) {
    $.get(location.origin + '/actuator/prometheus', function (res) {
      var obj = strToObj(res);
      console.log(obj);
      $('#yxsc').html(countDown(obj.process_uptime_seconds));
      // CPU使用率饼图
      createPie(obj.system_cpu_usage, 'sys-cpu-used', 'small');
      //平均负载数值
      $('#pjfz').html(obj.system_load_average_1m);

      fillTextColor('#pjfz', obj.system_load_average_1m, [3, 5]);
      //CPU数量数值
      $('#cpusl').html(parseInt(obj.system_cpu_count));

      createPie(obj.process_cpu_usage, 'java-cpu-used', 'small');
      createPie(
        obj.process_files_open_files / obj.process_files_max_files,
        'file-used',
        'small',
      );
      $('#zdwjs').html(parseInt(obj.process_files_max_files));
      $('#dkwjs').html(parseInt(obj.process_files_open_files));
      fillTextColorByRatio(
        '#dkwjs',
        obj.process_files_open_files / obj.process_files_max_files,
        [0.7, 0.9],
      );

      //   createPie()
      var maxMemory = getValueByReduce(obj.jvm_memory_max_bytes, 'heap');
      var addMemory = obj.jvm_memory_max_bytes.find(function (item) {
        return (
          item.conditions.area === 'heap' &&
          item.conditions.id === 'PS Survivor Space'
        );
      });
      if (addMemory) {
        maxMemory += Number(addMemory.value);
      }
      var usedMemory = getValueByReduce(obj.jvm_memory_used_bytes, 'heap');
      console.log('最大内存：', maxMemory);
      createPie(usedMemory / maxMemory, 'memory-used', 'small');
      var maxMemoryText =
        maxMemory / 1024 / 1024 < 1000
          ? parseInt(maxMemory / 1024 / 1024) + 'MB'
          : parseInt(maxMemory / 1024 / 1024 / 1024) + 'GB';
      var usedMemoryText =
        usedMemory / 1024 / 1024 < 1000
          ? parseInt(usedMemory / 1024 / 1024) + 'MB'
          : parseInt(usedMemory / 1024 / 1024 / 1024) + 'GB';
      $('#zdncs').html(maxMemoryText);
      $('#ysync').html(usedMemoryText);
      fillTextColorByRatio('#ysync', usedMemory / maxMemory, [0.7, 0.9]);
      $('#hyxcs').html(parseInt(obj.jvm_threads_live_threads));

      var maxThreads = getValueByReduce(obj.tomcat_threads_config_max_threads);
      var usedThreads = getValueByReduce(obj.tomcat_threads_busy_threads);
      var curThreads = getValueByReduce(obj.tomcat_threads_current_threads);

      createPie(usedThreads / maxThreads, 'threads-used', 'big');
      $('#zdxcs').html(maxThreads);
      $('#gzxcs').html(usedThreads);
      fillTextColorByRatio('#gzxcs', usedThreads / maxThreads, [0.7, 0.9]);
      $('#dqxcs').html(curThreads);

      var maxConnects = getValueByReduce(obj.hikaricp_connections_max);
      var activeConnects = getValueByReduce(obj.hikaricp_connections_active);
      var idleConnects = getValueByReduce(obj.hikaricp_connections_idle);
      var minConnects = getValueByReduce(obj.hikaricp_connections_min);

      createPie(activeConnects / maxConnects, 'connect-used', 'big');
      $('#zdljs').html(maxConnects);
      $('#hyljs').html(activeConnects);
      fillTextColorByRatio('#hyljs', activeConnects / maxConnects, [0.7, 0.9]);
      $('#kxljs').html(idleConnects);
      $('#zxljs').html(minConnects);
      // $.get('http://192.168.10.51:8088/health.json', function (data) {
      $.get(location.origin + '/actuator/health', function (data) {
        var details = data.components.diskSpace.details;
        //系统磁盘空间饼图
        createPie(
          (details.total - details.free) / details.total,
          'sys-disk-used',
          'small',
        );
        var dbStatus = data.components.db.status;
        var redisStatus = data.components.db.redis;
        if (dbStatus === 'UP') {
          $('.db-suc').show();
          $('.db-fail').hide();
        } else {
          $('.db-fail').show();
          $('.db-suc').hide();
        }
        if (!redisStatus) {
          $('.level3').hide();
        } else if (redisStatus === 'UP') {
          $('.level3').show();
          $('.redis-suc').show();
          $('.redis-fail').hide();
        } else {
          $('.level3').show();
          $('.redis-fail').show();
          $('.redis-suc').hide();
        }
      });
    });
  }
  init();
  var t = parseInt(localStorage.intervalTime);
  window.refreshTimer = setInterval(init, t || 5000);
  $('#refresh-time').val(t || 5000);
  $('#refresh-time').on('change', function () {
    clearInterval(window.refreshTimer);
    window.refreshTimer = null;
    window.refreshTimer = setInterval(init, this.value);
    localStorage.setItem('intervalTime', this.value);
  });
  $('#refresh-btn').on('click', init);
});
