Cleanup of server code
This commit is contained in:
parent
b7553be7b9
commit
2d378e83c9
14
server.go
14
server.go
@ -42,9 +42,9 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var geo *geoContext
|
var geo *geoData
|
||||||
if request.Geo != nil {
|
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})
|
entries := getRecords(queryContext{geo, request.Profile, request.WalkingDist})
|
||||||
@ -54,7 +54,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
|||||||
response := jsonQueryResponse{
|
response := jsonQueryResponse{
|
||||||
Count: len(foundEntries),
|
Count: len(foundEntries),
|
||||||
Columns: make(map[string]jsonColumn),
|
Columns: make(map[string]jsonColumn),
|
||||||
Items: make([]jsonRecord, 0)}
|
Records: make([]jsonRecord, 0)}
|
||||||
|
|
||||||
for name, value := range features {
|
for name, value := range features {
|
||||||
column := jsonColumn{Value: value, Steps: request.HintSteps}
|
column := jsonColumn{Value: value, Steps: request.HintSteps}
|
||||||
@ -64,14 +64,10 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
|||||||
features,
|
features,
|
||||||
name,
|
name,
|
||||||
request.MinScore,
|
request.MinScore,
|
||||||
queryBounds{request.Range.Min, request.Range.Max},
|
|
||||||
request.HintSteps)
|
request.HintSteps)
|
||||||
|
|
||||||
for _, hint := range hints {
|
for _, hint := range hints {
|
||||||
jsonHint := jsonProjection{
|
jsonHint := jsonProjection{hint.compatibility, hint.count, hint.sample}
|
||||||
Sample: hint.sample,
|
|
||||||
Stats: jsonStats{hint.stats.compatibility, hint.stats.count}}
|
|
||||||
|
|
||||||
column.Hints = append(column.Hints, jsonHint)
|
column.Hints = append(column.Hints, jsonHint)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +88,7 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
|||||||
AccessCount: value.accessCount,
|
AccessCount: value.accessCount,
|
||||||
Id: value.id}
|
Id: value.id}
|
||||||
|
|
||||||
response.Items = append(response.Items, item)
|
response.Records = append(response.Records, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
js, err := json.Marshal(response)
|
js, err := json.Marshal(response)
|
||||||
|
46
types.go
46
types.go
@ -34,25 +34,18 @@ type jsonAccessReply struct {
|
|||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonRange struct {
|
type jsonGeoData struct {
|
||||||
Max float64 `json:"max"`
|
|
||||||
Min float64 `json:"min"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type jsonGeo struct {
|
|
||||||
Latitude float64 `json:"latitude"`
|
Latitude float64 `json:"latitude"`
|
||||||
Longitude float64 `json:"longitude"`
|
Longitude float64 `json:"longitude"`
|
||||||
Valid bool `json:"valid"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonQueryRequest struct {
|
type jsonQueryRequest struct {
|
||||||
Features featureMap `json:"features"`
|
Features featureMap `json:"features"`
|
||||||
Geo *jsonGeo `json:"geo"`
|
Geo *jsonGeoData `json:"geo"`
|
||||||
HintSteps int `json:"hintSteps"`
|
HintSteps int `json:"hintSteps"`
|
||||||
MaxResults int `json:"maxResults"`
|
MaxResults int `json:"maxResults"`
|
||||||
MinScore float64 `json:"minScore"`
|
MinScore float64 `json:"minScore"`
|
||||||
Profile featureMap `json:"profile"`
|
Profile featureMap `json:"profile"`
|
||||||
Range jsonRange `json:"range"`
|
|
||||||
WalkingDist float64 `json:"walkingDist"`
|
WalkingDist float64 `json:"walkingDist"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,29 +56,25 @@ type jsonColumn struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type jsonProjection struct {
|
type jsonProjection struct {
|
||||||
Sample float64 `json:"sample"`
|
|
||||||
Stats jsonStats `json:"stats"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type jsonStats struct {
|
|
||||||
Compatibility float64 `json:"compatibility"`
|
Compatibility float64 `json:"compatibility"`
|
||||||
Count int `json:"count"`
|
Count int `json:"count"`
|
||||||
|
Sample float64 `json:"sample"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonRecord struct {
|
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"`
|
Name string `json:"name"`
|
||||||
Score float64 `json:"score"`
|
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 {
|
type jsonQueryResponse struct {
|
||||||
Columns map[string]jsonColumn `json:"columns"`
|
Columns map[string]jsonColumn `json:"columns"`
|
||||||
Count int `json:"count"`
|
Count int `json:"count"`
|
||||||
Items []jsonRecord `json:"items"`
|
Records []jsonRecord `json:"records"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonCategory struct {
|
type jsonCategory struct {
|
||||||
@ -112,27 +101,18 @@ type jsonRemoveCategoryResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type queryContext struct {
|
type queryContext struct {
|
||||||
geo *geoContext
|
geo *geoData
|
||||||
profile featureMap
|
profile featureMap
|
||||||
walkingDist float64
|
walkingDist float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type queryProjection struct {
|
type queryProjection struct {
|
||||||
sample float64
|
|
||||||
stats recordStats
|
|
||||||
}
|
|
||||||
|
|
||||||
type recordStats struct {
|
|
||||||
compatibility float64
|
compatibility float64
|
||||||
count int
|
count int
|
||||||
|
sample float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type queryBounds struct {
|
type geoData struct {
|
||||||
max float64
|
|
||||||
min float64
|
|
||||||
}
|
|
||||||
|
|
||||||
type geoContext struct {
|
|
||||||
latitude float64
|
latitude float64
|
||||||
longitude float64
|
longitude float64
|
||||||
}
|
}
|
||||||
@ -144,7 +124,7 @@ type record struct {
|
|||||||
distanceToStn float64
|
distanceToStn float64
|
||||||
distanceToUser float64
|
distanceToUser float64
|
||||||
features featureMap
|
features featureMap
|
||||||
geo geoContext
|
geo geoData
|
||||||
id int
|
id int
|
||||||
name string
|
name string
|
||||||
score float64
|
score float64
|
||||||
|
28
util.go
28
util.go
@ -65,21 +65,23 @@ func walkMatches(entries records, features featureMap, minScore float64, callbac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func statRecords(entries records, features featureMap, minScore float64) recordStats {
|
func statRecords(entries records, features featureMap, minScore float64) (float64, int) {
|
||||||
var stats recordStats
|
var compatibility float64
|
||||||
|
var count int
|
||||||
|
|
||||||
walkMatches(entries, features, minScore, func(record record, score float64) {
|
walkMatches(entries, features, minScore, func(record record, score float64) {
|
||||||
stats.compatibility += record.compatibility
|
compatibility += record.compatibility
|
||||||
stats.count++
|
count++
|
||||||
})
|
})
|
||||||
|
|
||||||
return stats
|
return compatibility, count
|
||||||
}
|
}
|
||||||
|
|
||||||
func stepRange(bounds queryBounds, steps int, callback func(float64)) {
|
func stepRange(min, max float64, steps int, callback func(float64)) {
|
||||||
stepSize := (bounds.max - bounds.min) / float64(steps)
|
stepSize := (max - min) / float64(steps)
|
||||||
|
|
||||||
for i := 0; i < steps; i++ {
|
for i := 0; i < steps; i++ {
|
||||||
stepMax := bounds.max - stepSize*float64(i)
|
stepMax := max - stepSize*float64(i)
|
||||||
stepMin := stepMax - stepSize
|
stepMin := stepMax - stepSize
|
||||||
stepMid := (stepMin + stepMax) / 2
|
stepMid := (stepMin + stepMax) / 2
|
||||||
|
|
||||||
@ -99,17 +101,17 @@ func findRecords(entries records, features featureMap, minScore float64) records
|
|||||||
return foundEntries
|
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)
|
sampleFeatures := make(featureMap)
|
||||||
for key, value := range features {
|
for key, value := range features {
|
||||||
sampleFeatures[key] = value
|
sampleFeatures[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
var projection []queryProjection
|
var projection []queryProjection
|
||||||
stepRange(bounds, steps, func(sample float64) {
|
stepRange(-1.0, 1.0, steps, func(sample float64) {
|
||||||
sampleFeatures[featureName] = sample
|
sampleFeatures[featureName] = sample
|
||||||
stats := statRecords(entries, sampleFeatures, minScore)
|
compatibility, count := statRecords(entries, sampleFeatures, minScore)
|
||||||
projection = append(projection, queryProjection{sample: sample, stats: stats})
|
projection = append(projection, queryProjection{compatibility, count, sample})
|
||||||
})
|
})
|
||||||
|
|
||||||
return projection
|
return projection
|
||||||
@ -237,7 +239,7 @@ func getRecords(context queryContext) records {
|
|||||||
distanceToStn: distanceToStn,
|
distanceToStn: distanceToStn,
|
||||||
closestStn: closestStn,
|
closestStn: closestStn,
|
||||||
accessCount: accessCount,
|
accessCount: accessCount,
|
||||||
geo: geoContext{latitude, longitude},
|
geo: geoData{latitude, longitude},
|
||||||
id: id}
|
id: id}
|
||||||
|
|
||||||
entry.features = featureMap{
|
entry.features = featureMap{
|
||||||
|
Loading…
Reference in New Issue
Block a user