Skip to content

Commit 0a3e3b1

Browse files
authored
Vb/fix dd method dashboard (#1880)
1 parent 2b74007 commit 0a3e3b1

File tree

7 files changed

+99
-63
lines changed

7 files changed

+99
-63
lines changed

libs/labelbox/mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ ignore_errors = True
1111

1212
[mypy-lbox.exceptions]
1313
ignore_missing_imports = True
14+
15+
[mypy-lbox.call_info"]
16+
ignore_missing_imports = True

libs/labelbox/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dependencies = [
1212
"tqdm>=4.66.2",
1313
"geojson>=3.1.0",
1414
"mypy==1.10.1",
15-
"lbox-clients==1.1.0",
15+
"lbox-clients==1.1.1",
1616
]
1717
readme = "README.md"
1818
requires-python = ">=3.9,<3.13"

libs/labelbox/src/labelbox/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ def enable_experimental(self) -> bool:
151151
def app_url(self) -> str:
152152
return self._request_client.app_url
153153

154+
def set_sdk_method(self, sdk_method: str):
155+
self._request_client.sdk_method = sdk_method
156+
157+
def unset_sdk_method(self):
158+
self._request_client.sdk_method = None
159+
154160
def execute(
155161
self,
156162
query=None,

libs/labelbox/src/labelbox/pagination.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
# Size of a single page in a paginated query.
22
from abc import ABC, abstractmethod
3-
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
4-
5-
from typing import TYPE_CHECKING
3+
from typing import (
4+
TYPE_CHECKING,
5+
Any,
6+
Callable,
7+
Dict,
8+
List,
9+
Optional,
10+
Tuple,
11+
Type,
12+
Union,
13+
)
14+
15+
from lbox.call_info import call_info_as_str
616

717
if TYPE_CHECKING:
818
from labelbox import Client
@@ -49,9 +59,11 @@ def __init__(
4959
self._fetched_all = False
5060
self._data: List[Dict[str, Any]] = []
5161
self._data_ind = 0
62+
self._client = client
63+
self._client.set_sdk_method(call_info_as_str())
5264

5365
pagination_kwargs = {
54-
"client": client,
66+
"client": self._client,
5567
"obj_class": obj_class,
5668
"dereferencing": dereferencing,
5769
"experimental": experimental,
@@ -72,11 +84,13 @@ def __iter__(self):
7284
def __next__(self):
7385
if len(self._data) <= self._data_ind:
7486
if self._fetched_all:
87+
self._client.unset_sdk_method()
7588
raise StopIteration()
7689

7790
page_data, self._fetched_all = self.paginator.get_next_page()
7891
self._data.extend(page_data)
7992
if len(page_data) == 0:
93+
self._client.unset_sdk_method()
8094
raise StopIteration()
8195

8296
rval = self._data[self._data_ind]

libs/lbox-clients/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "lbox-clients"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
description = "This module contains client sdk uses to conntect to the Labelbox API and backends"
55
authors = [
66
{ name = "Labelbox", email = "engineering@labelbox.com" }
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import inspect
2+
import re
3+
import sys
4+
from typing import TypedDict
5+
6+
7+
def python_version_info():
8+
version_info = sys.version_info
9+
10+
return f"{version_info.major}.{version_info.minor}.{version_info.micro}-{version_info.releaselevel}"
11+
12+
13+
LABELBOX_CALL_PATTERN = re.compile(r"/labelbox/")
14+
TEST_FILE_PATTERN = re.compile(r".*test.*\.py$")
15+
16+
17+
class _RequestInfo(TypedDict):
18+
prefix: str
19+
class_name: str
20+
method_name: str
21+
22+
23+
def call_info():
24+
method_name: str = "Unknown"
25+
prefix = ""
26+
class_name = ""
27+
skip_methods = ["wrapper", "__init__", "execute"]
28+
skip_classes = ["PaginatedCollection", "_CursorPagination", "_OffsetPagination"]
29+
30+
try:
31+
call_info = None
32+
for stack in reversed(inspect.stack()):
33+
if LABELBOX_CALL_PATTERN.search(stack.filename):
34+
call_info = stack
35+
method_name: str = call_info.function
36+
class_name = call_info.frame.f_locals.get(
37+
"self", None
38+
).__class__.__name__
39+
if method_name not in skip_methods:
40+
if class_name not in skip_classes:
41+
if TEST_FILE_PATTERN.search(call_info.filename):
42+
prefix = "test:"
43+
else:
44+
if class_name == "NoneType":
45+
class_name = ""
46+
break
47+
48+
except Exception:
49+
pass
50+
return _RequestInfo(prefix=prefix, class_name=class_name, method_name=method_name)
51+
52+
53+
def call_info_as_str():
54+
info: _RequestInfo = call_info()
55+
return f"{info['prefix']}{info['class_name']}:{info['method_name']}"

libs/lbox-clients/src/lbox/request_client.py

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,22 @@
11
# for the Labelbox Python SDK
2-
import inspect
32
import json
43
import logging
54
import os
6-
import re
7-
import sys
85
from datetime import datetime, timezone
96
from types import MappingProxyType
10-
from typing import Callable, Dict, Optional, TypedDict
7+
from typing import Callable, Dict, Optional
118

129
import requests
1310
import requests.exceptions
1411
from google.api_core import retry
15-
from lbox import exceptions # type: ignore
12+
from lbox import exceptions
13+
from lbox.call_info import call_info_as_str, python_version_info # type: ignore
1614

1715
logger = logging.getLogger(__name__)
1816

1917
_LABELBOX_API_KEY = "LABELBOX_API_KEY"
2018

2119

22-
def python_version_info():
23-
version_info = sys.version_info
24-
25-
return f"{version_info.major}.{version_info.minor}.{version_info.micro}-{version_info.releaselevel}"
26-
27-
28-
LABELBOX_CALL_PATTERN = re.compile(r"/labelbox/")
29-
TEST_FILE_PATTERN = re.compile(r".*test.*\.py$")
30-
31-
32-
class _RequestInfo(TypedDict):
33-
prefix: str
34-
class_name: str
35-
method_name: str
36-
37-
38-
def call_info():
39-
method_name = "Unknown"
40-
prefix = ""
41-
class_name = ""
42-
skip_methods = ["wrapper", "__init__"]
43-
skip_classes = ["PaginatedCollection", "_CursorPagination", "_OffsetPagination"]
44-
45-
try:
46-
call_info = None
47-
for stack in reversed(inspect.stack()):
48-
if LABELBOX_CALL_PATTERN.search(stack.filename):
49-
call_info = stack
50-
method_name = call_info.function
51-
class_name = call_info.frame.f_locals.get(
52-
"self", None
53-
).__class__.__name__
54-
55-
if method_name not in skip_methods and class_name not in skip_classes:
56-
if TEST_FILE_PATTERN.search(call_info.filename):
57-
prefix = "test:"
58-
else:
59-
if class_name == "NoneType":
60-
class_name = ""
61-
break
62-
63-
except Exception:
64-
pass
65-
return _RequestInfo(prefix=prefix, class_name=class_name, method_name=method_name)
66-
67-
68-
def call_info_as_str():
69-
info = call_info()
70-
return f"{info['prefix']}{info['class_name']}:{info['method_name']}"
71-
72-
7320
class RequestClient:
7421
"""A Labelbox request client.
7522
@@ -114,6 +61,7 @@ def __init__(
11461
self.endpoint = endpoint
11562
self.rest_endpoint = rest_endpoint
11663
self.sdk_version = sdk_version
64+
self._sdk_method = None
11765
self._connection: requests.Session = self._init_connection()
11866

11967
def _init_connection(self) -> requests.Session:
@@ -126,6 +74,14 @@ def _init_connection(self) -> requests.Session:
12674
def headers(self) -> MappingProxyType:
12775
return self._connection.headers
12876

77+
@property
78+
def sdk_method(self):
79+
return self._sdk_method
80+
81+
@sdk_method.setter
82+
def sdk_method(self, value):
83+
self._sdk_method = value
84+
12985
def _default_headers(self):
13086
return {
13187
"Authorization": "Bearer %s" % self.api_key,
@@ -234,7 +190,9 @@ def convert_value(value):
234190
if files:
235191
del headers["Content-Type"]
236192
del headers["Accept"]
237-
headers["X-SDK-Method"] = call_info_as_str()
193+
headers["X-SDK-Method"] = (
194+
self.sdk_method if self.sdk_method else call_info_as_str()
195+
)
238196

239197
request = requests.Request(
240198
"POST",

0 commit comments

Comments
 (0)