Skip to content

Commit

Permalink
#2019 Update dwell time calculation to correctly track dwell times.
Browse files Browse the repository at this point in the history
Create dwell times even if they have a "negative" time (i.e. the "next"
flight takes off before the first flight lands).

Update loader to calculate UTC times at start of flight vo instead of as
flights and booking details are created. Updated sort to sort on UTC
time instead of local time.
  • Loading branch information
originalname51 committed Mar 3, 2021
1 parent ec3f880 commit 8c3b17c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Entity
@Table(name = "dwell_time")
public class DwellTime implements Serializable {

@Transient
public static Logger logger = LoggerFactory.getLogger(DwellTime.class);

private static final long serialVersionUID = 1L;

Expand All @@ -35,11 +42,14 @@ public DwellTime(Date arrival, Date departure, String airport, Pnr pnr) {
// java.lang.NullPointerException issue #307 code fix
if (this.departureTime != null && this.arrivalTime != null) {
long diff = this.departureTime.getTime() - this.arrivalTime.getTime();
if (diff > 0) {
if (diff < 0) {
logger.warn("Arrival Time of incoming flight AFTER departure time of outgoing flight. Likely bad data!");
}
int minutes = (int) TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
DecimalFormat df = new DecimalFormat("#.##");
this.dwellTime = Double.valueOf(df.format((double) minutes / 60));
}
} else {
logger.warn("Unable to calculate dwell time, etd or eta missing from arriving or departing flight!");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ public void calculateDwellTimes(Pnr pnr) {
if (CollectionUtils.isEmpty(legs)) {
return;
}


//order flight legs from first to last to create dwell times.
legs.sort(Comparator.comparing(FlightLeg::getLegNumber));
for (int i = 0; i < legs.size(); i++) {
if (i + 1 < legs.size()) { // If the 'next' leg actually exists
// 4 different combinations of flights and booking details n^2, where n = 2.
Expand Down Expand Up @@ -532,8 +534,18 @@ public Flight processFlightsAndBookingDetails(List<FlightVo> flights, Set<Flight
primeFlightCarrier, primeFlightNumber));
flights.add(flightVo);
}


for (FlightVo fvo : flights) {
String originAirport = fvo.getOrigin();
String destinationAirport = fvo.getDestination();
Date utcETADate = gtasLocalToUTCService.convertFromAirportCode(destinationAirport, fvo.getLocalEtaDate());
fvo.setUtcEtaDate(utcETADate);
Date utcETDDate = gtasLocalToUTCService.convertFromAirportCode(originAirport, fvo.getLocalEtdDate());
fvo.setUtcEtdDate(utcETDDate);
}

utils.sortFlightsByDate(flights);

Flight primeFlight = null;
for (int i = 0; i < flights.size(); i++) {
FlightVo fvo = flights.get(i);
Expand Down Expand Up @@ -571,13 +583,8 @@ public Flight processFlightsAndBookingDetails(List<FlightVo> flights, Set<Flight
// or tvl level 0 etd timestamp for PNR if this is the case.
mfd.setLocalEtdDate(primeFlightTimeStamp);
}
String originAirport = currentFlight.getOrigin();
String destinationAirport = currentFlight.getDestination();
Date utcETADate = gtasLocalToUTCService.convertFromAirportCode(destinationAirport,
fvo.getLocalEtaDate());
Date utcETDDate = gtasLocalToUTCService.convertFromAirportCode(originAirport, fvo.getLocalEtdDate());
mfd.setEta(utcETADate);
mfd.setEtd(utcETDDate);
mfd.setEta(fvo.getUtcEtaDate());
mfd.setEtd(fvo.getUtcEtdDate());
mfd = mutableFlightDetailsRepository.save(mfd);
currentFlight.setMutableFlightDetails(mfd);
primeFlight = currentFlight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,8 @@ private boolean isTestData(String s) {
BookingDetail convertFlightVoToBookingDetail(FlightVo fvo) throws ParseException {
BookingDetail bD = new BookingDetail();
BeanUtils.copyProperties(fvo, bD);
String originAirport = bD.getOrigin();
String destinationAirport = bD.getDestination();
Date utcETDDate = gtasLocalToUTCService.convertFromAirportCode(originAirport, fvo.getLocalEtdDate());
Date utcETADate = gtasLocalToUTCService.convertFromAirportCode(destinationAirport, fvo.getLocalEtaDate());
bD.setEta(utcETADate);
bD.setEtd(utcETDDate);

bD.setEta(fvo.getUtcEtaDate());
bD.setEtd(fvo.getUtcEtdDate());
Airport dest = getAirport(fvo.getDestination());
String destCountry = null;
if (dest != null) {
Expand Down Expand Up @@ -458,15 +453,10 @@ public void setDwellTime(Flight firstFlight, Flight secondFlight, Pnr pnr) {
}

public void setDwellTime(BookingDetail firstBooking, BookingDetail secondBooking, Pnr pnr) {
if (firstBooking != null && secondBooking != null
&& firstBooking.getDestination().equalsIgnoreCase(secondBooking.getOrigin())
&& (firstBooking.getEta() != null && secondBooking.getEtd() != null)) {

DwellTime d = new DwellTime(firstBooking.getEta(), secondBooking.getEtd(), secondBooking.getOrigin(), pnr);
d.setFlyingFrom(firstBooking.getOrigin());
d.setFlyingTo(secondBooking.getDestination());
pnr.addDwellTime(d);
}
}

public void setDwellTime(Flight firstFlight, BookingDetail secondBooking, Pnr pnr) {
Expand Down Expand Up @@ -498,7 +488,7 @@ public void setDwellTime(BookingDetail firstBooking, Flight secondFlight, Pnr pn
// The parsed message did not have the flights in proper order for flight leg
// generation (needed for dwell time and appropriate display)
void sortFlightsByDate(List<FlightVo> flights) {
flights.sort(Comparator.comparing(FlightVo::getLocalEtdDate));
flights.sort(Comparator.comparing(FlightVo::getUtcEtdDate));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ public class FlightVo implements Validatable {
// THIS IS NOT UTC TIME. LOCAL ETA DATE IS IN WHATEVER LOCAL TIME OF THE AIRPORT
// WHERE FLIGHT IS DESTINED TO
private Date localEtaDate;

// UTC TIME CONVERSIONS HAPPEN IN LOADER *NOT* GTAS PARSER.
private Date utcEtdDate;

// UTC TIME CONVERSIONS HAPPEN IN LOADER *NOT* GTAS PARSER.
private Date utcEtaDate;

private String marketingFlightNumber;
private boolean isCodeShareFlight = false;
private boolean isMarketingFlight = false;
private String idTag;

public void setUuid(UUID uuid) {
this.uuid = uuid;
}
Expand Down Expand Up @@ -137,7 +144,24 @@ public boolean isValid() {
public UUID getUuid() {
return uuid;
}

public Date getUtcEtdDate() {
return utcEtdDate;
}

public void setUtcEtdDate(Date utcEtdDate) {
this.utcEtdDate = utcEtdDate;
}

public Date getUtcEtaDate() {
return utcEtaDate;
}

public void setUtcEtaDate(Date utcEtaDate) {
this.utcEtaDate = utcEtaDate;
}

//TODO: CheckEqualityToBD
public boolean equalsThisBD(BookingDetail bookingDetail) {
return flightNumber != null && flightNumber.equals(bookingDetail.getFlightNumber()) && localEtdDate != null
&& DateUtils.stripTime(localEtdDate).equals(bookingDetail.getEtdDate()) && origin != null
Expand Down

0 comments on commit 8c3b17c

Please sign in to comment.