forked from pypi/warehouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: run tasks to calculate account-centric tasks (pypi#14143)
- Loading branch information
1 parent
87c081a
commit 8e7173e
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pretend | ||
|
||
from warehouse.accounts.tasks import compute_user_metrics | ||
|
||
from ...common.db.accounts import EmailFactory, UserFactory | ||
from ...common.db.packaging import ProjectFactory, ReleaseFactory | ||
|
||
|
||
def _create_email_project_with_release(user, verified): | ||
EmailFactory.create(user=user, verified=verified) | ||
result = ProjectFactory.create() | ||
ReleaseFactory.create(project=result, uploader=user) | ||
return result | ||
|
||
|
||
def test_compute_user_metrics(db_request, metrics): | ||
# Create an active user with no email | ||
UserFactory.create() | ||
# Create an inactive user | ||
UserFactory.create(is_active=False) | ||
# Create a user with an unverified email | ||
unverified_email_user = UserFactory.create() | ||
EmailFactory.create(user=unverified_email_user, verified=False) | ||
# Create a user with a verified email | ||
verified_email_user = UserFactory.create() | ||
EmailFactory.create(user=verified_email_user, verified=True) | ||
# Create a user with a verified email and a release | ||
verified_email_release_user = UserFactory.create() | ||
_create_email_project_with_release(verified_email_release_user, verified=True) | ||
# Create an active user with an unverified email and a release | ||
unverified_email_release_user = UserFactory.create(is_active=True) | ||
_create_email_project_with_release(unverified_email_release_user, verified=False) | ||
compute_user_metrics(db_request) | ||
|
||
assert metrics.gauge.calls == [ | ||
pretend.call("warehouse.users.count", 6), | ||
pretend.call("warehouse.users.count", 5, tags={"active": "true"}), | ||
pretend.call( | ||
"warehouse.users.count", 3, tags={"active": "true", "verified": "false"} | ||
), | ||
pretend.call( | ||
"warehouse.users.count", | ||
1, | ||
tags={"active": "true", "verified": "false", "releases": "true"}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from sqlalchemy import func | ||
|
||
from warehouse import tasks | ||
from warehouse.accounts.models import Email, User | ||
from warehouse.metrics import IMetricsService | ||
from warehouse.packaging.models import Release | ||
|
||
|
||
@tasks.task(ignore_result=True, acks_late=True) | ||
def compute_user_metrics(request): | ||
""" | ||
Report metrics about the users in the database. | ||
""" | ||
metrics = request.find_service(IMetricsService, context=None) | ||
|
||
# Total of users | ||
metrics.gauge( | ||
"warehouse.users.count", | ||
request.db.query(func.count(User.id)).scalar(), | ||
) | ||
|
||
# Total of active users | ||
metrics.gauge( | ||
"warehouse.users.count", | ||
request.db.query(func.count(User.id)).filter(User.is_active).scalar(), | ||
tags={"active": "true"}, | ||
) | ||
|
||
# Total active users with unverified emails | ||
metrics.gauge( | ||
"warehouse.users.count", | ||
request.db.query(func.count(User.id)) | ||
.outerjoin(Email) | ||
.filter(User.is_active) | ||
.filter((Email.verified == None) | (Email.verified == False)) # noqa E711 | ||
.scalar(), | ||
tags={"active": "true", "verified": "false"}, | ||
) | ||
|
||
# Total active users with unverified emails, and have project releases | ||
metrics.gauge( | ||
"warehouse.users.count", | ||
request.db.query(func.count(User.id)) | ||
.outerjoin(Email) | ||
.join(Release, Release.uploader_id == User.id) | ||
.filter(User.is_active) | ||
.filter((Email.verified == None) | (Email.verified == False)) # noqa E711 | ||
.scalar(), | ||
tags={"active": "true", "verified": "false", "releases": "true"}, | ||
) |