Skip to content

Commit cefd4c6

Browse files
committed
fix(faas): get namespace bug
1 parent bc648b3 commit cefd4c6

File tree

5 files changed

+120
-69
lines changed

5 files changed

+120
-69
lines changed

packages/faas/__tests__/index.test.ts

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FaaS } from '../src';
1+
import { FaaS, InvokeResult } from '../src';
22

33
describe('FaaS', () => {
44
const region = 'ap-guangzhou';
@@ -13,6 +13,7 @@ describe('FaaS', () => {
1313
};
1414
let reqId = '';
1515
const faas = new FaaS({
16+
debug: !!process.env.DEBUG,
1617
secretId: process.env.TENCENT_SECRET_ID,
1718
secretKey: process.env.TENCENT_SECRET_KEY,
1819
token: process.env.TENCENT_TOKEN,
@@ -48,9 +49,9 @@ describe('FaaS', () => {
4849
});
4950

5051
test('invoke', async () => {
51-
const res = await faas.invoke({
52+
const res = (await faas.invoke({
5253
...faasConfig,
53-
});
54+
})) as InvokeResult;
5455

5556
expect(res).toEqual({
5657
billDuration: expect.any(Number),
@@ -62,6 +63,8 @@ describe('FaaS', () => {
6263
log: expect.any(String),
6364
retMsg: expect.any(String),
6465
});
66+
67+
reqId = res.functionRequestId;
6568
});
6669
test('invoke with wrong region', async () => {
6770
try {
@@ -114,30 +117,30 @@ describe('FaaS', () => {
114117
expect(res).toBeInstanceOf(Array);
115118
});
116119

117-
if (reqId) {
118-
test('getLogDetail', async () => {
119-
const res = await faas.getLogDetail({
120-
...faasConfig,
121-
...clsConfig,
122-
reqId,
123-
});
124-
expect(res).toBeInstanceOf(Array);
120+
test('getLogDetail', async () => {
121+
const res = await faas.getLogDetail({
122+
...faasConfig,
123+
...clsConfig,
124+
reqId,
125125
});
126-
test('getLogByReqId', async () => {
127-
const res = await faas.getLogByReqId({
128-
...faasConfig,
129-
reqId,
130-
});
131-
expect(res).toEqual({
132-
requestId: reqId,
133-
retryNum: 0,
134-
startTime: expect.any(String),
135-
memoryUsage: expect.any(String),
136-
duration: expect.any(String),
137-
message: expect.any(String),
138-
});
126+
expect(res).toBeInstanceOf(Array);
127+
});
128+
test('getLogByReqId', async () => {
129+
const res = await faas.getLogByReqId({
130+
...faasConfig,
131+
reqId,
139132
});
140-
}
133+
134+
expect(res).toEqual({
135+
requestId: reqId,
136+
retryNum: 0,
137+
startTime: expect.any(String),
138+
memoryUsage: expect.any(String),
139+
duration: expect.any(String),
140+
message: expect.any(String),
141+
isCompleted: expect.any(Boolean),
142+
});
143+
});
141144

142145
test('getMetric', async () => {
143146
const res = await faas.getMetric({

packages/faas/src/apis.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ServiceType, ApiFactory } from '@tencent-sdk/capi';
1+
import { Capi, ServiceType, ApiFactory } from '@tencent-sdk/capi';
22

33
const ACTIONS = [
44
'CreateFunction',
@@ -22,11 +22,22 @@ const ACTIONS = [
2222

2323
export type ActionType = typeof ACTIONS[number];
2424

25-
const APIS = ApiFactory({
26-
isV3: true,
27-
serviceType: ServiceType.faas,
28-
version: '2018-04-16',
29-
actions: ACTIONS,
30-
});
25+
export type ApiMap = Record<ActionType, (capi: Capi, inputs: any) => any>;
3126

32-
export default APIS;
27+
function initializeApis({
28+
isV3 = true,
29+
debug = false,
30+
}: {
31+
isV3?: boolean;
32+
debug?: boolean;
33+
}): ApiMap {
34+
return ApiFactory({
35+
isV3,
36+
debug,
37+
serviceType: ServiceType.faas,
38+
version: '2018-04-16',
39+
actions: ACTIONS,
40+
});
41+
}
42+
43+
export { initializeApis };

packages/faas/src/index.ts

+32-36
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { camelCaseProps } from '@tencent-sdk/common';
33
import { Capi, ServiceType, CommonError } from '@tencent-sdk/capi';
44
import { Cls } from '@tencent-sdk/cls';
55
import { dtz, dayjs, formatDate, Dayjs, TIME_FORMAT } from './dayjs';
6-
import APIS, { ActionType } from './apis';
7-
import { getSearchSql } from './utils';
6+
import { initializeApis, ApiMap, ActionType } from './apis';
7+
import { getSearchSql, formatFaasLog } from './utils';
88
import { Monitor } from './monitor';
99
import {
1010
Credentials,
@@ -16,7 +16,6 @@ import {
1616
GetLogOptions,
1717
GetLogDetailOptions,
1818
ClsConfig,
19-
LogContent,
2019
SearchLogItem,
2120
SearchLogDetailItem,
2221
InvokeOptions,
@@ -37,9 +36,11 @@ export * from './monitor';
3736
export class FaaS {
3837
credentials: Credentials;
3938
region: string;
39+
debug: boolean;
4040
capi: Capi;
4141
cls: Cls;
4242
monitor: Monitor;
43+
apis: ApiMap;
4344
clsConfigCache: { [prop: string]: { logsetId: string; topicId: string } };
4445

4546
constructor({
@@ -54,6 +55,7 @@ export class FaaS {
5455
secretKey,
5556
token,
5657
};
58+
this.debug = debug;
5759
this.region = region;
5860
this.capi = new Capi({
5961
debug,
@@ -82,6 +84,10 @@ export class FaaS {
8284

8385
// 函数 CLS 配置缓存
8486
this.clsConfigCache = {};
87+
88+
this.apis = initializeApis({
89+
debug,
90+
});
8591
}
8692

8793
/**
@@ -108,7 +114,7 @@ export class FaaS {
108114
Action: ActionType;
109115
[key: string]: any;
110116
}) {
111-
const result = await APIS[Action](this.capi, data);
117+
const result = await this.apis[Action](this.capi, data);
112118
return result;
113119
}
114120

@@ -154,7 +160,8 @@ export class FaaS {
154160
});
155161
Namespaces = Namespaces.concat(res);
156162
}
157-
return Namespaces.map((item: { Name: string }) => item.Name);
163+
164+
return Namespaces;
158165
}
159166

160167
/**
@@ -207,7 +214,10 @@ export class FaaS {
207214
}: GetFaasOptions): Promise<FunctionInfo | null> {
208215
try {
209216
// 判断 namespace 是否存在
210-
const namespaces = await this.getNamespaces();
217+
const namespacesList = await this.getNamespaces();
218+
const namespaces = namespacesList.map(
219+
(item: { Name: string }) => item.Name,
220+
);
211221
if (namespaces.indexOf(namespace) === -1) {
212222
throw new CommonError(ERRORS.NAMESPACE_NOT_EXIST_ERROR);
213223
}
@@ -425,6 +435,7 @@ export class FaaS {
425435
const logs = [];
426436
for (let i = 0, len = results.length; i < len; i++) {
427437
const curReq = results[i];
438+
curReq.isCompleted = false;
428439
curReq.startTime = formatDate(curReq.startTime);
429440

430441
const detailLog = await this.getLogDetail({
@@ -434,20 +445,12 @@ export class FaaS {
434445
startTime: startDate.format(TIME_FORMAT),
435446
endTime: endDate.format(TIME_FORMAT),
436447
});
437-
curReq.message = (detailLog || [])
438-
.map(({ content }: { content: string }) => {
439-
try {
440-
const info = JSON.parse(content) as LogContent;
441-
if (info.SCF_Type === 'Custom') {
442-
curReq.memoryUsage = info.SCF_MemUsage;
443-
curReq.duration = info.SCF_Duration;
444-
}
445-
return info.SCF_Message;
446-
} catch (e) {
447-
return '';
448-
}
449-
})
450-
.join('');
448+
const formatedInfo = formatFaasLog(detailLog || []);
449+
curReq.message = formatedInfo.message;
450+
curReq.memoryUsage = formatedInfo.memoryUsage;
451+
curReq.isCompleted = formatedInfo.isCompleted;
452+
curReq.duration = formatedInfo.duration;
453+
451454
logs.push(curReq);
452455
}
453456
return logs;
@@ -513,7 +516,9 @@ export class FaaS {
513516
}
514517
const endDate = dayjs(endTime);
515518

516-
console.log(`[FAAS] 通过请求 ID 获取日志: ${reqId}`);
519+
if (this.debug) {
520+
console.log(`[FAAS] 通过请求 ID 获取日志: ${reqId}`);
521+
}
517522

518523
const detailLog = await this.getLogDetail({
519524
logsetId: logsetId,
@@ -529,22 +534,13 @@ export class FaaS {
529534
memoryUsage: '',
530535
duration: '',
531536
message: '',
537+
isCompleted: false,
532538
};
533-
curReq.message = (detailLog || [])
534-
.map(({ content, timestamp }) => {
535-
try {
536-
const info = JSON.parse(content) as LogContent;
537-
if (info.SCF_Type === 'Custom') {
538-
curReq.memoryUsage = info.SCF_MemUsage;
539-
curReq.duration = info.SCF_Duration;
540-
curReq.startTime = timestamp;
541-
}
542-
return info.SCF_Message;
543-
} catch (e) {
544-
return '';
545-
}
546-
})
547-
.join('');
539+
const formatedInfo = formatFaasLog(detailLog || []);
540+
curReq.message = formatedInfo.message;
541+
curReq.memoryUsage = formatedInfo.memoryUsage;
542+
curReq.isCompleted = formatedInfo.isCompleted;
543+
curReq.duration = formatedInfo.duration;
548544

549545
return curReq;
550546
}

packages/faas/src/typings/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export interface SearchLogItem {
178178
memoryUsage: string;
179179
duration: string;
180180
message: string;
181+
isCompleted: boolean;
181182
}
182183

183184
// 查询得到的日志详情日志

packages/faas/src/utils.ts

+40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LogContent } from './typings/index';
12
import { StatusSqlMap, GetLogOptions } from './typings';
23

34
const StatusMap: StatusSqlMap = {
@@ -39,3 +40,42 @@ export function getSearchSql(options: GetLogOptions) {
3940

4041
return sql;
4142
}
43+
44+
/**
45+
* 判断请求日志是否是完整的
46+
* @param {string} msg - log message
47+
* @returns {boolean}
48+
*/
49+
export function isCompleteRequest(msg: string) {
50+
return msg.indexOf('END RequestId') > -1;
51+
}
52+
53+
export function formatFaasLog(detailLog: { content: string }[]) {
54+
let memoryUsage = '';
55+
let duration = '';
56+
let isCompleted = false;
57+
let message = (detailLog || [])
58+
.map(({ content }: { content: string }) => {
59+
try {
60+
const info = JSON.parse(content) as LogContent;
61+
if (info.SCF_Type === 'Custom') {
62+
memoryUsage = info.SCF_MemUsage;
63+
duration = info.SCF_Duration;
64+
}
65+
// 判断当前调用是否完成,如果完成则设置 isCompleted 为 true
66+
if (!isCompleted) {
67+
isCompleted = isCompleteRequest(info.SCF_Message);
68+
}
69+
return info.SCF_Message;
70+
} catch (e) {
71+
return '';
72+
}
73+
})
74+
.join('');
75+
return {
76+
memoryUsage,
77+
duration,
78+
isCompleted,
79+
message,
80+
};
81+
}

0 commit comments

Comments
 (0)