1
restaurant-search/server.go

330 lines
8.3 KiB
Go
Raw Normal View History

/*
* 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
import (
"database/sql"
"encoding/json"
2015-06-24 10:11:43 +00:00
"flag"
2015-03-26 04:20:34 +00:00
"fmt"
"log"
2015-06-27 03:48:44 +00:00
"math"
"net/http"
2015-07-31 03:50:24 +00:00
"runtime"
2015-03-24 03:45:18 +00:00
"strings"
2015-07-31 05:59:36 +00:00
"sync"
2015-07-31 04:13:31 +00:00
"time"
2015-06-24 10:11:43 +00:00
2015-06-28 07:54:22 +00:00
"github.com/GaryBoone/GoStats/stats"
2015-06-24 10:11:43 +00:00
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
2015-07-31 05:59:36 +00:00
func prepareColumn(request jsonQueryRequest, entries, foundEntries records, features featureMap, modes modeMap, name string, column *jsonColumn, wg *sync.WaitGroup) {
2015-07-31 03:50:24 +00:00
mode := modes[name]
2015-07-31 05:59:36 +00:00
*column = jsonColumn{
2015-07-31 03:50:24 +00:00
Bracket: jsonBracket{Max: -1.0, Min: 1.0},
Mode: mode.String(),
Steps: request.Resolution,
2015-07-31 05:59:36 +00:00
Value: features[name]}
2015-07-31 03:50:24 +00:00
hints := project(
entries,
features,
modes,
name,
request.MinScore,
request.Resolution)
for _, hint := range hints {
jsonHint := jsonProjection{hint.compatibility, hint.count, hint.sample}
column.Hints = append(column.Hints, jsonHint)
}
var d stats.Stats
for _, record := range foundEntries {
if feature, ok := record.features[name]; ok {
d.Update(feature)
}
}
if d.Count() > 0 {
var dev float64
if d.Count() > 1 {
dev = d.SampleStandardDeviation() * 3
}
mean := d.Mean()
column.Bracket.Max = math.Min(mean+dev, d.Max())
column.Bracket.Min = math.Max(mean-dev, d.Min())
}
2015-07-31 05:59:36 +00:00
wg.Done()
2015-07-31 03:50:24 +00:00
}
func executeQuery(rw http.ResponseWriter, req *http.Request) {
2015-07-31 04:13:31 +00:00
startTime := time.Now()
2015-03-25 03:00:54 +00:00
var request jsonQueryRequest
2015-03-24 11:52:40 +00:00
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
2015-03-25 11:17:12 +00:00
var geo *geoData
2015-03-24 13:55:25 +00:00
if request.Geo != nil {
2015-03-25 11:17:12 +00:00
geo = &geoData{request.Geo.Latitude, request.Geo.Longitude}
2015-03-24 13:55:25 +00:00
}
2015-03-25 09:41:19 +00:00
entries := getRecords(queryContext{geo, request.Profile, request.WalkingDist})
2015-03-25 04:12:22 +00:00
features := fixFeatures(request.Features)
2015-06-29 04:06:26 +00:00
modes := fixModes(request.Modes)
2015-06-29 04:06:26 +00:00
foundEntries := findRecords(entries, features, modes, request.MinScore)
2015-03-27 03:03:23 +00:00
sorter := recordSorter{entries: foundEntries, key: request.SortKey, ascending: request.SortAsc}
sorter.sort()
2015-03-25 03:33:41 +00:00
2015-03-25 04:23:06 +00:00
response := jsonQueryResponse{
2015-07-31 05:59:36 +00:00
Columns: make(map[string]*jsonColumn),
2015-06-29 10:09:34 +00:00
Count: len(foundEntries),
2015-06-29 04:06:26 +00:00
MinScore: request.MinScore,
2015-06-28 04:30:22 +00:00
Records: make([]jsonRecord, 0)}
2015-03-25 04:23:06 +00:00
2015-07-31 05:59:36 +00:00
var wg sync.WaitGroup
wg.Add(len(features))
2015-06-28 07:54:22 +00:00
2015-07-31 05:59:36 +00:00
for name := range features {
column := &jsonColumn{}
prepareColumn(request, entries, foundEntries, features, modes, name, column, &wg)
response.Columns[name] = column
2015-03-25 03:33:41 +00:00
}
2015-06-27 03:48:44 +00:00
for index, record := range foundEntries {
2015-03-25 10:57:42 +00:00
if index >= request.MaxResults {
2015-03-25 03:33:41 +00:00
break
}
2015-03-25 09:41:19 +00:00
item := jsonRecord{
2015-06-27 03:48:44 +00:00
Name: record.name,
Url: record.url,
Score: record.score,
Compatibility: record.compatibility,
DistanceToUser: record.distanceToUser,
DistanceToStn: record.distanceToStn,
ClosestStn: record.closestStn,
AccessCount: record.accessCount,
Id: record.id}
2015-03-25 03:33:41 +00:00
2015-03-25 11:17:12 +00:00
response.Records = append(response.Records, item)
2015-03-25 03:33:41 +00:00
}
2015-07-31 04:13:31 +00:00
response.ElapsedTime = time.Since(startTime).Nanoseconds()
2015-03-25 03:33:41 +00:00
js, err := json.Marshal(response)
if err != nil {
log.Fatal(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func getCategories(rw http.ResponseWriter, req *http.Request) {
2015-03-25 10:25:14 +00:00
categoryRows, err := db.Query("SELECT description, id FROM categories")
if err != nil {
log.Fatal(err)
}
2015-03-25 10:25:14 +00:00
defer categoryRows.Close()
2015-03-24 11:59:56 +00:00
var categories []jsonCategory
2015-03-25 10:25:14 +00:00
for categoryRows.Next() {
var (
description string
id int
)
2015-03-25 10:25:14 +00:00
if err := categoryRows.Scan(&description, &id); err != nil {
log.Fatal(err)
}
2015-03-24 11:59:56 +00:00
categories = append(categories, jsonCategory{description, id})
}
2015-03-25 10:25:14 +00:00
if err := categoryRows.Err(); err != nil {
log.Fatal(err)
}
js, err := json.Marshal(categories)
if err != nil {
log.Fatal(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func addCategory(rw http.ResponseWriter, req *http.Request) {
2015-03-24 13:34:06 +00:00
var request jsonAddCategoryRequest
2015-03-24 03:22:21 +00:00
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
2015-03-24 13:34:06 +00:00
response := jsonAddCategoryResponse{Description: strings.TrimSpace(request.Description)}
2015-03-24 03:22:21 +00:00
if len(request.Description) > 0 {
result, err := db.Exec("INSERT INTO categories(description) VALUES(?)", request.Description)
if err != nil {
log.Fatal(err)
2015-03-24 03:22:21 +00:00
}
insertId, err := result.LastInsertId()
if err != nil {
log.Fatal(err)
2015-03-24 03:22:21 +00:00
}
affectedRows, err := result.RowsAffected()
if err != nil {
log.Fatal(err)
2015-03-24 03:22:21 +00:00
}
2015-03-24 03:22:21 +00:00
response.Success = affectedRows > 0
response.Id = int(insertId)
}
js, err := json.Marshal(response)
if err != nil {
log.Fatal(err)
2015-03-24 03:22:21 +00:00
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func removeCategory(rw http.ResponseWriter, req *http.Request) {
2015-03-24 13:34:06 +00:00
var request jsonRemoveCategoryRequest
2015-03-24 02:58:00 +00:00
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
2015-04-18 05:23:35 +00:00
_, err := db.Exec("DELETE FROM categories WHERE id = (?)", request.Id)
js, err := json.Marshal(jsonRemoveCategoryResponse{err == nil})
if err != nil {
log.Fatal(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.Write(js)
}
func accessReview(rw http.ResponseWriter, req *http.Request) {
2015-03-25 10:10:34 +00:00
var request jsonAccessRequest
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
2015-03-26 03:18:43 +00:00
reviewsResult, err := db.Exec("UPDATE reviews SET accessCount = accessCount + 1 WHERE id = (?)", request.Id)
if err != nil {
log.Fatal(err)
}
2015-03-25 10:10:34 +00:00
2015-03-26 03:18:43 +00:00
rowsAffected, err := reviewsResult.RowsAffected()
if err != nil {
log.Fatal(err)
2015-03-25 10:10:34 +00:00
}
2015-03-26 03:18:43 +00:00
if rowsAffected == 0 || len(request.Profile) == 0 {
return
}
2015-03-25 10:10:34 +00:00
2015-03-26 03:18:43 +00:00
historyResult, err := db.Exec("INSERT INTO history(date, reviewId) VALUES(NOW(), ?)", request.Id)
if err != nil {
log.Fatal(err)
2015-03-25 10:10:34 +00:00
}
2015-03-26 03:18:43 +00:00
insertId, err := historyResult.LastInsertId()
2015-03-25 10:10:34 +00:00
if err != nil {
log.Fatal(err)
}
2015-03-26 03:18:43 +00:00
for id, value := range request.Profile {
2015-04-18 05:23:35 +00:00
catRow := db.QueryRow("SELECT EXISTS(SELECT NULL FROM categories WHERE id = ?)", id)
var catExists int
if err := catRow.Scan(&catExists); err != nil {
log.Fatal(err)
}
if catExists == 0 {
continue
}
2015-03-26 03:18:43 +00:00
if _, err := db.Exec("INSERT INTO historyGroups(categoryId, categoryValue, historyId) VALUES(?, ?, ?)", id, value, insertId); err != nil {
log.Fatal(err)
}
}
}
2015-03-26 04:20:34 +00:00
func clearHistory(rw http.ResponseWriter, req *http.Request) {
2015-03-27 04:13:37 +00:00
if _, err := db.Exec("DELETE FROM historyGroups"); err != nil {
2015-03-26 04:20:34 +00:00
log.Fatal(err)
}
2015-03-27 04:13:37 +00:00
if _, err := db.Exec("DELETE FROM history"); err != nil {
2015-03-26 04:20:34 +00:00
log.Fatal(err)
}
rw.Header().Set("Content-Type", "text/plain")
fmt.Fprint(rw, "History tables cleared")
}
func main() {
2015-07-31 03:50:24 +00:00
runtime.GOMAXPROCS(runtime.NumCPU())
2015-06-24 10:11:43 +00:00
staticDir := flag.String("static", "static", "path to static files")
portNum := flag.Int("port", 8080, "port to serve content on")
dataSrc := flag.String("data", "hscd@/hscd", "data source for database")
flag.Parse()
2015-06-24 10:11:43 +00:00
var err error
2015-06-29 10:09:34 +00:00
if db, err = sql.Open("mysql", *dataSrc); err != nil {
log.Fatal(err)
}
defer db.Close()
http.HandleFunc("/query", executeQuery)
http.HandleFunc("/categories", getCategories)
http.HandleFunc("/learn", addCategory)
http.HandleFunc("/forget", removeCategory)
http.HandleFunc("/access", accessReview)
2015-03-26 04:20:34 +00:00
http.HandleFunc("/clear", clearHistory)
2015-06-24 10:11:43 +00:00
http.Handle("/", http.FileServer(http.Dir(*staticDir)))
2015-06-24 10:11:43 +00:00
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *portNum), nil))
}