import axios from 'axios'
import { getDatesFormattedBy90day, getTimestampsBy90day } from './util.js'

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

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

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

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

const step = '532800'
// 平均利用率趋势
export const getAverageUtilizationTrend = () =>
  axios
    .get(`/prometheus/api/v1/query_range?query=${encodeURIComponent('avg(1 - avg(rate(node_cpu_seconds_total{mode="idle"}[2m]))) * 100')}&start=${getTimestampsBy90day().ninetyDaysAgo}&end=${getTimestampsBy90day().today}&step=${step}`)
    .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)
    })
