Skip to content

Commit

Permalink
Modify the NN assignment logic to utilize the new status value
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Dickinson committed Jan 31, 2024
1 parent 4ebfbb0 commit b48fcf1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
28 changes: 19 additions & 9 deletions src/meshapi/tests/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class TestFindGaps(TestCase):
admin_c = Client()

def add_data(self, b, m, i, index=101, nn=False):
i = i.copy()
b["zip_code"] += index
b["address_truth_sources"] = ["NYCPlanningLabs"]

Expand All @@ -192,9 +193,6 @@ def add_data(self, b, m, i, index=101, nn=False):
if i["abandon_date"] == "":
i["abandon_date"] = None

if "install_number" in i:
i["install_number"] = None

building_obj = Building(**b)
building_obj.save()
i["building"] = building_obj
Expand All @@ -207,6 +205,7 @@ def add_data(self, b, m, i, index=101, nn=False):
install_obj = Install(**i)
install_obj.save()
i["install_number"] = install_obj.install_number
return i

def setUp(self):
self.admin_user = User.objects.create_superuser(
Expand All @@ -226,28 +225,39 @@ def setUp(self):
for i in range(113, 130):
self.add_data(build, memb, inst, index=i, nn=True)

# Inactive install, reserves the install number as an NN even though
# no NN is technically assigned
inst["install_number"] = 130
inst["install_status"] = Install.InstallStatus.INACTIVE
self.add_data(build, memb, inst, index=130, nn=False)

# Old join request, doesn't reserve the NN
inst["install_number"] = 131
inst["install_status"] = Install.InstallStatus.REQUEST_RECEIVED
self.add_data(build, memb, inst, index=131, nn=False)

# Then create another couple installs
# These will get numbers assigned next
b2 = sample_building.copy()
m2 = sample_member.copy()
self.inst2 = sample_install.copy()
self.add_data(b2, m2, self.inst2, index=5002, nn=False)
self.inst2 = self.add_data(b2, m2, self.inst2, index=5002, nn=False)

b3 = sample_building.copy()
m3 = sample_member.copy()
self.inst3 = sample_install.copy()
self.add_data(b3, m3, self.inst3, index=5003, nn=False)
self.inst3 = self.add_data(b3, m3, self.inst3, index=5003, nn=False)

b4 = sample_building.copy()
m4 = sample_member.copy()
self.inst4 = sample_install.copy()
self.add_data(b4, m4, self.inst4, index=5004, nn=False)
self.inst4 = self.add_data(b4, m4, self.inst4, index=5004, nn=False)

def test_nn_search_for_new_number(self):
# Try to give NNs to all the installs. Should end up with two right
# next to each other and then one at the end.

for inst, nn in [(self.inst2, 111), (self.inst3, 112), (self.inst4, 130)]:
for inst, nn in [(self.inst2, 111), (self.inst3, 112), (self.inst4, 131)]:
response = self.admin_c.post(
"/api/v1/nn-assign/",
{"install_number": inst["install_number"], "password": os.environ.get("NN_ASSIGN_PSK")},
Expand Down Expand Up @@ -275,5 +285,5 @@ def test_nn_search_for_new_number(self):
self.assertIsNotNone(Install.objects.filter(network_number=129)[0].install_number)
self.assertIsNotNone(Building.objects.filter(primary_nn=129)[0].id)

self.assertIsNotNone(Install.objects.filter(network_number=130)[0].install_number)
self.assertIsNotNone(Building.objects.filter(primary_nn=130)[0].id)
self.assertIsNotNone(Install.objects.filter(network_number=131)[0].install_number)
self.assertIsNotNone(Building.objects.filter(primary_nn=131)[0].id)
10 changes: 9 additions & 1 deletion src/meshapi/views/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,19 @@ def network_number_assignment(request):
else:
free_nn = None

defined_nns = set(Install.objects.values_list("network_number", flat=True))
defined_nns = set(
Install.objects.exclude(install_status=Install.InstallStatus.REQUEST_RECEIVED, network_number__isnull=True)
.order_by("install_number")
.values_list("install_number", flat=True)
).union(set(Install.objects.values_list("network_number", flat=True)))

# Find the first valid NN that isn't in use
free_nn = next(i for i in range(NETWORK_NUMBER_MIN, NETWORK_NUMBER_MAX + 1) if i not in defined_nns)

# Sanity check to make sure we don't assign something crazy
assert free_nn > 100
assert free_nn < 8000

# Set the NN on both the install and the Building
nn_install.network_number = free_nn
nn_building.primary_nn = free_nn
Expand Down

0 comments on commit b48fcf1

Please sign in to comment.