/**
 *  性能统计服务
 */
import HelperUtil from '../../utils/HelperUtil'
import PerformanceStatisticAPI from '../../api/restApis/PerformanceStatisticAPI'
import {RT_PERTYPE_KEY} from '../../utils/constantCollection'

// 静态方法
let staticMethods = {
  /**
   * @Description  : 获取性能事件
   * @author       : ls
   * @date         : 2020/5/12 16:42
   * @param        :
   * @return       :
   */
  getPerformTypeService: function () {
    return PerformanceStatisticAPI.getPerformTypeAPI().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, RT_PERTYPE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 性能分布统计
   * @author       : ls
   * @date         : 2020/5/12 17:39
   * @param        :
   * @return       :
   */
  queryPerformDistributionReport: function (statisticsList) {
    return PerformanceStatisticAPI.queryPerformDistributionReportApi(statisticsList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 性能趋势
   * @author       : ls
   * @date         : 2020/5/13 10:38
   * @param        :
   * @return       :
   */
  queryPerformTrendStatistic: function (valueType, statisticsList) {
    return PerformanceStatisticAPI.putPerformanceStatistic(valueType, statisticsList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 获取统计图数据
  queryPerformStatisticsData: function (valueType, statisticsList) {
    return PerformanceStatisticAPI.putPerformanceStatistic(valueType, statisticsList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 性能top
   * @author       : ls
   * @date         : 2020/5/13 16:38
   * @param        :
   * @return       :
   */
  queryPerformTopBottomData: function (valueType, valueTopN, statisticsList) {
    return PerformanceStatisticAPI.putPerformanceReports(valueType, valueTopN, statisticsList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  queryPerformReportsData: function (valueType, valueTopN, statisticsList) {
    return PerformanceStatisticAPI.putPerformanceReports(valueType, valueTopN, statisticsList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  }
}

function PerformanceStatisticService (websocketService = null) {
  return Object.freeze(Object.assign(
    {
      // object methods comes here
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(PerformanceStatisticService, staticMethods)
export default PerformanceStatisticService
