Skip to content

Commit

Permalink
fix(api): add withIcons on routes to not fetch icon if not needed (#3192
Browse files Browse the repository at this point in the history
)

Signed-off-by: Benjamin Coenen <[email protected]>
  • Loading branch information
bnjjj authored and yesnault committed Aug 13, 2018
1 parent 906aac7 commit 5f1d5ed
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 34 deletions.
12 changes: 10 additions & 2 deletions engine/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (api *API) getApplicationsHandler() service.Handler {
projectKey := vars["permProjectKey"]
withPermissions := r.FormValue("permission")
withUsage := FormBool(r, "withUsage")
withIcon := FormBool(r, "withIcon")

var u = getUser(ctx)
requestedUserName := r.Header.Get("X-Cds-Username")
Expand All @@ -56,8 +57,11 @@ func (api *API) getApplicationsHandler() service.Handler {
return sdk.WrapError(err, "getApplicationsHandler> unable to load user '%s' permissions", requestedUserName)
}
}

applications, err := application.LoadAll(api.mustDB(), api.Cache, projectKey, u)
loadOpts := []application.LoadOptionFunc{}
if withIcon {
loadOpts = append(loadOpts, application.LoadOptions.WithIcon)
}
applications, err := application.LoadAll(api.mustDB(), api.Cache, projectKey, u, loadOpts...)
if err != nil {
return sdk.WrapError(err, "getApplicationsHandler> Cannot load applications from db")
}
Expand Down Expand Up @@ -244,6 +248,7 @@ func (api *API) getApplicationHandler() service.Handler {
withSchedulers := FormBool(r, "withSchedulers")
withKeys := FormBool(r, "withKeys")
withUsage := FormBool(r, "withUsage")
withIcon := FormBool(r, "withIcon")
withDeploymentStrategies := FormBool(r, "withDeploymentStrategies")
withVulnerabilities := FormBool(r, "withVulnerabilities")
branchName := r.FormValue("branchName")
Expand Down Expand Up @@ -272,6 +277,9 @@ func (api *API) getApplicationHandler() service.Handler {
if withVulnerabilities {
loadOptions = append(loadOptions, application.LoadOptions.WithVulnerabilities)
}
if withIcon {
loadOptions = append(loadOptions, application.LoadOptions.WithIcon)
}

app, errApp := application.LoadByName(api.mustDB(), api.Cache, projectKey, applicationName, getUser(ctx), loadOptions...)
if errApp != nil {
Expand Down
85 changes: 54 additions & 31 deletions engine/api/application/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import (
"github.com/ovh/cds/sdk"
)

const appRows = `
application.id,
application.name,
application.project_id,
application.repo_fullname,
application.repositories_manager_id,
application.last_modified,
application.metadata,
application.workflow_migration,
application.vcs_server,
application.vcs_strategy,
application.description
`

// LoadOptionFunc is a type for all options in LoadOptions
type LoadOptionFunc *func(gorp.SqlExecutor, cache.Store, *sdk.Application, *sdk.User) error

Expand All @@ -33,6 +47,7 @@ var LoadOptions = struct {
WithDeploymentStrategies LoadOptionFunc
WithClearDeploymentStrategies LoadOptionFunc
WithVulnerabilities LoadOptionFunc
WithIcon LoadOptionFunc
}{
Default: &loadDefaultDependencies,
WithVariables: &loadVariables,
Expand All @@ -47,12 +62,13 @@ var LoadOptions = struct {
WithDeploymentStrategies: &loadDeploymentStrategies,
WithClearDeploymentStrategies: &loadDeploymentStrategiesWithClearPassword,
WithVulnerabilities: &loadVulnerabilities,
WithIcon: &loadIcon,
}

// LoadOldApplicationWorkflowToClean load application to clean
func LoadOldApplicationWorkflowToClean(db gorp.SqlExecutor) ([]sdk.Application, error) {
apps := []sdk.Application{}
query := `SELECT application.* FROM application where workflow_migration = 'CLEANING'`
query := fmt.Sprintf(`SELECT %s FROM application where workflow_migration = 'CLEANING'`, appRows)
if _, err := db.Select(&apps, query); err != nil {
if err == sql.ErrNoRows {
return apps, nil
Expand All @@ -77,16 +93,16 @@ func LoadByName(db gorp.SqlExecutor, store cache.Store, projectKey, appName stri
var args []interface{}

if u == nil || u.Admin {
query = `
SELECT application.*
query = fmt.Sprintf(`
SELECT %s
FROM application
JOIN project ON project.id = application.project_id
WHERE project.projectkey = $1
AND application.name = $2`
AND application.name = $2`, appRows)
args = []interface{}{projectKey, appName}
} else {
query = `
SELECT distinct application.*
query = fmt.Sprintf(`
SELECT distinct %s
FROM application
JOIN project ON project.id = application.project_id
JOIN application_group on application.id = application_group.application_id
Expand All @@ -96,7 +112,7 @@ func LoadByName(db gorp.SqlExecutor, store cache.Store, projectKey, appName stri
application_group.group_id = ANY(string_to_array($3, ',')::int[])
OR
$4 = ANY(string_to_array($3, ',')::int[])
)`
)`, appRows)
var groupID string
for i, g := range u.Groups {
if i == 0 {
Expand All @@ -117,22 +133,22 @@ func LoadAndLockByID(db gorp.SqlExecutor, store cache.Store, id int64, u *sdk.Us
var args []interface{}

if u == nil || u.Admin {
query = `
SELECT application.*
query = fmt.Sprintf(`
SELECT %s
FROM application
WHERE application.id = $1 FOR UPDATE NOWAIT`
WHERE application.id = $1 FOR UPDATE NOWAIT`, appRows)
args = []interface{}{id}
} else {
query = `
SELECT distinct application.*
query = fmt.Sprintf(`
SELECT distinct %s
FROM application
JOIN application_group on application.id = application_group.application_id
WHERE application.id = $1
AND (
application_group.group_id = ANY(string_to_array($2, ',')::int[])
OR
$3 = ANY(string_to_array($2, ',')::int[])
) FOR UPDATE NOWAIT`
) FOR UPDATE NOWAIT`, appRows)
var groupID string

for i, g := range u.Groups {
Expand All @@ -154,22 +170,22 @@ func LoadByID(db gorp.SqlExecutor, store cache.Store, id int64, u *sdk.User, opt
var args []interface{}

if u == nil || u.Admin {
query = `
SELECT application.*
query = fmt.Sprintf(`
SELECT %s
FROM application
WHERE application.id = $1`
WHERE application.id = $1`, appRows)
args = []interface{}{id}
} else {
query = `
SELECT distinct application.*
query = fmt.Sprintf(`
SELECT distinct %s
FROM application
JOIN application_group on application.id = application_group.application_id
WHERE application.id = $1
AND (
application_group.group_id = ANY(string_to_array($2, ',')::int[])
OR
$3 = ANY(string_to_array($2, ',')::int[])
)`
)`, appRows)
var groupID string

for i, g := range u.Groups {
Expand All @@ -188,11 +204,11 @@ func LoadByID(db gorp.SqlExecutor, store cache.Store, id int64, u *sdk.User, opt
// LoadByWorkflowID loads applications from database for a given workflow id
func LoadByWorkflowID(db gorp.SqlExecutor, workflowID int64) ([]sdk.Application, error) {
apps := []sdk.Application{}
query := `SELECT DISTINCT application.* FROM application
query := fmt.Sprintf(`SELECT DISTINCT %s FROM application
JOIN workflow_node_context ON workflow_node_context.application_id = application.id
JOIN workflow_node ON workflow_node.id = workflow_node_context.workflow_node_id
JOIN workflow ON workflow.id = workflow_node.workflow_id
WHERE workflow.id = $1`
WHERE workflow.id = $1`, appRows)

if _, err := db.Select(&apps, query, workflowID); err != nil {
if err == sql.ErrNoRows {
Expand All @@ -207,11 +223,11 @@ func LoadByWorkflowID(db gorp.SqlExecutor, workflowID int64) ([]sdk.Application,
// LoadByEnvName loads applications from database for a given project key and environment name
func LoadByEnvName(db gorp.SqlExecutor, projKey, envName string) ([]sdk.Application, error) {
apps := []sdk.Application{}
query := `SELECT DISTINCT application.* FROM application
query := fmt.Sprintf(`SELECT DISTINCT %s FROM application
JOIN pipeline_trigger ON application.id = pipeline_trigger.src_application_id OR application.id = pipeline_trigger.dest_application_id
JOIN environment ON environment.id = pipeline_trigger.src_environment_id OR environment.id = pipeline_trigger.dest_environment_id
JOIN project ON application.project_id = project.id
WHERE project.projectkey = $1 AND environment.name = $2`
WHERE project.projectkey = $1 AND environment.name = $2`, appRows)

if _, err := db.Select(&apps, query, projKey, envName); err != nil {
if err == sql.ErrNoRows {
Expand All @@ -225,11 +241,11 @@ func LoadByEnvName(db gorp.SqlExecutor, projKey, envName string) ([]sdk.Applicat

// LoadByPipeline Load application where pipeline is attached
func LoadByPipeline(db gorp.SqlExecutor, store cache.Store, pipelineID int64, u *sdk.User, opts ...LoadOptionFunc) ([]sdk.Application, error) {
query := `SELECT distinct application.*
query := fmt.Sprintf(`SELECT distinct %s
FROM application
JOIN application_pipeline ON application.id = application_pipeline.application_id
WHERE application_pipeline.pipeline_id = $1
ORDER BY application.name`
ORDER BY application.name`, appRows)
app, err := loadapplications(db, store, u, opts, query, pipelineID)
if err != nil {
return nil, sdk.WrapError(err, "LoadByPipeline (%d)", pipelineID)
Expand Down Expand Up @@ -326,16 +342,16 @@ func LoadAll(db gorp.SqlExecutor, store cache.Store, key string, u *sdk.User, op
var args []interface{}

if u == nil || u.Admin {
query = `
SELECT application.*
query = fmt.Sprintf(`
SELECT %s
FROM application
JOIN project ON project.id = application.project_id
WHERE project.projectkey = $1
ORDER BY application.name ASC`
ORDER BY application.name ASC`, appRows)
args = []interface{}{key}
} else {
query = `
SELECT distinct application.*
query = fmt.Sprintf(`
SELECT distinct %s
FROM application
JOIN project ON project.id = application.project_id
WHERE application.id IN (
Expand All @@ -345,7 +361,7 @@ func LoadAll(db gorp.SqlExecutor, store cache.Store, key string, u *sdk.User, op
WHERE group_user.user_id = $2
)
AND project.projectkey = $1
ORDER by application.name ASC`
ORDER by application.name ASC`, appRows)
args = []interface{}{key, u.ID}
}
return loadapplications(db, store, u, opts, query, args...)
Expand Down Expand Up @@ -413,3 +429,10 @@ func loadapplications(db gorp.SqlExecutor, store cache.Store, u *sdk.User, opts

return apps, nil
}

// LoadIcon return application icon given his application id
func LoadIcon(db gorp.SqlExecutor, appID int64) (string, error) {
icon, err := db.SelectStr("SELECT icon FROM application WHERE id = $1", appID)

return icon, sdk.WrapError(err, "application.loadIcon>")
}
9 changes: 9 additions & 0 deletions engine/api/application/dao_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ var (
return nil
}

loadIcon = func(db gorp.SqlExecutor, store cache.Store, app *sdk.Application, u *sdk.User) error {
var err error
app.Icon, err = LoadIcon(db, app.ID)
if err != nil && err != sql.ErrNoRows {
return sdk.WrapError(err, "application.loadIcon> Unable to load icon")
}
return nil
}

loadVulnerabilities = func(db gorp.SqlExecutor, store cache.Store, app *sdk.Application, u *sdk.User) error {
var err error
app.Vulnerabilities, err = LoadVulnerabilities(db, app.ID)
Expand Down
5 changes: 4 additions & 1 deletion engine/api/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ func (api *API) getProjectHandler() service.Handler {
withWorkflowNames := FormBool(r, "withWorkflowNames")
withPlatforms := FormBool(r, "withPlatforms")
withFeatures := FormBool(r, "withFeatures")
withIcon := FormBool(r, "withIcon")

opts := []project.LoadOptionFunc{
project.LoadOptions.WithFavorites,
project.LoadOptions.WithIcon,
}
if withVariables {
opts = append(opts, project.LoadOptions.WithVariables)
Expand Down Expand Up @@ -239,6 +239,9 @@ func (api *API) getProjectHandler() service.Handler {
if withFeatures {
opts = append(opts, project.LoadOptions.WithFeatures)
}
if withIcon {
opts = append(opts, project.LoadOptions.WithIcon)
}

p, errProj := project.Load(api.mustDB(), api.Cache, key, getUser(ctx), opts...)
if errProj != nil {
Expand Down
1 change: 1 addition & 0 deletions ui/src/app/service/application/application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class ApplicationService {
params = params.append('withWorkflow', 'true');
params = params.append('withNotifs', 'true');
params = params.append('withUsage', 'true');
params = params.append('withIcon', 'true');
params = params.append('withKeys', 'true');
params = params.append('withDeploymentStrategies', 'true');
params = params.append('withVulnerabilities', 'true');
Expand Down

0 comments on commit 5f1d5ed

Please sign in to comment.