-
Notifications
You must be signed in to change notification settings - Fork 897
Return full item data instead of just ids option #1193
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
Draft
AbhilashVijayakumar
wants to merge
13
commits into
gorse-io:master
Choose a base branch
from
AbhilashVijayakumar:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+85
−7
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eed368c
local test steps update in readme
AbhilashVijayakumar 643bb16
update readme for local testing
AbhilashVijayakumar 204e8ec
work in progress
AbhilashVijayakumar 87cc1fa
return full item + test update
AbhilashVijayakumar 50dd26d
remove unrelated changes
AbhilashVijayakumar 3d6f362
clenaup
AbhilashVijayakumar 3d2a5a7
if include-items query parameter respond with full items instead of j…
AbhilashVijayakumar 48790d9
Merge remote-tracking branch 'upstream/master'
AbhilashVijayakumar 4f0718b
parameter name update as per review comments
AbhilashVijayakumar 82d19c9
update API v2 response & tests
AbhilashVijayakumar e87468c
parameter name update
AbhilashVijayakumar 40b0bea
linter fix
AbhilashVijayakumar ce0ebdd
test fixes
AbhilashVijayakumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -539,7 +539,8 @@ func (s *RestServer) CreateWebService() { | |
| Param(ws.QueryParameter("write-back-delay", "Timestamp delay of write back feedback (format 0h0m0s)").DataType("string")). | ||
| Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). | ||
| Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). | ||
| Returns(http.StatusOK, "OK", []string{}). | ||
| Param(ws.QueryParameter("return-items", "Include full item data in response").DataType("boolean")). | ||
| Returns(http.StatusOK, "OK", []data.Item{}). | ||
| Writes([]string{})) | ||
| ws.Route(ws.GET("/recommend/{user-id}/{category}").To(s.getRecommend). | ||
| Deprecate().Doc("Get recommendation for user. Set X-API-Version: 2 to return scores."). | ||
|
|
@@ -552,7 +553,8 @@ func (s *RestServer) CreateWebService() { | |
| Param(ws.QueryParameter("write-back-delay", "Timestamp delay of write back feedback (format 0h0m0s)").DataType("string")). | ||
| Param(ws.QueryParameter("n", "Number of returned items").DataType("integer")). | ||
| Param(ws.QueryParameter("offset", "Offset of returned items").DataType("integer")). | ||
| Returns(http.StatusOK, "OK", []string{}). | ||
| Param(ws.QueryParameter("return-items", "Include full item data in response").DataType("boolean")). | ||
| Returns(http.StatusOK, "OK", []data.Item{}). | ||
| Writes([]string{})) | ||
| ws.Route(ws.POST("/session/recommend").To(s.sessionRecommend). | ||
| Doc("Get recommendation for session."). | ||
|
|
@@ -883,13 +885,14 @@ func (s *RestServer) getRecommend(request *restful.Request, response *restful.Re | |
| } else { | ||
| scores = []cache.Score{} | ||
| } | ||
| results := lo.Map(scores, func(item cache.Score, index int) string { | ||
| return item.Id | ||
| itemIds := lo.Map(scores, func(score cache.Score, _ int) string { | ||
| return score.Id | ||
| }) | ||
| includeItems := request.QueryParameter("return-items") == "true" | ||
| // write back | ||
| if writeBackFeedback != "" { | ||
| startTime := time.Now() | ||
| for _, itemId := range results { | ||
| for _, itemId := range itemIds { | ||
| // insert to data store | ||
| feedback := data.Feedback{ | ||
| FeedbackKey: data.FeedbackKey{ | ||
|
|
@@ -906,12 +909,48 @@ func (s *RestServer) getRecommend(request *restful.Request, response *restful.Re | |
| } | ||
| } | ||
| } | ||
| // Fetch full item data only when requested | ||
| var itemMap map[string]data.Item | ||
| if includeItems { | ||
| fetchedItems, err := s.DataClient.BatchGetItems(ctx, itemIds) | ||
| if err != nil { | ||
| InternalServerError(response, err) | ||
| return | ||
| } | ||
| itemMap = make(map[string]data.Item, len(fetchedItems)) | ||
| for _, item := range fetchedItems { | ||
| itemMap[item.ItemId] = item | ||
| } | ||
| } | ||
| // Send result | ||
| if apiVersion == "2" { | ||
| Ok(response, scores) | ||
| if includeItems { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change variable names as well |
||
| scoredItems := make([]ScoredItem, 0, len(scores)) | ||
| for _, s := range scores { | ||
| si := ScoredItem{Id: s.Id, Score: s.Score} | ||
| if item, ok := itemMap[s.Id]; ok { | ||
| si.Item = &item | ||
| } | ||
| scoredItems = append(scoredItems, si) | ||
| } | ||
| Ok(response, scoredItems) | ||
| } else { | ||
| Ok(response, scores) | ||
| } | ||
| return | ||
| } | ||
| Ok(response, results) | ||
| // Send response: include full item data only when requested | ||
| if includeItems { | ||
| items := make([]data.Item, 0) | ||
| for _, id := range itemIds { | ||
| if item, ok := itemMap[id]; ok { | ||
| items = append(items, item) | ||
| } | ||
| } | ||
| Ok(response, items) | ||
| } else { | ||
| Ok(response, itemIds) | ||
| } | ||
| } | ||
|
|
||
| func (s *RestServer) sessionRecommend(request *restful.Request, response *restful.Response) { | ||
|
|
@@ -1011,6 +1050,13 @@ func (s *RestServer) sessionRecommend(request *restful.Request, response *restfu | |
| Ok(response, result) | ||
| } | ||
|
|
||
| // ScoredItem is a scored item with optional full item data for X-Api-Version: 2. | ||
| type ScoredItem struct { | ||
| Id string `json:"Id"` | ||
| Score float64 `json:"Score"` | ||
| Item *data.Item `json:"Item,omitempty"` | ||
| } | ||
|
|
||
| // Success is the returned data structure for data insert operations. | ||
| type Success struct { | ||
| RowAffected int | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Move BatchGetItems to return item clause