Skip to content

Commit

Permalink
enhance: resource fetch optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Nov 1, 2023
1 parent c0ac3df commit f589dce
Showing 1 changed file with 68 additions and 62 deletions.
130 changes: 68 additions & 62 deletions api/build/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,68 +156,6 @@ func GetBuildGraph(c *gin.Context) {

logger.Infof("constructing graph for build %s", entry)

// retrieve the steps for the build from the step table
steps := []*library.Step{}
page := 1
perPage := 100

for page > 0 {
// retrieve build steps (per page) from the database
stepsPart, stepsCount, err := database.FromContext(c).ListStepsForBuild(b, nil, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to retrieve steps for build %s: %w", entry, err)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

// add page of steps to list steps
steps = append(steps, stepsPart...)

// assume no more pages exist if under 100 results are returned
if int(stepsCount) < perPage {
page = 0
} else {
page++
}
}

if len(steps) == 0 {
retErr := fmt.Errorf("no steps found for build %s", entry)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

// retrieve the services for the build from the service table
services := []*library.Service{}
page = 1
perPage = 100

for page > 0 {
// retrieve build services (per page) from the database
servicesPart, servicesCount, err := database.FromContext(c).ListServicesForBuild(ctx, b, nil, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to retrieve services for build %s: %w", entry, err)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

// add page of services to list services
services = append(services, servicesPart...)

// assume no more pages exist if under 100 results are returned
if int(servicesCount) < perPage {
page = 0
} else {
page++
}
}

logger.Info("retrieving pipeline configuration")

var config []byte
Expand Down Expand Up @@ -295,6 +233,74 @@ func GetBuildGraph(c *gin.Context) {
return
}

// retrieve the steps for the build from the step table
steps := []*library.Step{}
page := 1
perPage := 100

// only fetch steps when necessary
if len(p.Stages) > 0 || len(p.Steps) > 0 {
for page > 0 {
// retrieve build steps (per page) from the database
stepsPart, stepsCount, err := database.FromContext(c).ListStepsForBuild(b, nil, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to retrieve steps for build %s: %w", entry, err)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

// add page of steps to list steps
steps = append(steps, stepsPart...)

// assume no more pages exist if under 100 results are returned
if int(stepsCount) < perPage {
page = 0
} else {
page++
}
}
}

if len(steps) == 0 {
retErr := fmt.Errorf("no steps found for build %s", entry)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

// retrieve the services for the build from the service table
services := []*library.Service{}
page = 1
perPage = 100

// only fetch services when necessary
if len(p.Services) > 0 {
for page > 0 {
// retrieve build services (per page) from the database
servicesPart, servicesCount, err := database.FromContext(c).ListServicesForBuild(ctx, b, nil, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to retrieve services for build %s: %w", entry, err)

util.HandleError(c, http.StatusNotFound, retErr)

return
}

// add page of services to list services
services = append(services, servicesPart...)

// assume no more pages exist if under 100 results are returned
if int(servicesCount) < perPage {
page = 0
} else {
page++
}
}
}

// this is a simple check
// but it will save on processing a massive build that should not be rendered
complexity := len(steps) + len(p.Stages) + len(services)
Expand Down

0 comments on commit f589dce

Please sign in to comment.