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) {
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) {

View File

@ -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
}

16
util.go
View File

@ -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)
}