Skip to content

Commit

Permalink
Add files to get UISP data and process nn
Browse files Browse the repository at this point in the history
  • Loading branch information
andybaumgar committed May 4, 2023
1 parent 1cbb381 commit 112b963
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
36 changes: 36 additions & 0 deletions supportbot/supportbot/utils/uisp_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import requests
import json
from dotenv import load_dotenv
import os
import re

load_dotenv()

def get_uisp_devices():
response = requests.get("https://uisp.mesh.nycmesh.net/nms/api/v2.1/devices", headers={'x-auth-token': os.environ.get('NYCMESH_TOOL_AUTH_TOKEN')}, verify=False)

devices = json.loads(response.content)

if devices == []:
raise ValueError('Problem downloading UISP devices.')

return devices

def nn_from_uisp_name(uisp_name):
matches = re.findall("(?:^|-)(\d{3,})(?:-|$)", uisp_name)
if not matches:
return None

return int(matches[0])

def filter_devices_by_nn(devices, nn):
filtered_devices = []
for device in devices:
try:
name = device['identification']['displayName']
nn = nn_from_uisp_name(name)
if nn:
filtered_devices.append(device)
except:
pass
return filtered_devices
16 changes: 16 additions & 0 deletions tests/test_uisp_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
from dotenv import load_dotenv
from supportbot.utils.uisp_data import nn_from_uisp_name, get_uisp_devices

load_dotenv()

def test_get_uisp_devices():
assert len(get_uisp_devices()) > 0

def test_nn_from_uisp_name():
assert nn_from_uisp_name("lbe-5134-diy-455") == 5134
assert nn_from_uisp_name("lbe-5134") == 5134
assert nn_from_uisp_name("5134-lbe") == 5134

if __name__ == "__main__":
test_nn_from_uisp_name()

0 comments on commit 112b963

Please sign in to comment.