-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Minimum release interval policy #272
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ce94c33
init
adityachoudhari26 b6a6bfa
fix query
adityachoudhari26 e7ce8cb
format
adityachoudhari26 f992cba
safer duration str check
adityachoudhari26 67e356d
add timer UI
adityachoudhari26 f5634af
open policy drawer
adityachoudhari26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import type * as SCHEMA from "@ctrlplane/db/schema"; | ||
import type { | ||
EnvironmentCondition, | ||
JobCondition, | ||
|
@@ -6,8 +7,11 @@ import type { | |
} from "@ctrlplane/validators/jobs"; | ||
import type { ReleaseCondition } from "@ctrlplane/validators/releases"; | ||
import type { NodeProps } from "reactflow"; | ||
import { useEffect, useState } from "react"; | ||
import { IconCheck, IconLoader2, IconMinus, IconX } from "@tabler/icons-react"; | ||
import { differenceInMilliseconds } from "date-fns"; | ||
import _ from "lodash"; | ||
import prettyMilliseconds from "pretty-ms"; | ||
import { Handle, Position } from "reactflow"; | ||
import colors from "tailwindcss/colors"; | ||
|
||
|
@@ -21,12 +25,14 @@ import { | |
import { JobFilterType, JobStatus } from "@ctrlplane/validators/jobs"; | ||
import { ReleaseFilterType } from "@ctrlplane/validators/releases"; | ||
|
||
import { EnvironmentPolicyDrawerTab } from "~/app/[workspaceSlug]/(app)/_components/environment-policy-drawer/EnvironmentPolicyDrawer"; | ||
import { useReleaseChannelDrawer } from "~/app/[workspaceSlug]/(app)/_components/release-channel-drawer/useReleaseChannelDrawer"; | ||
import { useQueryParams } from "~/app/[workspaceSlug]/(app)/_components/useQueryParams"; | ||
import { api } from "~/trpc/react"; | ||
|
||
type ReleaseSequencingNodeProps = NodeProps<{ | ||
workspaceId: string; | ||
policyType?: "cancel" | "wait"; | ||
policy?: SCHEMA.EnvironmentPolicy; | ||
releaseId: string; | ||
releaseVersion: string; | ||
deploymentId: string; | ||
|
@@ -52,8 +58,8 @@ const Waiting: React.FC = () => ( | |
); | ||
|
||
const Loading: React.FC = () => ( | ||
<div className="animate-spin rounded-full bg-muted-foreground p-0.5 dark:text-black"> | ||
<IconLoader2 strokeWidth={3} className="h-3 w-3" /> | ||
<div className="rounded-full bg-muted-foreground p-0.5 dark:text-black"> | ||
<IconLoader2 strokeWidth={3} className="h-3 w-3 animate-spin" /> | ||
</div> | ||
); | ||
|
||
|
@@ -243,31 +249,122 @@ const ReleaseChannelCheck: React.FC<ReleaseSequencingNodeProps["data"]> = ({ | |
); | ||
}; | ||
|
||
export const ReleaseSequencingNode: React.FC<ReleaseSequencingNodeProps> = ({ | ||
data, | ||
const MinReleaseIntervalCheck: React.FC<ReleaseSequencingNodeProps["data"]> = ({ | ||
policy, | ||
deploymentId, | ||
environmentId, | ||
}) => { | ||
return ( | ||
<> | ||
<div | ||
className={cn( | ||
"relative w-[250px] space-y-1 rounded-md border px-2 py-1.5 text-sm", | ||
)} | ||
> | ||
<WaitingOnActiveCheck {...data} /> | ||
<ReleaseChannelCheck {...data} /> | ||
const [timeLeft, setTimeLeft] = useState<number | null>(null); | ||
const { setParams } = useQueryParams(); | ||
|
||
const { data: latestRelease, isLoading } = | ||
api.release.latest.completed.useQuery( | ||
{ deploymentId, environmentId }, | ||
{ enabled: policy != null }, | ||
); | ||
|
||
useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. generated by ai |
||
if (!latestRelease || !policy?.minimumReleaseInterval) return; | ||
|
||
const calculateTimeLeft = () => { | ||
const timePassed = differenceInMilliseconds( | ||
new Date(), | ||
latestRelease.createdAt, | ||
); | ||
return Math.max(0, policy.minimumReleaseInterval - timePassed); | ||
}; | ||
|
||
setTimeLeft(calculateTimeLeft()); | ||
|
||
const interval = setInterval(() => { | ||
const remaining = calculateTimeLeft(); | ||
setTimeLeft(remaining); | ||
|
||
if (remaining <= 0) clearInterval(interval); | ||
}, 1000); | ||
|
||
return () => clearInterval(interval); | ||
}, [latestRelease, policy?.minimumReleaseInterval]); | ||
|
||
if (policy == null) return null; | ||
const { minimumReleaseInterval } = policy; | ||
if (minimumReleaseInterval === 0) return null; | ||
if (isLoading) | ||
return ( | ||
<div className="flex items-center gap-2"> | ||
<Loading /> | ||
</div> | ||
); | ||
|
||
if (latestRelease == null || timeLeft === 0) | ||
return ( | ||
<div className="flex items-center gap-2"> | ||
<Passing /> | ||
<span className="flex items-center gap-1"> | ||
Minimum | ||
<Button | ||
variant="link" | ||
onClick={() => | ||
setParams({ | ||
environment_policy_id: policy.id, | ||
tab: EnvironmentPolicyDrawerTab.Rollout, | ||
}) | ||
} | ||
className="h-fit px-0 py-0 text-inherit underline-offset-2" | ||
> | ||
release interval | ||
</Button> | ||
passed | ||
</span> | ||
</div> | ||
<Handle | ||
type="target" | ||
className="h-2 w-2 rounded-full border border-neutral-500" | ||
style={{ background: colors.neutral[800] }} | ||
position={Position.Left} | ||
/> | ||
<Handle | ||
type="source" | ||
className="h-2 w-2 rounded-full border border-neutral-500" | ||
style={{ background: colors.neutral[800] }} | ||
position={Position.Right} | ||
/> | ||
</> | ||
); | ||
|
||
return ( | ||
<div className="flex items-center gap-2"> | ||
<Waiting /> | ||
<span className="flex items-center gap-1"> | ||
<Button | ||
variant="link" | ||
onClick={() => | ||
setParams({ | ||
environment_policy_id: policy.id, | ||
tab: EnvironmentPolicyDrawerTab.Rollout, | ||
}) | ||
} | ||
className="h-fit px-0 py-0 text-inherit underline-offset-2" | ||
> | ||
Waiting {prettyMilliseconds(timeLeft ?? 0, { compact: true })} | ||
</Button> | ||
till next release | ||
</span> | ||
</div> | ||
); | ||
}; | ||
|
||
export const ReleaseSequencingNode: React.FC<ReleaseSequencingNodeProps> = ({ | ||
data, | ||
}) => ( | ||
<> | ||
<div | ||
className={cn( | ||
"relative w-[250px] space-y-1 rounded-md border px-2 py-1.5 text-sm", | ||
)} | ||
> | ||
<WaitingOnActiveCheck {...data} /> | ||
<ReleaseChannelCheck {...data} /> | ||
<MinReleaseIntervalCheck {...data} /> | ||
</div> | ||
<Handle | ||
type="target" | ||
className="h-2 w-2 rounded-full border border-neutral-500" | ||
style={{ background: colors.neutral[800] }} | ||
position={Position.Left} | ||
/> | ||
<Handle | ||
type="source" | ||
className="h-2 w-2 rounded-full border border-neutral-500" | ||
style={{ background: colors.neutral[800] }} | ||
position={Position.Right} | ||
/> | ||
</> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "environment_policy" ADD COLUMN "minimum_release_interval" bigint DEFAULT 0 NOT NULL; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add validation for non-negative duration
While the form submission logic is correct, consider adding validation to ensure that
minimumReleaseInterval
is non-negative. This would prevent potential issues with negative durations.📝 Committable suggestion