Reimplement sort by line direction

This commit is contained in:
nemunaire 2022-10-23 10:59:56 +02:00
parent a0659dd9bd
commit da0187b74d
2 changed files with 57 additions and 16 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"time"
@ -14,24 +15,26 @@ import (
const IDFM_BASEURL = "https://api-iv.iledefrance-mobilites.fr/"
type IDFMRealTimeData struct {
LineId string `json:"lineId"`
ShortName string `json:"shortName"`
VehicleName string `json:"vehicleName,omitempty"`
LineDirection string `json:"lineDirection"`
Sens string `json:"sens,omitempty"`
Code string `json:"code,omitempty"`
Time string `json:"time"`
Schedule string `json:"schedule"`
Destination struct {
StopPointId string `json:"stopPointId"`
StopAreaId string `json:"stopAreaId"`
} `json:"destination,omitempty"`
Source string `json:"source,omitempty"`
}
type IDFMRealTime struct {
NextDepartures struct {
StatusCode int `json:"statusCode"`
Data []struct {
LineId string `json:"lineId"`
ShortName string `json:"shortName"`
VehicleName string `json:"vehicleName,omitempty"`
LineDirection string `json:"lineDirection"`
Sens string `json:"sens,omitempty"`
Code string `json:"code,omitempty"`
Time string `json:"time"`
Schedule string `json:"schedule"`
Destination struct {
StopPointId string `json:"stopPointId"`
StopAreaId string `json:"stopAreaId"`
} `json:"destination,omitempty"`
Source string `json:"source,omitempty"`
} `json:"data"`
StatusCode int `json:"statusCode"`
Data []IDFMRealTimeData `json:"data"`
} `json:"nextDepartures"`
CrowdsourcingReports struct {
congestions []struct {
@ -43,6 +46,34 @@ type IDFMRealTime struct {
} `json:"crowdsourcingReports"`
}
type ByRealTime []IDFMRealTimeData
func (s ByRealTime) Len() int {
return len(s)
}
func (s ByRealTime) Less(i, j int) bool {
if s[i].Sens == s[j].Sens {
nj, err := strconv.Atoi(s[j].Time)
if err != nil {
return false
}
ni, err := strconv.Atoi(s[i].Time)
if err != nil {
return true
}
return ni < nj
} else {
return s[i].Sens < s[j].Sens
}
}
func (s ByRealTime) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
type PGSchedule struct {
Destination string `json:"destination"`
Message string `json:"message"`
@ -129,6 +160,8 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
return
}
sort.Sort(ByRealTime(schedules.NextDepartures.Data))
pgs := []PGSchedule{}
for _, vehicule := range schedules.NextDepartures.Data {
if (way == "A" && vehicule.Sens == "-1") || (way == "R" && vehicule.Sens == "1") {

View File

@ -15,3 +15,11 @@ func (t *IDFMTime) UnmarshalJSON(b []byte) error {
func (t IDFMTime) MarshalJSON() ([]byte, error) {
return []byte(time.Time(t).Format("\"2006-01-02T15:04\"")), nil
}
func (t *IDFMTime) After(d IDFMTime) bool {
return time.Time(*t).After(time.Time(d))
}
func (t IDFMTime) String() string {
return time.Time(t).String()
}