'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../../tslib.es6-0d92ef81.js');
var tencentCloud_base_base = require('../../tencent-cloud/base/base.js');
require('axios');
require('../../nodejs/crypto.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);

function getRUMAllProject(_ref) {
  var secretId = _ref.secretId,
    secretKey = _ref.secretKey;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          return _context.abrupt("return", tencentCloud_base_base.fetchCloudData({
            secretId: secretId,
            secretKey: secretKey,
            action: 'DescribeProjects',
            payload: JSON.stringify({
              Limit: 1000,
              Offset: 0
            })
          }).then(function (res) {
            var resp = res.data.Response || {};
            return {
              data: resp.ProjectSet || [],
              total: resp.TotalCount || 0
            };
          })["catch"](function (err) {
            console.log('[getRUMAllProject] err: ', err);
          }));
        case 1:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
function getRUMScoreInfo(_ref2) {
  var secretId = _ref2.secretId,
    secretKey = _ref2.secretKey,
    startTime = _ref2.startTime,
    endTime = _ref2.endTime;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
    return _regeneratorRuntime__default["default"].wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          return _context2.abrupt("return", tencentCloud_base_base.fetchCloudData({
            secretId: secretId,
            secretKey: secretKey,
            action: 'DescribeScores',
            payload: JSON.stringify({
              StartTime: startTime,
              EndTime: endTime
            })
          }).then(function (res) {
            var resp = res.data.Response || {};
            return resp.ScoreSet || [];
          })["catch"](function (err) {
            console.log('[getRUMScores] err: ', err);
          }));
        case 1:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}
function getRUMScores(_ref3) {
  var secretId = _ref3.secretId,
    secretKey = _ref3.secretKey,
    startTime = _ref3.startTime,
    endTime = _ref3.endTime;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
    var scoreList, project, projectMap, score;
    return _regeneratorRuntime__default["default"].wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          _context3.next = 1;
          return getRUMScoreInfo({
            secretId: secretId,
            secretKey: secretKey,
            startTime: startTime,
            endTime: endTime
          });
        case 1:
          scoreList = _context3.sent;
          _context3.next = 2;
          return getRUMAllProject({
            secretId: secretId,
            secretKey: secretKey
          });
        case 2:
          project = _context3.sent;
          projectMap = project.data.reduce(function (acc, item) {
            acc[item.ID] = item;
            return acc;
          }, {});
          score = scoreList.map(function (item) {
            var projectInfo = projectMap[item.ProjectID] || {};
            return Object.assign(Object.assign({}, item), {
              PagePv: +item.PagePv,
              PageUv: +item.PageUv,
              PageDuration: +item.PageDuration,
              PageError: +item.PageError,
              ApiNum: +item.ApiNum,
              ApiFail: +item.ApiFail,
              ApiDuration: +item.ApiDuration,
              StaticNum: +item.StaticNum,
              StaticFail: +item.StaticFail,
              StaticDuration: +item.StaticDuration,
              Score: +item.Score,
              ProjectName: projectInfo.Name,
              GroupName: projectInfo.InstanceName || '',
              CreateUser: projectInfo.Creator,
              ProjectId: item.ProjectID
            });
          });
          return _context3.abrupt("return", score);
        case 3:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}

exports.getRUMAllProject = getRUMAllProject;
exports.getRUMScores = getRUMScores;
