diff --git a/server.go b/server.go index c9fbd95..9c44438 100644 --- a/server.go +++ b/server.go @@ -47,13 +47,12 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { geo = &geoContext{latitude: request.Geo.Latitude, longitude: request.Geo.Longitude} } - context := queryContext{geo, request.Profile, request.WalkingDist} - entries := getRecords(context) + entries := getRecords(queryContext{geo, request.Profile, request.WalkingDist}) features := fixFeatures(request.Features) - foundEntries := findRecords(entries, features, request.MinScore) response := jsonQueryResponse{ + Count: len(foundEntries), Columns: make(map[string]jsonColumn), Items: make([]jsonRecord, 0)} @@ -84,7 +83,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { break } - jsonEntry := jsonRecord{ + item := jsonRecord{ Name: value.name, Score: value.score, DistanceToUser: value.distanceToUser, @@ -93,7 +92,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { AccessCount: value.accessCount, Id: value.id} - response.Items = append(response.Items, jsonEntry) + response.Items = append(response.Items, item) } js, err := json.Marshal(response) diff --git a/util.go b/util.go index fe92d58..99cd11f 100644 --- a/util.go +++ b/util.go @@ -91,6 +91,7 @@ func findRecords(entries records, features featureMap, minScore float64) records var foundEntries records walkMatches(entries, features, minScore, func(record record, score float64) { + record.score = score foundEntries = append(foundEntries, record) }) @@ -151,8 +152,10 @@ func computeRecordGeo(entries records, context queryContext) { } func computeRecordPopularity(entries records, context queryContext) { - for _, record := range entries { - historyRows, err := db.Query("SELECT id FROM history WHERE reviewId = (?)", record.id) + for index := range entries { + entry := &entries[index] + + historyRows, err := db.Query("SELECT id FROM history WHERE reviewId = (?)", entry.id) if err != nil { log.Fatal(err) } @@ -193,12 +196,9 @@ func computeRecordPopularity(entries records, context queryContext) { log.Fatal(err) } - var compatibility float64 if groupCount > 0 { - compatibility = groupSum / float64(groupCount) + entry.compatibility = groupSum / float64(groupCount) } - - record.features["compatibility"] = compatibility } }