@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 179 lines 7.38 KB
Raw
1 import * as DevOpsGuru from "@alchemy.run/aws/DevOpsGuru";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import * as Stream from "effect/Stream";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 export class DevOpsGuruTestFunction extends Lambda.Function<Lambda.Function>()(
14 "DevOpsGuruTestFunction",
15 ) {}
16
17 export default DevOpsGuruTestFunction.make(
18 {
19 main,
20 functionUrl: true,
21 timeout: Duration.seconds(30),
22 },
23 Effect.gen(function* () {
24 // Event source: subscribe the host to insight lifecycle events. The
25 // deploy proves the EventBridge rule + invoke permission wiring.
26 yield* DevOpsGuru.consumeInsightEvents(
27 { kinds: ["new-insight", "severity-upgraded"] },
28 (events) =>
29 Stream.runForEach(events, (event) =>
30 Effect.log(
31 `devops-guru insight: ${event.detail.insightId} (${event.detail.insightSeverity})`,
32 ),
33 ),
34 );
35
36 // Every account-level capability the service exposes — the init phase
37 // registers the IAM grant for each; a representative subset is
38 // exercised over HTTP routes below.
39 const bound = {
40 describeAccountHealth: yield* DevOpsGuru.DescribeAccountHealth(),
41 describeAccountOverview: yield* DevOpsGuru.DescribeAccountOverview(),
42 describeAnomaly: yield* DevOpsGuru.DescribeAnomaly(),
43 describeFeedback: yield* DevOpsGuru.DescribeFeedback(),
44 describeInsight: yield* DevOpsGuru.DescribeInsight(),
45 describeOrganizationHealth: yield* DevOpsGuru.DescribeOrganizationHealth(),
46 describeOrganizationOverview: yield* DevOpsGuru.DescribeOrganizationOverview(),
47 describeOrganizationResourceCollectionHealth:
48 yield* DevOpsGuru.DescribeOrganizationResourceCollectionHealth(),
49 describeResourceCollectionHealth: yield* DevOpsGuru.DescribeResourceCollectionHealth(),
50 getCostEstimation: yield* DevOpsGuru.GetCostEstimation(),
51 startCostEstimation: yield* DevOpsGuru.StartCostEstimation(),
52 listAnomaliesForInsight: yield* DevOpsGuru.ListAnomaliesForInsight(),
53 listAnomalousLogGroups: yield* DevOpsGuru.ListAnomalousLogGroups(),
54 listEvents: yield* DevOpsGuru.ListEvents(),
55 listInsights: yield* DevOpsGuru.ListInsights(),
56 listMonitoredResources: yield* DevOpsGuru.ListMonitoredResources(),
57 listOrganizationInsights: yield* DevOpsGuru.ListOrganizationInsights(),
58 listRecommendations: yield* DevOpsGuru.ListRecommendations(),
59 putFeedback: yield* DevOpsGuru.PutFeedback(),
60 searchInsights: yield* DevOpsGuru.SearchInsights(),
61 searchOrganizationInsights: yield* DevOpsGuru.SearchOrganizationInsights(),
62 deleteInsight: yield* DevOpsGuru.DeleteInsight(),
63 };
64
65 return {
66 fetch: Effect.gen(function* () {
67 const request = yield* HttpServerRequest;
68 const url = new URL(request.originalUrl);
69 const pathname = url.pathname;
70
71 if (request.method === "GET" && pathname === "/bindings") {
72 return yield* HttpServerResponse.json({
73 bound: Object.keys(bound),
74 });
75 }
76
77 // Account health counters — works even before DevOps Guru is
78 // configured (returns zeroes).
79 if (request.method === "GET" && pathname === "/health") {
80 const health = yield* bound.describeAccountHealth();
81 return yield* HttpServerResponse.json({
82 openReactiveInsights: health.OpenReactiveInsights,
83 openProactiveInsights: health.OpenProactiveInsights,
84 metricsAnalyzed: health.MetricsAnalyzed,
85 });
86 }
87
88 // Account overview for the trailing 24 hours.
89 if (request.method === "GET" && pathname === "/overview") {
90 const overview = yield* bound.describeAccountOverview({
91 FromTime: new Date(Date.now() - 24 * 3600_000),
92 });
93 return yield* HttpServerResponse.json({
94 reactiveInsights: overview.ReactiveInsights,
95 proactiveInsights: overview.ProactiveInsights,
96 });
97 }
98
99 // Ongoing reactive insights.
100 if (request.method === "GET" && pathname === "/insights") {
101 const { ReactiveInsights } = yield* bound.listInsights({
102 StatusFilter: { Ongoing: { Type: "REACTIVE" } },
103 });
104 return yield* HttpServerResponse.json({
105 count: (ReactiveInsights ?? []).length,
106 });
107 }
108
109 // Search reactive insights in the trailing 24 hours. The service
110 // requires ToTime even though the SDK marks it optional.
111 if (request.method === "GET" && pathname === "/search") {
112 const { ReactiveInsights } = yield* bound.searchInsights({
113 Type: "REACTIVE",
114 StartTimeRange: {
115 FromTime: new Date(Date.now() - 24 * 3600_000),
116 ToTime: new Date(),
117 },
118 });
119 return yield* HttpServerResponse.json({
120 count: (ReactiveInsights ?? []).length,
121 });
122 }
123
124 // Per-stack health of the analyzed CloudFormation coverage.
125 if (request.method === "GET" && pathname === "/collection-health") {
126 const page = yield* bound.describeResourceCollectionHealth({
127 ResourceCollectionType: "AWS_CLOUD_FORMATION",
128 });
129 return yield* HttpServerResponse.json({
130 stacks: (page.CloudFormation ?? []).length,
131 });
132 }
133
134 // Monitored resources — a typed not-found means DevOps Guru has no
135 // resource collection configured; report zero coverage.
136 if (request.method === "GET" && pathname === "/monitored") {
137 const count = yield* bound.listMonitoredResources().pipe(
138 Effect.map(({ MonitoredResourceIdentifiers }) => MonitoredResourceIdentifiers.length),
139 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(0)),
140 );
141 return yield* HttpServerResponse.json({ count });
142 }
143
144 return yield* HttpServerResponse.json(
145 { error: "Not found", method: request.method, pathname },
146 { status: 404 },
147 );
148 }).pipe(Effect.orDie),
149 };
150 }).pipe(
151 Effect.provide(
152 Layer.mergeAll(
153 Lambda.EventSource,
154 DevOpsGuru.DescribeAccountHealthHttp,
155 DevOpsGuru.DescribeAccountOverviewHttp,
156 DevOpsGuru.DescribeAnomalyHttp,
157 DevOpsGuru.DescribeFeedbackHttp,
158 DevOpsGuru.DescribeInsightHttp,
159 DevOpsGuru.DescribeOrganizationHealthHttp,
160 DevOpsGuru.DescribeOrganizationOverviewHttp,
161 DevOpsGuru.DescribeOrganizationResourceCollectionHealthHttp,
162 DevOpsGuru.DescribeResourceCollectionHealthHttp,
163 DevOpsGuru.GetCostEstimationHttp,
164 DevOpsGuru.StartCostEstimationHttp,
165 DevOpsGuru.ListAnomaliesForInsightHttp,
166 DevOpsGuru.ListAnomalousLogGroupsHttp,
167 DevOpsGuru.ListEventsHttp,
168 DevOpsGuru.ListInsightsHttp,
169 DevOpsGuru.ListMonitoredResourcesHttp,
170 DevOpsGuru.ListOrganizationInsightsHttp,
171 DevOpsGuru.ListRecommendationsHttp,
172 DevOpsGuru.PutFeedbackHttp,
173 DevOpsGuru.SearchInsightsHttp,
174 DevOpsGuru.SearchOrganizationInsightsHttp,
175 DevOpsGuru.DeleteInsightHttp,
176 ),
177 ),
178 ),
179 );