Skip to content
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

chore: cover and bunify project functions in postgres_experiments.go #8912

Merged
merged 10 commits into from
Feb 29, 2024

Conversation

kkunapuli
Copy link
Contributor

@kkunapuli kkunapuli commented Feb 28, 2024

Description

RM-48 chore: cover and bunify project functions in postgres_experiments.go

Test Plan

ensure all unit/integration tests pass; there should be no change in behavior

ProjectExperiments is called when deleting a workspace. Create a workspace, add a project, and an experiment. Then, delete it. It completes without error.

Repeat above with deleting a project from a workspace.

Checklist

  • Changes have been manually QA'd
  • User-facing API changes need the "User-facing API Change" label.
  • Release notes should be added as a separate file under docs/release-notes/.
    See Release Note for details.
  • Licenses should be included for new code which was copied and/or modified from any external code.

Ticket

RM-48

@cla-bot cla-bot bot added the cla-signed label Feb 28, 2024
Copy link

netlify bot commented Feb 28, 2024

Deploy Preview for determined-ui canceled.

Name Link
🔨 Latest commit 2d7e83a
🔍 Latest deploy log https://app.netlify.com/sites/determined-ui/deploys/65dfc2c59a779d000835c605

Copy link

codecov bot commented Feb 28, 2024

Codecov Report

Attention: Patch coverage is 77.61194% with 15 lines in your changes are missing coverage. Please review.

Project coverage is 47.54%. Comparing base (4618389) to head (2d7e83a).
Report is 16 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8912   +/-   ##
=======================================
  Coverage   47.53%   47.54%           
=======================================
  Files        1066     1068    +2     
  Lines      170434   170513   +79     
  Branches     2235     2237    +2     
=======================================
+ Hits        81015    81066   +51     
- Misses      89261    89289   +28     
  Partials      158      158           
Flag Coverage Δ
backend 43.39% <77.61%> (+0.04%) ⬆️
harness 63.69% <ø> (ø)
web 42.53% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
master/internal/api_experiment.go 54.84% <ø> (ø)
master/internal/core_experiment.go 61.16% <100.00%> (ø)
master/internal/db/postgres_test_utils.go 83.21% <100.00%> (+0.24%) ⬆️
master/internal/api_project.go 24.59% <0.00%> (ø)
master/internal/api_workspace.go 62.78% <0.00%> (ø)
master/internal/webhooks/postgres_webhook.go 57.10% <0.00%> (+0.26%) ⬆️
master/internal/db/postgres_experiments.go 57.79% <86.95%> (+3.48%) ⬆️
master/internal/project/postgres_project.go 66.66% <66.66%> (ø)

... and 4 files with indirect coverage changes

@kkunapuli kkunapuli changed the title initial commit to save progress chore: cover and bunify project functions in postgres_experiments.go Feb 28, 2024
}
return &pID, nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved function to postgres_project.go

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

return 1, fmt.Errorf("given workspace or project is archived and cannot add new experiments")
}
return int(p.Id), nil
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved function to postgres_projects.go


// user is logged in again during experiment creation, meaning args don't match
mockUserArg := mock.MatchedBy(func(u model.User) bool {
return u.ID == curUser.ID
})

// Can't view forked experiment.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separated tests so they can be run individually

@kkunapuli kkunapuli marked this pull request as ready for review February 28, 2024 22:03
@kkunapuli kkunapuli requested a review from a team as a code owner February 28, 2024 22:03
@@ -527,7 +527,7 @@ func (a *apiServer) deleteWorkspace(
log.Debugf("deleting workspace %d projects", workspaceID)
holder := &workspacev1.Workspace{}
for _, pj := range projects {
expList, err := a.m.db.ProjectExperiments(int(pj.Id))
expList, err := db.ProjectExperiments(context.TODO(), int(pj.Id))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using context.TODO() here because otherwise I was running into context cancelled errors; I think that defer rows.Close() within ProjectExperiments(...) might be cancelling the context

Copy link
Contributor

@carolinaecalderon carolinaecalderon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; see nit/suggestion

}
return &pID, nil
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

Comment on lines +34 to +67
func ProjectExperiments(ctx context.Context, pID int) (experiments []*model.Experiment, err error) {
rows, err := Bun().NewSelect().
Model((*model.Experiment)(nil)).
Column("experiment.id").
Column("state").
Column("config").
Column("start_time").
Column("end_time").
Column("archived").
Column("owner_id").
Column("notes").
Column("job_id").
Column("project_id").
Column("unmanaged").
ColumnExpr("u.username as username").
Join("JOIN users AS u ON (experiment.owner_id = u.id)").
Where("experiment.project_id = ?", pID).
Rows(ctx)
if err != nil {
return nil, err
return nil, fmt.Errorf("selecting project experiments: %w", err)
}
defer rows.Close()

defer rows.Close()
for rows.Next() {
var exp model.Experiment
if err = rows.StructScan(&exp); err != nil {
return nil, errors.Wrap(err, "unable to read experiment from db")
if err := Bun().ScanRow(ctx, rows, &exp); err != nil {
return nil, fmt.Errorf("reading experiment from db: %w", err)
}
experiments = append(experiments, &exp)
}

if err := rows.Err(); err != nil {
return nil, fmt.Errorf("selecting project experiments: %w", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could you follow the pattern of user.GetUserSetting here so you can avoid scanning rows & running defer rows.Close() to cancel the context?
something like:

exps := []*model.Experiment
err := db.Bun().NewSelect().Model(&exp)...Scan(ctx)
return exps, err

if you've already tried this, disregard -- but hopefully this allows you to pass in the ctx (and not context.TODO()) to these calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion! I'm not going to do this for performance reasons; but it's good to know about the tradeoffs / alternatives.

@kkunapuli kkunapuli merged commit dffda27 into main Feb 29, 2024
71 of 83 checks passed
@kkunapuli kkunapuli deleted the kunapuli/bunify-postgres-project-experiment branch February 29, 2024 18:08
maxrussell pushed a commit that referenced this pull request Mar 21, 2024
…8912)

* initial commit to save progress

* missed a file

* remove duplicate func from workspace

* fix formatting

* fix bun query; do not redefine table nickname

* fix git check issues

* add clarity to error handling when workspace/project does not exist

* remove unnecessary print statements

* use new context for ProjectExperiments call

* use fresh context for deleting projects, too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants