0

Get top locations by total traffic anomalies generated.

by
Published Nov 16, 2023

Internet traffic anomalies are signals that might point to an outage, These alerts are automatically detected by Radar and then manually verified by our team. This endpoint returns the sum of alerts grouped by location.

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 top locations by total traffic anomalies generated.
8
 * Internet traffic anomalies are signals that might point to an outage,
9
        These alerts are automatically detected by Radar and then manually verified by our team.
10
        This endpoint returns the sum of alerts grouped by location.
11
        
12
 */
13
export async function main(
14
  auth: Cloudflare,
15
  limit: string | undefined,
16
  dateRange:
17
    | "1d"
18
    | "2d"
19
    | "7d"
20
    | "14d"
21
    | "28d"
22
    | "12w"
23
    | "24w"
24
    | "52w"
25
    | "1dControl"
26
    | "2dControl"
27
    | "7dControl"
28
    | "14dControl"
29
    | "28dControl"
30
    | "12wControl"
31
    | "24wControl"
32
    | undefined,
33
  dateStart: string | undefined,
34
  dateEnd: string | undefined,
35
  status: "VERIFIED" | "UNVERIFIED" | undefined,
36
  format: "JSON" | "CSV" | undefined
37
) {
38
  const url = new URL(
39
    `https://api.cloudflare.com/client/v4/radar/traffic_anomalies/locations`
40
  );
41
  for (const [k, v] of [
42
    ["limit", limit],
43
    ["dateRange", dateRange],
44
    ["dateStart", dateStart],
45
    ["dateEnd", dateEnd],
46
    ["status", status],
47
    ["format", format],
48
  ]) {
49
    if (v !== undefined && v !== "") {
50
      url.searchParams.append(k, v);
51
    }
52
  }
53
  const response = await fetch(url, {
54
    method: "GET",
55
    headers: {
56
      "X-AUTH-EMAIL": auth.email,
57
      "X-AUTH-KEY": auth.key,
58
      Authorization: "Bearer " + auth.token,
59
    },
60
    body: undefined,
61
  });
62
  if (!response.ok) {
63
    const text = await response.text();
64
    throw new Error(`${response.status} ${text}`);
65
  }
66
  return await response.json();
67
}
68