From nobody Sat Dec 21 16:49:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=patchew-devel-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1645633003; cv=none; d=zohomail.com; s=zohoarc; b=Z2+wEBHCBtb4ef9HK1ck3f+bCnAp5WZ6iHrP7igJB0ruEvPH2BBKWfBLfjjTMMoT939nbpkkLhlYSvMacpLhKy2oTOF0R20Rrgru+hg0J2q5u+WN1FN42vY1m7WCCf2Dhv75yLeq0K4rysu1nZ3ccDxuKVp4XltNJ0oQlhIaLlo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1645633003; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=yh753vb6K9aSMpYYLJ0VHonyzpc1Qm/SxfiVNIratRI=; b=VYcDTO92z1YkwjA3U+VqvqY56pEKJH4QlWd0RrKalDSiw7JEr1JoByWdJUkjVykcPERNQ/lr1PvKocSNTL7R1U5wA2eZWl+brSvo2JB/hDy10GQsDLOm7Y00uUV6MdVx0laTfwUk6iG3E3FqdxeIXJyB08e2OXPkUyc52IvRZXs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1645633003629260.01728900311525; Wed, 23 Feb 2022 08:16:43 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-101-FiNcOBfSNAmlC-sIBz5rUw-1; Wed, 23 Feb 2022 11:16:41 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 17BB880DE1A; Wed, 23 Feb 2022 16:16:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0EE687DE5C; Wed, 23 Feb 2022 16:16:40 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0744046FA1; Wed, 23 Feb 2022 16:16:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 21NFnSkZ026363 for ; Wed, 23 Feb 2022 10:49:28 -0500 Received: by smtp.corp.redhat.com (Postfix) id 680C87F0D0; Wed, 23 Feb 2022 15:49:28 +0000 (UTC) Received: from avogadro.lan (unknown [10.39.193.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id D22B57DE49 for ; Wed, 23 Feb 2022 15:49:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645633002; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=yh753vb6K9aSMpYYLJ0VHonyzpc1Qm/SxfiVNIratRI=; b=Srmkg/lOelzAiNI5frBFomnF0j+IIKifNLneI6hmavuag4k8C2DJHRLKVe9CuWTYqa5nb8 Z3BETwuTG392beLjzp3rRcZu1WrI5sJnxYdAkH2JWKT5hBGIwDk7VLn+AYRWMsU3Z40UnQ jL7JRYdCdkGI5Wr97q5jOoiHa+opmfk= X-MC-Unique: FiNcOBfSNAmlC-sIBz5rUw-1 From: Paolo Bonzini To: patchew-devel@redhat.com Date: Wed, 23 Feb 2022 16:49:16 +0100 Message-Id: <20220223154916.274020-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: patchew-devel@redhat.com Subject: [Patchew-devel] [PATCH v2] series-list: optimize generation of page links X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=patchew-devel-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1645633004271100001 Content-Type: text/plain; charset="utf-8" In the worst case, a SELECT COUNT(*) has to do the same table scan as the actual query, while this is not true if the query has a LIMIT clause. So, pass the query directly to gen_page_links and just print "..." if there are more than 10 pages of results. Then, after gen_page_links, apply the order by clause for the final rendering of the results. Signed-off-by: Paolo Bonzini --- www/views.py | 68 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/www/views.py b/www/views.py index 0dbd344..25cdadf 100644 --- a/www/views.py +++ b/www/views.py @@ -132,27 +132,35 @@ def view_project_list(request): return render_page(request, "project-list.html", projects=3Dprepare_pr= ojects()) =20 =20 -def gen_page_links(total, cur_page, pagesize, extra_params): +def gen_page_links(query, cur_page, pagesize, extra_params): + # stop a little after the current page + limit =3D max(cur_page + 3, 10) + + # include one extra record in the limit, so that the final "..." can b= e printed, + # but do not go all the way to the end + total =3D query[:pagesize * limit + 1].count() max_page =3D int((total + pagesize - 1) / pagesize) + start =3D 10 if max_page <=3D 10 else 3 + ret =3D [] ddd =3D False - for i in range(1, max_page + 1): - if i =3D=3D cur_page: - ret.append( - { - "title": str(i), - "url": "?page=3D" + str(i) + extra_params, - "class": "active", - } - ) - ddd =3D False - elif i < 10 or abs(i - cur_page) < 3 or max_page - i < 3: - ret.append({"title": str(i), "url": "?page=3D" + str(i) + extr= a_params}) - ddd =3D False - else: + i =3D 1 + offset =3D 1 + while offset <=3D total: + if (i > start and abs(i - cur_page) >=3D 3 and max_page - i >=3D 3= ) or (offset > pagesize * limit): if not ddd: - ret.append({"title": "...", "class": "disabled", "url": "#= "}) - ddd =3D True + result =3D {"title": "...", "class": "disabled", "url": "#= "} + ret.append(result) + ddd =3D True + else: + result =3D {"title": str(i), "url": "?page=3D" + str(i) + extr= a_params} + if i =3D=3D cur_page: + result["class"] =3D "active" + ret.append(result) + ddd =3D False + + i +=3D 1 + offset +=3D pagesize =20 return ret =20 @@ -173,23 +181,13 @@ def prepare_navigate_list(cur, *path): return r =20 =20 -def render_series_list_page(request, query, search=3DNone, project=3DNone, +def render_series_list_page(request, base_query, search=3DNone, project=3D= None, title=3D'Search results', link_icon=3DNone, link_url=3DNone, link_text=3DNone, keywords=3D= [], is_search=3DFalse): sort =3D request.GET.get("sort") - if sort =3D=3D "replied": - query =3D query.order_by(F('last_reply_date').desc(nulls_last=3DTr= ue), '-date') - order_by_reply =3D True - else: - query =3D query.order_by('-date') - order_by_reply =3D False - query =3D query.prefetch_related("topic") cur_page =3D get_page_from_request(request) start =3D (cur_page - 1) * PAGE_SIZE - series =3D query[start : start + PAGE_SIZE] - if not series and cur_page > 1: - raise Http404("Page not found") params =3D "" if sort: params +=3D "&" + urllib.parse.urlencode({"sort": sort}) @@ -205,7 +203,19 @@ def render_series_list_page(request, query, search=3DN= one, project=3DNone, else: search =3D "project:%s" % project nav_path =3D prepare_navigate_list(project) - page_links =3D gen_page_links(query.count(), cur_page, PAGE_SIZE, para= ms) + page_links =3D gen_page_links(base_query, cur_page, PAGE_SIZE, params) + + if sort =3D=3D "replied": + query =3D base_query.order_by(F('last_reply_date').desc(nulls_last= =3DTrue), '-date') + order_by_reply =3D True + else: + query =3D base_query.order_by('-date') + order_by_reply =3D False + query =3D query.prefetch_related("topic") + series =3D query[start : start + PAGE_SIZE] + if not series and cur_page > 1: + raise Http404("Page not found") + if project: title +=3D ' for ' + project return render_page( --=20 2.34.1 _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://listman.redhat.com/mailman/listinfo/patchew-devel