Prepare columns in go routines
This commit is contained in:
parent
8abbc17629
commit
7ed040573a
94
server.go
94
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,6 +39,51 @@ import (
|
|||||||
|
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
|
|
||||||
|
func prepareColumn(request jsonQueryRequest, entries, foundEntries records, features featureMap, modes modeMap, name string, value float64, columns chan jsonColumn) {
|
||||||
|
mode := modes[name]
|
||||||
|
|
||||||
|
column := jsonColumn{
|
||||||
|
Bracket: jsonBracket{Max: -1.0, Min: 1.0},
|
||||||
|
Mode: mode.String(),
|
||||||
|
Steps: request.Resolution,
|
||||||
|
Value: value,
|
||||||
|
name: name}
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
|
columns <- column
|
||||||
|
}
|
||||||
|
|
||||||
func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
||||||
var request jsonQueryRequest
|
var request jsonQueryRequest
|
||||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||||
@ -64,48 +110,14 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
|
|||||||
MinScore: request.MinScore,
|
MinScore: request.MinScore,
|
||||||
Records: make([]jsonRecord, 0)}
|
Records: make([]jsonRecord, 0)}
|
||||||
|
|
||||||
|
columns := make(chan jsonColumn, len(features))
|
||||||
for name, value := range features {
|
for name, value := range features {
|
||||||
mode, _ := modes[name]
|
go prepareColumn(request, entries, foundEntries, features, modes, name, value, columns)
|
||||||
|
}
|
||||||
|
|
||||||
column := jsonColumn{
|
for i := 0; i < len(features); i++ {
|
||||||
Bracket: jsonBracket{Max: -1.0, Min: 1.0},
|
column := <-columns
|
||||||
Mode: mode.String(),
|
response.Columns[column.name] = column
|
||||||
Steps: request.Resolution,
|
|
||||||
Value: value}
|
|
||||||
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
|
|
||||||
response.Columns[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