New Feature: de-norming effective location into asset table
Right now, we calculate an assets 'location' by looking at -
- The asset's
rtd_location_idif it's not checked out - The user's location, if it's set, and the asset is checked out
- The asset's
rtd_location_idif the asset is checked out to a user who has no location - The parent-asset's
rtd_location_idif the asset is checked out to another asset, and that asset isn't checked out to anyone - The location ID if the asset is checked out to a location
- and, potentially, some other, harder calculations for some other edge cases, perhaps? (asset checked out to asset checked out to asset checked out to user?)
This results in enormously complex queries that are hard to optimize for. And they can be very slow. And it can be enormously difficult to determine whether or not they are actually correct.
Instead, we should create an effective_location_id (or maybe just location_id if that's not in use?) which is de-normed onto the asset, and updated whenever the asset is checked in or out.
Then, we need to find any queries which try to determine an asset's location, and instead modify them to query the new de-normed value.
We will also need to build some migration queries to pre-populate the location based on these algorithms. We can do a lot of it in SQL, but we might want to just do a lot of it in PHP, or maybe just for those locations that are "harder." Especially for those assets that are checked out to other assets, that could make sense. Maybe that's the only case we'd do in PHP.
The truth is, 9 times out of 10, the asset is either checked in and lives in its rtd_location_id, or it's checked out to a person and thus it is at that user's location_id. But the other stuff is still possible, and we still need to support it.
NOTE: under the current system, as well as under this new proposed system, it is possible for an asset to have 'no location at all.' We will probably want to account for that, perhaps under the "Locations listing (/locations)" - a value for <No Location>.
NOTE 2: We've seen, in the wild, an asset checked out to itself. That's obviously very, very bad, and will cause infinite loops. We can probably build in some kind of hierarchy-checker whenever we use recursion and if it goes more than 'n' levels deep (10?) we should at least raise an exception. Or we can modify the checkout method to just check-in an asset if it determines you're trying to check out an asset to itself. It might also be able to create asset-loops, where asset A is checked out to asset B, asset B is checked out to asset C, and asset C is checked out to asset A. The hierarchy checker should be able to catch those as well.
This makes sense and will certainly simplify a lot of logic. I'm trying to remember now if theres a reason I didn't go this route when working on checkout-to-*... If there is I'm not recalling it though, may just have been that it felt "wrong" to have the asset know these details, although in retrospect the amount of code to avoid it feels a lot wronger.
Great recap, @uberbrady!
I think I already fixed Note 2 last week when we discovered that issue.
NOTE: under the current system, as well as under this new proposed system, it is possible for an asset to have 'no location at all.' We will probably want to account for that, perhaps under the "Locations listing (/locations)" - a value for
.
I think leaving it blank is more useful visual feedback, but am open to discussion.
I'll probably start on the migrations for this ish this week.
This makes sense and will certainly simplify a lot of logic. I'm trying to remember now if theres a reason I didn't go this route when working on checkout-to-
I think you just thought it felt icky. And it does. But....
Hey that’s UI, and that’s outside of my pay grade.