1

Cleanup of server code

This commit is contained in:
Alex Yatskov 2015-03-25 20:17:12 +09:00
parent b7553be7b9
commit 2d378e83c9
3 changed files with 39 additions and 61 deletions

View File

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

View File

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

28
util.go
View File

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