From 2a3230042149fd6e2c1c21f78d564321b549f125 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 25 Mar 2015 18:22:57 +0900 Subject: [PATCH] Cleanup --- server.go | 3 ++- util.go | 51 +++++++++++++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/server.go b/server.go index 980ce95..c9fbd95 100644 --- a/server.go +++ b/server.go @@ -72,6 +72,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { jsonHint := jsonProjection{ Sample: hint.sample, Stats: jsonStats{Count: hint.stats.count, Compatibility: hint.stats.compatibility}} + column.Hints = append(column.Hints, jsonHint) } @@ -268,5 +269,5 @@ func main() { http.HandleFunc("/access", accessReview) http.Handle("/", http.FileServer(http.Dir(dir))) - log.Fatal(http.ListenAndServe(":3000", nil)) + log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/util.go b/util.go index b5c81ae..fe92d58 100644 --- a/util.go +++ b/util.go @@ -118,35 +118,35 @@ func computeRecordGeo(entries records, context queryContext) { distUserMin := math.MaxFloat64 distUserMax := 0.0 - for _, record := range entries { + for index := range entries { + entry := &entries[index] + if context.geo != nil { userPoint := geo.NewPoint(context.geo.latitude, context.geo.longitude) - recordPoint := geo.NewPoint(record.geo.latitude, context.geo.longitude) - record.distanceToUser = userPoint.GreatCircleDistance(recordPoint) + entryPoint := geo.NewPoint(entry.geo.latitude, context.geo.longitude) + entry.distanceToUser = userPoint.GreatCircleDistance(entryPoint) } - if record.distanceToUser < distUserMin { - distUserMin = record.distanceToUser - } - if record.distanceToUser > distUserMax { - distUserMax = record.distanceToUser - } + distUserMin = math.Min(entry.distanceToUser, distUserMin) + distUserMax = math.Max(entry.distanceToUser, distUserMax) } distUserRange := distUserMax - distUserMin - for _, record := range entries { - nearby := -((record.distanceToUser-distUserMin)/distUserRange - 0.5) * 2.0 + for index := range entries { + entry := &entries[index] - accessible := 1.0 - (record.distanceToStn / context.walkingDist) - if accessible < -1.0 { - accessible = 1.0 - } else if accessible > 1.0 { - accessible = 1.0 + var accessible, nearby float64 + if distUserRange > 0 { + nearby = -((entry.distanceToUser-distUserMin)/distUserRange - 0.5) * 2.0 + + accessible = 1.0 - (entry.distanceToStn / context.walkingDist) + accessible = math.Max(accessible, -1.0) + accessible = math.Min(accessible, 1.0) } - record.features["nearby"] = nearby - record.features["accessible"] = accessible + entry.features["nearby"] = nearby + entry.features["accessible"] = accessible } } @@ -234,14 +234,14 @@ func getRecords(context queryContext) records { distanceToStn: distanceToStn, closestStn: closestStn, accessCount: accessCount, + geo: geoContext{latitude, longitude}, id: id} - entry.features = make(featureMap) - entry.features["delicious"] = delicious - entry.features["accomodating"] = accomodating - entry.features["affordable"] = affordable - entry.features["atmospheric"] = atmospheric - entry.geo = geoContext{latitude, longitude} + entry.features = featureMap{ + "delicious": delicious, + "accomodating": accomodating, + "affordable": affordable, + "atmospheric": atmospheric} entries = append(entries, entry) } @@ -249,5 +249,8 @@ func getRecords(context queryContext) records { log.Fatal(err) } + computeRecordPopularity(entries, context) + computeRecordGeo(entries, context) + return entries }