Skip to content

Commit

Permalink
Add more instance metadata to '/about' and '/api/meta'
Browse files Browse the repository at this point in the history
  • Loading branch information
hwittenborn committed Apr 7, 2022
1 parent 07e4c26 commit 7662a13
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
20 changes: 14 additions & 6 deletions aurweb/routers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from aurweb import config, db
from aurweb.models.account_type import TRUSTED_USER_ID
from aurweb.models.api_key import ApiKey
from aurweb.models.package import Package
from aurweb.models.package_base import PackageBase
from aurweb.models.package_notification import PackageNotification
from aurweb.models.package_request import CLOSED_ID, PENDING_ID, PackageRequest
from aurweb.models.request_type import RequestType
from aurweb.models.user import User
from aurweb.packages.util import get_pkg_or_base
from aurweb.routers.html import get_number_of_commits
from aurweb.templates import make_context, render_template

router = APIRouter()
Expand Down Expand Up @@ -54,13 +56,16 @@ async def api(request: Request):

@router.get("/api/meta")
async def api_meta():
packages = db.query(PackageBase).all()
packages = db.query(Package).count()
orphan_packages = (
db.query(PackageBase)
.filter(PackageBase.MaintainerUID == None) # noqa: E711
.all()
.count()
)
trusted_users = db.query(User).filter(User.AccountTypeID == TRUSTED_USER_ID).all()
users = db.query(User).count()
maintainers = db.query(PackageBase).group_by(PackageBase.MaintainerUID).count()
trusted_users = db.query(User).filter(User.AccountTypeID == TRUSTED_USER_ID).count()
commits = get_number_of_commits()

return {
"ssh_key_fingerprints": {
Expand All @@ -69,9 +74,12 @@ async def api_meta():
"RSA": config.get("fingerprints", "RSA"),
},
"statistics": {
"packages": len(packages),
"orphan_packages": len(orphan_packages),
"trusted_users": len(trusted_users),
"packages": packages,
"orphan_packages": orphan_packages,
"package_commits": commits,
"users": users,
"maintainers": maintainers,
"trusted_users": trusted_users,
},
}

Expand Down
49 changes: 49 additions & 0 deletions aurweb/routers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from http import HTTPStatus

import pygit2
from fastapi import APIRouter, Form, HTTPException, Request, Response
from fastapi.responses import HTMLResponse, RedirectResponse
from prometheus_client import (
Expand All @@ -18,8 +19,11 @@
import aurweb.models.package_request
from aurweb import cookies, db, models
from aurweb.auth import requires_auth
from aurweb.models.account_type import TRUSTED_USER_ID
from aurweb.models.package import Package
from aurweb.models.package_base import PackageBase
from aurweb.models.package_request import PENDING_ID
from aurweb.models.user import User
from aurweb.packages.search import PackageSearch
from aurweb.packages.util import updated_packages
from aurweb.templates import make_context, render_template
Expand Down Expand Up @@ -94,11 +98,56 @@ async def index(request: Request):
return render_template(request, "home.html", context)


def get_number_of_commits():
commits = 0

for directory in os.listdir("/aurweb/aur.git"):
repo = pygit2.Repository(f"/aurweb/aur.git/{directory}")
branch = repo.references.get("refs/heads/master")

# The branch won't exist for the repository if there haven't been any
# commits to it yet.
if branch is None:
continue

for commit in repo.walk(branch.target.hex):
commits += 1

return commits


@router.get("/about", response_class=HTMLResponse)
async def about(request: Request):
"""Instance information."""
context = make_context(request, "About")

# Get the number of packages.
context["number_of_packages"] = db.query(Package).count()

# Get the number of orphan packages.
context["number_of_orphaned_packages"] = (
db.query(PackageBase)
.filter(PackageBase.MaintainerUID == None) # noqa: E711
.count()
)

# Get the number of users.
context["number_of_users"] = db.query(User).count()

# Get the number of users who maintain a package (users who are maintainers).
context["number_of_maintainers"] = (
db.query(PackageBase).group_by(PackageBase.MaintainerUID).count()
)

# Get the number of Trusted Users.
context["number_of_trusted_users"] = (
db.query(User).filter(User.AccountTypeID == TRUSTED_USER_ID).count()
)

# Get the number of commits.
context["number_of_commits"] = get_number_of_commits()

# Get the list of SSH keys.
context["ssh_key_ed25519"] = aurweb.config.get("fingerprints", "Ed25519")
context["ssh_key_ecdsa"] = aurweb.config.get("fingerprints", "ECDSA")
context["ssh_key_rsa"] = aurweb.config.get("fingerprints", "RSA")
Expand Down
28 changes: 28 additions & 0 deletions templates/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,35 @@
<h1>About</h1>
<hr>
</div>
<div class="item">
<h2>Statistics</h2>
<hr>

<div class="p-block">
<p class="title">Packages:</p>
<p>{{ number_of_packages }}</p>
</div>
<div class="p-block">
<p class="title">Orphaned Packages:</p>
<p>{{ number_of_orphaned_packages }}</p>
</div>
<div class="p-block">
<p class="title">Package Commits:</p>
<p>{{ number_of_commits }}</p>
</div>
<div class="p-block">
<p class="title">Users:</p>
<p>{{ number_of_users }}</p>
</div>
<div class="p-block">
<p class="title">Maintainers:</p>
<p>{{ number_of_maintainers }}</p>
</div>
<div class="p-block">
<p class="title">Trusted Users:</p>
<p>{{ number_of_trusted_users }}</p>
</div>
</div>
<div class="item">
<h2>SSH Keys</h2>
<hr>
Expand Down
2 changes: 1 addition & 1 deletion templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="big-intro">
<h2>The makedeb Package Repository</h2>
<p>The community-maintained repository for the makedeb packaging tool.</p>
<p><a href="/packages">View available packages</a>, <a href="https://docs.makedeb.org/mpr/using-the-mpr/installing-packages/">install a package</a>, or <a href="https://docs.makedeb.org/mpr/using-the-mpr/uploading-pkgbuilds/">upload your own</a>.</p>
<p><a href="/packages">View available packages</a>, <a href="https://docs.makedeb.org/using-the-mpr/installing-packages">install a package</a>, or <a href="https://docs.makedeb.org/using-the-mpr/uploading-packages">upload your own</a>.</p>
</div>
<div class="packages">
<div class="newest-packages">
Expand Down

0 comments on commit 7662a13

Please sign in to comment.