Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-3590] Implement untyped stats API #1522

Merged
merged 2 commits into from
Jan 3, 2024
Merged
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
152 changes: 10 additions & 142 deletions ably.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,122 +751,6 @@
constructor(message: string, code: number, statusCode: number, cause?: string | Error | ErrorInfo);
}

/**
* Contains the aggregate counts for messages and data transferred.
*/
interface StatsMessageCount {
/**
* The count of all messages.
*/
count: number;
/**
* The total number of bytes transferred for all messages.
*/
data: number;
}

/**
* Contains a breakdown of summary stats data for different (channel vs presence) message types.
*/
interface StatsMessageTypes {
/**
* A {@link StatsMessageCount} object containing the count and byte value of messages and presence messages.
*/
all: StatsMessageCount;
/**
* A {@link StatsMessageCount} object containing the count and byte value of messages.
*/
messages: StatsMessageCount;
/**
* A {@link StatsMessageCount} object containing the count and byte value of presence messages.
*/
presence: StatsMessageCount;
}

/**
* Contains the aggregate counts for requests made.
*/
interface StatsRequestCount {
/**
* The number of requests that failed.
*/
failed: number;
/**
* The number of requests that were refused, typically as a result of permissions or a limit being exceeded.
*/
refused: number;
/**
* The number of requests that succeeded.
*/
succeeded: number;
}

/**
* Contains the aggregate data for usage of a resource in a specific scope.
*/
interface StatsResourceCount {
/**
* The average number of resources of this type used for this period.
*/
mean: number;
/**
* The minimum total resources of this type used for this period.
*/
min: number;
/**
* The total number of resources opened of this type.
*/
opened: number;
/**
* The peak number of resources of this type used for this period.
*/
peak: number;
/**
* The number of resource requests refused within this period.
*/
refused: number;
}

/**
* Contains a breakdown of summary stats data for different (TLS vs non-TLS) connection types.
*/
interface StatsConnectionTypes {
/**
* A {@link StatsResourceCount} object containing a breakdown of usage by scope over TLS connections (both TLS and non-TLS).
*/
all: StatsResourceCount;
/**
* A {@link StatsResourceCount} object containing a breakdown of usage by scope over non-TLS connections.
*/
plain: StatsResourceCount;
/**
* A {@link StatsResourceCount} object containing a breakdown of usage by scope over TLS connections.
*/
tls: StatsResourceCount;
}

/**
* Contains a breakdown of summary stats data for traffic over various transport types.
*/
interface StatsMessageTraffic {
/**
* A {@link StatsMessageTypes} object containing a breakdown of usage by message type for all messages (includes `realtime`, `rest` and `webhook` messages).
*/
all: StatsMessageTypes;
/**
* A {@link StatsMessageTypes} object containing a breakdown of usage by message type for messages transferred over a realtime transport such as WebSocket.
*/
realtime: StatsMessageTypes;
/**
* A {@link StatsMessageTypes} object containing a breakdown of usage by message type for messages transferred over a rest transport such as WebSocket.
*/
rest: StatsMessageTypes;
/**
* A {@link StatsMessageTypes} object containing a breakdown of usage by message type for messages delivered using webhooks.
*/
webhook: StatsMessageTypes;
}

/**
* Contains an Ably Token and its associated metadata.
*/
Expand Down Expand Up @@ -2304,8 +2188,8 @@
*/
get(name: string, channelOptions?: ChannelOptions): T;
/**
* @experimental This is a preview feature and may change in a future non-major release.

Check warning on line 2191 in ably.d.ts

View workflow job for this annotation

GitHub Actions / lint

Invalid JSDoc tag name "experimental"
* This experimental method allows you to create custom realtime data feeds by selectively subscribing

Check warning on line 2192 in ably.d.ts

View workflow job for this annotation

GitHub Actions / lint

Expected no lines between tags
* to receive only part of the data from the channel.
* See the [announcement post](https://pages.ably.com/subscription-filters-preview) for more information.
*
Expand Down Expand Up @@ -2563,42 +2447,26 @@
* Contains application statistics for a specified time interval and time period.
*/
class Stats {
/**
* A {@link StatsMessageTypes} object containing the aggregate count of all message stats.
*/
all: StatsMessageTypes;
/**
* A {@link StatsRequestCount} object containing a breakdown of API Requests.
*/
apiRequests: StatsRequestCount;
/**
* A {@link StatsResourceCount} object containing a breakdown of channels.
*/
channels: StatsResourceCount;
/**
* A {@link StatsConnectionTypes} object containing a breakdown of connection related stats, such as min, mean and peak connections.
*/
connections: StatsConnectionTypes;
/**
* A {@link StatsMessageTraffic} object containing the aggregate count of inbound message stats.
*/
inbound: StatsMessageTraffic;
/**
* The UTC time at which the time period covered begins. If `unit` is set to `minute` this will be in the format `YYYY-mm-dd:HH:MM`, if `hour` it will be `YYYY-mm-dd:HH`, if `day` it will be `YYYY-mm-dd:00` and if `month` it will be `YYYY-mm-01:00`.
*/
intervalId: string;
/**
* A {@link StatsMessageTraffic} object containing the aggregate count of outbound message stats.
* For entries that are still in progress, such as the current month: the last sub-interval included in this entry (in format yyyy-mm-dd:hh:mm:ss), else undefined.
*/
inProgress?: string;
/**
* The statistics for this time interval and time period. See the JSON schema which the {@link Stats.schema | `schema`} property points to for more information.
*/
outbound: StatsMessageTraffic;
entries: Partial<Record<string, number>>;
/**
* A {@link StatsMessageTypes} object containing the aggregate count of persisted message stats.
* The URL of a [JSON Schema](https://json-schema.org/) which describes the structure of this `Stats` object.
*/
persisted: StatsMessageTypes;
schema: string;
/**
* A {@link StatsRequestCount} object containing a breakdown of Ably Token requests.
* The ID of the Ably application the statistics are for.
*/
tokenRequests: StatsRequestCount;
appId: string;
}

/**
Expand Down
40 changes: 16 additions & 24 deletions src/common/lib/client/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,31 +210,23 @@ export class Rest {
if (this.client.options.headers) Utils.mixin(headers, this.client.options.headers);

const requestBody = Utils.encodeBody(requestBodyDTO, this.client._MsgPack, format);
Resource.post(
this.client,
'/messages',
requestBody,
headers,
{ newBatchResponse: 'true' },
null,
(err, body, headers, unpacked) => {
if (err) {
callback(err);
return;
}
Resource.post(this.client, '/messages', requestBody, headers, {}, null, (err, body, headers, unpacked) => {
if (err) {
callback(err);
return;
}

const batchResults = (
unpacked ? body : Utils.decodeBody(body, this.client._MsgPack, format)
) as BatchPublishResult[];
const batchResults = (
unpacked ? body : Utils.decodeBody(body, this.client._MsgPack, format)
) as BatchPublishResult[];

// I don't love the below type assertions for `callback` but not sure how to avoid them
if (singleSpecMode) {
(callback as StandardCallback<BatchPublishResult>)(null, batchResults[0]);
} else {
(callback as StandardCallback<BatchPublishResult[]>)(null, batchResults);
}
// I don't love the below type assertions for `callback` but not sure how to avoid them
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For internal typings I don't think we would really lose any strictness by just doing:

  batchPublish<T extends BatchPublishSpec | BatchPublishSpec[]>(
    specOrSpecs: T,
    callback?: StandardCallback<BatchPublishResult | BatchPublishResult[]>
  ): void | Promise<BatchPublishResult | BatchPublishResult[]> {

If we did that, there would be no need to use type assertions here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point; on the other hand I guess it's nice to be able to have an overload that matches the type declared in ably.d.ts.

if (singleSpecMode) {
(callback as StandardCallback<BatchPublishResult>)(null, batchResults[0]);
} else {
(callback as StandardCallback<BatchPublishResult[]>)(null, batchResults);
}
);
});
}

batchPresence(channels: string[]): Promise<BatchPresenceResult>;
Expand All @@ -257,7 +249,7 @@ export class Rest {
this.client,
'/presence',
headers,
{ newBatchResponse: 'true', channels: channelsParam },
{ channels: channelsParam },
null,
(err, body, headers, unpacked) => {
if (err) {
Expand Down Expand Up @@ -304,7 +296,7 @@ export class Rest {
`/keys/${keyName}/revokeTokens`,
requestBody,
headers,
{ newBatchResponse: 'true' },
{},
null,
(err, body, headers, unpacked) => {
if (err) {
Expand Down
Loading
Loading