I am trying to filter the content from a URL Web API and I am using a GET method to obtain the complete data set, then I apply some filters to that response and I get my desired results, but the complete process of retrieval - filter - display results takes around 3-5 mins which is too much waiting time for users. I want to apply a POST method to filter directly from the URL request instead of retrieving the complete data set, in that way, I will get rid of my custom filters and greatly reduce the waiting time. How can I achieve this?
This is the current code I have:
from django.shortcuts import render
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from collections import Counter
from datetime import datetime, timedelta
import json, urllib.request, dateutil.parser, urllib.parse
class ChartData(APIView)
def get(self, request,format=None):
# Request access to the PO database and parse the JSON object
with urllib.request.urlopen(
"http://10.21.200.98:8081/T/ansdb/api/rows/PO/tickets?User_0001=Pat%20Trevor",
timeout=15) as url:
complete_data_user_0001 = json.loads(url.read().decode())
# Custom filter to the JSON response
# Count the number of times the user has created a PO between two given dates where the PO is not equal to N/A values
Counter([k['user_id'] for k in complete_data_user_0001 if
start_date < dateutil.parser.parse(
k.get('DateTime')) < end_date and
k['PO_value'] != 'N/A'])
return Response(data)
The filters that I would like to apply with a POST method are:
{
"filter": {
"filters": [
{
"field": "CreatedOnDate",
"operator": "gte",
"value": "2017-05-31 00:00:00"
},
{
"field": "CreatedOnDate",
"operator": "lte",
"value": "2017-06-04 00:00:00"
},
{
"field": "Triage_Subcategory",
"operator": "neq",
"value": "N/A"
}
],
"logic": "and"
}
}
The structure of the JSON object is:
[{
user_id : 0001
CreatedOn: "2017-02-16 15:54:48",
Problem: "AVAILABILILTY",
VIP: "YES",
PO_value: N/A
},
{
user_id : 0001
CreatedOn: "2017-01-10 18:14:28",
Problem: "AVAILABILILTY",
VIP: "YES",
PO_value: 00098324
},
...
}]
Any suggestion, approach or piece of code is appreciated.