Skip to content

Commit 5f211e0

Browse files
authored
fix(apigw): optimize remove apigw (#205)
* fix(apigw): optimize remove apigw * test(metrics): change time range
1 parent 3a6df89 commit 5f211e0

File tree

7 files changed

+64
-49
lines changed

7 files changed

+64
-49
lines changed

__tests__/apigw.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,8 @@ describe('apigw', () => {
618618
await apigw.remove(outputsWithId);
619619
const detail = await apigw.service.getById(outputsWithId.serviceId);
620620
expect(detail).toBeDefined();
621-
expect(detail.serviceName).toBe('serverless_unit_test');
622-
expect(detail.serviceDesc).toBe('Created By Serverless');
621+
expect(detail.ServiceName).toBe('serverless_unit_test');
622+
expect(detail.ServiceDesc).toBe('Created By Serverless');
623623
const apiList = await apigw.api.getList(outputsWithId.serviceId);
624624
expect(apiList.length).toBe(0);
625625
});

__tests__/metrics.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import moment from 'moment';
22
import { Metrics } from '../src';
3+
import { getYestoday } from '../src/utils';
34

45
describe('Metrics', () => {
56
const credentials = {
@@ -10,8 +11,9 @@ describe('Metrics', () => {
1011
funcName: 'serverless-unit-test',
1112
});
1213

13-
const rangeStart = '2020-09-09 10:00:00';
14-
const rangeEnd = '2020-09-09 11:00:00';
14+
const yestoday = getYestoday();
15+
const rangeStart = `${yestoday} 10:00:00`;
16+
const rangeEnd = `${yestoday} 11:00:00`;
1517

1618
test('should get metrics data', async () => {
1719
const res = await metrics.getDatas(rangeStart, rangeEnd, 0xfffffffffff);

src/modules/apigw/entities/service.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,16 @@ export default class ServiceEntity {
3838
}
3939

4040
async getById(serviceId: string) {
41-
const detail: Detail = await this.request({
42-
Action: 'DescribeService',
43-
ServiceId: serviceId,
44-
});
45-
46-
const outputs = {
47-
serviceId: detail.ServiceId,
48-
serviceName: detail.ServiceName,
49-
subDomain:
50-
detail!.OuterSubDomain && detail!.InnerSubDomain
51-
? [detail!.OuterSubDomain, detail!.InnerSubDomain]
52-
: detail!.OuterSubDomain || detail!.InnerSubDomain,
53-
serviceDesc: detail.ServiceDesc,
54-
};
41+
try {
42+
const detail: Detail = await this.request({
43+
Action: 'DescribeService',
44+
ServiceId: serviceId,
45+
});
5546

56-
return outputs;
47+
return detail;
48+
} catch (e) {
49+
return null;
50+
}
5751
}
5852

5953
/** 创建 API 网关服务 */
@@ -110,7 +104,7 @@ export default class ServiceEntity {
110104
serviceDesc = 'Created By Serverless Framework',
111105
} = serviceConf;
112106

113-
let detail: Detail;
107+
let detail: Detail | null;
114108

115109
let outputs: ApigwCreateOrUpdateServiceOutputs = {
116110
serviceId: serviceId,
@@ -123,10 +117,7 @@ export default class ServiceEntity {
123117
let exist = false;
124118

125119
if (serviceId) {
126-
detail = await this.request({
127-
Action: 'DescribeService',
128-
ServiceId: serviceId,
129-
});
120+
detail = await this.getById(serviceId);
130121
if (detail) {
131122
detail.InnerSubDomain = detail.InternalSubDomain;
132123
exist = true;

src/modules/apigw/index.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ export default class Apigw {
133133
} = inputs;
134134

135135
// check service exist
136-
const detail = await this.request({
137-
Action: 'DescribeService',
138-
ServiceId: serviceId,
139-
});
136+
const detail = await this.service.getById(serviceId);
140137
if (!detail) {
141138
console.log(`Service ${serviceId} not exist`);
142139
return;
@@ -207,30 +204,36 @@ export default class Apigw {
207204
const endpoints = inputs.endpoints || [];
208205
const stateApiList = oldState.apiList || [];
209206

210-
const serviceDetail = await this.service.getById(serviceId);
211-
212-
const apiList: ApiEndpoint[] = await this.api.bulkDeploy({
213-
apiList: endpoints,
214-
stateList: stateApiList,
215-
serviceId,
216-
environment,
217-
});
207+
const detail = await this.service.getById(serviceId);
208+
if (detail) {
209+
const apiList: ApiEndpoint[] = await this.api.bulkDeploy({
210+
apiList: endpoints,
211+
stateList: stateApiList,
212+
serviceId,
213+
environment,
214+
});
218215

219-
await this.service.release({ serviceId, environment });
216+
await this.service.release({ serviceId, environment });
220217

221-
console.log(`Deploy service ${serviceId} success`);
218+
console.log(`Deploy service ${serviceId} success`);
222219

223-
const outputs: ApigwDeployOutputs = {
224-
created: false,
225-
serviceId,
226-
serviceName: serviceDetail.serviceName,
227-
subDomain: serviceDetail.subDomain,
228-
protocols: inputs.protocols,
229-
environment: environment,
230-
apiList,
231-
};
220+
const subDomain =
221+
detail!.OuterSubDomain && detail!.InnerSubDomain
222+
? [detail!.OuterSubDomain, detail!.InnerSubDomain]
223+
: detail!.OuterSubDomain || detail!.InnerSubDomain;
232224

233-
return outputs;
225+
const outputs: ApigwDeployOutputs = {
226+
created: false,
227+
serviceId,
228+
serviceName: detail.ServiceName,
229+
subDomain: subDomain,
230+
protocols: inputs.protocols,
231+
environment: environment,
232+
apiList,
233+
};
234+
235+
return outputs;
236+
}
234237
}
235238
}
236239

src/modules/metrics/formatter/formatCustomMetrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function formatCustomMetrics(resList: MetricsResponseList) {
8282
};
8383
});
8484

85-
if (requestDatas) {
85+
if (requestDatas && requestDatas.length > 0) {
8686
for (const latencyDetail of latencyDetailList) {
8787
if (!latency.y) {
8888
latency.y = [];

src/modules/metrics/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ export default class Metrics {
157157
throw new ApiTypeError(`PARAMETER_METRICS`, 'The rangeStart provided is after the rangeEnd');
158158
}
159159

160+
console.log(`Getting metrics data from ${startTimeStr} to ${endTimeStr}`);
161+
160162
// custom metrics maximum 8 day
161163
if (startTime.diff(endTime, 'days') >= 8) {
162164
throw new ApiTypeError(

src/utils/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,20 @@ export function traverseDirSync(
230230
}
231231
return ls;
232232
}
233+
234+
export function getToday(date?: Date) {
235+
if (!date) {
236+
date = new Date();
237+
}
238+
const year = date.getFullYear();
239+
const month = date.getMonth() + 1;
240+
const day = date.getDate();
241+
242+
return `${year}-${month < 10 ? '0' : ''}${month}-${day}`;
243+
}
244+
245+
export function getYestoday() {
246+
const timestamp = Date.now() - 24 * 60 * 60 * 1000;
247+
const yestoday = getToday(new Date(timestamp));
248+
return yestoday;
249+
}

0 commit comments

Comments
 (0)