0

Get Speed Tests Histogram

by
Published Nov 16, 2023

Get an histogram from the previous 90 days of Cloudflare Speed Test data, split into fixed bandwidth (Mbps), latency (ms) or jitter (ms) buckets.

Script cloudflare Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get Speed Tests Histogram
8
 * Get an histogram from the previous 90 days of Cloudflare Speed Test data, split into fixed bandwidth (Mbps), latency (ms) or jitter (ms) buckets.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  name: string | undefined,
13
  dateEnd: string | undefined,
14
  asn: string | undefined,
15
  location: string | undefined,
16
  bucketSize: string | undefined,
17
  metricGroup: "BANDWIDTH" | "LATENCY" | "JITTER" | undefined,
18
  format: "JSON" | "CSV" | undefined
19
) {
20
  const url = new URL(
21
    `https://api.cloudflare.com/client/v4/radar/quality/speed/histogram`
22
  );
23
  for (const [k, v] of [
24
    ["name", name],
25
    ["dateEnd", dateEnd],
26
    ["asn", asn],
27
    ["location", location],
28
    ["bucketSize", bucketSize],
29
    ["metricGroup", metricGroup],
30
    ["format", format],
31
  ]) {
32
    if (v !== undefined && v !== "") {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const response = await fetch(url, {
37
    method: "GET",
38
    headers: {
39
      "X-AUTH-EMAIL": auth.email,
40
      "X-AUTH-KEY": auth.key,
41
      Authorization: "Bearer " + auth.token,
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51