2015-03-24 06:17:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
|
|
|
|
* Author: Alex Yatskov <alex@foosoft.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2015-03-27 02:26:41 +00:00
|
|
|
import "sort"
|
|
|
|
|
2015-06-29 04:06:26 +00:00
|
|
|
type modeType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ModeTypeNone modeType = iota
|
|
|
|
ModeTypeProduct
|
|
|
|
ModeTypeDist
|
|
|
|
)
|
|
|
|
|
2015-03-25 03:33:41 +00:00
|
|
|
type featureMap map[string]float64
|
2015-06-29 04:06:26 +00:00
|
|
|
type modeMap map[string]modeType
|
2015-03-24 13:36:53 +00:00
|
|
|
|
2015-03-25 10:10:34 +00:00
|
|
|
type jsonAccessRequest struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
Profile featureMap `json:"profile"`
|
|
|
|
}
|
|
|
|
|
2015-03-25 11:17:12 +00:00
|
|
|
type jsonGeoData struct {
|
2015-03-24 13:36:53 +00:00
|
|
|
Latitude float64 `json:"latitude"`
|
|
|
|
Longitude float64 `json:"longitude"`
|
|
|
|
}
|
|
|
|
|
2015-03-25 03:00:54 +00:00
|
|
|
type jsonQueryRequest struct {
|
2015-06-28 04:30:22 +00:00
|
|
|
Features featureMap `json:"features"`
|
|
|
|
Geo *jsonGeoData `json:"geo"`
|
|
|
|
MaxResults int `json:"maxResults"`
|
|
|
|
MinScore float64 `json:"minScore"`
|
2015-06-29 04:31:04 +00:00
|
|
|
Modes map[string]string `json:"modes"`
|
2015-06-28 04:30:22 +00:00
|
|
|
Profile featureMap `json:"profile"`
|
|
|
|
Resolution int `json:"resolution"`
|
|
|
|
SortAsc bool `json:"sortAsc"`
|
|
|
|
SortKey string `json:"sortKey"`
|
|
|
|
WalkingDist float64 `json:"walkingDist"`
|
2015-03-24 13:36:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 03:00:54 +00:00
|
|
|
type jsonColumn struct {
|
2015-06-27 04:53:26 +00:00
|
|
|
Bracket jsonBracket `json:"bracket"`
|
|
|
|
Hints []jsonProjection `json:"hints"`
|
2015-06-29 04:06:26 +00:00
|
|
|
Mode string `json:"mode"`
|
2015-06-27 04:53:26 +00:00
|
|
|
Steps int `json:"steps"`
|
|
|
|
Value float64 `json:"value"`
|
2015-03-25 03:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type jsonProjection struct {
|
|
|
|
Compatibility float64 `json:"compatibility"`
|
|
|
|
Count int `json:"count"`
|
2015-03-25 11:17:12 +00:00
|
|
|
Sample float64 `json:"sample"`
|
2015-03-25 03:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type jsonRecord struct {
|
|
|
|
AccessCount int `json:"accessCount"`
|
2015-03-25 11:17:12 +00:00
|
|
|
ClosestStn string `json:"closestStn"`
|
2015-03-26 04:09:58 +00:00
|
|
|
Compatibility float64 `json:"compatibility"`
|
2015-03-25 11:17:12 +00:00
|
|
|
DistanceToStn float64 `json:"distanceToStn"`
|
|
|
|
DistanceToUser float64 `json:"distanceToUser"`
|
2015-03-25 03:00:54 +00:00
|
|
|
Id int `json:"id"`
|
2015-03-25 11:17:12 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Score float64 `json:"score"`
|
2015-03-26 03:18:43 +00:00
|
|
|
Url string `json:"url"`
|
2015-03-25 03:00:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-27 04:53:26 +00:00
|
|
|
type jsonBracket struct {
|
2015-06-27 03:48:44 +00:00
|
|
|
Min float64 `json:"min"`
|
|
|
|
Max float64 `json:"max"`
|
|
|
|
}
|
|
|
|
|
2015-03-25 03:00:54 +00:00
|
|
|
type jsonQueryResponse struct {
|
2015-06-28 04:30:22 +00:00
|
|
|
Columns map[string]jsonColumn `json:"columns"`
|
|
|
|
Count int `json:"count"`
|
|
|
|
MinScore float64 `json:"minScore"`
|
|
|
|
Records []jsonRecord `json:"records"`
|
2015-03-25 03:00:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 13:36:53 +00:00
|
|
|
type jsonCategory struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
Id int `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonAddCategoryRequest struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonAddCategoryResponse struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
Id int `json:"id"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonRemoveCategoryRequest struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonRemoveCategoryResponse struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
}
|
|
|
|
|
2015-03-24 11:04:52 +00:00
|
|
|
type queryContext struct {
|
2015-03-25 11:17:12 +00:00
|
|
|
geo *geoData
|
2015-03-24 11:04:52 +00:00
|
|
|
profile featureMap
|
2015-03-24 08:29:24 +00:00
|
|
|
walkingDist float64
|
|
|
|
}
|
|
|
|
|
2015-03-24 11:04:52 +00:00
|
|
|
type queryProjection struct {
|
2015-03-24 08:29:24 +00:00
|
|
|
compatibility float64
|
2015-03-24 06:17:37 +00:00
|
|
|
count int
|
2015-03-25 11:17:12 +00:00
|
|
|
sample float64
|
2015-03-24 06:17:37 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 11:17:12 +00:00
|
|
|
type geoData struct {
|
2015-03-24 08:58:35 +00:00
|
|
|
latitude float64
|
|
|
|
longitude float64
|
|
|
|
}
|
|
|
|
|
2015-03-24 11:04:52 +00:00
|
|
|
type record struct {
|
2015-03-24 06:17:37 +00:00
|
|
|
accessCount int
|
2015-03-25 03:00:54 +00:00
|
|
|
closestStn string
|
2015-03-24 08:29:24 +00:00
|
|
|
compatibility float64
|
|
|
|
distanceToStn float64
|
|
|
|
distanceToUser float64
|
2015-03-24 11:04:52 +00:00
|
|
|
features featureMap
|
2015-03-25 11:17:12 +00:00
|
|
|
geo geoData
|
2015-03-24 06:17:37 +00:00
|
|
|
id int
|
|
|
|
name string
|
2015-03-24 08:29:24 +00:00
|
|
|
score float64
|
2015-03-25 03:00:54 +00:00
|
|
|
url string
|
2015-03-24 06:17:37 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 11:04:52 +00:00
|
|
|
type records []record
|
2015-03-24 06:17:37 +00:00
|
|
|
|
2015-03-27 02:26:41 +00:00
|
|
|
type recordSorter struct {
|
|
|
|
ascending bool
|
|
|
|
entries records
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
2015-06-24 10:11:43 +00:00
|
|
|
func (s recordSorter) sort() {
|
|
|
|
if s.ascending {
|
|
|
|
sort.Sort(s)
|
2015-03-27 02:26:41 +00:00
|
|
|
} else {
|
2015-06-24 10:11:43 +00:00
|
|
|
sort.Sort(sort.Reverse(s))
|
2015-03-27 02:26:41 +00:00
|
|
|
}
|
2015-03-24 06:17:37 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 10:11:43 +00:00
|
|
|
func (s recordSorter) Len() int {
|
|
|
|
return len(s.entries)
|
2015-03-27 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 10:11:43 +00:00
|
|
|
func (s recordSorter) Less(i, j int) bool {
|
|
|
|
entry1 := s.entries[i]
|
|
|
|
entry2 := s.entries[j]
|
2015-03-27 02:26:41 +00:00
|
|
|
|
2015-06-24 10:11:43 +00:00
|
|
|
switch s.key {
|
2015-03-27 02:26:41 +00:00
|
|
|
case "accessCount":
|
|
|
|
return entry1.accessCount < entry2.accessCount
|
|
|
|
case "closestStn":
|
|
|
|
return entry1.closestStn < entry2.closestStn
|
|
|
|
case "compatibility":
|
|
|
|
return entry1.compatibility < entry2.compatibility
|
|
|
|
case "distanceToStn":
|
|
|
|
return entry1.distanceToStn < entry2.distanceToStn
|
|
|
|
case "distanceToUser":
|
|
|
|
return entry1.distanceToUser < entry2.distanceToUser
|
|
|
|
case "name":
|
|
|
|
return entry1.name < entry2.name
|
|
|
|
default:
|
|
|
|
return entry1.score < entry2.score
|
|
|
|
}
|
2015-03-24 06:17:37 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 10:11:43 +00:00
|
|
|
func (s recordSorter) Swap(i, j int) {
|
|
|
|
s.entries[i], s.entries[j] = s.entries[j], s.entries[i]
|
2015-03-24 06:17:37 +00:00
|
|
|
}
|
2015-06-29 04:06:26 +00:00
|
|
|
|
|
|
|
func (m modeType) String() string {
|
|
|
|
switch m {
|
|
|
|
case ModeTypeProduct:
|
|
|
|
return "prod"
|
|
|
|
case ModeTypeDist:
|
|
|
|
return "dist"
|
|
|
|
default:
|
|
|
|
return "invalid"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func strToModeType(mode string) modeType {
|
|
|
|
switch mode {
|
|
|
|
case "prod":
|
|
|
|
return ModeTypeProduct
|
|
|
|
case "dist":
|
|
|
|
return ModeTypeDist
|
|
|
|
default:
|
|
|
|
return ModeTypeNone
|
|
|
|
}
|
|
|
|
}
|