-
Notifications
You must be signed in to change notification settings - Fork 24
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
RHINENG-14146 Optimize Get Recommendations Query #282
base: main
Are you sure you want to change the base?
RHINENG-14146 Optimize Get Recommendations Query #282
Conversation
I'll fix the unittest soon, please feel free to go through the other changes meanwhile. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good to me. 👍 I will continue reviewing latest updated changes specific to date field.
Added just two inline suggestions. Please feel free to skip if it doesn't make sense.
Thanks!
internal/api/handlers.go
Outdated
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) | |
recommendationData := map[string]interface{}{ | |
"id": recommendation.ID, | |
"source_id": recommendation.SourceID, | |
"cluster_uuid": recommendation.ClusterUUID, | |
"cluster_alias": recommendation.ClusterAlias, | |
"project": recommendation.Project, | |
"workload_type": recommendation.WorkloadType, | |
"workload": recommendation.Workload, | |
"container": recommendation.Container, | |
"last_reported": recommendation.LastReported, | |
"recommendations": UpdateRecommendationJSON(handlerName, recommendation.ID, recommendation.ClusterUUID, unitChoices, setk8sUnits, recommendation.Recommendations) | |
} | |
With this approach, it will look more clean and concise. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you'll like this change 🙂 579879c
internal/api/handlers.go
Outdated
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC).Truncate(time.Second) | ||
|
||
startTime := time.Date(2023, 3, 23, 0, 0, 0, 0, time.UTC).Truncate(time.Second) | ||
endTime := time.Date(2023, 3, 24, 0, 0, 0, 0, time.UTC).Truncate(time.Second) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC).Truncate(time.Second) | |
startTime := time.Date(2023, 3, 23, 0, 0, 0, 0, time.UTC).Truncate(time.Second) | |
endTime := time.Date(2023, 3, 24, 0, 0, 0, 0, time.UTC).Truncate(time.Second) | |
firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC) | |
startTime := time.Date(2023, 3, 23, 0, 0, 0, 0, time.UTC) | |
endTime := time.Date(2023, 3, 24, 0, 0, 0, 0, time.UTC) |
It won't require to apply Truncate
as you are already passing seconds as zero.
startDateSlice := append(dateSlice, startDate.Format(timeLayout)) | ||
queryParams["DATE(recommendation_sets.monitoring_end_time) >= ?"] = startDateSlice | ||
endDateStr := c.QueryParam("end_date") | ||
queryParams["recommendation_sets.monitoring_end_time >= ?"] = startTimestamp.Truncate(time.Second) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any specific reason behind applying Truncate
here? Is it really required here?
startDateStr := c.QueryParam("start_date") | ||
|
||
if startDateStr == "" { | ||
startDate = firstOfMonth | ||
startTimestamp = firstOfMonth |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startTimestamp = firstOfMonth | |
// Consider first day of the month when start_date is empty | |
startTimestamp = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC) |
Will it be possible to remove firstOfMonth
and assign value directly with comment for additional info.
if endDateStr == "" { | ||
endDate = now | ||
endTimestamp = now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
endTimestamp = now | |
endTimestamp = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC) |
endDateSlice := append(dateSlice, endDate.Format(timeLayout)) | ||
|
||
queryParams["DATE(recommendation_sets.monitoring_end_time) <= ?"] = endDateSlice | ||
queryParams["recommendation_sets.monitoring_end_time <= ?"] = endTimestamp.Truncate(time.Second) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with above suggestion, no need of calling Truncate
function.
Request to rebase! the IQE tests are running most of the times now! |
d96ab53
to
bd8f4da
Compare
/retest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The query
var in GetRecommendationSets
and GetRecommendationSetByID
is exactly same part from a filter Where("recommendation_sets.id = ?", recommendationID)
. This is good candidate to consolidate and take out in another common function.
bd8f4da
to
7e745bc
Compare
/retest |
On IQE I can see its failing because of syntax error, however I dont see this locally? hmm.. is it because locally I am on 1.22 version of Go? need to check,
|
I'll take a look |
Why do we need this change? 💭
The current sql query is expensive considering there is a lot of records to sift through.
Analysis details are present in the JIRA card.
That said, some important details are provided in the Additional section below.
Documentation update? 📝
Security Checklist 🔒
Upon raising this PR please go through RedHatInsights/secure-coding-checklist
💂♂️ Checklist 🎯
Additional 📣
Indexes added
clusters (last_reported_at)
recommendation_sets (workload_id)
workloads (cluster_id)
Other Optimizations
Query Diffs
GetRecommendationSet
Old
New
GetRecommendationSetByID
Old
New