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, 06:00 UTC
| 1 | import * as cloudwatch from "@distilled.cloud/aws/cloudwatch"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import { createInternalTags, createTagsList, diffTags } from "alchemy/Tags"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | |
| 7 | export type CloudWatchTags = Record<string, string>; |
| 8 | |
| 9 | export const createName = (id: string, providedName: string | undefined, maxLength: number) => |
| 10 | providedName |
| 11 | ? Effect.succeed(providedName) |
| 12 | : createPhysicalName({ |
| 13 | id, |
| 14 | maxLength, |
| 15 | }); |
| 16 | |
| 17 | export const toTagRecord = (tags: cloudwatch.Tag[] | undefined): CloudWatchTags => |
| 18 | Object.fromEntries( |
| 19 | (tags ?? []) |
| 20 | .filter( |
| 21 | (tag): tag is { Key: string; Value: string } => |
| 22 | typeof tag.Key === "string" && typeof tag.Value === "string", |
| 23 | ) |
| 24 | .map((tag) => [tag.Key, tag.Value]), |
| 25 | ); |
| 26 | |
| 27 | export const createManagedTags = Effect.fn(function* ( |
| 28 | id: string, |
| 29 | tags: Record<string, string> | undefined, |
| 30 | ) { |
| 31 | return { |
| 32 | ...(yield* createInternalTags(id)), |
| 33 | ...tags, |
| 34 | }; |
| 35 | }); |
| 36 | |
| 37 | export const updateResourceTags = Effect.fn(function* ({ |
| 38 | id, |
| 39 | resourceArn, |
| 40 | olds, |
| 41 | news, |
| 42 | }: { |
| 43 | id: string; |
| 44 | resourceArn: string; |
| 45 | olds: Record<string, string> | undefined; |
| 46 | news: Record<string, string> | undefined; |
| 47 | }) { |
| 48 | const oldTags = olds ? yield* createManagedTags(id, olds) : {}; |
| 49 | const newTags = yield* createManagedTags(id, news); |
| 50 | const { removed, upsert } = diffTags(oldTags, newTags); |
| 51 | |
| 52 | if (removed.length > 0) { |
| 53 | yield* cloudwatch.untagResource({ |
| 54 | ResourceARN: resourceArn, |
| 55 | TagKeys: removed, |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | if (upsert.length > 0) { |
| 60 | yield* cloudwatch.tagResource({ |
| 61 | ResourceARN: resourceArn, |
| 62 | Tags: upsert, |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | return newTags; |
| 67 | }); |
| 68 | |
| 69 | export const readResourceTags = (resourceArn: string) => |
| 70 | cloudwatch |
| 71 | .listTagsForResource({ |
| 72 | ResourceARN: resourceArn, |
| 73 | }) |
| 74 | .pipe(Effect.map((response) => toTagRecord(response.Tags))); |
| 75 | |
| 76 | export const createTagList = (tags: Record<string, string>) => createTagsList(tags); |
| 77 | |
| 78 | export const retryConcurrent = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 79 | effect.pipe( |
| 80 | Effect.retry({ |
| 81 | while: (error: any) => |
| 82 | error?._tag === "ConcurrentModificationException" || |
| 83 | error?._tag === "ConflictException" || |
| 84 | error?._tag === "LimitExceededException", |
| 85 | schedule: Schedule.max([Schedule.exponential(200), Schedule.recurs(8)]), |
| 86 | }), |
| 87 | ); |
| 88 | |
| 89 | const normalizeDimensions = (dimensions: cloudwatch.Dimension[] | undefined) => |
| 90 | [...(dimensions ?? [])].sort((a, b) => |
| 91 | `${a.Name ?? ""}:${a.Value ?? ""}`.localeCompare(`${b.Name ?? ""}:${b.Value ?? ""}`), |
| 92 | ); |
| 93 | |
| 94 | const normalizeSingleMetricDetector = ( |
| 95 | input: Pick< |
| 96 | cloudwatch.PutAnomalyDetectorInput, |
| 97 | "Namespace" | "MetricName" | "Dimensions" | "Stat" | "SingleMetricAnomalyDetector" |
| 98 | >, |
| 99 | ) => { |
| 100 | const singleMetric = input.SingleMetricAnomalyDetector; |
| 101 | return { |
| 102 | Namespace: singleMetric?.Namespace ?? input.Namespace, |
| 103 | MetricName: singleMetric?.MetricName ?? input.MetricName, |
| 104 | Dimensions: normalizeDimensions(singleMetric?.Dimensions ?? input.Dimensions), |
| 105 | Stat: singleMetric?.Stat ?? input.Stat, |
| 106 | }; |
| 107 | }; |
| 108 | |
| 109 | export const detectorIdentity = ( |
| 110 | input: Pick< |
| 111 | cloudwatch.PutAnomalyDetectorInput, |
| 112 | | "Namespace" |
| 113 | | "MetricName" |
| 114 | | "Dimensions" |
| 115 | | "Stat" |
| 116 | | "SingleMetricAnomalyDetector" |
| 117 | | "MetricMathAnomalyDetector" |
| 118 | >, |
| 119 | ) => |
| 120 | JSON.stringify({ |
| 121 | SingleMetric: normalizeSingleMetricDetector(input), |
| 122 | MetricMathAnomalyDetector: input.MetricMathAnomalyDetector, |
| 123 | }); |
| 124 | |
| 125 | export const matchesDetectorIdentity = ( |
| 126 | detector: cloudwatch.AnomalyDetector, |
| 127 | input: cloudwatch.PutAnomalyDetectorInput, |
| 128 | ) => detectorIdentity(detector) === detectorIdentity(input); |
| 129 | |
| 130 | export const sortByLogicalId = <T extends { LogicalId: string }>(items: T[]) => |
| 131 | [...items].sort((a, b) => a.LogicalId.localeCompare(b.LogicalId)); |