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.
Filter query: has-agent connecting, connected, disconnected, timeout #5145
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
Filter query: has-agent connecting, connected, disconnected, timeout #5145
Changes from 1 commit
ee6577d
7d61519
b5a1ecc
dca2b8b
0a4746c
70952e1
7965a54
c1bd839
2af4133
f9e2167
45957c1
66892d7
fc93f90
e587b5d
458a8eb
81d9fef
5ca8c17
ec2d571
4b2f831
3c0d4fe
9067a9e
c14663c
9c2f64a
44bf343
598b140
f3787d1
ba13e03
d74b072
4255448
4ace659
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Should this case also verify that the state isn't actually
disconnected
either via timeout ordisonnected_at
?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.
I extended the condition.
Frankly speaking,, I'm thinking about refactoring that part of the code and creating an extra "runtime" column for
agent status
. I'm not quite sure if it's possible to use SQL clauses to reflect this logic. I really wouldn't like to create functions...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.
I guess that you can take a look at the CASE logic implementation one more time as I've rewritten it to look similar to the Go code.
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.
@mtojek I think the case logic is exactly what you want. But you would have to add that case logic to all reads. So either remove all
GetWorkspaceAgentsByXXX
and make 1 with a filter (like workspaces/template/etc). Or you could also make aview
. I looked into this before with sqlc in an issue here:#2201
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.
Hey @Emyrk, thanks for jumping in!
I had a chat about this issue with @mafredri to try
WITH
expression first. I guess that I can transform it into aVIEW
if there are performance/clean-sql concerns. Otherwise, I am happy to do this in a follow-up.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.
The only reason I mentioned a
view
is because sqlc creates our model types from the sql.So you have
GetWorkspaceAgentByAuthToken
,GetWorkspaceAgentByID
,GetWorkspaceAgentByInstanceID
,GetWorkspaceAgentsByResourceIDs
,GetWorkspaceAgentsCreatedAfter
. If you update 1 to select this extra "status" column dynamically, sqlc creates a new model type for this. It will not betype WorkspaceAgent
. I think it has the name of the function, egGetWorkspaceAgentByXXX
(iirc?). So you will need to add this new column to all the calls, and I think each call gets a unique model type. A way around this is to consolidate all these calls too.A "view" is a way to repackage the extra dynamic column in a way sqlc still sees it as a single type. So all the above queries go from
... FROM workspace_agents ...
to... FROM workspace_agents_view ...
. The sqlc type will betype WorkspaceAgentsView
.So tl;dr the view is just a way to keep the sqlc clean. You'll see when you run a
make gen
with a dynamic column (SELECT *, "unknown" as status FROM workspace_agents
).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.
Fortunately, at moment we only need it to search workspaces using filtering. I guess that we don't need to hurry up and inject the case logic everywhere.
In terms of clean code and the benefits of sqlc, I totally agree with you. I admit that I wouldn't like to get stuck in this pull request with too many changes and I'm wondering if I can split it into multiple PRs.
Follow-up ideas:
WITH
expression with aview
. Adjust the rest of the codebase to use it.status
dynamically set in SQL. Remove the agent status logic from Go code - we don't need it in two places, except fordatabasefake
.Frankly speaking, if there aren't big performance or clean code concerns around this PR, I would leave it as is, and focus on improvements once the requested feature is delivered.
Let me know what you think.
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.
@Emyrk @mafredri After further research I noticed a possible blocker for a view. The CASE logic
requires
agent_inactive_disconnect_timeout_seconds
, which is a coderd option, and it's currently provided via query.Unless we can change it to a static value, then I suppose we can't use views here.
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.
That's a good point @mtojek, it definitely hampers the use case. Ideally this value would come from either the terraform provider or be stored in the database by some other means, at which point it can be part of the query.
I don't find it unthinkable that we would serialize certain coder server runtime properties in the database, either as a temp table or one that is (re)written on startup. The inactivity seconds could then be stored there and joined.