def switch_search(request): """Controller for switch search""" if 'switch' in request.GET: return switch_do_search(request) info_dict = { 'form': forms.SwitchTrackerForm(), } info_dict.update(SWP_DEFAULTS) return render(request, 'machinetracker/switch_search.html', info_dict)
def switch_do_search(request): """Does a search in cam and arp based on a switch""" querydict = ProcessInput(request.GET).swp() form = forms.SwitchTrackerForm(querydict) info_dict = { 'form': form, 'form_data': None, 'mac_tracker': None, 'mac_tracker_count': 0, } if form.is_valid(): switch = form.cleaned_data['switch'] module = form.cleaned_data.get('module') port_interface = form.cleaned_data.get('port') days = form.cleaned_data['days'] from_time = date.today() - timedelta(days=days) criteria = {} if module: criteria['module'] = module # If port is specified, match on ifindex if port_interface: try: cam_with_ifindex = Cam.objects.filter( Q(sysname__istartswith=switch) | Q(netbox__sysname__istartswith=switch), end_time__gt=from_time, port=port_interface, **criteria ).values('ifindex')[0] criteria['ifindex'] = cam_with_ifindex['ifindex'] except IndexError: criteria['port'] = port_interface cam_result = Cam.objects.select_related('netbox').filter( Q(sysname__istartswith=switch) | Q(netbox__sysname__istartswith=switch), end_time__gt=from_time, **criteria ).order_by('sysname', 'module', 'mac', '-start_time') # Get last topo jobs on netboxes netboxes_topo = get_last_job_log_from_netboxes(cam_result, 'topo') # Flag rows overdue as fishy for row in cam_result: if row.netbox in netboxes_topo: job_log = netboxes_topo[row.netbox] fishy = job_log and job_log.is_overdue() row.fishy = job_log if fishy else None swp_count = len(cam_result) swp_tracker = track_mac(('mac', 'sysname', 'module', 'port'), cam_result, dns=False) info_dict.update({ 'form_data': form.cleaned_data, 'mac_tracker': swp_tracker, 'mac_tracker_count': swp_count, }) info_dict.update(SWP_DEFAULTS) return render(request, 'machinetracker/switch_search.html', info_dict)