|
1 | | -import { CapiCredentials, RegionType } from './../interface'; |
2 | 1 | import { Cls as ClsClient } from '@tencent-sdk/cls'; |
| 2 | +import dayjs, { Dayjs } from 'dayjs'; |
3 | 3 | import { |
4 | 4 | ClsDelopyIndexInputs, |
5 | 5 | ClsDeployInputs, |
6 | 6 | ClsDeployLogsetInputs, |
7 | 7 | ClsDeployOutputs, |
8 | 8 | ClsDeployTopicInputs, |
| 9 | + GetLogOptions, |
| 10 | + GetLogDetailOptions, |
| 11 | + LogContent, |
9 | 12 | } from './interface'; |
| 13 | +import { CapiCredentials, RegionType } from './../interface'; |
10 | 14 | import { ApiError } from '../../utils/error'; |
11 | | -import { createLogset, createTopic, updateIndex } from './utils'; |
| 15 | +import { createLogset, createTopic, updateIndex, getSearchSql } from './utils'; |
| 16 | + |
| 17 | +const TimeFormat = 'YYYY-MM-DD HH:mm:ss'; |
12 | 18 |
|
13 | 19 | export default class Cls { |
14 | 20 | credentials: CapiCredentials; |
@@ -192,4 +198,96 @@ export default class Cls { |
192 | 198 |
|
193 | 199 | return {}; |
194 | 200 | } |
| 201 | + |
| 202 | + async getLogList(data: GetLogOptions) { |
| 203 | + const clsClient = new ClsClient({ |
| 204 | + region: this.region, |
| 205 | + secretId: this.credentials.SecretId!, |
| 206 | + secretKey: this.credentials.SecretKey!, |
| 207 | + token: this.credentials.Token, |
| 208 | + debug: false, |
| 209 | + }); |
| 210 | + |
| 211 | + const { endTime, interval = 3600 } = data; |
| 212 | + let startDate: Dayjs; |
| 213 | + let endDate: Dayjs; |
| 214 | + |
| 215 | + // 默认获取从当前到一个小时前时间段的日志 |
| 216 | + if (!endTime) { |
| 217 | + endDate = dayjs(); |
| 218 | + startDate = endDate.add(-1, 'hour'); |
| 219 | + } else { |
| 220 | + endDate = dayjs(endTime); |
| 221 | + startDate = dayjs(endDate.valueOf() - Number(interval) * 1000); |
| 222 | + } |
| 223 | + |
| 224 | + const sql = getSearchSql({ |
| 225 | + ...data, |
| 226 | + startTime: startDate.valueOf(), |
| 227 | + endTime: endDate.valueOf(), |
| 228 | + }); |
| 229 | + const searchParameters = { |
| 230 | + logset_id: data.logsetId, |
| 231 | + topic_ids: data.topicId, |
| 232 | + start_time: startDate.format(TimeFormat), |
| 233 | + end_time: endDate.format(TimeFormat), |
| 234 | + // query_string 必须用 cam 特有的 url 编码方式 |
| 235 | + query_string: sql, |
| 236 | + limit: data.limit || 10, |
| 237 | + sort: 'desc', |
| 238 | + }; |
| 239 | + const { results = [] } = await clsClient.searchLog(searchParameters); |
| 240 | + const logs = []; |
| 241 | + for (let i = 0, len = results.length; i < len; i++) { |
| 242 | + const curReq = results[i]; |
| 243 | + const detailLog = await this.getLogDetail({ |
| 244 | + logsetId: data.logsetId, |
| 245 | + topicId: data.topicId, |
| 246 | + reqId: curReq.requestId, |
| 247 | + startTime: startDate.format(TimeFormat), |
| 248 | + endTime: endDate.format(TimeFormat), |
| 249 | + }); |
| 250 | + curReq.message = (detailLog || []) |
| 251 | + .map(({ content }: { content: string }) => { |
| 252 | + try { |
| 253 | + const info = JSON.parse(content) as LogContent; |
| 254 | + if (info.SCF_Type === 'Custom') { |
| 255 | + curReq.memoryUsage = info.SCF_MemUsage; |
| 256 | + curReq.duration = info.SCF_Duration; |
| 257 | + } |
| 258 | + return info.SCF_Message; |
| 259 | + } catch (e) { |
| 260 | + return ''; |
| 261 | + } |
| 262 | + }) |
| 263 | + .join(''); |
| 264 | + logs.push(curReq); |
| 265 | + } |
| 266 | + return logs; |
| 267 | + } |
| 268 | + async getLogDetail(data: GetLogDetailOptions) { |
| 269 | + const clsClient = new ClsClient({ |
| 270 | + region: this.region, |
| 271 | + secretId: this.credentials.SecretId!, |
| 272 | + secretKey: this.credentials.SecretKey!, |
| 273 | + token: this.credentials.Token, |
| 274 | + debug: false, |
| 275 | + }); |
| 276 | + |
| 277 | + data.startTime = data.startTime || dayjs(data.endTime).add(-1, 'hour').format(TimeFormat); |
| 278 | + |
| 279 | + const sql = `SCF_RequestId:${data.reqId} AND SCF_RetryNum:0`; |
| 280 | + const searchParameters = { |
| 281 | + logset_id: data.logsetId, |
| 282 | + topic_ids: data.topicId, |
| 283 | + start_time: data.startTime as string, |
| 284 | + end_time: data.endTime, |
| 285 | + // query_string 必须用 cam 特有的 url 编码方式 |
| 286 | + query_string: sql, |
| 287 | + limit: 100, |
| 288 | + sort: 'asc', |
| 289 | + }; |
| 290 | + const { results = [] } = await clsClient.searchLog(searchParameters); |
| 291 | + return results; |
| 292 | + } |
195 | 293 | } |
0 commit comments