UNPKG

youzanyun-devtool-worker

Version:

217 lines (216 loc) 8.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const lodash_1 = require("lodash"); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const spring4js_nodejs_1 = require("spring4js-nodejs"); let hasFetchedMepData = false; let MepService = class MepService { constructor() { } async start() { this.loggger = this.logService.getLogger('MepService'); const dataDir = this.configService.getDataDir(); this.mepListCacheFile = path_1.default.resolve(dataDir, 'mep-total.json'); const exists = await fs_extra_1.default.pathExists(this.mepListCacheFile); if (exists) { const stat = fs_extra_1.default.statSync(this.mepListCacheFile); const distance = (Date.now() - Math.floor(stat.mtimeMs)) / (24 * 60 * 60 * 1000); if (distance > 2) { fs_extra_1.default.readFileSync(this.mepListCacheFile); } } } async getAllMep() { let mepInfo; let exists = await fs_extra_1.default.pathExists(this.mepListCacheFile); if (exists) { mepInfo = await fs_extra_1.default.readJson(this.mepListCacheFile); } this.fetchAllMep(); return mepInfo; } async getAllMepMap() { let mepTopicMap = {}; let mepIdMap = {}; let mepList = await this.getAllMep(); for (let bizLine of mepList) { let categoryList = bizLine.pushCategoryDTOList || []; for (let category of categoryList) { let list = category.bizTypeDTOList || []; for (let mep of list) { let item = { id: mep.id, name: mep.name, bizType: mep.bizType, bizLineId: bizLine.bizLineId, }; mepTopicMap[mep.bizType] = item; mepIdMap[mep.id] = item; } } } return { mepTopicMap, mepIdMap }; } async getMepTestHistory(projectId, mepId) { let localTestList = await this.getMepLocalTestHistory(projectId, mepId); let { mepTopicMap, mepIdMap } = await this.getAllMepMap(); let mep = mepIdMap[mepId]; let remoteTestList = await this.mockPlatformService.queryMepRemoteTestHistory(projectId, mep.bizType); let testList = [...localTestList]; for (let remote of remoteTestList) { try { let mep = mepTopicMap[remote.interfaceName] || {}; testList.push({ createdAt: remote.createdAt, retmote: true, item: mep, param: remote.inParam && JSON.parse(remote.inParam), response: { timeCost: '--', result: remote.outParam && JSON.parse(remote.outParam), }, }); } catch (err) { this.loggger.error('getMepTestHistory', err); } } testList.sort((a, b) => { return -(a.createdAt - b.createdAt); }); return testList; } async getMepLocalTestHistory(projectId, mepId) { let mepList = []; let dataDir = this.configService.getDataDir(); let mepHistoryCacheFile = path_1.default.resolve(dataDir, `mep-test-history.${projectId}.${mepId}.json`); let exists = await fs_extra_1.default.pathExists(mepHistoryCacheFile); if (exists) { mepList = await fs_extra_1.default.readJson(mepHistoryCacheFile); } return mepList; } async saveMepTestHistory(projectId, mepId, callList) { let dataDir = this.configService.getDataDir(); let bepHistoryCacheFile = path_1.default.resolve(dataDir, `mep-test-history.${projectId}.${mepId}.json`); if (callList.length > 20) { callList = callList.slice(0, 20); } await fs_extra_1.default.writeJson(bepHistoryCacheFile, callList, { spaces: 2 }); } async testLocalMep(projectId, item, param) { let projectInfo = await this.projectService.getProjectById(projectId); let port; if (projectInfo.isJava) { port = 7001; } else if (projectInfo.isNodeJs) { port = 8001; } else if (projectInfo.isPhp) { } let response = { timeCost: 0, result: {}, }; let startTime = Date.now(); try { let resData = await this.requestService.post(`http://127.0.0.1:${port}/message-extension-point/com.youzan.cloud.base.service.api.MessageService/handleMessage`, { topic: item.bizType, data: JSON.stringify(param), }, { headers: { 'param-type': '' }, validateStatus: function (status) { return true; }, }); response.result = resData; } catch (e) { response.result = { code: -1, comment: '调用错误', message: e.message, }; } response.timeCost = Date.now() - startTime; let testInfo = { createdAt: Date.now(), item, param, response, }; let callHistory = await this.getMepLocalTestHistory(projectId, item.id); callHistory.splice(0, 0, testInfo); if (callHistory.length > 20) { callHistory = callHistory.slice(0, 20); } await this.saveMepTestHistory(projectId, item.id, callHistory); return response; } async fetchAllMep() { if (hasFetchedMepData) { return null; } hasFetchedMepData = true; let info = await this.documentCenterApiService.getAllMepInfo(); await fs_extra_1.default.ensureFile(this.mepListCacheFile); await fs_extra_1.default.writeJson(this.mepListCacheFile, info, { spaces: 2 }); return info; } generateMepCallExample(fieldDefinition) { let fieldExample = {}; fieldDefinition.path = fieldDefinition.name; let stack = [fieldDefinition]; while (stack.length != 0) { let currentFieldDefinition = stack.pop(); let isBasic = currentFieldDefinition.array == 0 && (!currentFieldDefinition.children || currentFieldDefinition.children.length == 0); if (isBasic) { (0, lodash_1.set)(fieldExample, currentFieldDefinition.path, currentFieldDefinition.example); } else { let prefixPath; if (currentFieldDefinition.array) { (0, lodash_1.set)(fieldExample, currentFieldDefinition.path, [{}]); prefixPath = currentFieldDefinition.path + '[0]'; if (!currentFieldDefinition.children || currentFieldDefinition.children.length == 0) { (0, lodash_1.set)(fieldExample, prefixPath, currentFieldDefinition.example); } } else { (0, lodash_1.set)(fieldExample, currentFieldDefinition.path, {}); prefixPath = currentFieldDefinition.path; } for (let sub of currentFieldDefinition.children) { sub.path = prefixPath + '.' + sub.name; stack.push(sub); } } } return fieldExample; } }; tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], MepService.prototype, "projectService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], MepService.prototype, "configService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], MepService.prototype, "requestService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], MepService.prototype, "documentCenterApiService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], MepService.prototype, "mockPlatformService", void 0); tslib_1.__decorate([ (0, spring4js_nodejs_1.Resource)() ], MepService.prototype, "logService", void 0); MepService = tslib_1.__decorate([ (0, spring4js_nodejs_1.Service)() ], MepService); exports.default = MepService;