-
Notifications
You must be signed in to change notification settings - Fork 894
chore: reimplement activity status and autostop improvements #12175
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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
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. Should we test this? 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. I'm gonna talk with Michael about this! |
This file contains hidden or 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,43 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useEffectEvent } from "./hookPolyfills"; | ||
|
||
interface UseTimeOptions { | ||
/** | ||
* Can be set to `true` to disable checking for updates in circumstances where it is known | ||
* that there is no work to do. | ||
*/ | ||
disabled?: boolean; | ||
|
||
/** | ||
* The amount of time in milliseconds that should pass between checking for updates. | ||
*/ | ||
interval?: number; | ||
} | ||
|
||
/** | ||
* useTime allows a component to rerender over time without a corresponding state change. | ||
* An example could be a relative timestamp (eg. "in 5 minutes") that should count down as it | ||
* approaches. | ||
*/ | ||
export function useTime<T>(func: () => T, options: UseTimeOptions = {}): T { | ||
const [computedValue, setComputedValue] = useState(() => func()); | ||
const { disabled = false, interval = 1000 } = options; | ||
|
||
const thunk = useEffectEvent(func); | ||
|
||
useEffect(() => { | ||
if (disabled) { | ||
return; | ||
} | ||
|
||
const handle = setInterval(() => { | ||
setComputedValue(() => thunk()); | ||
}, interval); | ||
|
||
return () => { | ||
clearInterval(handle); | ||
}; | ||
}, [thunk, disabled, interval]); | ||
|
||
return computedValue; | ||
} |
This file contains hidden or 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 hidden or 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,45 @@ | ||
import dayjs from "dayjs"; | ||
import type { Workspace } from "api/typesGenerated"; | ||
|
||
export type WorkspaceActivityStatus = | ||
| "ready" | ||
| "connected" | ||
| "inactive" | ||
| "notConnected" | ||
| "notRunning"; | ||
|
||
export function getWorkspaceActivityStatus( | ||
workspace: Workspace, | ||
): WorkspaceActivityStatus { | ||
const builtAt = dayjs(workspace.latest_build.created_at); | ||
const usedAt = dayjs(workspace.last_used_at); | ||
const now = dayjs(); | ||
|
||
if (workspace.latest_build.status !== "running") { | ||
return "notRunning"; | ||
} | ||
|
||
// This needs to compare to `usedAt` instead of `now`, because the "grace period" for | ||
// marking a workspace as "Connected" is a lot longer. If you compared `builtAt` to `now`, | ||
// you could end up switching from "Ready" to "Connected" without ever actually connecting. | ||
const isBuiltRecently = builtAt.isAfter(usedAt.subtract(1, "second")); | ||
// By default, agents report connection stats every 30 seconds, so 2 minutes should be | ||
// plenty. Disconnection will be reflected relatively-quickly | ||
const isUsedRecently = usedAt.isAfter(now.subtract(2, "minute")); | ||
|
||
// If the build is still "fresh", it'll be a while before the `last_used_at` gets bumped in | ||
// a significant way by the agent, so just label it as ready instead of connected. | ||
// Wait until `last_used_at` is after the time that the build finished, _and_ still | ||
// make sure to check that it's recent, so that we don't show "Ready" indefinitely. | ||
if (isUsedRecently && isBuiltRecently && workspace.health.healthy) { | ||
return "ready"; | ||
} | ||
|
||
if (isUsedRecently) { | ||
return "connected"; | ||
} | ||
|
||
// TODO: It'd be nice if we could differentiate between "connected but inactive" and | ||
// "not connected", but that will require some relatively substantial backend work. | ||
return "inactive"; | ||
} |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.