Skip to content

Commit 1b2cd56

Browse files
Merge pull request #1543 from contentstack/feat/dx-1201-get-rate-limit
dx | 1201 | get and remove rate limit commands
2 parents 4ffeb88 + c52b22e commit 1b2cd56

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { cliux, configHandler } from '@contentstack/cli-utilities';
2+
import { Command } from '@contentstack/cli-command';
3+
import { RateLimitConfig } from '../../../interfaces';
4+
5+
export default class RateLimitGetCommand extends Command {
6+
static description: string = 'Get rate-limit of organizations';
7+
static examples = ['$ csdx config:get:rate-limit'];
8+
9+
async run() {
10+
try {
11+
const rateLimit = configHandler.get('rateLimit') || {};
12+
const formatLimit = (limit) => (limit ? `${limit.value}(${limit.utilize}%)` : '0');
13+
const tableData = Object.entries(rateLimit).map(([org, limits]: [string, RateLimitConfig]) => ({
14+
Org: org === 'default' ? 'default' : org,
15+
'Get Limit': formatLimit(limits.getLimit),
16+
Limit: formatLimit(limits.limit),
17+
'Bulk Limit': formatLimit(limits.bulkLimit),
18+
}));
19+
20+
const columns = {
21+
Org: {
22+
minWidth: 10,
23+
},
24+
'Get Limit': {
25+
minWidth: 20,
26+
},
27+
Limit: {
28+
minWidth: 20,
29+
},
30+
'Bulk Limit': {
31+
minWidth: 20,
32+
},
33+
};
34+
35+
cliux.table(tableData, columns, { printLine: cliux.print });
36+
} catch (error) {
37+
this.log('Unable to retrieve the rate limits configuration', error instanceof Error ? error.message : error);
38+
}
39+
}
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { cliux, configHandler, FlagInput, flags } from '@contentstack/cli-utilities';
2+
import { Command } from '@contentstack/cli-command';
3+
import { askOrgID } from '../../../utils/interactive';
4+
5+
export default class RateLimitRemoveCommand extends Command {
6+
static description: string = 'Remove rate-limit of the organization';
7+
8+
static flags: FlagInput = {
9+
org: flags.string({
10+
description: 'Provide the organization UID',
11+
}),
12+
};
13+
static examples = ['$ csdx config:remove:rate-limit --org <<org_uid>>'];
14+
async run() {
15+
try {
16+
const { flags } = await this.parse(RateLimitRemoveCommand);
17+
let { org } = flags;
18+
if (!org) {
19+
org = await askOrgID();
20+
}
21+
const rateLimit = configHandler.get('rateLimit') || {};
22+
23+
if (!rateLimit[org]) {
24+
cliux.print(`No rate limit found for the organization UID: ${org}`, { color: 'red' });
25+
return;
26+
}
27+
configHandler.delete(`rateLimit.${org}`);
28+
cliux.print(`Rate limit entry for organization UID ${org} has been removed.`, { color: 'green' });
29+
} catch (error) {
30+
this.log('Unable to remove the rate limit entry', error instanceof Error ? error.message : error);
31+
}
32+
}
33+
}

packages/contentstack-config/src/interfaces/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@ export interface Region {
1919
personalizeUrl: string;
2020
launchHubUrl: string;
2121
}
22+
23+
24+
export interface RateLimitConfig {
25+
getLimit?: {
26+
value: number;
27+
utilize: number;
28+
};
29+
limit?: {
30+
value: number;
31+
utilize: number;
32+
};
33+
bulkLimit?: {
34+
value: number;
35+
utilize: number;
36+
};
37+
}

packages/contentstack-config/src/utils/interactive.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,11 @@ export async function askEarlyAccessHeaderAlias(): Promise<string> {
9696
validate: inquireRequireFieldValidation,
9797
});
9898
}
99+
100+
export const askOrgID = async (): Promise<string> => {
101+
return cliux.inquire<string>({
102+
type: 'input',
103+
message: 'Provide the organization UID',
104+
name: 'org',
105+
});
106+
};

0 commit comments

Comments
 (0)