Prepare columns in go routines
This commit is contained in:
parent
8abbc17629
commit
7ed040573a
74
server.go
74
server.go
@ -30,6 +30,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/GaryBoone/GoStats/stats"
|
"github.com/GaryBoone/GoStats/stats"
|
||||||
@ -38,40 +39,15 @@ import (
|
|||||||
|
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
|
|
||||||
func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
func prepareColumn(request jsonQueryRequest, entries, foundEntries records, features featureMap, modes modeMap, name string, value float64, columns chan jsonColumn) {
|
||||||
var request jsonQueryRequest
|
mode := modes[name]
|
||||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
||||||
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var geo *geoData
|
|
||||||
if request.Geo != nil {
|
|
||||||
geo = &geoData{request.Geo.Latitude, request.Geo.Longitude}
|
|
||||||
}
|
|
||||||
|
|
||||||
entries := getRecords(queryContext{geo, request.Profile, request.WalkingDist})
|
|
||||||
features := fixFeatures(request.Features)
|
|
||||||
modes := fixModes(request.Modes)
|
|
||||||
|
|
||||||
foundEntries := findRecords(entries, features, modes, request.MinScore)
|
|
||||||
sorter := recordSorter{entries: foundEntries, key: request.SortKey, ascending: request.SortAsc}
|
|
||||||
sorter.sort()
|
|
||||||
|
|
||||||
response := jsonQueryResponse{
|
|
||||||
Columns: make(map[string]jsonColumn),
|
|
||||||
Count: len(foundEntries),
|
|
||||||
MinScore: request.MinScore,
|
|
||||||
Records: make([]jsonRecord, 0)}
|
|
||||||
|
|
||||||
for name, value := range features {
|
|
||||||
mode, _ := modes[name]
|
|
||||||
|
|
||||||
column := jsonColumn{
|
column := jsonColumn{
|
||||||
Bracket: jsonBracket{Max: -1.0, Min: 1.0},
|
Bracket: jsonBracket{Max: -1.0, Min: 1.0},
|
||||||
Mode: mode.String(),
|
Mode: mode.String(),
|
||||||
Steps: request.Resolution,
|
Steps: request.Resolution,
|
||||||
Value: value}
|
Value: value,
|
||||||
|
name: name}
|
||||||
|
|
||||||
hints := project(
|
hints := project(
|
||||||
entries,
|
entries,
|
||||||
@ -105,7 +81,43 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
|||||||
column.Bracket.Min = math.Max(mean-dev, d.Min())
|
column.Bracket.Min = math.Max(mean-dev, d.Min())
|
||||||
}
|
}
|
||||||
|
|
||||||
response.Columns[name] = column
|
columns <- column
|
||||||
|
}
|
||||||
|
|
||||||
|
func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
var request jsonQueryRequest
|
||||||
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||||
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var geo *geoData
|
||||||
|
if request.Geo != nil {
|
||||||
|
geo = &geoData{request.Geo.Latitude, request.Geo.Longitude}
|
||||||
|
}
|
||||||
|
|
||||||
|
entries := getRecords(queryContext{geo, request.Profile, request.WalkingDist})
|
||||||
|
features := fixFeatures(request.Features)
|
||||||
|
modes := fixModes(request.Modes)
|
||||||
|
|
||||||
|
foundEntries := findRecords(entries, features, modes, request.MinScore)
|
||||||
|
sorter := recordSorter{entries: foundEntries, key: request.SortKey, ascending: request.SortAsc}
|
||||||
|
sorter.sort()
|
||||||
|
|
||||||
|
response := jsonQueryResponse{
|
||||||
|
Columns: make(map[string]jsonColumn),
|
||||||
|
Count: len(foundEntries),
|
||||||
|
MinScore: request.MinScore,
|
||||||
|
Records: make([]jsonRecord, 0)}
|
||||||
|
|
||||||
|
columns := make(chan jsonColumn, len(features))
|
||||||
|
for name, value := range features {
|
||||||
|
go prepareColumn(request, entries, foundEntries, features, modes, name, value, columns)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(features); i++ {
|
||||||
|
column := <-columns
|
||||||
|
response.Columns[column.name] = column
|
||||||
}
|
}
|
||||||
|
|
||||||
for index, record := range foundEntries {
|
for index, record := range foundEntries {
|
||||||
@ -288,6 +300,8 @@ func clearHistory(rw http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
|
||||||
staticDir := flag.String("static", "static", "path to static files")
|
staticDir := flag.String("static", "static", "path to static files")
|
||||||
portNum := flag.Int("port", 8080, "port to serve content on")
|
portNum := flag.Int("port", 8080, "port to serve content on")
|
||||||
dataSrc := flag.String("data", "hscd@/hscd", "data source for database")
|
dataSrc := flag.String("data", "hscd@/hscd", "data source for database")
|
||||||
|
Loading…
Reference in New Issue
Block a user