Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

tunnel-census.ts

86 lines3.2 KB

1import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
2import * as Clock from "effect/Clock";
3import * as Config from "effect/Config";
4import * as Console from "effect/Console";
5import * as Effect from "effect/Effect";
6import * as Redacted from "effect/Redacted";
7import * as Schema from "effect/Schema";
8import * as FetchHttpClient from "effect/http/FetchHttpClient";
9import * as HttpClient from "effect/http/HttpClient";
10import * as HttpClientRequest from "effect/http/HttpClientRequest";
11import * as HttpClientResponse from "effect/http/HttpClientResponse";
12​
13const PREFIX = "t3coderelay-managedendpoint-";
14const PAGE_SIZE = 1_000;
15const DAY_MS = 86_400_000;
16​
17// Cloudflare documents every field here as optional, so a tunnel missing one
18// must not fail the whole page.
19const Tunnel = Schema.Struct({
20 name: Schema.optional(Schema.NullOr(Schema.String)),
21 status: Schema.optional(Schema.NullOr(Schema.String)),
22 created_at: Schema.optional(Schema.NullOr(Schema.String)),
23 conns_inactive_at: Schema.optional(Schema.NullOr(Schema.String)),
24});
25type Tunnel = typeof Tunnel.Type;
26const TunnelPage = Schema.Struct({ result: Schema.Array(Tunnel) });
27​
28const stageOf = (name: string | null | undefined) =>
29 name ? name.slice(PREFIX.length).replace(/-[a-f0-9]{16}$/u, "") : "unknown";
30​
31const ageBucket = (tunnel: Tunnel, now: number) => {
32 const since = Date.parse(
33 (tunnel.status === "down" ? tunnel.conns_inactive_at : tunnel.created_at) ?? "",
34 );
35 if (Number.isNaN(since)) return "unknown";
36 const days = (now - since) / DAY_MS;
37 if (days > 90) return ">90d";
38 if (days > 30) return ">30d";
39 if (days > 7) return ">7d";
40 return "<=7d";
41};
42​
43const main = Effect.gen(function* () {
44 const accountId = yield* Config.String("CLOUDFLARE_ACCOUNT_ID");
45 const token = yield* Config.Redacted("CLOUDFLARE_API_TOKEN");
46 const client = yield* HttpClient.HttpClient;
47 const now = yield* Clock.currentTimeMillis;
48​
49 const listPage = (page: number) =>
50 HttpClientRequest.get(`https://api.cloudflare.com/client/v4/accounts/${accountId}/cfd_tunnel`, {
51 urlParams: {
52 is_deleted: "false",
53 include_prefix: PREFIX,
54 per_page: String(PAGE_SIZE),
55 page: String(page),
56 },
57 }).pipe(
58 HttpClientRequest.bearerToken(Redacted.value(token)),
59 client.execute,
60 Effect.flatMap(HttpClientResponse.filterStatusOk),
61 Effect.flatMap(HttpClientResponse.schemaBodyJson(TunnelPage)),
62 Effect.map((body) => body.result),
63 );
64​
65 const counts = new Map<string, number>();
66 let total = 0;
67 for (let page = 1; ; page += 1) {
68 const tunnels = yield* listPage(page);
69 for (const tunnel of tunnels) {
70 total += 1;
71 const idle =
72 tunnel.status === "down" || tunnel.status === "inactive" ? ageBucket(tunnel, now) : "-";
73 const key = `${stageOf(tunnel.name)}\t${tunnel.status ?? "unknown"}\t${idle}`;
74 counts.set(key, (counts.get(key) ?? 0) + 1);
75 }
76 if (tunnels.length < PAGE_SIZE) break;
77 }
78​
79 yield* Console.log("stage\tstatus\tidle\tcount");
80 for (const [key, count] of [...counts].sort(([a], [b]) => a.localeCompare(b))) {
81 yield* Console.log(`${key}\t${count}`);
82 }
83 yield* Console.log(`total\t\t\t${total}`);
84}).pipe(Effect.provide(FetchHttpClient.layer));
85​
86NodeRuntime.runMain(main);