Skip to content

Commit

Permalink
Improve performance when getting all products for a project (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfreda authored Jan 30, 2024
1 parent 0061237 commit a7391bc
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions internal/api/v2/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strconv"
"strings"

"github.com/hashicorp-forge/hermes/internal/helpers"
"github.com/hashicorp-forge/hermes/internal/server"
"github.com/hashicorp-forge/hermes/pkg/algolia"
"github.com/hashicorp-forge/hermes/pkg/models"
Expand Down Expand Up @@ -477,31 +476,20 @@ func ProjectHandler(srv server.Server) http.Handler {
// getProductsForProject returns a slice of unique products for all Hermes
// document related resources associated with the project.
func getProductsForProject(proj models.Project, db *gorm.DB) ([]string, error) {
// Get Hermes document related resources for project.
_, hdrrs, err := proj.GetRelatedResources(db)
var productNames []string
err := db.Raw(`
SELECT DISTINCT p.name
FROM products p
INNER JOIN documents d ON p.id = d.product_id
INNER JOIN project_related_resource_hermes_documents prrhd ON d.id = prrhd.document_id
INNER JOIN project_related_resources prr ON prrhd.id = prr.related_resource_id
WHERE prr.project_id = ? AND prr.related_resource_type = 'project_related_resource_hermes_documents'
`, proj.ID).Scan(&productNames).Error
if err != nil {
return nil, fmt.Errorf("error getting related resources: %w", err)
return nil, fmt.Errorf("error getting products for project: %w", err)
}

products := []string{}
for _, hdrr := range hdrrs {
// Get document from database.
doc := models.Document{
GoogleFileID: hdrr.Document.GoogleFileID,
}
if err := doc.Get(db); err != nil {
return nil, fmt.Errorf(
"error getting document from database: %w, document_id: %s",
err, hdrr.Document.GoogleFileID)
}
product := doc.Product.Name

if !helpers.StringSliceContains(products, product) {
products = append(products, product)
}
}

return products, nil
return productNames, nil
}

// getProjectIDFromPath returns the project ID from a request path and
Expand Down

0 comments on commit a7391bc

Please sign in to comment.