Tuesday, April 23, 2019

Adding and editing aircraft - Update

A few months ago, I discussed how models get assigned to aircraft when you add a new aircraft, or when you edit an existing one.  This bit of code has always been surprisingly complex and convoluted, but over this past weekend I went in to add some new intelligence to it and was able to clean it up in the process.

The whole complexity here arises from the fact that MyFlightbook tries hard to share aircraft among pilots (for reasons discussed here).  That is, if two or more pilots fly N12345, I want there to be only one instance of N12345 in the system, used by each of those pilots.

This leads to some interesting scenarios, so I thought I'd geek out a bit and describe them here, including the enhancements I just made.

The key to the new enhancement is the notion of finding a "close" or a "perfect" match for an aircraft within a set of aircraft that share a tailnumber: 
  • A "perfect" match means that the model of one aircraft is exactly the same as the model for the other.  E.g., a Cessna C-172N is a perfect match for a C-172N, but not a perfect match for a C-172S.  
  • A "close" match is one where the category/class of two aircraft is the same AND they have the same non-empty ICAO identifier.  E.g., a C-172N and a C-172S both have the ICAO identifier "C172", so they are a close match.
  • The "best" match out of a set of aircraft is a perfect match, if one exists, otherwise a close match, if one exists.  There is not always a "best" match.
OK, so what happens when you *add* an aircraft to your account?  There are now four possible outcomes:
  • If there is no aircraft in the system with that tail number, it is simply added and the model you specified is used.  Piece of cake, very simple, exactly as you'd expect.
  • If there are already one or more aircraft with the same tail number in the system, then I try to find a perfect or close match.  
    • If a perfect match is found, then that aircraft is added to your account.  Just like above, and as you'd expect, but you might acquire some images, notes, and maintenance in the process.
    • Otherwise, if a close match is found, then that aircraft is added to your account, but I send you an email telling you of the substitution (e.g., you added a PA28-161 but it's actually a PA28-181.)
    • Otherwise, I create a new version of the aircraft using the model you specified.  E.g., if you specified N12345 is a Boeing 737 but N12345 is already in the system as a C-172, then the C-172 is left alone and a new Boeing with tail N12345 is created and added to your account.  This is the new enhancement; previously, you'd have picked up the C-172 and received an email; you'd then have to edit N12345 to be the Boeing 737.
Now you have an aircraft in your account - what happens if you try to edit it?

Simple edits like adding notes, pictures, or maintenance events are straightforward - the underlying aircraft is edited.

But things get complicated in two additional scenarios: modifying the model of the aircraft, and/or modifying the tail number of the aircraft.

I discussed editing the model of the aircraft in my earlier blog post; this is essentially unchanged (and indeed used the notion of perfect/close match described above).  So if you edit a Super Cub to be on floats, or edit a Cirrus SR22 to be an Airbus A-320, the system will automatically clone the aircraft for you, with two minor qualifiers/exceptions:
  • If the alternative version already exists, then instead of cloning the aircraft, it will just re-use the existing clone.
  • If you're the only pilot using the aircraft, then no clone is created; I just edit the underlying aircraft, unless there is already an alternative that meets the "best match" criteria above, in which case that match is used.
There had been a corner case here that my enhancement has mitigated: suppose you owned an aircraft with vanity registration NABC, but then bought a new aircraft that isn't the same model, and carried NABC over to the new aircraft.  If you were the only pilot using NABC, there had been no way to get both versions of NABC into the system side by side without contacting me (I have an admin tool to do this).  But with the enhancement above, you can now just add the new version of NABC to your account, and the cloning should occur automatically (assuming there's no "best" match).

A similar issue arises when you edit the tail number of an aircraft in your account.  Usually, there is no reason to edit an aircraft's tail.  (After all, if an airframe gets a new registration, it generally makes the most sense to treat it in MyFlightbook as a new aircraft.)  So the primary reason for doing this is to handle a typo.

There are a few scenarios here, so let's assume you're editing N12345 to be N54321.
  • If you're the only one flying N12345:
    • If N54321 is not in the system, the existing aircraft simply has its tail changed.  Because the existing aircraft is changed, all of your flights in N12345 magically become flights in N54321, not because the flights themselves are changed (they aren't), but because the underlying aircraft they use has changed.
    • If one or more versions of N54321 are in the system, then the best match (as described above) is simply added to your account, or else a clone is created (if there is no best match).  No flights are modified, so in this scenario your flights do NOT reflect a change.
  • If other pilots are flying N12345, then I explicitly do NOT want to modify N12345 in the database (thereby changing their logbooks).  Therefore, I treat this as the creation of a new aircraft and it follows all of the rules for adding a new aircraft above.  Assuming that this is most likely due to a typo, though, the system detects this scenario and modifies your flights to be in the newly added/created aircraft.
Phew.  Who would have thought that adding/editing of aircraft would be so complicated?