-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interface.objects.order_naturally() enhancements #1523
Comments
Interface.objects.order_naturally()
enhancements
@tarkatronic great work, thanks for the contribution! Unfortunately it still needs some work. With this approach, |
Ok, I think I figured out the trick. The solution, as always, is to add more regex. Previously, we were treating the zero in e.g. I seem to have this working perfectly locally. I just need to clean it up a bit and extend your test. |
I'm happy with how this is working right now. This issue can be re-opened if any notices a particular problem with the new logic (please be sure to include instructions detailing the steps needed to reproduce). |
@jeremystretch Now, this data is purely theoretical; I'm not sure this could happen in a real scenario, but let's just suppose it could. You have the following:
Now, my expected/preferred ordering for this would be
However currently I am getting
This is due to a combination of two things. The first is the removal of the The second part is due to the addition of the So I've arrived at the following: type_re = r"SUBSTRING({} FROM '^([^0-9]+)')"
id_re = r"CAST(SUBSTRING({} FROM '^(?:[^0-9]+)([0-9]+)$') AS integer)"
slot_re = r"CAST(SUBSTRING({} FROM '^(?:[^0-9]+)([0-9]+)\/?') AS integer)"
subslot_re = r"COALESCE(CAST(SUBSTRING({} FROM '^(?:[^0-9]+)(?:[0-9]+\/)([0-9]+)') AS integer), 0)"
position_re = r"COALESCE(CAST(SUBSTRING({} FROM '^(?:[^0-9]+)(?:[0-9]+\/){{2}}([0-9]+)') AS integer), 0)"
subposition_re = r"COALESCE(CAST(SUBSTRING({} FROM '^(?:[^0-9]+)(?:[0-9]+\/){{3}}([0-9]+)') AS integer), 0)"
channel_re = r"COALESCE(CAST(SUBSTRING({} FROM ':([0-9]+)(\.[0-9]+)?$') AS integer), 0)"
vc_re = r"COALESCE(CAST(SUBSTRING({} FROM '\.([0-9]+)$') AS integer), 0)" Thoughts? |
I've made some tweaks, and interfaces are now ordered as:
I want to keep interfaces without a slash in their name at the bottom of the list, because a device typically has either slotted or non-slotted physical interfaces. In the cases where both exist, the non-slotted interfaces tend to be "special" interfaces like These changes will appear in v2.2. I'm going to close out this issue for now, but as is tradition it can be re-opened if further adjustments are needed. |
Hey @jeremystretch, interested in another small enhancement? In my local project, I've created a fields = {
'_type': SubstrFrom(field, self.TYPE_RE),
'_id': Cast(SubstrFrom(field, self.ID_RE), IntegerField()),
'_slot': Cast(SubstrFrom(field, self.SLOT_RE), IntegerField()),
'_subslot': Coalesce(Cast(SubstrFrom(field, self.SUBSLOT_RE), IntegerField()), Value(0)),
'_position': Coalesce(Cast(SubstrFrom(field, self.POSITION_RE), IntegerField()), Value(0)),
'_subposition': Coalesce(Cast(SubstrFrom(field, self.SUBPOSITION_RE), IntegerField()), Value(0)),
'_channel': Coalesce(Cast(SubstrFrom(field, self.CHANNEL_RE), IntegerField()), Value(0)),
'_vc': Coalesce(Cast(SubstrFrom(field, self.VC_RE), IntegerField()), Value(0)),
} |
Issue type
[X] Feature request
[X] Bug report
[ ] Documentation
Environment
Description
I have discovered a few issues/limitations in the
order_naturally()
method which I have had to address in a local project, and would be happy to submit a PR for if desired.The first is a bug in the actual ordering. Given interfaces with the following names:
I would expect
order_naturally()
to return them in that same order. However, it is instead returning them in the order:This is a side effect of the regular expressions used in the method being anchored to the end of the string.
Second, I have added support for a third slash in the name (
Ethernet1/2/3/4
).And finally, because it is highly recommended you do not use the
.extra()
method, I have updated the method to use.annotate()
andRawSQL()
instead.Again, I can have a pull request ready for this ASAP if desired, along with appropriate tests.
The text was updated successfully, but these errors were encountered: