[Dashboard] Add utility functions for actor and memory APIs (#11011)

* Add actor and memory utility functions needed by upcoming logical view and memory view APIs

* Add a method to allow printing Dict custom class and add support for hot-reloading local dev environment.

* Address PR comments

* Add unit tests from test metrics to branch for new memory_utils module

* Add note about sorting / grouping

* lint

Co-authored-by: Max Fitton <max@semprehealth.com>
This commit is contained in:
Max Fitton
2020-10-01 23:48:03 -07:00
committed by GitHub
co-authored by Max Fitton
parent 874da9a25f
commit 9a6d01ebf9
4 changed files with 636 additions and 2 deletions
+14 -2
View File
@@ -15,7 +15,7 @@ from base64 import b64decode
from collections.abc import MutableMapping, Mapping
from collections import namedtuple
from typing import Any
import os
import aioredis
import aiohttp.web
from aiohttp import hdrs
@@ -218,13 +218,21 @@ class CustomEncoder(json.JSONEncoder):
async def rest_response(success, message, **kwargs) -> aiohttp.web.Response:
# In the dev context we allow a dev server running on a
# different port to consume the API, meaning we need to allow
# cross-origin access
if os.environ.get("RAY_DASHBOARD_DEV") == "1":
headers = {"Access-Control-Allow-Origin": "*"}
else:
headers = {}
return aiohttp.web.json_response(
{
"result": success,
"msg": message,
"data": to_google_style(kwargs)
},
dumps=functools.partial(json.dumps, cls=CustomEncoder))
dumps=functools.partial(json.dumps, cls=CustomEncoder),
headers=headers)
def to_camel_case(snake_str):
@@ -238,6 +246,7 @@ def to_camel_case(snake_str):
def to_google_style(d):
"""Recursive convert all keys in dict to google style."""
new_dict = {}
for k, v in d.items():
if isinstance(v, dict):
new_dict[to_camel_case(k)] = to_google_style(v)
@@ -387,6 +396,9 @@ class Dict(MutableMapping):
def __iter__(self):
return iter(copy.deepcopy(self._data))
def __str__(self):
return str(self._data)
def reset(self, d):
assert isinstance(d, Mapping)
for key in self._data.keys() - d.keys():