...
 
Commits (2)
package main
import (
"time"
"log"
)
func BenchAll() {
backends := map[string]queryBackend{"ctr": ctrDummy, "db": dbDummy}
log.Printf("Bench StopStats over %d stops", len(stops))
for name, b := range backends {
usec := benchStopStats(b)
log.Printf("StopStats %s: %d usec ( %.2f usec/query )", name, usec, float64(usec)/float64(len(stops)))
}
}
func benchStopStats(b queryBackend) int64 {
benchStart := time.Now()
var measuredTime int64
for _, s := range stops {
benchStart = time.Now()
b.GetStopStats(s,0,0)
measuredTime += time.Since(benchStart).Microseconds()
}
return measuredTime
}
\ No newline at end of file
......@@ -5,7 +5,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/paulmach/go.geojson"
"time"
"log"
)
func returnGeoJSON(c *gin.Context, data []byte) {
......@@ -96,10 +95,13 @@ func GetStopStats(c *gin.Context) {
id := Atoi32(c.Param("id"), c)
fromTime := Atoi32(c.DefaultQuery("from_time", "0"), c)
toTime := Atoi32(c.DefaultQuery("to_time", "0"), c)
s := stops[id]
stats := ctrDummy.GetStopStats(s, fromTime, toTime)
startCTR := CTRStart(id, fromTime, toTime)
endCTR := CTREnd(id, fromTime, toTime)
boardCTR := CTRBoard(id, fromTime, toTime)
startCTR, _ := stats.Properties["start"].(uint64)
endCTR, _ := stats.Properties["end"].(uint64)
boardCTR, _ := stats.Properties["board"].(uint64)
c.JSON(200, gin.H{
"start": startCTR,
......@@ -108,9 +110,7 @@ func GetStopStats(c *gin.Context) {
"board": boardCTR,
})
log.Println("wasn't that fun")
s := stops[id]
s.GetStopStatsDB(fromTime, toTime)
//dbDummy.GetStopStats(s, fromTime, toTime)
}
func GetXY(c *gin.Context) {
......
......@@ -47,6 +47,9 @@ func main() {
router.GET("/stop_stats/:id", GetStopStats)
router.GET("/xy", GetXY)
router.POST("/xy_area", GetXYArea)
BenchAll()
err := router.Run(":8088")
LogAndQuit(err)
}
......
......@@ -3,7 +3,6 @@ package main
import (
"github.com/paulmach/go.geojson"
"time"
"log"
)
type Stop struct {
......@@ -56,65 +55,3 @@ func (l Line) ToGeoJSON() *geojson.Feature {
f.Properties["stops"] = stopIds
return f
}
func (s Stop) GetStopStatsDB(fromTime uint32, toTime uint32) *geojson.Feature {
f := s.ToGeoJSON()
rows, err := db.Raw("SELECT line_id FROM line_stop WHERE stop_id = ?", s.ID).Rows()
LogAndPanic(err)
lines := make([]string, 0)
var line_id string
var board, start, end uint32
defer rows.Close()
for rows.Next() {
rows.Scan(&line_id)
lines = append(lines, line_id)
}
log.Println(lines)
for _, l := range lines {
// FIXME missing a join with line_stops for avg time!
var fromJourney, toJourney, lboard, lstart, lend uint32
rows, err = db.Raw("SELECT min(journey_id) FROM journey WHERE line_id = ? and start_time >= to_timestamp(?) at time zone 'UTC'", l, fromTime).Rows()
LogAndPanic(err)
rows.Next()
rows.Scan(&fromJourney)
rows, err = db.Raw("SELECT max(journey_id) FROM journey WHERE line_id = ? and start_time < to_timestamp(?) at time zone 'UTC'", l, toTime).Rows()
LogAndPanic(err)
rows.Next()
rows.Scan(&toJourney)
rows, err = db.Raw("select count(1) from stage s where board_stop = ? and line_id = ? and journey_id between ? and ?", s.ID, l, fromJourney, toJourney).Rows()
LogAndPanic(err)
rows.Next()
rows.Scan(&lboard)
rows, err = db.Raw("select count(1) from stage s where board_stop = ? and line_id = ? and journey_id between ? and ? " +
" and num_stage = 0", s.ID, l, fromJourney, toJourney).Rows()
LogAndPanic(err)
rows.Next()
rows.Scan(&lstart)
rows, err = db.Raw("select count(1) from stage s where alight_stop = ? and line_id = ? and journey_id between ? and ? " +
" and num_stage = (select max(num_stage) from stage where trip_id = s.trip_id)", s.ID, l, fromJourney, toJourney).Rows()
LogAndPanic(err)
rows.Next()
rows.Scan(&lend)
board += lboard
start += lstart
end += lend
}
log.Println(board, start, end)
f.Properties["start"] = start
f.Properties["board"] = board
f.Properties["end"] = end
f.Properties["switch"] = board-start
return f
}
\ No newline at end of file
package main
import (
"github.com/paulmach/go.geojson"
)
type queryBackend interface {
GetStopStats(s Stop, fromTime uint32, toTime uint32) *geojson.Feature
}
type tdb struct {}
type tctr struct {}
var ctrDummy tctr = tctr{}
var dbDummy tdb = tdb{}
func (c tctr) GetStopStats(s Stop, fromTime uint32, toTime uint32) *geojson.Feature {
f := s.ToGeoJSON()
start := CTRStart(s.Ctr_ID, fromTime, toTime)
end := CTREnd(s.Ctr_ID, fromTime, toTime)
board := CTRBoard(s.Ctr_ID, fromTime, toTime)
f.Properties["start"] = start
f.Properties["board"] = board
f.Properties["end"] = end
f.Properties["switch"] = board-start
return f
}
func (d tdb) GetStopStats(s Stop, fromTime uint32, toTime uint32) *geojson.Feature {
f := s.ToGeoJSON()
rows, err := db.Raw("SELECT line_id FROM line_stop WHERE stop_id = ?", s.ID).Rows()
LogAndPanic(err)
defer rows.Close()
lines := make([]string, 0)
var line_id string
var board, start, end uint32
for rows.Next() {
rows.Scan(&line_id)
lines = append(lines, line_id)
}
for _, l := range lines {
// FIXME missing a join with line_stops for avg time!
var fromJourney, toJourney, lboard, lstart, lend uint32
if fromTime == 0 && toTime == 0 {
rows, err = db.Raw("SELECT min(journey_id) FROM journey WHERE line_id = ? ", l).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&fromJourney)
rows, err = db.Raw("SELECT max(journey_id) FROM journey WHERE line_id = ? ", l).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&toJourney)
} else {
rows, err = db.Raw("SELECT min(journey_id) FROM journey WHERE line_id = ? and start_time >= to_timestamp(?) at time zone 'UTC'", l, fromTime).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&fromJourney)
rows, err = db.Raw("SELECT max(journey_id) FROM journey WHERE line_id = ? and start_time < to_timestamp(?) at time zone 'UTC'", l, toTime).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&toJourney)
}
rows, err = db.Raw("select count(1) from stage s where board_stop = ? and line_id = ? and journey_id between ? and ?", s.ID, l, fromJourney, toJourney).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&lboard)
rows, err = db.Raw("select count(1) from stage s where board_stop = ? and line_id = ? and journey_id between ? and ? " +
" and num_stage = 0", s.ID, l, fromJourney, toJourney).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&lstart)
rows, err = db.Raw("select count(1) from stage s where alight_stop = ? and line_id = ? and journey_id between ? and ? " +
" and num_stage = (select max(num_stage) from stage where trip_id = s.trip_id)", s.ID, l, fromJourney, toJourney).Rows()
LogAndPanic(err)
defer rows.Close()
rows.Next()
rows.Scan(&lend)
board += lboard
start += lstart
end += lend
}
f.Properties["start"] = start
f.Properties["board"] = board
f.Properties["end"] = end
f.Properties["switch"] = board-start
return f
}
\ No newline at end of file
package main
import (
"github.com/jinzhu/gorm"
"github.com/gin-gonic/gin"
"log"
"database/sql"
"strconv"
"strings"
)
......@@ -52,3 +54,9 @@ func Atoi32(s string, c *gin.Context) uint32 {
LogAndPanic2(err, c)
return uint32(i)
}
func SQLPanic(db *gorm.DB, query string, values... interface{}) *sql.Rows {
rows, err := db.Raw(query, values...).Rows()
LogAndPanic(err)
return rows
}
\ No newline at end of file