Skip to content

Commit

Permalink
Merge pull request #1339 from whylabs/client-cache-refactor
Browse files Browse the repository at this point in the history
Factor out the whylabs client cache so sessions can use it
  • Loading branch information
naddeoa authored Aug 30, 2023
2 parents df35a1c + a79b9f2 commit 84a7fd2
Show file tree
Hide file tree
Showing 13 changed files with 774 additions and 237 deletions.
151 changes: 137 additions & 14 deletions python/examples/integrations/Guest Session.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -28,17 +28,28 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initialing session with config /home/anthony/.config/whylogs/config.ini\n",
"Initializing session with config /home/anthony/.config/whylogs/config.ini\n",
"\n",
"✅ Using session type: WHYLABS_ANONYMOUS\n",
" ⤷ session id: session-XfQUnBhJ\n"
" ⤷ session id: session-6LpLjnAE\n"
]
},
{
"data": {
"text/plain": [
"<whylogs.api.whylabs.session.session.GuestSession at 0x7fc674559c70>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
Expand All @@ -49,17 +60,18 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"✅ Aggregated 48842 rows into profile foo\n",
"\n",
"Visualize and explore this profile with one-click\n",
"🔍 https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-547v42LMV5rKJi4R&sessionToken=session-XfQUnBhJ\n"
"🔍 https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-zv5Qm5zwJw0XEzpo&sessionToken=session-6LpLjnAE\n"
]
}
],
Expand All @@ -69,17 +81,18 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"✅ Aggregated 48842 rows into profile \n",
"\n",
"Visualize and explore this profile with one-click\n",
"🔍 https://hub.whylabsapp.com/resources/model-1/profiles?profile=1689292800000&sessionToken=session-XfQUnBhJ\n"
"🔍 https://hub.whylabsapp.com/resources/model-1/profiles?profile=1691712000000&sessionToken=session-6LpLjnAE\n"
]
}
],
Expand All @@ -90,37 +103,147 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"✅ Aggregated 48842 lines into profile 'foo', 48842 lines into profile 'bar'\n",
"\n",
"Visualize and explore the profiles with one-click\n",
"🔍 https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-6xJz70VbH7duHTaW&profile=ref-E2sM0Wd9RkPYPuu9&sessionToken=session-XfQUnBhJ\n",
"🔍 https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-aj7Q52Zszb0VhjeW&profile=ref-6awZJWQI347XFBgD&sessionToken=session-6LpLjnAE\n",
"\n",
"Or view each profile individually\n",
" ⤷ https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-6xJz70VbH7duHTaW&sessionToken=session-XfQUnBhJ\n",
" ⤷ https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-E2sM0Wd9RkPYPuu9&sessionToken=session-XfQUnBhJ\n"
" ⤷ https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-aj7Q52Zszb0VhjeW&sessionToken=session-6LpLjnAE\n",
" ⤷ https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-6awZJWQI347XFBgD&sessionToken=session-6LpLjnAE\n"
]
},
{
"data": {
"text/plain": [
"<whylogs.api.logger.result_set.ViewResultSet at 0x7f99ddb67670>"
"<whylogs.api.logger.result_set.ViewResultSet at 0x7fc5dc310d30>"
]
},
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"why.log(multiple={'foo': df, 'bar': df}) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Switch to an autheneticated session"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initializing session with config /home/anthony/.config/whylogs/config.ini\n",
"\n",
"✅ Using session type: WHYLABS\n",
" ⤷ org id: org-JpsdM6\n",
" ⤷ api key: MPq7Hg002z\n",
" ⤷ default dataset: model-62\n"
]
},
{
"data": {
"text/plain": [
"<whylogs.api.whylabs.session.session.ApiKeySession at 0x7fc6765e76a0>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"why.init(reinit=True, allow_anonymous=False, whylabs_api_key=\"MPq7Hg002z.Na5VweqsJfu5ArGILjQTlGAyPyOhtOnEVEtqY2b5PXNGJLZLjHscT:org-JpsdM6\", default_dataset_id=\"model-62\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"✅ Aggregated 48842 rows into profile real_dataset\n",
"\n",
"Visualize and explore this profile with one-click\n",
"🔍 https://hub.whylabsapp.com/resources/model-62/profiles?profile=ref-WvU6X5tH0Nrkh4a3\n"
]
}
],
"source": [
"profile = why.log(df, name=\"real_dataset\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Or upload via the whylabs writer\n",
"This will use the session for credentials as well, it just won't have all of the fancy output."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(True, 'log-KCaCKErR8Gi7TooV')]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"profile.writer('whylabs').write()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(True, 'ref-vdBRFKAO8y9J2C7M')]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# as a reference profile\n",
"profile.writer('whylabs').option(reference_profile_name=\"authenticated_ref\").write()"
]
}
],
"metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ def __init__(

@pytest.fixture(scope="module", autouse=True)
def init_tests() -> Generator[None, None, None]:
SessionManager.reset() # Ensure this test is in a good state from previous moduels
init(default_dataset_id=dataset_id)
yield
SessionManager._SessionManager__instance = None # type: ignore
SessionManager.reset() # Leave it in a good state for future modules


params = [MPRollingLogger, ThreadRollingLogger]
Expand Down
36 changes: 36 additions & 0 deletions python/tests/api/whylabs/session/test_whylabs_client_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from whylogs.api.whylabs.session.whylabs_client_cache import ClientCacheConfig


def test_empty_cache_key_works() -> None:
cache = dict()

key1 = ClientCacheConfig()
cache[key1] = 1

key2 = ClientCacheConfig()
cache[key2] = 2

assert len(cache) == 1
assert cache[key1] == 2


def test_cache_key_works() -> None:
cache = dict()

key1 = ClientCacheConfig(
api_key="api_key", endpoint_hostname="endpoint_hostname", whylabs_api_endpoint="whylabs_api_endpoint"
)
cache[key1] = 1

key2 = ClientCacheConfig(
api_key="api_key", endpoint_hostname="endpoint_hostname", whylabs_api_endpoint="whylabs_api_endpoint"
)
cache[key2] = 2

key3 = ClientCacheConfig(api_key="api_key")
cache[key3] = 3

assert len(cache) == 2
assert cache[key1] == 2
assert cache[key2] == 2
assert cache[key3] == 3
Loading

0 comments on commit 84a7fd2

Please sign in to comment.