diff --git a/src/ahriman/web/routes.py b/src/ahriman/web/routes.py index 48ef29a9..9f50a8fa 100644 --- a/src/ahriman/web/routes.py +++ b/src/ahriman/web/routes.py @@ -23,6 +23,7 @@ from ahriman.web.views.ahriman import AhrimanView from ahriman.web.views.index import IndexView from ahriman.web.views.package import PackageView from ahriman.web.views.packages import PackagesView +from ahriman.web.views.status import StatusView def setup_routes(application: Application) -> None: @@ -44,6 +45,8 @@ def setup_routes(application: Application) -> None: GET /api/v1/package/:base get package base status POST /api/v1/package/:base update package base status + GET /api/v1/status get web service status itself + :param application: web application instance """ application.router.add_get("/", IndexView) @@ -58,3 +61,5 @@ def setup_routes(application: Application) -> None: application.router.add_delete("/api/v1/packages/{package}", PackageView) application.router.add_get("/api/v1/packages/{package}", PackageView) application.router.add_post("/api/v1/packages/{package}", PackageView) + + application.router.add_get("/api/v1/status", StatusView) diff --git a/src/ahriman/web/views/status.py b/src/ahriman/web/views/status.py new file mode 100644 index 00000000..a5726f64 --- /dev/null +++ b/src/ahriman/web/views/status.py @@ -0,0 +1,49 @@ +# +# Copyright (c) 2021 ahriman team. +# +# This file is part of ahriman +# (see https://github.com/arcan1s/ahriman). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +from aiohttp.web import Response, json_response + +import ahriman.version as version + +from ahriman.web.views.base import BaseView + + +class StatusView(BaseView): + """ + web service status web view + """ + + async def get(self) -> Response: + """ + get current service status + :return: 200 with service status object + """ + packages = self.service.packages + per_status = {"total": len(packages)} + for _, status in packages: + per_status.setdefault(status.status.name, 0) + per_status[status.status.name] += 1 + + data = { + "architecture": self.service.architecture, + "packages": per_status, + "repository": self.service.repository.name, + "version": version.__version__, + } + return json_response(data) diff --git a/tests/ahriman/web/views/test_view_status.py b/tests/ahriman/web/views/test_view_status.py new file mode 100644 index 00000000..5078870b --- /dev/null +++ b/tests/ahriman/web/views/test_view_status.py @@ -0,0 +1,22 @@ +from pytest_aiohttp import TestClient + +import ahriman.version as version + +from ahriman.models.build_status import BuildStatusEnum +from ahriman.models.package import Package + + +async def test_get(client: TestClient, package_ahriman: Package) -> None: + """ + must generate web service status correctly) + """ + await client.post(f"/api/v1/packages/{package_ahriman.base}", + json={"status": BuildStatusEnum.Success.value, "package": package_ahriman.view()}) + + response = await client.get("/api/v1/status") + assert response.status == 200 + + json = await response.json() + assert json["version"] == version.__version__ + assert json["packages"] + assert json["packages"]["total"] == 1