@@ -3,8 +3,8 @@ import { camelCaseProps } from '@tencent-sdk/common';
3
3
import { Capi , ServiceType , CommonError } from '@tencent-sdk/capi' ;
4
4
import { Cls } from '@tencent-sdk/cls' ;
5
5
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' ;
8
8
import { Monitor } from './monitor' ;
9
9
import {
10
10
Credentials ,
@@ -16,7 +16,6 @@ import {
16
16
GetLogOptions ,
17
17
GetLogDetailOptions ,
18
18
ClsConfig ,
19
- LogContent ,
20
19
SearchLogItem ,
21
20
SearchLogDetailItem ,
22
21
InvokeOptions ,
@@ -37,9 +36,11 @@ export * from './monitor';
37
36
export class FaaS {
38
37
credentials : Credentials ;
39
38
region : string ;
39
+ debug : boolean ;
40
40
capi : Capi ;
41
41
cls : Cls ;
42
42
monitor : Monitor ;
43
+ apis : ApiMap ;
43
44
clsConfigCache : { [ prop : string ] : { logsetId : string ; topicId : string } } ;
44
45
45
46
constructor ( {
@@ -54,6 +55,7 @@ export class FaaS {
54
55
secretKey,
55
56
token,
56
57
} ;
58
+ this . debug = debug ;
57
59
this . region = region ;
58
60
this . capi = new Capi ( {
59
61
debug,
@@ -82,6 +84,10 @@ export class FaaS {
82
84
83
85
// 函数 CLS 配置缓存
84
86
this . clsConfigCache = { } ;
87
+
88
+ this . apis = initializeApis ( {
89
+ debug,
90
+ } ) ;
85
91
}
86
92
87
93
/**
@@ -108,7 +114,7 @@ export class FaaS {
108
114
Action : ActionType ;
109
115
[ key : string ] : any ;
110
116
} ) {
111
- const result = await APIS [ Action ] ( this . capi , data ) ;
117
+ const result = await this . apis [ Action ] ( this . capi , data ) ;
112
118
return result ;
113
119
}
114
120
@@ -154,7 +160,8 @@ export class FaaS {
154
160
} ) ;
155
161
Namespaces = Namespaces . concat ( res ) ;
156
162
}
157
- return Namespaces . map ( ( item : { Name : string } ) => item . Name ) ;
163
+
164
+ return Namespaces ;
158
165
}
159
166
160
167
/**
@@ -207,7 +214,10 @@ export class FaaS {
207
214
} : GetFaasOptions ) : Promise < FunctionInfo | null > {
208
215
try {
209
216
// 判断 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
+ ) ;
211
221
if ( namespaces . indexOf ( namespace ) === - 1 ) {
212
222
throw new CommonError ( ERRORS . NAMESPACE_NOT_EXIST_ERROR ) ;
213
223
}
@@ -425,6 +435,7 @@ export class FaaS {
425
435
const logs = [ ] ;
426
436
for ( let i = 0 , len = results . length ; i < len ; i ++ ) {
427
437
const curReq = results [ i ] ;
438
+ curReq . isCompleted = false ;
428
439
curReq . startTime = formatDate ( curReq . startTime ) ;
429
440
430
441
const detailLog = await this . getLogDetail ( {
@@ -434,20 +445,12 @@ export class FaaS {
434
445
startTime : startDate . format ( TIME_FORMAT ) ,
435
446
endTime : endDate . format ( TIME_FORMAT ) ,
436
447
} ) ;
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
+
451
454
logs . push ( curReq ) ;
452
455
}
453
456
return logs ;
@@ -513,7 +516,9 @@ export class FaaS {
513
516
}
514
517
const endDate = dayjs ( endTime ) ;
515
518
516
- console . log ( `[FAAS] 通过请求 ID 获取日志: ${ reqId } ` ) ;
519
+ if ( this . debug ) {
520
+ console . log ( `[FAAS] 通过请求 ID 获取日志: ${ reqId } ` ) ;
521
+ }
517
522
518
523
const detailLog = await this . getLogDetail ( {
519
524
logsetId : logsetId ,
@@ -529,22 +534,13 @@ export class FaaS {
529
534
memoryUsage : '' ,
530
535
duration : '' ,
531
536
message : '' ,
537
+ isCompleted : false ,
532
538
} ;
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 ;
548
544
549
545
return curReq ;
550
546
}
0 commit comments