Commit 7239b144 authored by dagal's avatar dagal

sql implementation, hard part

parent f491826f
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
digest = "1:a4b5a6b32f33323c7850fae4ec085a0fbac3be93c057b6b8cef5bcabe24645ac"
name = "github.com/gin-contrib/sse"
packages = ["."]
pruneopts = "UT"
revision = "5545eab6dad3bbbd6c5ae9186383c2a9d23c0dae"
[[projects]]
digest = "1:d8bd2a337f6ff2188e08f72c614f2f3f0fd48e6a7b37a071b197e427d77d3a47"
name = "github.com/gin-gonic/gin"
packages = [
".",
"binding",
"internal/json",
"render",
]
pruneopts = "UT"
revision = "b75d67cd51eb53c3c3a2fc406524c940021ffbda"
version = "v1.4.0"
[[projects]]
digest = "1:318f1c959a8a740366fce4b1e1eb2fd914036b4af58fbd0a003349b305f118ad"
name = "github.com/golang/protobuf"
packages = ["proto"]
pruneopts = "UT"
revision = "c823c79ea1570fb5ff454033735a8e68575d1d0f"
version = "v1.3.0"
[[projects]]
digest = "1:27f44b9a8c2995c35ec25a9948ffa96f997f8eedbc5bb34838f10dcf620aaee3"
name = "github.com/jinzhu/gorm"
packages = [
".",
"dialects/sqlite",
]
pruneopts = "UT"
revision = "836fb2c19d84dac7b0272958dfb9af7cf0d0ade4"
version = "v1.9.10"
[[projects]]
digest = "1:01ed62f8f4f574d8aff1d88caee113700a2b44c42351943fa73cc1808f736a50"
name = "github.com/jinzhu/inflection"
packages = ["."]
pruneopts = "UT"
revision = "f5c5f50e6090ae76a29240b61ae2a90dd810112e"
version = "v1.0.0"
[[projects]]
digest = "1:f5a2051c55d05548d2d4fd23d244027b59fbd943217df8aa3b5e170ac2fd6e1b"
name = "github.com/json-iterator/go"
packages = ["."]
pruneopts = "UT"
revision = "0ff49de124c6f76f8494e194af75bde0f1a49a29"
version = "v1.1.6"
[[projects]]
digest = "1:e150b5fafbd7607e2d638e4e5cf43aa4100124e5593385147b0a74e2733d8b0d"
name = "github.com/mattn/go-isatty"
packages = ["."]
pruneopts = "UT"
revision = "c2a7a6ca930a4cd0bc33a3f298eb71960732a3a7"
version = "v0.0.7"
[[projects]]
digest = "1:79e87abf06b873987dee86598950f5b51732ac454d5a5cab6445a14330e6c9e3"
name = "github.com/mattn/go-sqlite3"
packages = ["."]
pruneopts = "UT"
revision = "b612a2feea6aa87c6d052d9086572551df06497e"
version = "v1.11.0"
[[projects]]
digest = "1:33422d238f147d247752996a26574ac48dcf472976eda7f5134015f06bf16563"
name = "github.com/modern-go/concurrent"
packages = ["."]
pruneopts = "UT"
revision = "bacd9c7ef1dd9b15be4a9909b8ac7a4e313eec94"
version = "1.0.3"
[[projects]]
digest = "1:e32bdbdb7c377a07a9a46378290059822efdce5c8d96fe71940d87cb4f918855"
name = "github.com/modern-go/reflect2"
packages = ["."]
pruneopts = "UT"
revision = "4b7aa43c6742a2c18fdef89dd197aaae7dac7ccd"
version = "1.0.1"
[[projects]]
digest = "1:36cfc0b6facbc68c92a4d55f32eff22b872a899ab75c384ac24a08dd33bc1380"
name = "github.com/paulmach/go.geojson"
packages = ["."]
pruneopts = "UT"
revision = "a5c451da2d9c67c590ae6be92565b67698286a1a"
version = "v1.4"
[[projects]]
digest = "1:d0072748c62defde1ad99dde77f6ffce492a0e5aea9204077e497c7edfb86653"
name = "github.com/ugorji/go"
packages = ["codec"]
pruneopts = "UT"
revision = "2adff0894ba3bc2eeb9f9aea45fefd49802e1a13"
version = "v1.1.4"
[[projects]]
branch = "master"
digest = "1:1d1072628733c7eee71145fb18c925f92c498c670657697694375914f9b95ec2"
name = "golang.org/x/sys"
packages = ["unix"]
pruneopts = "UT"
revision = "2aa67d56cdd77167a5fb51358e8c3ce04bad68bb"
[[projects]]
digest = "1:cbc72c4c4886a918d6ab4b95e347ffe259846260f99ebdd8a198c2331cf2b2e9"
name = "gopkg.in/go-playground/validator.v8"
packages = ["."]
pruneopts = "UT"
revision = "5f1438d3fca68893a817e4a66806cea46a9e4ebf"
version = "v8.18.2"
[[projects]]
digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96"
name = "gopkg.in/yaml.v2"
packages = ["."]
pruneopts = "UT"
revision = "51d6538a90f86fe93ac480b35f37b2be17fef232"
version = "v2.2.2"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = [
"github.com/gin-gonic/gin",
"github.com/jinzhu/gorm",
"github.com/jinzhu/gorm/dialects/sqlite",
"github.com/paulmach/go.geojson",
]
solver-name = "gps-cdcl"
solver-version = 1
......@@ -5,6 +5,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/paulmach/go.geojson"
"time"
"log"
)
func returnGeoJSON(c *gin.Context, data []byte) {
......@@ -106,6 +107,10 @@ func GetStopStats(c *gin.Context) {
"switch": boardCTR - startCTR,
"board": boardCTR,
})
log.Println("wasn't that fun")
s := stops[id]
s.GetStopStatsDB(fromTime, toTime)
}
func GetXY(c *gin.Context) {
......
......@@ -13,13 +13,14 @@ var stops = map[uint32]Stop{}
var lines = map[string]Line{}
var start_time = time.Now()
var end_time = time.Now()
var db *gorm.DB
func main() {
var gtfsFlags ArrayFlags
flag.Var(&gtfsFlags, "gtfs", "path to GTFS")
flag.Parse()
db := connectDB()
db = connectDB()
defer db.Close()
//db.LogMode(true)
db.SingularTable(true)
......
......@@ -3,6 +3,7 @@ package main
import (
"github.com/paulmach/go.geojson"
"time"
"log"
)
type Stop struct {
......@@ -55,3 +56,65 @@ 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment