Skip to content

Commit

Permalink
RecommendationSetResult; an API-ready struct
Browse files Browse the repository at this point in the history
  • Loading branch information
saltgen committed Dec 23, 2024
1 parent 3ca4814 commit 579879c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
56 changes: 22 additions & 34 deletions internal/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,19 @@ func GetRecommendationSetList(c echo.Context) error {
}
setk8sUnits := !trueUnits

allRecommendations := []map[string]interface{}{}

for _, recommendation := range recommendationSets {
recommendationData := make(map[string]interface{})

recommendationData["id"] = recommendation.ID
recommendationData["source_id"] = recommendation.SourceID
recommendationData["cluster_uuid"] = recommendation.ClusterUUID
recommendationData["cluster_alias"] = recommendation.ClusterAlias
recommendationData["project"] = recommendation.Project
recommendationData["workload_type"] = recommendation.WorkloadType
recommendationData["workload"] = recommendation.Workload
recommendationData["container"] = recommendation.Container
recommendationData["last_reported"] = recommendation.LastReported
recommendationData["recommendations"] = UpdateRecommendationJSON(handlerName, recommendation.ID, recommendation.ClusterUUID, unitChoices, setk8sUnits, recommendation.Recommendations)
allRecommendations = append(allRecommendations, recommendationData)
}

interfaceSlice := make([]interface{}, len(allRecommendations))
for i, v := range allRecommendations {
for i := range recommendationSets {
recommendationSets[i].RecommendationsJSON = UpdateRecommendationJSON(
handlerName,
recommendationSets[i].ID,
recommendationSets[i].ClusterUUID,
unitChoices,
setk8sUnits,
recommendationSets[i].Recommendations,
)
}

interfaceSlice := make([]interface{}, len(recommendationSets))
for i, v := range recommendationSets {
interfaceSlice[i] = v
}
results := CollectionResponse(interfaceSlice, c.Request(), count, limit, offset)
Expand Down Expand Up @@ -221,21 +214,16 @@ func GetRecommendationSet(c echo.Context) error {
return c.JSON(http.StatusNotFound, echo.Map{"status": "not_found", "message": "recommendation not found"})
}

recommendationSlice := make(map[string]interface{})

if len(recommendationSet.Recommendations) != 0 {
recommendationSlice["id"] = recommendationSet.ID
recommendationSlice["source_id"] = recommendationSet.SourceID
recommendationSlice["cluster_uuid"] = recommendationSet.ClusterUUID
recommendationSlice["cluster_alias"] = recommendationSet.ClusterAlias
recommendationSlice["project"] = recommendationSet.Project
recommendationSlice["workload_type"] = recommendationSet.WorkloadType
recommendationSlice["workload"] = recommendationSet.Workload
recommendationSlice["container"] = recommendationSet.Container
recommendationSlice["last_reported"] = recommendationSet.LastReported
recommendationSlice["recommendations"] = UpdateRecommendationJSON(handlerName, recommendationSet.ID, recommendationSet.ClusterUUID, unitChoices, setk8sUnits, recommendationSet.Recommendations)
}
return c.JSON(http.StatusOK, recommendationSlice)
recommendationSet.RecommendationsJSON = UpdateRecommendationJSON(
handlerName,
recommendationSet.ID,
recommendationSet.ClusterUUID,
unitChoices,
setk8sUnits,
recommendationSet.Recommendations)
}
return c.JSON(http.StatusOK, recommendationSet)
}

func GetAppStatus(c echo.Context) error {
Expand Down
26 changes: 16 additions & 10 deletions internal/model/recommendation_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@ type RecommendationSet struct {
}

type RecommendationSetResult struct {
ClusterAlias string `gorm:"column:cluster_alias"`
ClusterUUID string `gorm:"column:cluster_uuid"`
Container string `json:"container"`
ID string `json:"id"`
LastReported string `gorm:"column:last_reported"`
Project string `gorm:"column:project"`
Recommendations datatypes.JSON `json:"recommendations"`
SourceID string `gorm:"column:source_id"`
Workload string `gorm:"column:workload"`
WorkloadType string `gorm:"column:workload_type"`
/*
Intended to be an API-ready struct
Updated recommendation data is saved to RecommendationsJSON
Before the API response is sent
*/
ClusterAlias string `json:"cluster_alias"`
ClusterUUID string `json:"cluster_uuid"`
Container string `json:"container"`
ID string `json:"id"`
LastReported string `json:"last_reported"`
Project string `json:"project"`
Recommendations datatypes.JSON `json:"-"`
RecommendationsJSON map[string]interface{} `gorm:"-" json:"recommendations"`
SourceID string `json:"source_id"`
Workload string `json:"workload"`
WorkloadType string `json:"workload_type"`
}

func (r *RecommendationSet) AfterFind(tx *gorm.DB) error {
Expand Down

0 comments on commit 579879c

Please sign in to comment.