import axios from 'axios'
// 获取今天以及 90 天之前的日期,以 2024-06-01 的格式返回

function getDatesFormatted() {
  // 获取今天的日期
  const today = new Date()

  // 获取90天前的日期
  const ninetyDaysAgo = new Date(today)
  ninetyDaysAgo.setDate(today.getDate() - 90)

  // 格式化日期函数
  function formatDate(date) {
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    return `${year}-${month}-${day}`
  }

  // 返回格式化后的日期
  return {
    today: formatDate(today),
    ninetyDaysAgo: formatDate(ninetyDaysAgo)
  }
}

// 单位机时统计
export const getDepartDuration = () =>
  axios
    .post(`/screen_server/hpc/findTopByOrg?startDate=${getDatesFormatted().today}&endDate=${getDatesFormatted().ninetyDaysAgo}`)
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      console.log(error)
    })

// 软件机时统计
export const getSoftwareDuration = () =>
  axios
    .post(`/screen_server/hpc/findTopBySoft?startDate=${getDatesFormatted().today}&endDate=${getDatesFormatted().ninetyDaysAgo}`)
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      console.log(error)
    })

// 当前在用软件
export const getInUseSoftware = () =>
  axios
    .post(`/screen_server/hpc/findTopSoftList?startDate=${getDatesFormatted().today}&endDate=${getDatesFormatted().ninetyDaysAgo}`)
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      console.log(error)
    })

// 用户机时用量
export const getUserUseTrend = () =>
  axios
    .post(`/screen_server/hpc/findTopByUser?startDate=${getDatesFormatted().today}&endDate=${getDatesFormatted().ninetyDaysAgo}`)
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      console.log(error)
    })
// 软件集成数量
export const getSoftwareIntegrate = () =>
  axios
    .post(`/screen_server/hpc/findSoftCount?startDate=${getDatesFormatted().today}&endDate=${getDatesFormatted().ninetyDaysAgo}`)
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      console.log(error)
    })

// 查看帮助
export const getHelp = () =>
  axios
    .post('/screen_server/hpc/help/query')
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      console.log(error)
    })
// 实时利用率
export const getRealTimeUtilization = () =>
  axios
    .get('/prometheus/api/v1/query?query=avg(1 - avg(rate(node_cpu_seconds_total{mode="idle"}[2m])) by (instance)) * 100')
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      console.log(error)
    })

// 平均利用率趋势
export const getAverageUtilizationTrend = () =>
  axios
    .get('/prometheus/api/v1/query_range')
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      console.log(error)
    })

// 查询平台统计数据
export const getPlateformSum = () =>
  axios
    .post('/screen_server/hpc/findSum')
    .then(function (response) {
      return response.data
    })
    .catch(function (error) {
      console.log(error)
    })
