'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../../tslib.es6-0d92ef81.js');
var axios = require('axios');
var tam_api_credential = require('./credential.js');

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

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);

function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
 * 根据项目id获取分数信息
 * @ignore
 * @param {object} options 参数
 * @param {Array<number>} options.projectIdList 项目Id列表
 * @param {string} options.date 日期，yyyyMMdd格式
 * @param {object} options.secretInfo 密钥信息
 * @param {string} options.secretInfo.apiKey apiKey
 * @param {string} options.secretInfo.loginName loginName
 * @param {Function} options.secretInfo.getPwdCode getPwdCode
 * @param {Function} options.secretInfo.encrypt encrypt
 * @returns {Promise<Array<object>>} 分数信息
 * @example
 * getTAMScoreInfoByProjectId({
 *   projectId: 123123,
 *   date: 20210106
 *   secretInfo: {
 *     apiKey: '',
 *     loginName: '',
 *     getPwdCode() {},
 *     encrypt() {},
 *   }
 * }).then((data) => {
 *   console.log(data)
 * })
 *
 * [
 *   {
 *     ProjectName: '社区',
 *     PagePv: 99,
 *     PageError: 0,
 *     PageDuration: 1409.8333,
 *     StaticFail: 8,
 *     CreateTime: '',
 *     ProjectId: 123123,
 *     PageUv: 23,
 *     ApiNum: 521,
 *     ApiFail: 78,
 *     ApiDuration: 1813.3333,
 *     StaticNum: 1494,
 *     StaticDuration: 103.75,
 *     Score: 80.9167,
 *     CreateUser: 'lee',
 *     GroupName: 'aGroup',
 *   },
 * ]
 *
 */
function getTAMScoreInfoByProjectId(_ref) {
  var projectId = _ref.projectId,
    startDate = _ref.startDate,
    secretInfo = _ref.secretInfo;
  var _a;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var credential, result;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return tam_api_credential.getCredential(secretInfo);
        case 1:
          credential = _context.sent;
          _context.next = 2;
          return axios__default["default"]({
            method: 'GET',
            url: "/api/interface/pro/scoreInfo?projectID=".concat(projectId, "&startDate=").concat(startDate),
            headers: Object.assign({}, credential)
          })["catch"](function (err) {
            console.log('[getTAMScoreInfoByProjectId.err]', err);
          });
        case 2:
          result = _context.sent;
          return _context.abrupt("return", ((_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.message) || []);
        case 3:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
/**
 * 根据 groupId 获取项目列表
 * @ignore
 * @param {object} options 配置
 * @param {number} options.groupId 小组Id
 * @param {object} options.secretInfo 密钥信息
 * @param {string} options.secretInfo.apiKey apiKey
 * @param {string} options.secretInfo.loginName loginName
 * @param {Function} options.secretInfo.getPwdCode getPwdCode
 * @param {Function} options.secretInfo.encrypt encrypt
 * @returns {Promise<array<object>>} 项目列表
 * @example
 * getProjectByGroupId({
 *   groupId: 1,
 *   secretInfo: {
 *     apiKey: '',
 *     loginName: '',
 *     getPwdCode() {},
 *     encrypt() {},
 *   }
 * }).then(resp => {
 *   console.log(resp)
 * })
 * [
 *   {
 *     ID: 56564,
 *     ProjectName: 'name',
 *     ProjectDesc: 'desc',
 *     ProjectKey: 'xxx',
 *     ProjectType: 'web',
 *     GroupId: 123,
 *     GroupName: 'xxx',
 *     GroupKey: 'xxx',
 *     InstanceID: 'rum-xxx',
 *     Url: '*.qq.com',
 *     CodePath: '',
 *     Rate: '100',
 *     CreateUser: 'xxx',
 *     EnableUrlGroup: true,
 *     KafkaHost: '',
 *     KafkaTopic: '',
 *     KafkaVersion: '',
 *     SaslUserName: '',
 *     SaslPassword: '',
 *     SaslMechanism: '',
 *     IsFollow: false,
 *     CreateTime: '2021-10-01T11:34:32+08:00',
 *   },
 *   {
 *     ID: 12345,
 *     // ...
 *   },
 * ];
 *
 */
function getProjectByGroupId(_ref2) {
  var groupId = _ref2.groupId,
    secretInfo = _ref2.secretInfo;
  var _a;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
    var credential, result;
    return _regeneratorRuntime__default["default"].wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 1;
          return tam_api_credential.getCredential(secretInfo);
        case 1:
          credential = _context2.sent;
          _context2.next = 2;
          return axios__default["default"]({
            method: 'GET',
            url: "/api/interface/pro/list?groupID=".concat(groupId),
            headers: Object.assign({}, credential)
          })["catch"](function (err) {
            console.log('[getProjectByGroupId.err]', err);
          });
        case 2:
          result = _context2.sent;
          return _context2.abrupt("return", ((_a = result === null || result === void 0 ? void 0 : result.data) === null || _a === void 0 ? void 0 : _a.result) || []);
        case 3:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}
/**
 * 根据 groupIdList 获取 projectList
 * @ignore
 * @param {object} options 配置
 * @param {Array<number>} options.groupIdList 小组Id列表
 * @param {object} options.secretInfo 密钥信息
 * @param {string} options.secretInfo.apiKey apiKey
 * @param {string} options.secretInfo.loginName loginName
 * @param {Function} options.secretInfo.getPwdCode getPwdCode
 * @param {Function} options.secretInfo.encrypt encrypt
 * @returns {Array<object>} 项目列表
 * @example
 * getAllProjectList({
 *   groupIdList: [1,2,3],
 *   secretInfo: {
 *     apiKey: '',
 *     loginName: '',
 *     getPwdCode() {},
 *     encrypt() {},
 *   }
 * }).then(resp => {
 *   console.log('resp)
 * })
 * [
 *   {
 *     ID: 56564,
 *     ProjectName: 'name',
 *     ProjectDesc: 'desc',
 *     ProjectKey: 'xxx',
 *     ProjectType: 'web',
 *     GroupId: 123,
 *     GroupName: 'xxx',
 *     GroupKey: 'xxx',
 *     InstanceID: 'rum-xxx',
 *     Url: '*.qq.com',
 *     CodePath: '',
 *     Rate: '100',
 *     CreateUser: 'xxx',
 *     EnableUrlGroup: true,
 *     KafkaHost: '',
 *     KafkaTopic: '',
 *     KafkaVersion: '',
 *     SaslUserName: '',
 *     SaslPassword: '',
 *     SaslMechanism: '',
 *     IsFollow: false,
 *     CreateTime: '2021-10-01T11:34:32+08:00',
 *   },
 *   {
 *     ID: 12345,
 *     // ...
 *   },
 * ];
 */
function getAllProjectList(_ref3) {
  var groupIdList = _ref3.groupIdList,
    secretInfo = _ref3.secretInfo;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
    var res, _iterator, _step, groupId, projectList, _t;
    return _regeneratorRuntime__default["default"].wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          res = [];
          _iterator = _createForOfIteratorHelper(groupIdList);
          _context3.prev = 1;
          _iterator.s();
        case 2:
          if ((_step = _iterator.n()).done) {
            _context3.next = 5;
            break;
          }
          groupId = _step.value;
          _context3.next = 3;
          return getProjectByGroupId({
            groupId: groupId,
            secretInfo: secretInfo
          });
        case 3:
          projectList = _context3.sent;
          res.push.apply(res, _toConsumableArray__default["default"](projectList || []));
        case 4:
          _context3.next = 2;
          break;
        case 5:
          _context3.next = 7;
          break;
        case 6:
          _context3.prev = 6;
          _t = _context3["catch"](1);
          _iterator.e(_t);
        case 7:
          _context3.prev = 7;
          _iterator.f();
          return _context3.finish(7);
        case 8:
          return _context3.abrupt("return", res);
        case 9:
        case "end":
          return _context3.stop();
      }
    }, _callee3, null, [[1, 6, 7, 8]]);
  }));
}
/**
 * 根据 projectIdList 获取一天的汇总分数
 * @ignore
 * @param {object} options 参数
 * @param {Array<number>} options.projectIdList 项目Id列表
 * @param {string} options.date 日期，yyyyMMdd格式
 * @param {object} options.secretInfo 密钥信息
 * @param {string} options.secretInfo.apiKey apiKey
 * @param {string} options.secretInfo.loginName loginName
 * @param {Function} options.secretInfo.getPwdCode getPwdCode
 * @param {Function} options.secretInfo.encrypt encrypt
 * @returns {Promise<Array<object>>} 分数列表
 *
 * @example
 * getSummaryScoreByProjectList({
 *   projectList: [],
 *   date: 20210106
 *   secretInfo: {
 *     apiKey: '',
 *     loginName: '',
 *     getPwdCode() {},
 *     encrypt() {},
 *   }
 * }).then((data) => {
 *   console.log(data)
 * })
 *
 * [
 *   {
 *     ProjectName: 'name',
 *     PagePv: 99,
 *     PageError: 0,
 *     PageDuration: 1409.8333,
 *     StaticFail: 8,
 *     CreateTime: '',
 *     ProjectId: 123123,
 *     PageUv: 23,
 *     ApiNum: 521,
 *     ApiFail: 78,
 *     ApiDuration: 1813.3333,
 *     StaticNum: 1494,
 *     StaticDuration: 103.75,
 *     Score: 80.9167,
 *     CreateUser: 'xxx',
 *     GroupName: 'xxx',
 *   },
 *   {
 *     // ...
 *   }
 * ]
 */
function getSummaryScoreByProjectList(_ref4) {
  var projectList = _ref4.projectList,
    date = _ref4.date,
    secretInfo = _ref4.secretInfo;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4() {
    var res, _iterator2, _step2, project, temp, projectInfo, _t2;
    return _regeneratorRuntime__default["default"].wrap(function (_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          res = [];
          _iterator2 = _createForOfIteratorHelper(projectList);
          _context4.prev = 1;
          _iterator2.s();
        case 2:
          if ((_step2 = _iterator2.n()).done) {
            _context4.next = 6;
            break;
          }
          project = _step2.value;
          _context4.next = 3;
          return getTAMScoreInfoByProjectId({
            projectId: project.ID,
            startDate: date,
            secretInfo: secretInfo
          });
        case 3:
          temp = _context4.sent;
          projectInfo = Object.assign(Object.assign({}, project), temp[0] || {}); // 过滤掉没有数据的项目
          if (projectInfo.PagePv) {
            _context4.next = 4;
            break;
          }
          return _context4.abrupt("continue", 5);
        case 4:
          res.push(projectInfo);
        case 5:
          _context4.next = 2;
          break;
        case 6:
          _context4.next = 8;
          break;
        case 7:
          _context4.prev = 7;
          _t2 = _context4["catch"](1);
          _iterator2.e(_t2);
        case 8:
          _context4.prev = 8;
          _iterator2.f();
          return _context4.finish(8);
        case 9:
          return _context4.abrupt("return", res);
        case 10:
        case "end":
          return _context4.stop();
      }
    }, _callee4, null, [[1, 7, 8, 9]]);
  }));
}
/**
 * 根据 groupIdList 获取汇总数据
 * @ignore
 * @param {object} options 配置
 * @param {string} options.date 日期，yyyyMMdd格式
 * @param {Array<number>} options.groupIdList groupId列表
 * @param {object} options.secretInfo 密钥信息
 * @param {string} options.secretInfo.apiKey apiKey
 * @param {string} options.secretInfo.loginName loginName
 * @param {Function} options.secretInfo.getPwdCode getPwdCode
 * @param {Function} options.secretInfo.encrypt encrypt
 * @returns {Promise<({ data: object, projectIdList: Array<number> })>} 汇总数据
 * @example
 * getTAMSummaryScoreByGroupIdList({
 *   groupIdList: [1,2,3],
 *   date: '20210106',
 *   secretInfo: {
 *     apiKey: '',
 *     loginName: '',
 *     getPwdCode() {},
 *     encrypt() {},
 *   }
 * }).then(resp => {
 *   console.log(resp)
 * })
 *
 * {
 *   data:
 *   [
 *     {
 *       ProjectName: '社区',
 *       PagePv: 99,
 *       PageError: 0,
 *       PageDuration: 1409.8333,
 *       StaticFail: 8,
 *       CreateTime: '',
 *       ProjectId: 123123,
 *       PageUv: 23,
 *       ApiNum: 521,
 *       ApiFail: 78,
 *       ApiDuration: 1813.3333,
 *       StaticNum: 1494,
 *       StaticDuration: 103.75,
 *       Score: 80.9167,
 *       CreateUser: 'lee',
 *       GroupName: 'aGroup',
 *     },
 *     {
 *       ProjectId: 123,
 *       // ...
 *     },
 *   ],
 *   projectIdList: [
 *     123123,
 *     123,
 *   ],
 * };
 *
 */
function getTAMSummaryScoreByGroupIdList(_ref5) {
  var date = _ref5.date,
    groupIdList = _ref5.groupIdList,
    secretInfo = _ref5.secretInfo;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee5() {
    var projectList, projectIdList, res;
    return _regeneratorRuntime__default["default"].wrap(function (_context5) {
      while (1) switch (_context5.prev = _context5.next) {
        case 0:
          _context5.next = 1;
          return getAllProjectList({
            groupIdList: groupIdList,
            secretInfo: secretInfo
          });
        case 1:
          projectList = _context5.sent;
          projectIdList = projectList.map(function (item) {
            return item.ID;
          });
          _context5.next = 2;
          return getSummaryScoreByProjectList({
            date: date,
            projectList: projectList,
            secretInfo: secretInfo
          });
        case 2:
          res = _context5.sent;
          return _context5.abrupt("return", {
            data: res,
            projectIdList: projectIdList
          });
        case 3:
        case "end":
          return _context5.stop();
      }
    }, _callee5);
  }));
}

exports.getProjectByGroupId = getProjectByGroupId;
exports.getTAMScoreInfoByProjectId = getTAMScoreInfoByProjectId;
exports.getTAMSummaryScoreByGroupIdList = getTAMSummaryScoreByGroupIdList;
