performance tweaks

This commit is contained in:
rimu 2024-02-13 21:28:33 +13:00
parent a66311d683
commit f7fea98dff
5 changed files with 54 additions and 5 deletions

View file

@ -60,6 +60,7 @@ def home_page(type, sort):
return return_304(current_etag)
page = request.args.get('page', 1, type=int)
low_bandwidth = request.cookies.get('low_bandwidth', '0') == '1'
if current_user.is_anonymous:
flash(_('Create an account to tailor this feed to your interests.'))
@ -110,7 +111,7 @@ def home_page(type, sort):
posts = posts.order_by(desc(Post.last_active))
# Pagination
posts = posts.paginate(page=page, per_page=100, error_out=False)
posts = posts.paginate(page=page, per_page=100 if current_user.is_authenticated and not low_bandwidth else 50, error_out=False)
if type == 'home':
next_url = url_for('main.index', page=posts.next_num, sort=sort) if posts.has_next else None
prev_url = url_for('main.index', page=posts.prev_num, sort=sort) if posts.has_prev and page != 1 else None
@ -125,7 +126,7 @@ def home_page(type, sort):
return render_template('index.html', posts=posts, active_communities=active_communities, show_post_community=True,
POST_TYPE_IMAGE=POST_TYPE_IMAGE, POST_TYPE_LINK=POST_TYPE_LINK,
low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1',
low_bandwidth=low_bandwidth,
SUBSCRIPTION_PENDING=SUBSCRIPTION_PENDING, SUBSCRIPTION_MEMBER=SUBSCRIPTION_MEMBER,
etag=f"{type}_{sort}_{hash(str(g.site.last_active))}", next_url=next_url, prev_url=prev_url,
rss_feed=f"https://{current_app.config['SERVER_NAME']}/feed",

View file

@ -103,7 +103,7 @@
</div>
<div class="comment_actions hidable">
{% if post.comments_enabled %}
<a href="{{ url_for('post.add_reply', post_id=post.id, comment_id=comment['comment'].id) }}" rel="nofollow"><span class="fe fe-reply"></span> reply</a>
<a href="{{ url_for('post.add_reply', post_id=post.id, comment_id=comment['comment'].id) }}" rel="nofollow noindex"><span class="fe fe-reply"></span> reply</a>
{% endif %}
<div class="voting_buttons_new">
{% with comment=comment['comment'] %}
@ -120,7 +120,7 @@
{% if current_user.is_authenticated and current_user.verified and current_user.id == comment['comment'].author.id %}
{% include "post/_reply_notification_toggle.html" %}
{% endif %}
<a href="{{ url_for('post.post_reply_options', post_id=post.id, comment_id=comment['comment'].id) }}" class="comment_actions_link" rel="nofollow"><span class="fe fe-options" title="Options"> </span></a>
<a href="{{ url_for('post.post_reply_options', post_id=post.id, comment_id=comment['comment'].id) }}" class="comment_actions_link" rel="nofollow noindex"><span class="fe fe-options" title="Options"> </span></a>
</div>
{% if comment['replies'] %}
{% if comment['comment'].depth <= THREAD_CUTOFF_DEPTH %}

View file

@ -62,7 +62,8 @@ def return_304(etag, content_type=None):
# Jinja: when a file was modified. Useful for cache-busting
def getmtime(filename):
return os.path.getmtime('static/' + filename)
if os.path.exists('static/' + filename):
return os.path.getmtime('static/' + filename)
# do a GET request to a uri, return the result

View file

@ -10,3 +10,9 @@ sudo systemctl restart celeryd or sudo service celeryd restart
*** check for celery-related problems by looking in /var/log/celery ***
for profiling:
use
python profile_app.py
instead of
flask run

41
profile_app.py Normal file
View file

@ -0,0 +1,41 @@
#!flask/bin/python
import os
from flask import session, g, json
from flask_babel import get_locale
from werkzeug.middleware.profiler import ProfilerMiddleware
from app import create_app, db, cli
from app.models import Site
from app.utils import gibberish, shorten_number, community_membership, getmtime, digits, user_access, ap_datetime, \
can_create, can_upvote, can_downvote, current_theme, shorten_string, shorten_url
app = create_app()
with app.app_context():
app.jinja_env.globals['getmtime'] = getmtime
app.jinja_env.globals['len'] = len
app.jinja_env.globals['digits'] = digits
app.jinja_env.globals['str'] = str
app.jinja_env.globals['shorten_number'] = shorten_number
app.jinja_env.globals['community_membership'] = community_membership
app.jinja_env.globals['json_loads'] = json.loads
app.jinja_env.globals['user_access'] = user_access
app.jinja_env.globals['ap_datetime'] = ap_datetime
app.jinja_env.globals['can_create'] = can_create
app.jinja_env.globals['can_upvote'] = can_upvote
app.jinja_env.globals['can_downvote'] = can_downvote
app.jinja_env.globals['theme'] = current_theme
app.jinja_env.globals['file_exists'] = os.path.exists
app.jinja_env.filters['shorten'] = shorten_string
app.jinja_env.filters['shorten_url'] = shorten_url
app.config['PROFILE'] = True
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
app.run(debug = True, host='127.0.0.1')
@app.before_request
def before_request():
session['nonce'] = gibberish()
g.locale = str(get_locale())
g.site = Site.query.get(1)