Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a /stats endpoint #4469

Merged
merged 3 commits into from
Aug 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/unit/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def add_policy(name, filename):
),
pretend.call("classifiers", "/classifiers/", domain=warehouse),
pretend.call("search", "/search/", domain=warehouse),
pretend.call("stats", "/stats/", domain=warehouse),
pretend.call(
"accounts.profile",
"/user/{username}/",
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
robotstxt,
opensearchxml,
search,
stats,
force_status,
flash_messages,
forbidden_include,
Expand Down Expand Up @@ -533,6 +534,24 @@ def test_classifiers(db_request):
}


def test_stats(db_request):

project = ProjectFactory.create()
release1 = ReleaseFactory.create(project=project)
release1.created = datetime.date(2011, 1, 1)
FileFactory.create(
release=release1,
filename="{}-{}.tar.gz".format(project.name, release1.version),
python_version="source",
size=69,
)

assert stats(db_request) == {
"total_packages_size": 69,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

"top_packages": [(project.name, 69)],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

}


def test_health():
request = pretend.stub(
db=pretend.stub(execute=pretend.call_recorder(lambda q: None))
Expand Down
3 changes: 3 additions & 0 deletions warehouse/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def includeme(config):
# Search Routes
config.add_route("search", "/search/", domain=warehouse)

# Stats Routes
config.add_route("stats", "/stats/", domain=warehouse)

# Accounts
config.add_route(
"accounts.profile",
Expand Down
49 changes: 49 additions & 0 deletions warehouse/templates/pages/stats.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{#
# 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.
-#}
{% extends "base.html" %}

{% block title %}Stats{% endblock %}

{% block content %}
<section class="horizontal-section">
<div class="narrow-container">
<h1 class="page-title">PyPI Stats</h1>
<p>We all love stats, so here are some useful ones about PyPI.</p>

<h2>Top Storage Users</h2>
<p>Here is a list of the top 100 pacakges based on sum of their
packages sizes (in bytes).</p>

<table id="all_pypi">
<tr>
<td>All of PyPI: </td>
<td>{{ total_packages_size }}</td>
</tr>
</table>
<br>
<table id="top_packages">
<tr>
<th>Package Name</th>
<th>Sum of Release Files (bytes)</th>
<tr>
{% for package in top_packages %}
<tr>
<td>{{ package[0] }}</td>
<td>{{ package[1] }}</td>
<tr>
{% endfor %}
</table>
</div>
</section>
{% endblock %}
17 changes: 17 additions & 0 deletions warehouse/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,23 @@ def filter_key(item):
}


@view_config(route_name="stats", renderer="pages/stats.html")
def stats(request):
total_size_query = request.db.query(func.sum(File.size)).all()
top_100_packages = (
request.db.query(File.name, func.sum(File.size))
.group_by(File.name)
.order_by(func.sum(File.size).desc())
.limit(100)
.all()
)

return {
"total_packages_size": total_size_query[0][0],
"top_packages": top_100_packages,
}


@view_config(
route_name="includes.current-user-indicator",
renderer="includes/current-user-indicator.html",
Expand Down