Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4127,26 +4127,22 @@ Get current occupancy metrics for a room

```
USAGE
$ ably rooms occupancy get ROOM [-v] [--json | --pretty-json] [--client-id <value>]
$ ably rooms occupancy get ROOM [-v] [--json | --pretty-json]

ARGUMENTS
ROOM Room to get occupancy for

FLAGS
-v, --verbose Output verbose logs
--client-id=<value> Overrides any default client ID when using API authentication. Use "none" to explicitly set
no client ID. Not applicable when using token authentication.
--json Output in JSON format
--pretty-json Output in colorized JSON format
-v, --verbose Output verbose logs
--json Output in JSON format
--pretty-json Output in colorized JSON format

DESCRIPTION
Get current occupancy metrics for a room

EXAMPLES
$ ably rooms occupancy get my-room

$ ABLY_API_KEY="YOUR_API_KEY" ably rooms occupancy get my-room

$ ably rooms occupancy get my-room --json

$ ably rooms occupancy get my-room --pretty-json
Expand All @@ -4156,14 +4152,14 @@ _See code: [src/commands/rooms/occupancy/get.ts](https://github.com/ably/ably-cl

## `ably rooms occupancy subscribe ROOM`

Subscribe to real-time occupancy metrics for a room
Subscribe to occupancy events on a room

```
USAGE
$ ably rooms occupancy subscribe ROOM [-v] [--json | --pretty-json] [--client-id <value>] [-D <value>]

ARGUMENTS
ROOM Room to subscribe to occupancy for
ROOM Room to subscribe to occupancy events

FLAGS
-D, --duration=<value> Automatically exit after N seconds
Expand All @@ -4174,14 +4170,14 @@ FLAGS
--pretty-json Output in colorized JSON format

DESCRIPTION
Subscribe to real-time occupancy metrics for a room
Subscribe to occupancy events on a room

EXAMPLES
$ ably rooms occupancy subscribe my-room

$ ably rooms occupancy subscribe my-room --json

$ ably rooms occupancy subscribe --pretty-json my-room
$ ably rooms occupancy subscribe my-room --duration 30
```

_See code: [src/commands/rooms/occupancy/subscribe.ts](https://github.com/ably/ably-cli/blob/v0.17.0/src/commands/rooms/occupancy/subscribe.ts)_
Expand Down
54 changes: 4 additions & 50 deletions src/commands/channels/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AblyBaseCommand } from "../../base-command.js";
import { productApiFlags } from "../../flags.js";
import {
formatCountLabel,
formatLabel,
formatLimitWarning,
formatResource,
} from "../../utils/output.js";
Expand All @@ -13,23 +12,9 @@ import {
formatPaginationLog,
} from "../../utils/pagination.js";

interface ChannelMetrics {
connections?: number;
presenceConnections?: number;
presenceMembers?: number;
publishers?: number;
subscribers?: number;
}

interface ChannelStatus {
occupancy?: {
metrics?: ChannelMetrics;
};
}

interface ChannelItem {
channelId: string;
status?: ChannelStatus;
[key: string]: unknown;
}

// Type for channel listing request parameters
Expand Down Expand Up @@ -121,10 +106,7 @@ export default class ChannelsList extends AblyBaseCommand {
const next = buildPaginationNext(hasMore);
this.logJsonResult(
{
channels: channels.map((channel: ChannelItem) => ({
channelId: channel.channelId,
metrics: channel.status?.occupancy?.metrics || {},
})),
channels: channels.map((channel: ChannelItem) => channel.channelId),
hasMore,
...(next && { next }),
timestamp: new Date().toISOString(),
Expand All @@ -139,39 +121,11 @@ export default class ChannelsList extends AblyBaseCommand {
}

this.log(
`Found ${formatCountLabel(channels.length, "active channel")}:`,
`Found ${formatCountLabel(channels.length, "active channel")}:\n`,
);

for (const channel of channels) {
this.log(`${formatResource(channel.channelId)}`);

// Show occupancy if available
if (channel.status?.occupancy?.metrics) {
const { metrics } = channel.status.occupancy;
this.log(
` ${formatLabel("Connections")} ${metrics.connections || 0}`,
);
this.log(
` ${formatLabel("Publishers")} ${metrics.publishers || 0}`,
);
this.log(
` ${formatLabel("Subscribers")} ${metrics.subscribers || 0}`,
);

if (metrics.presenceConnections !== undefined) {
this.log(
` ${formatLabel("Presence Connections")} ${metrics.presenceConnections}`,
);
}

if (metrics.presenceMembers !== undefined) {
this.log(
` ${formatLabel("Presence Members")} ${metrics.presenceMembers}`,
);
}
}

this.log(""); // Add a line break between channels
}

if (hasMore) {
Expand All @@ -180,7 +134,7 @@ export default class ChannelsList extends AblyBaseCommand {
flags.limit,
"channels",
);
if (warning) this.log(warning);
if (warning) this.log(`\n${warning}`);
}
}
} catch (error) {
Expand Down
47 changes: 24 additions & 23 deletions src/commands/channels/occupancy/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface OccupancyMetrics {
presenceSubscribers: number;
publishers: number;
subscribers: number;
objectPublishers: number;
objectSubscribers: number;
}

export default class ChannelsOccupancyGet extends AblyBaseCommand {
Expand Down Expand Up @@ -64,14 +66,18 @@ export default class ChannelsOccupancyGet extends AblyBaseCommand {
presenceSubscribers: 0,
publishers: 0,
subscribers: 0,
objectPublishers: 0,
objectSubscribers: 0,
};

// Output the occupancy metrics based on format
if (this.shouldOutputJson(flags)) {
this.logJsonResult(
{
channel: channelName,
metrics: occupancyMetrics,
occupancy: {
channelName,
metrics: occupancyMetrics,
},
},
flags,
);
Expand All @@ -80,32 +86,27 @@ export default class ChannelsOccupancyGet extends AblyBaseCommand {
`Occupancy metrics for channel ${formatResource(channelName)}:\n`,
);
this.log(
`${formatLabel("Connections")} ${occupancyMetrics.connections ?? 0}`,
`${formatLabel("Connections")} ${occupancyMetrics.connections}`,
);
this.log(`${formatLabel("Publishers")} ${occupancyMetrics.publishers}`);
this.log(
`${formatLabel("Publishers")} ${occupancyMetrics.publishers ?? 0}`,
`${formatLabel("Subscribers")} ${occupancyMetrics.subscribers}`,
);
this.log(
`${formatLabel("Subscribers")} ${occupancyMetrics.subscribers ?? 0}`,
`${formatLabel("Presence Connections")} ${occupancyMetrics.presenceConnections}`,
);
this.log(
`${formatLabel("Presence Members")} ${occupancyMetrics.presenceMembers}`,
);
this.log(
`${formatLabel("Presence Subscribers")} ${occupancyMetrics.presenceSubscribers}`,
);
this.log(
`${formatLabel("Object Publishers")} ${occupancyMetrics.objectPublishers}`,
);
this.log(
`${formatLabel("Object Subscribers")} ${occupancyMetrics.objectSubscribers}`,
);

if (occupancyMetrics.presenceConnections !== undefined) {
this.log(
`${formatLabel("Presence Connections")} ${occupancyMetrics.presenceConnections}`,
);
}

if (occupancyMetrics.presenceMembers !== undefined) {
this.log(
`${formatLabel("Presence Members")} ${occupancyMetrics.presenceMembers}`,
);
}

if (occupancyMetrics.presenceSubscribers !== undefined) {
this.log(
`${formatLabel("Presence Subscribers")} ${occupancyMetrics.presenceSubscribers}`,
);
}
}
} catch (error) {
this.fail(error, flags, "occupancyGet", {
Expand Down
30 changes: 24 additions & 6 deletions src/commands/channels/occupancy/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class ChannelsOccupancySubscribe extends AblyBaseCommand {
await channel.subscribe(occupancyEventName, (message: Ably.Message) => {
const timestamp = formatMessageTimestamp(message.timestamp);
const event = {
channel: channelName,
channelName,
event: occupancyEventName,
data: message.data,
timestamp,
Expand All @@ -103,19 +103,37 @@ export default class ChannelsOccupancySubscribe extends AblyBaseCommand {
);

if (this.shouldOutputJson(flags)) {
this.logJsonEvent(event, flags);
this.logJsonEvent({ occupancy: event }, flags);
} else {
this.log(formatTimestamp(timestamp));
this.log(`${formatLabel("Channel")} ${formatResource(channelName)}`);
this.log(
`${formatTimestamp(timestamp)} ${formatResource(`Channel: ${channelName}`)} | ${formatEventType("Occupancy Update")}`,
`${formatLabel("Event")} ${formatEventType("Occupancy Update")}`,
);

if (message.data !== null && message.data !== undefined) {
if (message.data?.metrics) {
const metrics = message.data.metrics;
this.log(`${formatLabel("Connections")} ${metrics.connections}`);
this.log(`${formatLabel("Publishers")} ${metrics.publishers}`);
this.log(`${formatLabel("Subscribers")} ${metrics.subscribers}`);
this.log(
`${formatLabel("Occupancy Data")} ${JSON.stringify(message.data, null, 2)}`,
`${formatLabel("Presence Connections")} ${metrics.presenceConnections}`,
);
this.log(
`${formatLabel("Presence Members")} ${metrics.presenceMembers}`,
);
this.log(
`${formatLabel("Presence Subscribers")} ${metrics.presenceSubscribers}`,
);
this.log(
`${formatLabel("Object Publishers")} ${metrics.objectPublishers}`,
);
this.log(
`${formatLabel("Object Subscribers")} ${metrics.objectSubscribers}`,
);
}

this.log(""); // Empty line for better readability
this.log("");
}
});

Expand Down
60 changes: 12 additions & 48 deletions src/commands/rooms/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,16 @@ import {
formatCountLabel,
formatLimitWarning,
formatResource,
formatLabel,
} from "../../utils/output.js";
import {
buildPaginationNext,
collectFilteredPaginatedResults,
formatPaginationLog,
} from "../../utils/pagination.js";

// Add interface definitions at the beginning of the file
interface RoomMetrics {
connections?: number;
presenceConnections?: number;
presenceMembers?: number;
publishers?: number;
subscribers?: number;
}

interface RoomStatus {
occupancy?: {
metrics?: RoomMetrics;
};
}

interface RoomItem {
channelId: string;
room: string;
status?: RoomStatus;
[key: string]: unknown;
}

Expand Down Expand Up @@ -140,47 +123,28 @@ export default class RoomsList extends ChatBaseCommand {
// Output rooms based on format
if (this.shouldOutputJson(flags)) {
const next = buildPaginationNext(hasMore);
this.logJsonResult({ rooms, hasMore, ...(next && { next }) }, flags);
this.logJsonResult(
{
rooms: rooms.map((room) => room.room),
hasMore,
...(next && { next }),
timestamp: new Date().toISOString(),
total: rooms.length,
},
flags,
);
} else {
if (rooms.length === 0) {
this.log("No active chat rooms found.");
return;
}

this.log(
`Found ${formatCountLabel(rooms.length, "active chat room")}:`,
`Found ${formatCountLabel(rooms.length, "active chat room")}:\n`,
);

for (const room of rooms) {
this.log(`${formatResource(room.room)}`);

// Show occupancy if available
if (room.status?.occupancy?.metrics) {
const { metrics } = room.status.occupancy;
this.log(
` ${formatLabel("Connections")} ${metrics.connections || 0}`,
);
this.log(
` ${formatLabel("Publishers")} ${metrics.publishers || 0}`,
);
this.log(
` ${formatLabel("Subscribers")} ${metrics.subscribers || 0}`,
);

if (metrics.presenceConnections !== undefined) {
this.log(
` ${formatLabel("Presence Connections")} ${metrics.presenceConnections}`,
);
}

if (metrics.presenceMembers !== undefined) {
this.log(
` ${formatLabel("Presence Members")} ${metrics.presenceMembers}`,
);
}
}

this.log(""); // Add a line break between rooms
}

if (hasMore) {
Expand All @@ -189,7 +153,7 @@ export default class RoomsList extends ChatBaseCommand {
flags.limit,
"rooms",
);
if (warning) this.log(warning);
if (warning) this.log(`\n${warning}`);
}
}
} catch (error) {
Expand Down
Loading
Loading