go routines /handlers/application/journeys

This commit is contained in:
root 2023-04-20 15:28:27 +03:00
parent 46a72e1aef
commit c34d08ff70
1 changed files with 87 additions and 70 deletions

View File

@ -27,7 +27,14 @@ var Arrive any
func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
var (
journeys_results *navitia.JourneyResults
carpool_results any
vehicle_results []any
)
vehiclech := make(chan []any, 1)
navitiaCh := make(chan *navitia.JourneyResults, 1)
carpoolCh := make(chan any, 1)
locTime, errTime := time.LoadLocation("Europe/Paris")
if errTime != nil {
fmt.Println("Loading timezone location Europe/Paris error : ")
@ -70,91 +77,101 @@ func (h *ApplicationHandler) JourneysSearch(w http.ResponseWriter, r *http.Reque
w.WriteHeader(http.StatusBadRequest)
return
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
journeysRequest := func() {
//TODO make it a library
session, _ := navitia.NewCustom(
h.config.GetString("services.navitia.api_key"),
"https://api.navitia.io/v1",
&http.Client{})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
navitiaCh <- nil
return
}
request := navitia.JourneyRequest{
From: types.ID(fmt.Sprintf("%f", departuregeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", departuregeo.Geometry.Point[1])),
To: types.ID(fmt.Sprintf("%f", destinationgeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", destinationgeo.Geometry.Point[1])),
Date: departuredatetime.Add(-2 * time.Hour),
DateIsArrival: false, //TODO
}
journeys, err = session.Journeys(context.Background(), request)
if err != nil {
fmt.Println(err)
// w.WriteHeader(http.StatusBadRequest)
// return
}
navitiaCh <- journeys
//TODO make it a library
session, _ := navitia.NewCustom(
h.config.GetString("services.navitia.api_key"),
"https://api.navitia.io/v1",
&http.Client{})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
request := navitia.JourneyRequest{
From: types.ID(fmt.Sprintf("%f", departuregeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", departuregeo.Geometry.Point[1])),
To: types.ID(fmt.Sprintf("%f", destinationgeo.Geometry.Point[0]) + ";" + fmt.Sprintf("%f", destinationgeo.Geometry.Point[1])),
Date: departuredatetime.Add(-2 * time.Hour),
DateIsArrival: false, //TODO
}
journeys, err = session.Journeys(context.Background(), request)
if err != nil {
fmt.Println(err)
// w.WriteHeader(http.StatusBadRequest)
// return
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CARPOOL
// carpoolrequest := fmt.Sprintf(
// "https://api.rdex.ridygo.fr/journeys.json?p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=%f&p[from][longitude]=%f&p[to][latitude]=%f&p[to][longitude]=%f&p[outward][mindate]=%s&p[outward][maxdate]=%s",
// departuregeo.Geometry.Point[1], departuregeo.Geometry.Point[0],
// destinationgeo.Geometry.Point[1], destinationgeo.Geometry.Point[0],
// departuredatetime.Format("2006-01-02"), departuredatetime.Add(24*time.Hour).Format("2006-01-02"))
carpoolrequest := "https://api.rdex.ridygo.fr/journeys.json"
carpoolRequest := func() {
carpoolrequest := "https://api.rdex.ridygo.fr/journeys.json"
client := &http.Client{}
req, err := http.NewRequest("GET", carpoolrequest, nil)
if err != nil {
fmt.Println(err)
}
req.URL.RawQuery = fmt.Sprintf(
"p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=%f&p[from][longitude]=%f&p[to][latitude]=%f&p[to][longitude]=%f&p[outward][mindate]=%s&p[outward][maxdate]=%s",
departuregeo.Geometry.Point[1], departuregeo.Geometry.Point[0],
destinationgeo.Geometry.Point[1], destinationgeo.Geometry.Point[0],
departuredatetime.Format("2006-01-02"), departuredatetime.Format("2006-01-02"))
req.Header.Set("X-API-KEY", "123456")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
if err == nil && resp.StatusCode == http.StatusOK {
err = json.NewDecoder(resp.Body).Decode(&carpoolresults)
client := &http.Client{}
req, err := http.NewRequest("GET", carpoolrequest, nil)
if err != nil {
fmt.Println(err)
}
if carpoolresults == nil {
req.URL.RawQuery = fmt.Sprintf(
"p[driver][state]=1&frequency=punctual&p[passenger][state]=0&p[from][latitude]=%f&p[from][longitude]=%f&p[to][latitude]=%f&p[to][longitude]=%f&p[outward][mindate]=%s&p[outward][maxdate]=%s",
departuregeo.Geometry.Point[1], departuregeo.Geometry.Point[0],
destinationgeo.Geometry.Point[1], destinationgeo.Geometry.Point[0],
departuredatetime.Format("2006-01-02"), departuredatetime.Format("2006-01-02"))
req.Header.Set("X-API-KEY", "123456")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
if err == nil && resp.StatusCode == http.StatusOK {
err = json.NewDecoder(resp.Body).Decode(&carpoolresults)
if err != nil {
fmt.Println(err)
}
if carpoolresults == nil {
carpoolresults = []any{}
}
} else {
carpoolresults = []any{}
}
} else {
carpoolresults = []any{}
carpoolCh <- carpoolresults
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Vehicles
vehiclerequest := &fleets.GetVehiclesRequest{
Namespaces: []string{"parcoursmob"},
}
vehicleresp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), vehiclerequest)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
for _, vehicle := range vehicleresp.Vehicles {
v := vehicle.ToStorageType()
if v.Free(departuredatetime.Add(-24*time.Hour), departuredatetime.Add(168*time.Hour)) {
vehicles = append(vehicles, v)
vehicleRequest := func() {
vehiclerequest := &fleets.GetVehiclesRequest{
Namespaces: []string{"parcoursmob"},
}
vehicleresp, err := h.services.GRPC.Fleets.GetVehicles(context.TODO(), vehiclerequest)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
for _, vehicle := range vehicleresp.Vehicles {
v := vehicle.ToStorageType()
if v.Free(departuredatetime.Add(-24*time.Hour), departuredatetime.Add(168*time.Hour)) {
vehicles = append(vehicles, v)
}
}
vehiclech <- vehicles
}
go journeysRequest()
go carpoolRequest()
go vehicleRequest()
carpool_results = <-carpoolCh
journeys_results = <-navitiaCh
vehicle_results = <-vehiclech
}
h.Renderer.JourneysSearch(w, r, carpoolresults, journeys, vehicles, searched, departuregeo, destinationgeo, departuredate, departuretime)
h.Renderer.JourneysSearch(w, r, carpool_results, journeys_results, vehicle_results, searched, departuregeo, destinationgeo, departuredate, departuretime)
}
type GroupsModule []groupstorage.Group