Skip to content

Commit

Permalink
Improve event performance
Browse files Browse the repository at this point in the history
  • Loading branch information
loleg committed Jun 22, 2023
1 parent 2788e23 commit 23a6fb4
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 29 deletions.
23 changes: 8 additions & 15 deletions dribdat/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,15 @@ def ProjectActivity(project, of_type, user, action=None, comments=None):
"""Generate an activity of a certain type in the project."""
activity = Activity(
name=of_type,
user_id=user.id,
project_id=project.id,
project_progress=project.progress,
project_version=project.versions.count(),
action=action
)
activity.user_id = user.id
# Regular posts are 1 star
score = 1
# Booster stars give projects double points
if of_type == 'boost':
score = 2
# Post comments are activity contents
if comments is not None and len(comments) > 3:
activity.content = comments
if project.score is None:
project.score = 0
# Check for existing stars
allstars = Activity.query.filter_by(
name='star',
Expand All @@ -228,15 +221,15 @@ def ProjectActivity(project, of_type, user, action=None, comments=None):
return # One star per user
elif of_type == 'unstar':
if allstars.count() > 0:
allstars[0].delete()
project.score = project.score - score
project.save()
return
allstars.first().delete()
activity = None
# Save current project score
project.score = project.score + score
activity.project_score = project.score
project.update()
project.save()
db.session.add(activity)
db.session.add(project)
if activity is not None:
activity.project_score = project.score
db.session.add(activity)
db.session.commit()


Expand Down
12 changes: 12 additions & 0 deletions dribdat/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def init_app(config_object=ProdConfig):
register_loggers(app)
register_shellcontext(app)
register_commands(app)
register_caching(app)
return app


Expand Down Expand Up @@ -189,3 +190,14 @@ def register_loggers(app):
stream_handler = logging.StreamHandler()
app.logger.addHandler(stream_handler)
app.logger.setLevel(logging.INFO)


def register_caching(app):
"""Prevent cached responses in debug."""
if 'DEBUG' in app.config and app.config['DEBUG']:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
3 changes: 2 additions & 1 deletion dribdat/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ class DevConfig(Config):
# Put the db file in project root
DB_PATH = os.path.join(Config.PROJECT_ROOT, DB_NAME)
SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(DB_PATH)
DEBUG_TB_ENABLED = True
CACHE_TYPE = 'NullCache' # Do not cache
DEBUG_TB_ENABLED = True # Enable the Debug Toolbar
ASSETS_DEBUG = True # Don't bundle/minify static assets
WTF_CSRF_ENABLED = False # Allows form testing

Expand Down
12 changes: 5 additions & 7 deletions dribdat/templates/macros/_event.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@
</div>
{% endif %}
</div>
{% if False and project.team_count > 0 %}
<!-- TODO: this is way too slow at the moment -->
<div class="team-counter"
title="{{ project.team_count }} supporting">
{% if project.team_count == 1 %}
{% if project.is_challenge %}
<div class="team-counter" title="Team size ({{ project.score }})">
{% if project.score == 1 %}
<i class="fa fa-user"></i>
{% elif project.team_count == 2 %}
{% elif project.score == 2 %}
<i class="fa fa-user"></i><i class="fa fa-user"></i>
{% elif project.team_count > 2 %}
{% elif project.score > 2 %}
<i class="fa fa-users"></i>
{% endif %}
</div>
Expand Down
5 changes: 3 additions & 2 deletions dribdat/templates/public/project.html
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,9 @@ <h5 class="modal-title" id="addUserLabel">Add user to team</h5>
</div>
<div class="modal-body">
<p class="text-small text-left">
Use the <a href="{{ url_for('admin.users')}}">admin</a>
to look up a user, and place the exact username here to add to the team.
Use the <a href="{{ url_for('public.all_participants')}}">participant list</a>
or <a href="{{ url_for('admin.users')}}">admin</a>
to look up a user. Then place the exact username here to add them to the team.
You can also remove a user with the <b>X</b> button next to their profile.
</p>
<form action="{{ url_for('project.project_star_user', project_id=project.id)}}" method="post">
Expand Down
7 changes: 4 additions & 3 deletions dribdat/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,12 +916,13 @@ def update_null_fields(self):

def calculate_score(self): # noqa: C901
"""Calculate score of a project based on base progress."""
if self.is_challenge:
return 0
score = self.progress or 0
cqu = Activity.query.filter_by(project_id=self.id)
c_s = cqu.count()
# Challenges only get a point per team-member
if self.is_challenge:
return cqu.filter_by(name='star').count()
# Get a point for every (join, update, comment ..) activity in dribs
c_s = cqu.count()
score = score + (1 * c_s)
# Extra point for every boost (upvote)
c_a = cqu.filter_by(name="boost").count()
Expand Down
24 changes: 24 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ def tests_project_box(self, db):
dpkg_html = box_project(project.url)
assert "onebox" in dpkg_html

def test_project_score(self, db):
"""Test role factory."""
project = ProjectFactory()
project.save()
assert project.is_challenge
project.update()
assert project.score == 0
user1 = UserFactory()
user1.save()
ProjectActivity(project, 'star', user1)
project.update()
assert project.score == 1
user2 = UserFactory()
user2.save()
ProjectActivity(project, 'star', user1)
project.update()
assert project.score == 1
ProjectActivity(project, 'star', user2)
project.update()
assert project.score == 2
ProjectActivity(project, 'unstar', user2)
project.update()
assert project.score == 1

# @pytest.mark.usefixtures('db')
# class TestResource:
# """Resource (Component) tests."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_github(self):

def test_gitlab(self):
"""Test parsing a GitLab readme."""
test_url = 'https://gitlab.com/dribdat/dribdat'
test_url = 'https://gitlab.com/seismist/dribdat'
test_obj = GetProjectData(test_url)
assert 'name' in test_obj
assert test_obj['name'] == 'dribdat'
Expand Down

0 comments on commit 23a6fb4

Please sign in to comment.