add watcher cache support

This commit is contained in:
2021-03-20 05:42:33 +03:00
parent e7736e985f
commit 71196dc58b
7 changed files with 126 additions and 64 deletions

View File

@ -33,7 +33,7 @@ class AhrimanView(BaseView):
get current service status
:return: 200 with service status object
'''
return json_response(AhrimanView.status_view(self.service.status))
return json_response(self.service.status.view())
async def post(self) -> Response:
'''

View File

@ -17,14 +17,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from dataclasses import asdict
from typing import Any, Dict
from aiohttp.web import View
from ahriman.core.watcher.watcher import Watcher
from ahriman.models.build_status import BuildStatus
from ahriman.models.package import Package
class BaseView(View):
@ -39,28 +34,3 @@ class BaseView(View):
'''
watcher: Watcher = self.request.app['watcher']
return watcher
@staticmethod
def package_view(package: Package, status: BuildStatus) -> Dict[str, Any]:
'''
generate json package view
:param package: package definitions
:param status: package build status
:return: json-friendly dictionary
'''
return {
'status': BaseView.status_view(status),
'package': asdict(package)
}
@staticmethod
def status_view(status: BuildStatus) -> Dict[str, Any]:
'''
generate json status view
:param status: build status
:return: json-friendly dictionary
'''
return {
'status': status.status.value,
'timestamp': status.timestamp
}

View File

@ -41,7 +41,10 @@ class PackageView(BaseView):
except KeyError:
raise HTTPNotFound()
response = PackageView.package_view(package, status)
response = {
'package': package.view(),
'status': status.view()
}
return json_response(response)
async def delete(self) -> Response:
@ -71,7 +74,7 @@ class PackageView(BaseView):
data = await self.request.json()
try:
package = Package(**data['package']) if 'package' in data else None
package = Package.from_json(data['package']) if 'package' in data else None
status = BuildStatusEnum(data['status'])
except Exception as e:
raise HTTPBadRequest(text=str(e))

View File

@ -33,8 +33,10 @@ class PackagesView(BaseView):
:return: 200 with package description on success
'''
response = [
PackagesView.package_view(package, status)
for package, status in self.service.packages
{
'package': package.view(),
'status': status.view()
} for package, status in self.service.packages
]
return json_response(response)