From 5b158c27b21dc23e3a359d5a89d2e1a253b08be8 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 24 Mar 2015 17:58:35 +0900 Subject: [PATCH] Refactoring --- server.go | 15 +++++++++++++++ types.go | 13 +++++++++---- util.go | 16 ++++++++-------- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/server.go b/server.go index cc3cc78..45feeb8 100644 --- a/server.go +++ b/server.go @@ -37,6 +37,21 @@ var db *sql.DB func executeQuery(rw http.ResponseWriter, req *http.Request) { 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) { diff --git a/types.go b/types.go index 23f4de3..dd86ef8 100644 --- a/types.go +++ b/types.go @@ -23,7 +23,7 @@ package main type Context struct { - hasPosition bool + geo Geo latitude float64 longitude float64 profile Features @@ -42,20 +42,25 @@ type RecordStats struct { count int } -type Range struct { +type Bounds struct { max float64 min float64 } +type Geo struct { + latitude float64 + longitude float64 + valid bool +} + type Record struct { accessCount int compatibility float64 distanceToStn float64 distanceToUser float64 features Features + geo Geo id int - latitude float64 - longitude float64 name string score float64 } diff --git a/util.go b/util.go index bf4c1dc..c512c63 100644 --- a/util.go +++ b/util.go @@ -57,11 +57,11 @@ func statRecords(records Records, features Features, minScore float64) RecordSta return stats } -func stepRange(rng Range, steps int, callback func(float64)) { - stepSize := (rng.max - rng.min) / float64(steps) +func stepRange(bounds Bounds, steps int, callback func(float64)) { + stepSize := (bounds.max - bounds.min) / float64(steps) for i := 0; i < steps; i++ { - stepMax := rng.max - stepSize*float64(i) + stepMax := bounds.max - stepSize*float64(i) stepMin := stepMax - stepSize stepMid := (stepMin + stepMax) / 2 @@ -79,14 +79,14 @@ func findRecords(records Records, features Features, minScore float64) { 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) for key, value := range features { sampleFeatures[key] = value } var projection []Projection - stepRange(rng, steps, func(sample float64) { + stepRange(bounds, steps, func(sample float64) { sampleFeatures[featureName] = sample stats := statRecords(records, sampleFeatures, minScore) projection = append(projection, Projection{sample: sample, stats: stats}) @@ -100,9 +100,9 @@ func computeRecordGeo(records Records, context Context) { distUserMax := 0.0 for _, record := range records { - if context.hasPosition { - userPoint := geo.NewPoint(context.latitude, context.longitude) - recordPoint := geo.NewPoint(record.latitude, context.longitude) + if context.geo.valid { + userPoint := geo.NewPoint(context.geo.latitude, context.geo.longitude) + recordPoint := geo.NewPoint(record.geo.latitude, context.geo.longitude) record.distanceToUser = userPoint.GreatCircleDistance(recordPoint) }