|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import type { TooltipProps } from "recharts"; |
| 4 | +import type { |
| 5 | + NameType, |
| 6 | + ValueType, |
| 7 | +} from "recharts/types/component/DefaultTooltipContent"; |
| 8 | +import { useParams } from "next/navigation"; |
| 9 | +import { formatDistanceToNowStrict, isAfter } from "date-fns"; |
| 10 | +import prettyMilliseconds from "pretty-ms"; |
| 11 | +import { |
| 12 | + Line, |
| 13 | + LineChart, |
| 14 | + ReferenceLine, |
| 15 | + ResponsiveContainer, |
| 16 | + Tooltip, |
| 17 | + XAxis, |
| 18 | + YAxis, |
| 19 | +} from "recharts"; |
| 20 | + |
| 21 | +import { |
| 22 | + Card, |
| 23 | + CardContent, |
| 24 | + CardDescription, |
| 25 | + CardHeader, |
| 26 | + CardTitle, |
| 27 | +} from "@ctrlplane/ui/card"; |
| 28 | + |
| 29 | +import type { RolloutInfo } from "../_utils/rollout"; |
| 30 | +import { RolloutTypeToOffsetFunction } from "~/app/[workspaceSlug]/(app)/policies/[policyId]/edit/rollouts/_components/equations"; |
| 31 | +import { api } from "~/trpc/react"; |
| 32 | +import { getCurrentRolloutPosition } from "../_utils/rollout"; |
| 33 | + |
| 34 | +const PrettyYAxisTick = (props: any) => { |
| 35 | + const { payload } = props; |
| 36 | + const { value } = payload; |
| 37 | + |
| 38 | + const minutes = Number.parseFloat(value); |
| 39 | + const ms = Math.round(minutes * 60_000); |
| 40 | + |
| 41 | + const prettyString = prettyMilliseconds(ms, { |
| 42 | + unitCount: 2, |
| 43 | + compact: true, |
| 44 | + verbose: false, |
| 45 | + }); |
| 46 | + |
| 47 | + return ( |
| 48 | + <g> |
| 49 | + <text {...props} fontSize={14} dy={5}> |
| 50 | + {prettyString} |
| 51 | + </text> |
| 52 | + </g> |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +const getRolloutTimeMessage = (rolloutTime: Date | null) => { |
| 57 | + if (rolloutTime == null) return "Rollout not started"; |
| 58 | + |
| 59 | + const now = new Date(); |
| 60 | + const isInFuture = isAfter(rolloutTime, now); |
| 61 | + |
| 62 | + const distanceToNow = formatDistanceToNowStrict(rolloutTime, { |
| 63 | + addSuffix: true, |
| 64 | + }); |
| 65 | + if (isInFuture) return `Version rolls out in ${distanceToNow}`; |
| 66 | + |
| 67 | + return `Version rolled out ${distanceToNow}`; |
| 68 | +}; |
| 69 | + |
| 70 | +const PrettyTooltip = ( |
| 71 | + props: TooltipProps<ValueType, NameType> & { |
| 72 | + rolloutInfoList: RolloutInfo["releaseTargetRolloutInfo"]; |
| 73 | + }, |
| 74 | +) => { |
| 75 | + const { label: position } = props; |
| 76 | + |
| 77 | + const releaseTarget = props.rolloutInfoList.at(Number(position)); |
| 78 | + |
| 79 | + if (releaseTarget == null) return null; |
| 80 | + |
| 81 | + const resourceName = releaseTarget.resource.name; |
| 82 | + const rolloutTimeMessage = getRolloutTimeMessage(releaseTarget.rolloutTime); |
| 83 | + |
| 84 | + return ( |
| 85 | + <div className="rounded-md border bg-neutral-900 p-2 text-sm"> |
| 86 | + <p>Resource: {resourceName}</p> |
| 87 | + <p>Rollout position: {position}</p> |
| 88 | + <p>{rolloutTimeMessage}</p> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +const RolloutCurve: React.FC<{ |
| 94 | + chartData: { x: number; y: number }[]; |
| 95 | + currentRolloutPosition: number; |
| 96 | + rolloutInfoList: RolloutInfo["releaseTargetRolloutInfo"]; |
| 97 | +}> = ({ chartData, currentRolloutPosition, rolloutInfoList }) => { |
| 98 | + return ( |
| 99 | + <div className="h-[300px] w-full"> |
| 100 | + <ResponsiveContainer width="100%" height={300}> |
| 101 | + <LineChart |
| 102 | + data={chartData} |
| 103 | + margin={{ top: 20, right: 20, bottom: 20, left: 20 }} |
| 104 | + > |
| 105 | + <XAxis |
| 106 | + dataKey="x" |
| 107 | + label={{ |
| 108 | + value: "Rollout position (0-indexed)", |
| 109 | + dy: 20, |
| 110 | + }} |
| 111 | + tick={{ fontSize: 14 }} |
| 112 | + /> |
| 113 | + <YAxis |
| 114 | + label={{ |
| 115 | + value: "Time (minutes)", |
| 116 | + angle: -90, |
| 117 | + dx: -20, |
| 118 | + }} |
| 119 | + tick={PrettyYAxisTick} |
| 120 | + /> |
| 121 | + <Line type="monotone" dataKey="y" stroke="#8884d8" dot={false} /> |
| 122 | + <ReferenceLine |
| 123 | + x={currentRolloutPosition} |
| 124 | + stroke="#9ca3af" |
| 125 | + strokeDasharray="5 5" |
| 126 | + strokeWidth={2} |
| 127 | + label={{ |
| 128 | + value: "Current rollout position", |
| 129 | + position: "top", |
| 130 | + fill: "#9ca3af", |
| 131 | + fontSize: 12, |
| 132 | + }} |
| 133 | + /> |
| 134 | + <Tooltip |
| 135 | + content={(props) => PrettyTooltip({ ...props, rolloutInfoList })} |
| 136 | + /> |
| 137 | + </LineChart> |
| 138 | + </ResponsiveContainer> |
| 139 | + </div> |
| 140 | + ); |
| 141 | +}; |
| 142 | + |
| 143 | +export const RolloutCurveChart: React.FC = () => { |
| 144 | + const { releaseId: versionId, environmentId } = useParams<{ |
| 145 | + releaseId: string; |
| 146 | + environmentId: string; |
| 147 | + }>(); |
| 148 | + |
| 149 | + const { data: rolloutInfo } = api.policy.rollout.list.useQuery( |
| 150 | + { environmentId, versionId }, |
| 151 | + { refetchInterval: 10_000 }, |
| 152 | + ); |
| 153 | + |
| 154 | + const rolloutPolicy = rolloutInfo?.rolloutPolicy; |
| 155 | + const numReleaseTargets = rolloutInfo?.releaseTargetRolloutInfo.length ?? 0; |
| 156 | + |
| 157 | + const rolloutType = rolloutPolicy?.rolloutType ?? "linear"; |
| 158 | + const timeScaleInterval = rolloutPolicy?.timeScaleInterval ?? 0; |
| 159 | + const positionGrowthFactor = rolloutPolicy?.positionGrowthFactor ?? 1; |
| 160 | + |
| 161 | + const offsetFunction = RolloutTypeToOffsetFunction[rolloutType]( |
| 162 | + positionGrowthFactor, |
| 163 | + timeScaleInterval, |
| 164 | + numReleaseTargets, |
| 165 | + ); |
| 166 | + |
| 167 | + const chartData = Array.from({ length: numReleaseTargets }, (_, i) => ({ |
| 168 | + x: i, |
| 169 | + y: offsetFunction(i), |
| 170 | + })); |
| 171 | + |
| 172 | + const currentRolloutPosition = |
| 173 | + getCurrentRolloutPosition(rolloutInfo?.releaseTargetRolloutInfo ?? []) ?? 0; |
| 174 | + |
| 175 | + console.log(currentRolloutPosition); |
| 176 | + |
| 177 | + return ( |
| 178 | + <Card className="p-2"> |
| 179 | + <CardHeader> |
| 180 | + <CardTitle>Rollout curve</CardTitle> |
| 181 | + <CardDescription> |
| 182 | + View the rollout curve for the deployment. |
| 183 | + </CardDescription> |
| 184 | + </CardHeader> |
| 185 | + <CardContent className="flex flex-col gap-4 p-4"> |
| 186 | + <RolloutCurve |
| 187 | + chartData={chartData} |
| 188 | + currentRolloutPosition={Number(currentRolloutPosition)} |
| 189 | + rolloutInfoList={rolloutInfo?.releaseTargetRolloutInfo ?? []} |
| 190 | + /> |
| 191 | + </CardContent> |
| 192 | + </Card> |
| 193 | + ); |
| 194 | +}; |
0 commit comments