K
K
Konstantin Malyarov2019-02-02 23:56:20
Django
Konstantin Malyarov, 2019-02-02 23:56:20

Why is it slow to respond?

The Ministry of Health of the Russian Federation has a register of MO .
To get it there is a SOAP service .
If the directory is large, then it is divided into parts, each part has a certain number of entries.
There are two methods, the first one is getting the number of parts, the second one is the number of records in one part:

def get_ref_book_parts(request):
    if request.user.is_authenticated:
        ref_book_code = request.GET.get("ref_book_code")
        version = request.GET.get("version")
        settings = zeep.Settings(strict=True)
        client = zeep.Client(wsdl=wsdl, settings=settings)
        return HttpResponse(
            re.sub(
                r'"',
                "*",
                str(client.service.getRefbookParts(user_key, ref_book_code, version)),
            )
            .replace("'", '"')
            .replace("None", "null")
        )
    else:
        return redirect("/api-auth/login/", locals())


def get_ref_book_partial(request):
    if request.user.is_authenticated:
        ref_book_code = request.GET.get("ref_book_code")
        version = request.GET.get("version")
        part_number = request.GET.get("part_number")
        settings = zeep.Settings(strict=True)
        client = zeep.Client(wsdl=wsdl, settings=settings)
        return HttpResponse(
            str(
                client.service.getRefbookPartial(
                    user_key, ref_book_code, version, part_number
                )
            )
        )
    else:
        return redirect("/api-auth/login/", locals())

Via VueJS + Axios made a request on page load and fetching all parts:
axios
            .get('/soap/get_ref_book_parts/', {
                params: {
                    ref_book_code: '1.2.643.5.1.13.13.11.1461', // Виды медицинских направлений
                    version: '5.61'
                }
            })
            .then(response => (getNames(parseInt(response.data.item[0].value, 10))))
            .catch(error => (console.log(error)))
            .then();

function getNames(parts) {
            for (parts; parts !== 0; parts--) {
                axios
                    .get('/soap/get_ref_book_partial/', {
                        params: {
                            ref_book_code: '1.2.643.5.1.13.13.11.1461', // Виды медицинских направлений
                            version: '5.61',
                            part_number: parts
                        }
                    })
                    .then(response => (console.log(response)))
                    .catch(error => (console.log(error)))
                    .then();
            }
        }

As a result - the directory of 26 parts.
Through for instantly makes 26 requests:
5c56031f30938095329987.png
How it looks from the server side:
5c5603928cd97794166789.png
The same as text:

[02/Feb/2019 20:43:57] "GET /soap/get_ref_book/?ref_book_code=1.2.643.5.1.13.2.1.1.358&version=1.0 HTTP/1.1" 200 15394
[02/Feb/2019 20:43: 57] "GET /soap/get_ref_book/?ref_book_code=1.2.643.5.1.13.13.11.1009&version=2.1 HTTP/1.1" 200 13556
[02/Feb/2019 20:44:29] "GET /soap/get_ref_book_parts/?ref_book_code =1.2.643.5.1.13.13.11.1461&version=5.61 HTTP/1.1" 200 167
[02/Feb/2019 20:45:54] "GET /soap/get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version= 5.61&part_number=24 HTTP/1.1" 200 7534088
[02/Feb/2019 20:46:08] "GET /soap/get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version=5.61&part_number=21 HTTP/1.1" 200 7501284
[02/Feb/2019 20:46:08] "GET /soap/get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version=5.61&part_number=25 HTTP/1.1" 200 7537635
[02/Feb/2019 20: 46:08] "GET /soap/get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version=5.61&part_number=22 HTTP/1.1" 200 7483618
[02/Feb/2019 20:46:11] "GET /soap /get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version=5.61&part_number=23 HTTP/1.1" 200 7504062
[02/Feb/2019 20:46:13] "GET /soap/get_ref_book_partial/?ref_book_code=1.2. 643.5.1.13.13.11.1461&version=5.61&part_number=20 HTTP/1.1" 200 7608268
[02/Feb/2019 20:49:29] "GET /soap/get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version= 5.61&part_number=23 HTTP/1.1"200 7504062
[02/Feb/2019 20:49:34] "GET /soap/get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version=5.61&part_number=22 HTTP/1.1" 200 7483618
[02/Feb/2019 20: 49:34] "GET /soap/get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version=5.61&part_number=25 HTTP/1.1" 200 7537635
[02/Feb/2019 20:49:35] "GET /soap /get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version=5.61&part_number=21 HTTP/1.1" 200 7501284
[02/Feb/2019 20:49:36] "GET /soap/get_ref_book_partial/?ref_book_code=1.2. 643.5.1.13.13.11.1461&version=5.61&part_number=24 HTTP/1.1" 200 7534088
[02/Feb/2019 20:49:38] "GET /soap/get_ref_book_partial/?ref_book_code=1.2.643.5.1.13.13.11.1461&version= 5.61&part_number=20 HTTP/1.1"200 7608268

Why does JS give 26 requests in turn, while the server processes 26 requests in batches of 5 with 5 minute intervals between batches?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Malyarov, 2019-02-05
@ Konstantin18ko

Decided by caching the page for 1 hour.
python manage.py createcachetable cache_table
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'cache_table',
}
}

from django.views.decorators.cache import cache_page

@cache_page(60 * 60) 
def get_ref_book_partial(request):
    if request.user.is_authenticated:
        ref_book_code = request.GET.get("ref_book_code")
        version = request.GET.get("version")
        part_number = request.GET.get("part_number")
        settings = zeep.Settings(strict=True)
        client = zeep.Client(wsdl=wsdl, settings=settings)
        return HttpResponse(
            str(
                client.service.getRefbookPartial(
                    user_key, ref_book_code, version, part_number
                )
            )
        )
    else:
        return redirect("/api-auth/login/", locals())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question