From 2d378e83c91cc47f15abfa3e66d448740242ca7a Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 25 Mar 2015 20:17:12 +0900 Subject: [PATCH] Cleanup of server code --- server.go | 14 +++++--------- types.go | 58 ++++++++++++++++++------------------------------------- util.go | 28 ++++++++++++++------------- 3 files changed, 39 insertions(+), 61 deletions(-) diff --git a/server.go b/server.go index 449f1d2..fa657d8 100644 --- a/server.go +++ b/server.go @@ -42,9 +42,9 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { return } - var geo *geoContext + var geo *geoData if request.Geo != nil { - geo = &geoContext{request.Geo.Latitude, request.Geo.Longitude} + geo = &geoData{request.Geo.Latitude, request.Geo.Longitude} } entries := getRecords(queryContext{geo, request.Profile, request.WalkingDist}) @@ -54,7 +54,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { response := jsonQueryResponse{ Count: len(foundEntries), Columns: make(map[string]jsonColumn), - Items: make([]jsonRecord, 0)} + Records: make([]jsonRecord, 0)} for name, value := range features { column := jsonColumn{Value: value, Steps: request.HintSteps} @@ -64,14 +64,10 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { features, name, request.MinScore, - queryBounds{request.Range.Min, request.Range.Max}, request.HintSteps) for _, hint := range hints { - jsonHint := jsonProjection{ - Sample: hint.sample, - Stats: jsonStats{hint.stats.compatibility, hint.stats.count}} - + jsonHint := jsonProjection{hint.compatibility, hint.count, hint.sample} column.Hints = append(column.Hints, jsonHint) } @@ -92,7 +88,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { AccessCount: value.accessCount, Id: value.id} - response.Items = append(response.Items, item) + response.Records = append(response.Records, item) } js, err := json.Marshal(response) diff --git a/types.go b/types.go index 21ace01..3c9a500 100644 --- a/types.go +++ b/types.go @@ -34,26 +34,19 @@ type jsonAccessReply struct { Url string `json:"url"` } -type jsonRange struct { - Max float64 `json:"max"` - Min float64 `json:"min"` -} - -type jsonGeo struct { +type jsonGeoData struct { Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` - Valid bool `json:"valid"` } type jsonQueryRequest struct { - Features featureMap `json:"features"` - Geo *jsonGeo `json:"geo"` - HintSteps int `json:"hintSteps"` - MaxResults int `json:"maxResults"` - MinScore float64 `json:"minScore"` - Profile featureMap `json:"profile"` - Range jsonRange `json:"range"` - WalkingDist float64 `json:"walkingDist"` + Features featureMap `json:"features"` + Geo *jsonGeoData `json:"geo"` + HintSteps int `json:"hintSteps"` + MaxResults int `json:"maxResults"` + MinScore float64 `json:"minScore"` + Profile featureMap `json:"profile"` + WalkingDist float64 `json:"walkingDist"` } type jsonColumn struct { @@ -63,29 +56,25 @@ type jsonColumn struct { } type jsonProjection struct { - Sample float64 `json:"sample"` - Stats jsonStats `json:"stats"` -} - -type jsonStats struct { Compatibility float64 `json:"compatibility"` Count int `json:"count"` + Sample float64 `json:"sample"` } type jsonRecord struct { + AccessCount int `json:"accessCount"` + ClosestStn string `json:"closestStn"` + DistanceToStn float64 `json:"distanceToStn"` + DistanceToUser float64 `json:"distanceToUser"` + Id int `json:"id"` Name string `json:"name"` Score float64 `json:"score"` - DistanceToUser float64 `json:"distanceToUser"` - DistanceToStn float64 `json:"distanceToStn"` - ClosestStn string `json:"closestStn"` - AccessCount int `json:"accessCount"` - Id int `json:"id"` } type jsonQueryResponse struct { Columns map[string]jsonColumn `json:"columns"` Count int `json:"count"` - Items []jsonRecord `json:"items"` + Records []jsonRecord `json:"records"` } type jsonCategory struct { @@ -112,27 +101,18 @@ type jsonRemoveCategoryResponse struct { } type queryContext struct { - geo *geoContext + geo *geoData profile featureMap walkingDist float64 } type queryProjection struct { - sample float64 - stats recordStats -} - -type recordStats struct { compatibility float64 count int + sample float64 } -type queryBounds struct { - max float64 - min float64 -} - -type geoContext struct { +type geoData struct { latitude float64 longitude float64 } @@ -144,7 +124,7 @@ type record struct { distanceToStn float64 distanceToUser float64 features featureMap - geo geoContext + geo geoData id int name string score float64 diff --git a/util.go b/util.go index 3342d11..9adf193 100644 --- a/util.go +++ b/util.go @@ -65,21 +65,23 @@ func walkMatches(entries records, features featureMap, minScore float64, callbac } } -func statRecords(entries records, features featureMap, minScore float64) recordStats { - var stats recordStats +func statRecords(entries records, features featureMap, minScore float64) (float64, int) { + var compatibility float64 + var count int + walkMatches(entries, features, minScore, func(record record, score float64) { - stats.compatibility += record.compatibility - stats.count++ + compatibility += record.compatibility + count++ }) - return stats + return compatibility, count } -func stepRange(bounds queryBounds, steps int, callback func(float64)) { - stepSize := (bounds.max - bounds.min) / float64(steps) +func stepRange(min, max float64, steps int, callback func(float64)) { + stepSize := (max - min) / float64(steps) for i := 0; i < steps; i++ { - stepMax := bounds.max - stepSize*float64(i) + stepMax := max - stepSize*float64(i) stepMin := stepMax - stepSize stepMid := (stepMin + stepMax) / 2 @@ -99,17 +101,17 @@ func findRecords(entries records, features featureMap, minScore float64) records return foundEntries } -func project(entries records, features featureMap, featureName string, minScore float64, bounds queryBounds, steps int) []queryProjection { +func project(entries records, features featureMap, featureName string, minScore float64, steps int) []queryProjection { sampleFeatures := make(featureMap) for key, value := range features { sampleFeatures[key] = value } var projection []queryProjection - stepRange(bounds, steps, func(sample float64) { + stepRange(-1.0, 1.0, steps, func(sample float64) { sampleFeatures[featureName] = sample - stats := statRecords(entries, sampleFeatures, minScore) - projection = append(projection, queryProjection{sample: sample, stats: stats}) + compatibility, count := statRecords(entries, sampleFeatures, minScore) + projection = append(projection, queryProjection{compatibility, count, sample}) }) return projection @@ -237,7 +239,7 @@ func getRecords(context queryContext) records { distanceToStn: distanceToStn, closestStn: closestStn, accessCount: accessCount, - geo: geoContext{latitude, longitude}, + geo: geoData{latitude, longitude}, id: id} entry.features = featureMap{