1

Simplification

This commit is contained in:
Alex Yatskov 2015-07-31 14:59:36 +09:00
parent 9b771dc68e
commit 648d6a192d
2 changed files with 17 additions and 19 deletions

View File

@ -32,6 +32,7 @@ import (
"net/http" "net/http"
"runtime" "runtime"
"strings" "strings"
"sync"
"time" "time"
"github.com/GaryBoone/GoStats/stats" "github.com/GaryBoone/GoStats/stats"
@ -40,15 +41,14 @@ 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) { func prepareColumn(request jsonQueryRequest, entries, foundEntries records, features featureMap, modes modeMap, name string, column *jsonColumn, wg *sync.WaitGroup) {
mode := modes[name] 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: features[name]}
name: name}
hints := project( hints := project(
entries, entries,
@ -82,7 +82,7 @@ func prepareColumn(request jsonQueryRequest, entries, foundEntries records, feat
column.Bracket.Min = math.Max(mean-dev, d.Min()) column.Bracket.Min = math.Max(mean-dev, d.Min())
} }
columns <- column wg.Done()
} }
func executeQuery(rw http.ResponseWriter, req *http.Request) { func executeQuery(rw http.ResponseWriter, req *http.Request) {
@ -108,19 +108,18 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) {
sorter.sort() sorter.sort()
response := jsonQueryResponse{ response := jsonQueryResponse{
Columns: make(map[string]jsonColumn), Columns: make(map[string]*jsonColumn),
Count: len(foundEntries), Count: len(foundEntries),
MinScore: request.MinScore, MinScore: request.MinScore,
Records: make([]jsonRecord, 0)} Records: make([]jsonRecord, 0)}
columns := make(chan jsonColumn, len(features)) var wg sync.WaitGroup
for name, value := range features { wg.Add(len(features))
go prepareColumn(request, entries, foundEntries, features, modes, name, value, columns)
}
for i := 0; i < len(features); i++ { for name := range features {
column := <-columns column := &jsonColumn{}
response.Columns[column.name] = column prepareColumn(request, entries, foundEntries, features, modes, name, column, &wg)
response.Columns[name] = column
} }
for index, record := range foundEntries { for index, record := range foundEntries {

View File

@ -66,7 +66,6 @@ type jsonColumn struct {
Mode string `json:"mode"` Mode string `json:"mode"`
Steps int `json:"steps"` Steps int `json:"steps"`
Value float64 `json:"value"` Value float64 `json:"value"`
name string
} }
type jsonProjection struct { type jsonProjection struct {
@ -93,11 +92,11 @@ type jsonBracket struct {
} }
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"`
MinScore float64 `json:"minScore"` MinScore float64 `json:"minScore"`
Records []jsonRecord `json:"records"` Records []jsonRecord `json:"records"`
ElapsedTime int64 `json:"elapsedTime"` ElapsedTime int64 `json:"elapsedTime"`
} }
type jsonCategory struct { type jsonCategory struct {