Skip to content

Commit

Permalink
Apply device inactivity threshold at initial import time (#632)
Browse files Browse the repository at this point in the history
* Apply device inactivity threshold at initial import time

* Fix formatting on spacer line
  • Loading branch information
Andrew-Dickinson authored Oct 12, 2024
1 parent 7b21c21 commit 46d6597
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/meshdb/utils/spreadsheet_import/parse_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import django

from meshapi.util.uisp_import.constants import UISP_OFFLINE_DURATION_BEFORE_MARKING_INACTIVE

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "meshdb.settings")
django.setup()

import dateutil.parser

from meshapi.models import AccessPoint, Device, Install, Node, Sector
from meshapi.util.uisp_import.fetch_uisp import get_uisp_devices
Expand Down Expand Up @@ -99,25 +100,33 @@ def create_device(nn: int, uisp_device: dict, spreadsheet_sector: Optional[Sprea
notes=sector_notes if sector_notes else None,
)
else:
uisp_last_seen = (
parse_uisp_datetime(uisp_device["overview"]["lastSeen"]) if uisp_device["overview"]["lastSeen"] else None
)

if not uisp_device["overview"]["status"]:
# If UISP doesn't have a status value, assume active
status = Device.DeviceStatus.ACTIVE
elif uisp_device["overview"]["status"] == "active":
status = Device.DeviceStatus.ACTIVE
else:
status = Device.DeviceStatus.INACTIVE
if (
not uisp_last_seen
or (datetime.datetime.now(datetime.timezone.utc) - uisp_last_seen)
> UISP_OFFLINE_DURATION_BEFORE_MARKING_INACTIVE
):
# Only use an offline status if the device has been offline long enough
status = Device.DeviceStatus.INACTIVE
else:
status = Device.DeviceStatus.ACTIVE

device = Device(
node=node,
name=uisp_device["identification"]["name"],
uisp_id=uisp_device["identification"]["id"],
status=status,
install_date=parse_uisp_datetime(uisp_device["overview"]["createdAt"]).date(),
abandon_date=(
parse_uisp_datetime(uisp_device["overview"]["lastSeen"]).date()
if status == Device.DeviceStatus.INACTIVE
else None
),
abandon_date=(uisp_last_seen.date() if status == Device.DeviceStatus.INACTIVE and uisp_last_seen else None),
notes=f"Automatically imported from UISP on {datetime.date.today().isoformat()}\n\n",
)

Expand Down
2 changes: 1 addition & 1 deletion src/meshdb/utils/spreadsheet_import/parse_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_spreadsheet_install_notes(row: SpreadsheetRow) -> str:
notes = f"Spreadsheet Contact Notes:\n{row.contactNotes}"

if notes:
notes += f"-------\n\n"
notes += f"\n-------\n\n"

return notes

Expand Down

0 comments on commit 46d6597

Please sign in to comment.