jsdelivr/globalping-cli

Add rate limit and credits info to existing commands

Closed this issue · 1 comments

Requires #42 (comment) (only the part with ENV support so that we can test the token cases).

First, here's the relevant part of the docs:

image

1. When creating a new measurement fails with a 429 status

Calculate XX = X-RateLimit-Remaining + X-Credits-Remaining
Calculate YY = X-RateLimit-Remaining + X-Credits-Required

Note - X-Credits-Required is currently not returned by the API for requests without a token. This will be changed. Any missing header should be treated as 0.

1.1. If the user didn't provide a token

1.1.1.
If XX > 0, print a message: "You only have XX credits remaining, and YY were required. Try requesting fewer probes or wait X-RateLimit-Reset minutes for the limit to reset. You can get higher limits by creating an account. Sign up at https://globalping.io"

1.1.2.
If XX == 0, print a message: "You have run out of credits for this session. You can wait X-RateLimit-Reset minutes for the limit to reset or get higher limits by creating an account. Sign up at https://globalping.io"

1.2. If the user provided a token

1.2.1.
If XX > 0, print a message: "You only have XX credits remaining, and YY were required. Try requesting fewer probes or wait X-RateLimit-Reset minutes for the limit to reset. You can get higher limits by sponsoring us or hosting probes."

1.2.2.
If XX == 0, print a message: "You have run out of credits for this session. You can wait X-RateLimit-Reset minutes for the limit to reset or get higher limits by sponsoring us or hosting probes."

2. When infinite measurement fails while it's running (at least one test before succeeded)

Terminate, keep the output, and add a message below it (to stderr). If --share was used, the error message goes first, then the sharing message. The error message is the same as in 1.1.2/1.2.2 case, depending on if a token was used.

2.1. In single probe mode

Example:

> Santiago, CL, SA, The Constant Company, LLC (AS20473)
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=1.18 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=59 time=1.40 ms
64 bytes from 1.1.1.1: icmp_seq=3 ttl=59 time=1.38 ms
--- 1.1.1.1 ping statistics ---
3 packets transmitted, 3 received, 0.00% packet loss, time 2106ms
rtt min/avg/max/mdev = 1.180/1.320/1.400/0.099 ms
> You have run out of credits for this session...

2.2. Table mode output

Example:

Location                                                                     | Sent |    Loss |     Last |      Min |      Avg |      Max
Los Angeles (CA), US, NA, MULTACOM CORPORATION (AS35916)                     |    2 |   0.00% |  1.78 ms |  1.78 ms |  2.12 ms |  2.45 ms
Beijing, CN, AS, Shenzhen Tencent Computer Systems Company Limited (AS45090) |    1 | 100.00% |        - |        - |        - |        -
> You have run out of credits for this session...

3. Warning while infinite mode runs

3.1 In single probe mode

Let's add one line (stderr) above location:

> This infinite ping will consume 1 API credit for every 16 packets until stopped.
> Falkenstein, DE, EU, Hetzner Online GmbH (AS24940)
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=55 time=5.63 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=55 time=5.32 ms
64 bytes from 1.1.1.1: icmp_seq=3 ttl=55 time=5.28 ms
64 bytes from 1.1.1.1: icmp_seq=4 ttl=55 time=5.39 ms
64 bytes from 1.1.1.1: icmp_seq=5 ttl=55 time=5.35 ms
^C
--- 1.1.1.1 ping statistics ---
5 packets transmitted, 5 received, 0.00% packet loss, time 2621ms
rtt min/avg/max/mdev = 5.280/5.394/5.630/0.123 ms

3.2 Table mode

Let's add one line below and update it real-time:

Location                                               | Sent |    Loss |     Last |      Min |      Avg |      Max
Dallas (TX), US, NA, Baxet Group Inc. (AS398343)       |    6 |   0.00% |  7.19 ms |  7.16 ms |  7.27 ms |  7.36 ms
Kyiv, UA, EU, STARK INDUSTRIES SOLUTIONS LTD (AS44477) |    7 |   0.00% |  14.9 ms |  14.8 ms |  14.9 ms |  15.1 ms
Currently consuming ~XX API credits/minute.

Calculate XX as the sum of all probesCount (returned in the body response to POST) in the current infinite session (because 1 probe == 1 credit), divided by the session duration.

@radulucut let's do this next. Let me know if anything isn't clear.