import { getPartRatio } from '../../number/number.mjs';

var SORT_KEY = 'PageUv';
function formatNum(num) {
  if (typeof num !== 'number') {
    return num;
  }
  return +num.toFixed(2);
}
/**
 * 解析汇总数据
 * @ignore
 * @param {object} options 配置
 * @param {Array<object>} options.data 汇总数据
 * @param {object} options.extraDataMap 额外数据Map，键为 ProjectId
 * @param {Array<number>} options.ignoreProjectIdList 忽略的projectIdList
 * @param {Array<string>} options.sortKeyList 排序key列表
 * @returns {Array<object>} 处理后的数据
 * @example
 *
 * const result = parseSummaryScore({
 *   data
 * })
 * // 结果如下：
 * [
 *   {
 *     ProjectName: { name: 'ProjectName', value: '脚手架' },
 *     PagePv: { name: 'PagePv', value: 544343 },
 *     PageUv: { name: 'PageUv', value: 225275 },
 *     Score: { name: 'Score', value: 90 },
 *     PageDuration: { name: 'PageDuration', value: 2894.08 },
 *     PageError: { name: 'PageError', value: 627 },
 *     PageErrorRatio: { name: 'PageErrorRatio', value: 0.12 },
 *     ApiNum: { name: 'ApiNum', value: 2506167 },
 *     ApiFail: { name: 'ApiFail', value: 3468 },
 *     ApiFailRatio: { name: 'ApiFailRatio', value: 0.14 },
 *     ApiDuration: { name: 'ApiDuration', value: 337.67 },
 *     StaticNum: { name: 'StaticNum', value: 9857894 },
 *     StaticFail: { name: 'StaticFail', value: 1174 },
 *     StaticFailRatio: { name: 'StaticFailRatio', value: 0.01 },
 *     StaticDuration: { name: 'StaticDuration', value: 154.5 },
 *   },
 *   {
 *     ProjectName: { name: 'ProjectName', value: '社区' },
 *     // ...
 *   },
 * ]
 */
function parseSummaryScore(_ref) {
  var data = _ref.data,
    _ref$extraDataMap = _ref.extraDataMap,
    extraDataMap = _ref$extraDataMap === void 0 ? {} : _ref$extraDataMap,
    _ref$ignoreProjectIdL = _ref.ignoreProjectIdList,
    ignoreProjectIdList = _ref$ignoreProjectIdL === void 0 ? [] : _ref$ignoreProjectIdL,
    _ref$sortKeyList = _ref.sortKeyList,
    sortKeyList = _ref$sortKeyList === void 0 ? [] : _ref$sortKeyList;
  var res = data.filter(function (item) {
    return !ignoreProjectIdList.includes(+item.ProjectId);
  }).sort(function (a, b) {
    return +b[SORT_KEY] - +a[SORT_KEY];
  }).map(function (item) {
    var temp = {};
    var projectId = item.ProjectId;
    var list = Object.keys(item).map(function (iKey) {
      return {
        name: iKey,
        value: formatNum(item[iKey])
      };
    }).concat([{
      name: 'ApiFailRatio',
      value: getPartRatio(item.ApiNum, item.ApiFail)
    }, {
      name: 'StaticFailRatio',
      value: getPartRatio(item.StaticNum, item.StaticFail)
    }, {
      name: 'PageErrorRatio',
      value: getPartRatio(item.PagePv, item.PageError)
    }]).concat(extraDataMap[projectId] || []).filter(function (item) {
      return sortKeyList.includes(item.name);
    }).sort(function (a, b) {
      return sortKeyList.indexOf(a.name) - sortKeyList.indexOf(b.name);
    });
    list.forEach(function (item) {
      temp[item.name] = item;
    });
    return temp;
  });
  return res;
}

export { parseSummaryScore };
