import store from '@/store'
import http from './httpRequest'

// 获取kubesphere Token
export function getNewToken() {
}

// 获取监控中心的数据
export function getMetrics() {
  const metrics = {}
  http({
    url: '/kubesphereApi/kapis/monitoring.kubesphere.io/v1alpha3/cluster',
    method: 'get'
  }).then(({ data }) => {
    data.results.forEach((element) => {
      const key = element.metric_name
      if (element.data.result) {
        const value = element.data.result[0].value[1]
        metrics[key] = value
      }
    })
    store.dispatch('monitor/setMetrics', metrics)
  })
}

// 获取apiserver数据
export function getApiServer() {
  // 先声明一个数据接收
  const apiserver = {}
  // 发送指定的api请求
  http({
    url: '/kubesphereApi/kapis/monitoring.kubesphere.io/v1alpha3/components/apiserver',
    method: 'get'
    // 带上授权

  }).then(({ data }) => {
    // 获取的data结构体使用foreach进行遍历
    data.results.forEach((element) => {
      const key = element.metric_name
      if (element.data.result) {
        const value = element.data.result[0].value[1]
        apiserver[key] = value
      }
    })
    store.dispatch('monitor/setApiServer', apiserver)
  })
}

// 获取调度器信息
export function getScheduler() {
  // 先声明一个数据接收
  const scheduler = {}
  // 发送指定的api请求
  http({
    url: '/kubesphereApi/kapis/monitoring.kubesphere.io/v1alpha3/components/scheduler',
    method: 'get'
    // 带上授权

  }).then(({ data }) => {
    // 获取的data结构体使用foreach进行遍历
    data.results.forEach((element) => {
      const key = element.metric_name
      if (element.data.result) {
        const value = element.data.result[0].value[1]
        scheduler[key] = value
      }
    })
    store.dispatch('monitor/setScheduler', scheduler)
  })
}

export function getNodes() {
  // 发送指定的api请求
  var nodes = {}
  // 发送指定的api请求
  // this.$http({
  http({
    url: '/kubesphereApi/kapis/monitoring.kubesphere.io/v1alpha3/nodes',
    method: 'get',
    params: {
      sort_metric: "node_cpu_utilisation"
    }
    // 带上授权
  }).then(({ data }) => {
    // 获取的data结构体使用foreach进行遍历
    // data.results.forEach((element) => {
    //   const key = element.metric_name
    //   const host = element.data.result[0].metric.host_ip
    //   const role = element.data.result[0].metric.role
    //   const node = element.data.result[0].metric.node
    //   const value = element.data.result[0].value[1]

    //   // 有值
    //   if (host) {
    //     nodes['host'] = host
    //   }
    //   if (role) {
    //     nodes['role'] = role
    //   }
    //   if (node) {
    //     nodes['node'] = node
    //   }
    //   // this.$set(this.nodes, key, value)

    //   nodes[key] = value
    // })
    // this.$store.dispatch("monitor/setNodes", this.nodes);
    // 多节点
    var nodeList = [];
    for (var i = 0; i < data.total_item; i++) {
      var nodes = {};
      for (var j = 0; j < data.results.length; j++) {
        if (data.total_item == data.results[j].data.result.length) {
          const element = data.results[j];
          const key = element.metric_name
          if (key == "node_pod_succeeded_count" || element.data.result[i].metric == undefined) {
            continue;
          }
          const host = element.data.result[i].metric.host_ip;
          const role = element.data.result[i].metric.role;
          const node = element.data.result[i].metric.node;
          const value = element.data.result[i].value[1];

          // 有值
          if (host) {
            nodes['host'] = host;
          }
          if (role) {
            nodes['role'] = role;
          }
          if (node) {
            nodes['node'] = node;
          }
          // this.$set(this.nodes, key, value)

          nodes[key] = value;
        }
      }
      nodeList.push(nodes);
    }
    store.dispatch('monitor/setNodesList', nodeList)
    console.log('getNodes', store.state.monitor.nodesList)
  })
}

export function getPhysicalMonitor() {
  const physical = {}
  var nowtime = Math.round(new Date().getTime() / 1000).toString()
  http({
    url: '/kubesphereApi/kapis/monitoring.kubesphere.io/v1alpha3/cluster',
    method: 'get',
    params: http.adornParams({
      start: nowtime - 4000,
      end: nowtime,
      step: '300s',
      times: 50,
      metrics_filter:
        'cluster_cpu_utilisation|cluster_memory_utilisation|cluster_load1|cluster_load5|cluster_load15|cluster_disk_size_usage|cluster_disk_inode_utilisation|cluster_disk_inode_usage|cluster_disk_inode_total|cluster_disk_read_iops|cluster_disk_write_iops|cluster_disk_read_throughput|cluster_disk_write_throughput|cluster_net_bytes_transmitted|cluster_net_bytes_received|cluster_pod_running_count|cluster_pod_abnormal_count|cluster_pod_succeeded_count%24'
    })
  }).then(({ data }) => {
    data.results.forEach((element) => {
      const key = element.metric_name
      const value = element.data.result[0].values
      value.forEach((test) => {
        physical[key] = value
      })
    })
    store.dispatch('monitor/setPhysicalMonitor', physical) // this.physical=physical;
    // console.log('store.state.monitor.physicalMonitor', store.state.monitor.physicalMonitor)
  })
}

export function getAll() {
  getApiServer()
  getMetrics()
  getNodes()
  getScheduler()
}
