1

Refactoring

This commit is contained in:
Alex Yatskov 2015-03-24 17:58:35 +09:00
parent 55936cf98d
commit 5b158c27b2
3 changed files with 32 additions and 12 deletions

View File

@ -37,6 +37,21 @@ var db *sql.DB
func executeQuery(rw http.ResponseWriter, req *http.Request) { func executeQuery(rw http.ResponseWriter, req *http.Request) {
type Request struct { type Request struct {
features Features
bounds Bounds
geo Geo
walkingDist float64
minScore float64
hintSteps int
maxResults int
// features: _ctx.query.features || {},
// range: {min: -1.0, max: 1.0},
// profile: getProfile(),
// walkingDist: parseFloat($('#walkingDist').val()),
// minScore: parseFloat($('#minScore').val()),
// hintSteps: parseInt($('#hintSteps').val()),
// maxResults: parseInt($('#maxResults').val())
} }
// function runQuery(query, callback) { // function runQuery(query, callback) {

View File

@ -23,7 +23,7 @@
package main package main
type Context struct { type Context struct {
hasPosition bool geo Geo
latitude float64 latitude float64
longitude float64 longitude float64
profile Features profile Features
@ -42,20 +42,25 @@ type RecordStats struct {
count int count int
} }
type Range struct { type Bounds struct {
max float64 max float64
min float64 min float64
} }
type Geo struct {
latitude float64
longitude float64
valid bool
}
type Record struct { type Record struct {
accessCount int accessCount int
compatibility float64 compatibility float64
distanceToStn float64 distanceToStn float64
distanceToUser float64 distanceToUser float64
features Features features Features
geo Geo
id int id int
latitude float64
longitude float64
name string name string
score float64 score float64
} }

16
util.go
View File

@ -57,11 +57,11 @@ func statRecords(records Records, features Features, minScore float64) RecordSta
return stats return stats
} }
func stepRange(rng Range, steps int, callback func(float64)) { func stepRange(bounds Bounds, steps int, callback func(float64)) {
stepSize := (rng.max - rng.min) / float64(steps) stepSize := (bounds.max - bounds.min) / float64(steps)
for i := 0; i < steps; i++ { for i := 0; i < steps; i++ {
stepMax := rng.max - stepSize*float64(i) stepMax := bounds.max - stepSize*float64(i)
stepMin := stepMax - stepSize stepMin := stepMax - stepSize
stepMid := (stepMin + stepMax) / 2 stepMid := (stepMin + stepMax) / 2
@ -79,14 +79,14 @@ func findRecords(records Records, features Features, minScore float64) {
sort.Sort(foundRecords) sort.Sort(foundRecords)
} }
func project(records Records, features Features, featureName string, minScore float64, rng Range, steps int) []Projection { func project(records Records, features Features, featureName string, minScore float64, bounds Bounds, steps int) []Projection {
sampleFeatures := make(Features) sampleFeatures := make(Features)
for key, value := range features { for key, value := range features {
sampleFeatures[key] = value sampleFeatures[key] = value
} }
var projection []Projection var projection []Projection
stepRange(rng, steps, func(sample float64) { stepRange(bounds, steps, func(sample float64) {
sampleFeatures[featureName] = sample sampleFeatures[featureName] = sample
stats := statRecords(records, sampleFeatures, minScore) stats := statRecords(records, sampleFeatures, minScore)
projection = append(projection, Projection{sample: sample, stats: stats}) projection = append(projection, Projection{sample: sample, stats: stats})
@ -100,9 +100,9 @@ func computeRecordGeo(records Records, context Context) {
distUserMax := 0.0 distUserMax := 0.0
for _, record := range records { for _, record := range records {
if context.hasPosition { if context.geo.valid {
userPoint := geo.NewPoint(context.latitude, context.longitude) userPoint := geo.NewPoint(context.geo.latitude, context.geo.longitude)
recordPoint := geo.NewPoint(record.latitude, context.longitude) recordPoint := geo.NewPoint(record.geo.latitude, context.geo.longitude)
record.distanceToUser = userPoint.GreatCircleDistance(recordPoint) record.distanceToUser = userPoint.GreatCircleDistance(recordPoint)
} }