Django Progress bar for the backend to later integrate with frontend

I want to implement a progress bar since the request takes some time to give out the response when the api request is called i want the progress bar to start from 0 to 100 when the response loads views.py: class AllNodeReportView(View):

def get(self, request, *args, **kwargs):
    start_date = request.GET.get('start')
    end_date = request.GET.get('end')
    format_type = request.GET.get('format', 'json')

    try:
        start_datetime = datetime.strptime(start_date, '%Y-%m-%d')
        end_datetime = datetime.strptime(end_date, '%Y-%m-%d') + timedelta(days=1)
    except ValueError:
        return JsonResponse({"error": "Invalid date format. Use 'YYYY-MM-DD'."}, status=400)

    nodes = Node.objects.only('mac', 'location', 'unicast')

    response_data = []
    node_unicasts = nodes.values_list('unicast', flat=True)

    loc_tag_relations = LocTagRelation.objects.filter(
        unicastAddr__in=node_unicasts,
        uuid__gte=start_datetime.timestamp(),
        uuid__lt=end_datetime.timestamp()
    ).values('unicastAddr', 'tagMac').annotate(
        intime=Min('uuid'),
        outtime=Max('uuid')
    ).order_by('tagMac', 'intime')

    loc_tag_relation_map = {}
    for relation in loc_tag_relations:
        if relation['unicastAddr'] not in loc_tag_relation_map:
            loc_tag_relation_map[relation['unicastAddr']] = []
        loc_tag_relation_map[relation['unicastAddr']].append(relation)

    tag_macs = [relation['tagMac'] for relation in loc_tag_relations]
    tag_data = TagHistory.objects.filter(
        mac__in=tag_macs,
        lastUpdated__gte=start_datetime.timestamp(),
        lastUpdated__lt=end_datetime.timestamp()
    ).order_by('mac', 'lastUpdated')

    tag_data_map = {}
    for entry in tag_data:
        if entry.mac not in tag_data_map:
            tag_data_map[entry.mac] = []
        tag_data_map[entry.mac].append(entry)

    for node in nodes:
        if node.unicast in loc_tag_relation_map:
            for relation in loc_tag_relation_map[node.unicast]:
                tag_mac = relation['tagMac']
                if tag_mac in tag_data_map:
                    tag_entries = tag_data_map[tag_mac]
                    current_location = None
                    entry_time = None
                    entry_date = None  # Capture the actual date of entry
                    last_exit_time = None
                    ongoing_entry = None

                    for tag_entry in tag_entries:
                        timestamp = datetime.fromtimestamp(float(tag_entry.lastUpdated))
                        date_str = timestamp.strftime('%Y-%m-%d')
                        time_str = timestamp.strftime('%I:%M %p')

                        if current_location is None:
                            current_location = tag_entry.location
                            entry_time = time_str
                            entry_date = date_str  # Set the date when the entry starts
                            ongoing_entry = True
                        elif tag_entry.location != current_location:
                            if ongoing_entry and current_location == node.location:
                                response_data.append({
                                    'Date': entry_date,  # Use the captured entry date
                                    'NodeMac': node.mac,
                                    'TagMac': tag_mac,
                                    'InTime': entry_time,
                                    'OutTime': last_exit_time,
                                    'Location': current_location
                                })
                            current_location = tag_entry.location
                            entry_time = time_str
                            entry_date = date_str  # Update the date to the new entry
                            ongoing_entry = True

                        last_exit_time = time_str

                    if ongoing_entry and current_location == node.location:
                        response_data.append({
                            'Date': entry_date,  # Ensure the final entry uses the correct date
                            'NodeMac': node.mac,
                            'TagMac': tag_mac,
                            'InTime': entry_time,
                            'OutTime': last_exit_time,
                            'Location': current_location
                        })

    if format_type == 'pdf':
        return self.generate_pdf(response_data, start_date)
    elif format_type == 'csv':
        return self.generate_csv(response_data, start_date)
    else:
        return JsonResponse(response_data, safe=False)

I want to integrate the progress bar simultaneously while requesting the api

Back to Top