UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

217 lines (214 loc) 7.63 kB
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import _regeneratorRuntime from '@babel/runtime/regenerator'; import { _ as __awaiter } from '../tslib.es6-848120af.js'; import axios from 'axios'; /** 伽利略日志查询网关地址(蓝鲸 APIGW) */ var GALILEO_LOG_QUERY_URL = 'https://galileo-api.apigw.o.woa.com/prod/log/query'; /** * 排序类型,对应 proto LogSortType * @ignore */ var GalileoLogSortType; (function (GalileoLogSortType) { // 默认倒序 GalileoLogSortType[GalileoLogSortType["DEFAULT"] = 0] = "DEFAULT"; // 正序 GalileoLogSortType[GalileoLogSortType["ASC"] = 1] = "ASC"; // 倒序 GalileoLogSortType[GalileoLogSortType["DESC"] = 2] = "DESC"; })(GalileoLogSortType || (GalileoLogSortType = {})); /** * message 搜索的关键词组合逻辑,对应 proto MessageSearch.FilterType * @ignore */ var GalileoMessageFilterType; (function (GalileoMessageFilterType) { // 默认为或 GalileoMessageFilterType[GalileoMessageFilterType["EMPTY"] = 0] = "EMPTY"; // 或 GalileoMessageFilterType[GalileoMessageFilterType["OR"] = 1] = "OR"; // 且 GalileoMessageFilterType[GalileoMessageFilterType["AND"] = 2] = "AND"; })(GalileoMessageFilterType || (GalileoMessageFilterType = {})); /** * message 搜索模式,对应 proto MessageSearch.SearchType * @ignore */ var GalileoMessageSearchType; (function (GalileoMessageSearchType) { // 默认搜索 GalileoMessageSearchType[GalileoMessageSearchType["DEFAULT"] = 0] = "DEFAULT"; // 忽略大小写搜索 GalileoMessageSearchType[GalileoMessageSearchType["CASEINS"] = 1] = "CASEINS"; // 单词搜索 GalileoMessageSearchType[GalileoMessageSearchType["TOKEN"] = 2] = "TOKEN"; // 子串搜索 GalileoMessageSearchType[GalileoMessageSearchType["SUBSTR"] = 3] = "SUBSTR"; // 正则搜索 GalileoMessageSearchType[GalileoMessageSearchType["REGULAR"] = 4] = "REGULAR"; })(GalileoMessageSearchType || (GalileoMessageSearchType = {})); /** * 查询伽利略日志(调用蓝鲸网关 /prod/log/query) * * 参考文档:https://iwiki.woa.com/p/4007673553 * * 调用前需要在「伽利略 API 权限申请表」中申请 bk_app_code/bk_app_secret,以及对应 target 的查询权限。 * * @example * ```ts * import { queryGalileoLog, GalileoLogSortType } from 't-comm/lib/galileo-api'; * * const now = Date.now(); * const res = await queryGalileoLog( * { * target: 'PCG-123.galileo.apiserver', * namespace: 'Production', * start: now - 30 * 60 * 1000, * end: now, * limit: 100, * query: 'message:target and level:error', * sort_type: GalileoLogSortType.ASC, * }, * { * auth: { * bk_app_code: 'tip-tool', * bk_app_secret: 'xxx', * }, * }, * ); * console.log(res.logs, res.has_next_page, res.cursor); * ``` */ function queryGalileoLog(req, options) { var _ref = options || {}, auth = _ref.auth, _ref$url = _ref.url, url = _ref$url === void 0 ? GALILEO_LOG_QUERY_URL : _ref$url, _ref$timeout = _ref.timeout, timeout = _ref$timeout === void 0 ? 15000 : _ref$timeout; return new Promise(function (resolve, reject) { axios.post(url, Object.assign(Object.assign({}, req), { start: +req.start, end: +req.end }), { timeout: timeout, headers: { 'Content-Type': 'application/json', 'X-Bkapi-Authorization': JSON.stringify(Object.assign({ bk_app_code: auth.bk_app_code, bk_app_secret: auth.bk_app_secret }, auth.access_token ? { access_token: auth.access_token } : {})) } }).then(function (response) { if (response === null || response === void 0 ? void 0 : response.data) { resolve(response.data); return; } reject(response); })["catch"](function (error) { reject(error); }); }); } /** * 通过游标不断翻页,直到满足 limit 条数或 has_next_page=false。 * * 伽利略后端分片查询时,稀疏数据可能单次只返回少量条目,需要借助 cursor 继续翻页。 * 该方法会自动循环翻页直到累计条数 >= req.limit 或没有下一页。 * * @param req 查询参数,limit 为期望累计的总条数 * @param options 鉴权及请求配置 * @param extra 可选项:最大翻页次数、每页大小 * * @example * ```ts * const res = await queryGalileoLogAll( * { * target: 'PCG-123.galileo.apiserver', * namespace: 'Production', * start: Date.now() - 3600 * 1000, * end: Date.now(), * limit: 500, * query: 'level:error', * }, * { auth: { bk_app_code: 'xxx', bk_app_secret: 'xxx' } }, * ); * ``` */ function queryGalileoLogAll(req, options, extra) { var _a, _b, _c, _d, _e, _f, _g; return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var maxPage, pageSize, total, logs, cursor, lastRsp, i, need, rsp; return _regeneratorRuntime.wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: maxPage = (_a = extra === null || extra === void 0 ? void 0 : extra.maxPage) !== null && _a !== void 0 ? _a : 20; pageSize = Math.min((_b = extra === null || extra === void 0 ? void 0 : extra.pageSize) !== null && _b !== void 0 ? _b : 100, 100); total = req.limit; logs = []; cursor = req.cursor || ''; i = 0; case 1: if (!(i < maxPage)) { _context.next = 7; break; } need = total - logs.length; if (!(need <= 0)) { _context.next = 2; break; } return _context.abrupt("continue", 7); case 2: _context.next = 3; return queryGalileoLog(Object.assign(Object.assign({}, req), { cursor: cursor, limit: Math.min(need, pageSize) }), options); case 3: rsp = _context.sent; lastRsp = rsp; if (!(rsp.code !== 0)) { _context.next = 4; break; } return _context.abrupt("return", rsp); case 4: if ((_c = rsp.logs) === null || _c === void 0 ? void 0 : _c.length) { logs.push.apply(logs, _toConsumableArray(rsp.logs)); } if (rsp.has_next_page) { _context.next = 5; break; } return _context.abrupt("continue", 7); case 5: cursor = rsp.cursor || ''; if (cursor) { _context.next = 6; break; } return _context.abrupt("continue", 7); case 6: i++; _context.next = 1; break; case 7: return _context.abrupt("return", { code: (_d = lastRsp === null || lastRsp === void 0 ? void 0 : lastRsp.code) !== null && _d !== void 0 ? _d : 0, msg: (_e = lastRsp === null || lastRsp === void 0 ? void 0 : lastRsp.msg) !== null && _e !== void 0 ? _e : '', total: logs.length, logs: logs, cursor: (_f = lastRsp === null || lastRsp === void 0 ? void 0 : lastRsp.cursor) !== null && _f !== void 0 ? _f : '', has_next_page: (_g = lastRsp === null || lastRsp === void 0 ? void 0 : lastRsp.has_next_page) !== null && _g !== void 0 ? _g : false }); case 8: case "end": return _context.stop(); } }, _callee); })); } export { GalileoLogSortType, GalileoMessageFilterType, GalileoMessageSearchType, queryGalileoLog, queryGalileoLogAll };