Exemplo n.º 1
0
func countDays(start, end time.Time) int {
	endDateString := utils.DateToString(end)
	days := 0

	for element := start; utils.DateToString(element) != endDateString; element = element.Add(time.Hour * time.Duration(24)) {
		days = days + 1
	}

	return days
}
Exemplo n.º 2
0
func performQuery(query data.FlightQuery, googleQPXExpressCredentials string) {
	client := &http.Client{
		Transport: &transport.APIKey{Key: googleQPXExpressCredentials}}
	service, err := qpxexpress.New(client)
	if err != nil {
		panic(err)
	}

	tripRequest := qpxexpress.TripsSearchRequest{}

	trip1 := qpxexpress.SliceInput{}
	trip1.Date = utils.DateToString(query.StartDate)
	trip1.Destination = query.DestAirport
	trip1.Origin = query.StartAirport

	trip2 := qpxexpress.SliceInput{}
	trip2.Date = utils.DateToString(query.BackDate)
	trip2.Destination = query.StartAirport
	trip2.Origin = query.DestAirport

	trips := []*qpxexpress.SliceInput{}
	trips = append(trips, &trip1)
	trips = append(trips, &trip2)

	tripRequest.Request = &qpxexpress.TripOptionsRequest{}
	tripRequest.Request.Slice = trips
	tripRequest.Request.Refundable = false
	tripRequest.Request.Passengers = &qpxexpress.PassengerCounts{}
	tripRequest.Request.Passengers.AdultCount = 1
	// we only need one solution (it is already the cheapest one)
	tripRequest.Request.Solutions = 1

	result, err := service.Trips.Search(&tripRequest).Do()
	utils.CheckErr(err, "unable to retrieve trips from Google QPX Express")

	costLockMutex.Lock()
	// the first trip is already the cheapest one !
	if len(result.Trips.TripOption) == 0 {
		costLock[query] = "not found !"
	} else {
		costLock[query] = result.Trips.TripOption[0].SaleTotal
	}
	costLockMutex.Unlock()
}
Exemplo n.º 3
0
func DisplayRefreshPagePage(w http.ResponseWriter, r *http.Request) {
	toDisplay := make([]queryDisplayData, 0)

	for _, element := range flightQueries {
		startDateString := utils.DateToString(element.StartDate)
		endDateString := utils.DateToString(element.BackDate)
		duration := countDays(element.StartDate, element.BackDate)

		costLockMutex.Lock()
		price, ok := costLock[element]
		costLockMutex.Unlock()

		priceString := "???"

		if ok {
			priceString = price
		}

		displayData := queryDisplayData{
			Description: fmt.Sprintf("From %v to %v (%v days) - %v to %v", startDateString, endDateString, duration, element.StartAirport, element.DestAirport),
			Cost:        priceString,
		}

		toDisplay = append(toDisplay, displayData)
	}

	// create, initialize and use the template
	uninitializedTemplate := template.New("query calculation template")
	initializedTempalte, err := uninitializedTemplate.Parse(dataHtmlData)
	utils.CheckErr(err, "problem while parsing template")
	arguments := pageArgumentsData{
		Results: toDisplay,
	}
	err = initializedTempalte.Execute(w, arguments)
	utils.CheckErr(err, "problem while executing template")
}