Skip to content

Commit 9d92ed8

Browse files
committed
Fixed channels and rooms subcommands
1. list -> now only returns names of channels and rooms without occupancy 2. occupancy -> refactored to show all fields for json/non-json flags
1 parent f67c148 commit 9d92ed8

14 files changed

Lines changed: 452 additions & 543 deletions

File tree

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4127,26 +4127,22 @@ Get current occupancy metrics for a room
41274127

41284128
```
41294129
USAGE
4130-
$ ably rooms occupancy get ROOM [-v] [--json | --pretty-json] [--client-id <value>]
4130+
$ ably rooms occupancy get ROOM [-v] [--json | --pretty-json]
41314131
41324132
ARGUMENTS
41334133
ROOM Room to get occupancy for
41344134
41354135
FLAGS
4136-
-v, --verbose Output verbose logs
4137-
--client-id=<value> Overrides any default client ID when using API authentication. Use "none" to explicitly set
4138-
no client ID. Not applicable when using token authentication.
4139-
--json Output in JSON format
4140-
--pretty-json Output in colorized JSON format
4136+
-v, --verbose Output verbose logs
4137+
--json Output in JSON format
4138+
--pretty-json Output in colorized JSON format
41414139
41424140
DESCRIPTION
41434141
Get current occupancy metrics for a room
41444142
41454143
EXAMPLES
41464144
$ ably rooms occupancy get my-room
41474145
4148-
$ ABLY_API_KEY="YOUR_API_KEY" ably rooms occupancy get my-room
4149-
41504146
$ ably rooms occupancy get my-room --json
41514147
41524148
$ ably rooms occupancy get my-room --pretty-json
@@ -4156,14 +4152,14 @@ _See code: [src/commands/rooms/occupancy/get.ts](https://github.com/ably/ably-cl
41564152

41574153
## `ably rooms occupancy subscribe ROOM`
41584154

4159-
Subscribe to real-time occupancy metrics for a room
4155+
Subscribe to occupancy events on a room
41604156

41614157
```
41624158
USAGE
41634159
$ ably rooms occupancy subscribe ROOM [-v] [--json | --pretty-json] [--client-id <value>] [-D <value>]
41644160
41654161
ARGUMENTS
4166-
ROOM Room to subscribe to occupancy for
4162+
ROOM Room to subscribe to occupancy events
41674163
41684164
FLAGS
41694165
-D, --duration=<value> Automatically exit after N seconds
@@ -4174,14 +4170,14 @@ FLAGS
41744170
--pretty-json Output in colorized JSON format
41754171
41764172
DESCRIPTION
4177-
Subscribe to real-time occupancy metrics for a room
4173+
Subscribe to occupancy events on a room
41784174
41794175
EXAMPLES
41804176
$ ably rooms occupancy subscribe my-room
41814177
41824178
$ ably rooms occupancy subscribe my-room --json
41834179
4184-
$ ably rooms occupancy subscribe --pretty-json my-room
4180+
$ ably rooms occupancy subscribe my-room --duration 30
41854181
```
41864182

41874183
_See code: [src/commands/rooms/occupancy/subscribe.ts](https://github.com/ably/ably-cli/blob/v0.17.0/src/commands/rooms/occupancy/subscribe.ts)_

src/commands/channels/list.ts

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { AblyBaseCommand } from "../../base-command.js";
33
import { productApiFlags } from "../../flags.js";
44
import {
55
formatCountLabel,
6-
formatLabel,
76
formatLimitWarning,
87
formatResource,
98
} from "../../utils/output.js";
@@ -13,23 +12,9 @@ import {
1312
formatPaginationLog,
1413
} from "../../utils/pagination.js";
1514

16-
interface ChannelMetrics {
17-
connections?: number;
18-
presenceConnections?: number;
19-
presenceMembers?: number;
20-
publishers?: number;
21-
subscribers?: number;
22-
}
23-
24-
interface ChannelStatus {
25-
occupancy?: {
26-
metrics?: ChannelMetrics;
27-
};
28-
}
29-
3015
interface ChannelItem {
3116
channelId: string;
32-
status?: ChannelStatus;
17+
[key: string]: unknown;
3318
}
3419

3520
// Type for channel listing request parameters
@@ -123,7 +108,6 @@ export default class ChannelsList extends AblyBaseCommand {
123108
{
124109
channels: channels.map((channel: ChannelItem) => ({
125110
channelId: channel.channelId,
126-
metrics: channel.status?.occupancy?.metrics || {},
127111
})),
128112
hasMore,
129113
...(next && { next }),
@@ -139,39 +123,11 @@ export default class ChannelsList extends AblyBaseCommand {
139123
}
140124

141125
this.log(
142-
`Found ${formatCountLabel(channels.length, "active channel")}:`,
126+
`Found ${formatCountLabel(channels.length, "active channel")}:\n`,
143127
);
144128

145129
for (const channel of channels) {
146130
this.log(`${formatResource(channel.channelId)}`);
147-
148-
// Show occupancy if available
149-
if (channel.status?.occupancy?.metrics) {
150-
const { metrics } = channel.status.occupancy;
151-
this.log(
152-
` ${formatLabel("Connections")} ${metrics.connections || 0}`,
153-
);
154-
this.log(
155-
` ${formatLabel("Publishers")} ${metrics.publishers || 0}`,
156-
);
157-
this.log(
158-
` ${formatLabel("Subscribers")} ${metrics.subscribers || 0}`,
159-
);
160-
161-
if (metrics.presenceConnections !== undefined) {
162-
this.log(
163-
` ${formatLabel("Presence Connections")} ${metrics.presenceConnections}`,
164-
);
165-
}
166-
167-
if (metrics.presenceMembers !== undefined) {
168-
this.log(
169-
` ${formatLabel("Presence Members")} ${metrics.presenceMembers}`,
170-
);
171-
}
172-
}
173-
174-
this.log(""); // Add a line break between channels
175131
}
176132

177133
if (hasMore) {
@@ -180,7 +136,7 @@ export default class ChannelsList extends AblyBaseCommand {
180136
flags.limit,
181137
"channels",
182138
);
183-
if (warning) this.log(warning);
139+
if (warning) this.log(`\n${warning}`);
184140
}
185141
}
186142
} catch (error) {

src/commands/channels/occupancy/get.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ export default class ChannelsOccupancyGet extends AblyBaseCommand {
7070
if (this.shouldOutputJson(flags)) {
7171
this.logJsonResult(
7272
{
73-
channel: channelName,
74-
metrics: occupancyMetrics,
73+
occupancy: {
74+
channelName,
75+
metrics: occupancyMetrics,
76+
},
7577
},
7678
flags,
7779
);
@@ -80,32 +82,21 @@ export default class ChannelsOccupancyGet extends AblyBaseCommand {
8082
`Occupancy metrics for channel ${formatResource(channelName)}:\n`,
8183
);
8284
this.log(
83-
`${formatLabel("Connections")} ${occupancyMetrics.connections ?? 0}`,
85+
`${formatLabel("Connections")} ${occupancyMetrics.connections}`,
8486
);
87+
this.log(`${formatLabel("Publishers")} ${occupancyMetrics.publishers}`);
8588
this.log(
86-
`${formatLabel("Publishers")} ${occupancyMetrics.publishers ?? 0}`,
89+
`${formatLabel("Subscribers")} ${occupancyMetrics.subscribers}`,
8790
);
8891
this.log(
89-
`${formatLabel("Subscribers")} ${occupancyMetrics.subscribers ?? 0}`,
92+
`${formatLabel("Presence Connections")} ${occupancyMetrics.presenceConnections}`,
93+
);
94+
this.log(
95+
`${formatLabel("Presence Members")} ${occupancyMetrics.presenceMembers}`,
96+
);
97+
this.log(
98+
`${formatLabel("Presence Subscribers")} ${occupancyMetrics.presenceSubscribers}`,
9099
);
91-
92-
if (occupancyMetrics.presenceConnections !== undefined) {
93-
this.log(
94-
`${formatLabel("Presence Connections")} ${occupancyMetrics.presenceConnections}`,
95-
);
96-
}
97-
98-
if (occupancyMetrics.presenceMembers !== undefined) {
99-
this.log(
100-
`${formatLabel("Presence Members")} ${occupancyMetrics.presenceMembers}`,
101-
);
102-
}
103-
104-
if (occupancyMetrics.presenceSubscribers !== undefined) {
105-
this.log(
106-
`${formatLabel("Presence Subscribers")} ${occupancyMetrics.presenceSubscribers}`,
107-
);
108-
}
109100
}
110101
} catch (error) {
111102
this.fail(error, flags, "occupancyGet", {

src/commands/channels/occupancy/subscribe.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export default class ChannelsOccupancySubscribe extends AblyBaseCommand {
8989
await channel.subscribe(occupancyEventName, (message: Ably.Message) => {
9090
const timestamp = formatMessageTimestamp(message.timestamp);
9191
const event = {
92-
channel: channelName,
92+
channelName,
9393
event: occupancyEventName,
9494
data: message.data,
9595
timestamp,
@@ -103,19 +103,35 @@ export default class ChannelsOccupancySubscribe extends AblyBaseCommand {
103103
);
104104

105105
if (this.shouldOutputJson(flags)) {
106-
this.logJsonEvent(event, flags);
106+
this.logJsonEvent({ occupancy: event }, flags);
107107
} else {
108+
this.log(formatTimestamp(timestamp));
109+
this.log(`${formatLabel("Channel")} ${formatResource(channelName)}`);
108110
this.log(
109-
`${formatTimestamp(timestamp)} ${formatResource(`Channel: ${channelName}`)} | ${formatEventType("Occupancy Update")}`,
111+
`${formatLabel("Event")} ${formatEventType("Occupancy Update")}`,
110112
);
111113

112-
if (message.data !== null && message.data !== undefined) {
114+
if (message.data?.metrics) {
115+
const metrics = message.data.metrics;
113116
this.log(
114-
`${formatLabel("Occupancy Data")} ${JSON.stringify(message.data, null, 2)}`,
117+
`${formatLabel("Connections")} ${metrics.connections ?? 0}`,
118+
);
119+
this.log(`${formatLabel("Publishers")} ${metrics.publishers ?? 0}`);
120+
this.log(
121+
`${formatLabel("Subscribers")} ${metrics.subscribers ?? 0}`,
122+
);
123+
this.log(
124+
`${formatLabel("Presence Connections")} ${metrics.presenceConnections ?? 0}`,
125+
);
126+
this.log(
127+
`${formatLabel("Presence Members")} ${metrics.presenceMembers ?? 0}`,
128+
);
129+
this.log(
130+
`${formatLabel("Presence Subscribers")} ${metrics.presenceSubscribers ?? 0}`,
115131
);
116132
}
117133

118-
this.log(""); // Empty line for better readability
134+
this.log("");
119135
}
120136
});
121137

src/commands/rooms/list.ts

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,16 @@ import {
55
formatCountLabel,
66
formatLimitWarning,
77
formatResource,
8-
formatLabel,
98
} from "../../utils/output.js";
109
import {
1110
buildPaginationNext,
1211
collectFilteredPaginatedResults,
1312
formatPaginationLog,
1413
} from "../../utils/pagination.js";
1514

16-
// Add interface definitions at the beginning of the file
17-
interface RoomMetrics {
18-
connections?: number;
19-
presenceConnections?: number;
20-
presenceMembers?: number;
21-
publishers?: number;
22-
subscribers?: number;
23-
}
24-
25-
interface RoomStatus {
26-
occupancy?: {
27-
metrics?: RoomMetrics;
28-
};
29-
}
30-
3115
interface RoomItem {
3216
channelId: string;
3317
room: string;
34-
status?: RoomStatus;
3518
[key: string]: unknown;
3619
}
3720

@@ -140,47 +123,30 @@ export default class RoomsList extends ChatBaseCommand {
140123
// Output rooms based on format
141124
if (this.shouldOutputJson(flags)) {
142125
const next = buildPaginationNext(hasMore);
143-
this.logJsonResult({ rooms, hasMore, ...(next && { next }) }, flags);
126+
this.logJsonResult(
127+
{
128+
rooms: rooms.map((room) => ({
129+
roomName: room.room,
130+
})),
131+
hasMore,
132+
...(next && { next }),
133+
timestamp: new Date().toISOString(),
134+
total: rooms.length,
135+
},
136+
flags,
137+
);
144138
} else {
145139
if (rooms.length === 0) {
146140
this.log("No active chat rooms found.");
147141
return;
148142
}
149143

150144
this.log(
151-
`Found ${formatCountLabel(rooms.length, "active chat room")}:`,
145+
`Found ${formatCountLabel(rooms.length, "active chat room")}:\n`,
152146
);
153147

154148
for (const room of rooms) {
155149
this.log(`${formatResource(room.room)}`);
156-
157-
// Show occupancy if available
158-
if (room.status?.occupancy?.metrics) {
159-
const { metrics } = room.status.occupancy;
160-
this.log(
161-
` ${formatLabel("Connections")} ${metrics.connections || 0}`,
162-
);
163-
this.log(
164-
` ${formatLabel("Publishers")} ${metrics.publishers || 0}`,
165-
);
166-
this.log(
167-
` ${formatLabel("Subscribers")} ${metrics.subscribers || 0}`,
168-
);
169-
170-
if (metrics.presenceConnections !== undefined) {
171-
this.log(
172-
` ${formatLabel("Presence Connections")} ${metrics.presenceConnections}`,
173-
);
174-
}
175-
176-
if (metrics.presenceMembers !== undefined) {
177-
this.log(
178-
` ${formatLabel("Presence Members")} ${metrics.presenceMembers}`,
179-
);
180-
}
181-
}
182-
183-
this.log(""); // Add a line break between rooms
184150
}
185151

186152
if (hasMore) {
@@ -189,7 +155,7 @@ export default class RoomsList extends ChatBaseCommand {
189155
flags.limit,
190156
"rooms",
191157
);
192-
if (warning) this.log(warning);
158+
if (warning) this.log(`\n${warning}`);
193159
}
194160
}
195161
} catch (error) {

0 commit comments

Comments
 (0)