Skip to content

Commit

Permalink
Merge pull request #795 from roboflow/fix/handle-malformed-fps
Browse files Browse the repository at this point in the history
Handle malformed usage_fps
  • Loading branch information
grzegorz-roboflow authored Nov 12, 2024
2 parents fe65f9e + a9f4ee8 commit b3af5d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions inference/core/interfaces/http/http_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,7 @@ async def legacy_infer_from_request(
raise MissingServiceSecretError(
"Service secret is required to disable inference usage tracking"
)
logger.info("Not counting inference for usage")
else:
request_model_id = model_id
logger.debug(
Expand Down
10 changes: 7 additions & 3 deletions inference/usage_tracking/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import atexit
import json
import mimetypes
import numbers
import socket
import sys
import time
Expand Down Expand Up @@ -315,6 +316,7 @@ def _update_usage_payload(
fps: float = 0,
):
source = str(source) if source else ""
frames = frames if isinstance(frames, numbers.Number) else 0
api_key_hash = self._calculate_api_key_hash(api_key=api_key)
if not resource_id and resource_details:
resource_id = UsageCollector._calculate_resource_hash(resource_details)
Expand All @@ -332,7 +334,9 @@ def _update_usage_payload(
source_usage["timestamp_start"] = time.time_ns()
source_usage["timestamp_stop"] = time.time_ns()
source_usage["processed_frames"] += frames if not inference_test_run else 0
source_usage["fps"] = round(fps, 2)
source_usage["fps"] = (
round(fps, 2) if isinstance(fps, numbers.Number) else 0
)
source_usage["source_duration"] += (
frames / fps if fps and not inference_test_run else 0
)
Expand All @@ -355,7 +359,7 @@ def record_usage(
resource_id: str = "",
inference_test_run: bool = False,
fps: float = 0,
) -> DefaultDict[str, Any]:
):
if not api_key:
return
if self._settings.opt_out and not api_key:
Expand Down Expand Up @@ -388,7 +392,7 @@ async def async_record_usage(
resource_id: str = "",
inference_test_run: bool = False,
fps: float = 0,
) -> DefaultDict[str, Any]:
):
if self._async_lock:
async with self._async_lock:
self.record_usage(
Expand Down
28 changes: 28 additions & 0 deletions tests/inference/unit_tests/usage_tracking/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,3 +855,31 @@ def test_system_info_with_no_dedicated_deployment_id():
}
for k, v in expected_system_info.items():
assert system_info[k] == v


def test_record_malformed_usage():
# given
collector = UsageCollector()

# when
collector.record_usage(
source=None,
category="model",
frames=None,
api_key="fake",
resource_details=None,
resource_id=None,
inference_test_run=None,
fps=None,
)

# then
assert "fake" in collector._usage
assert "model:None" in collector._usage["fake"]
assert collector._usage["fake"]["model:None"]["processed_frames"] == 0
assert collector._usage["fake"]["model:None"]["fps"] == 0
assert collector._usage["fake"]["model:None"]["source_duration"] == 0
assert collector._usage["fake"]["model:None"]["category"] == "model"
assert collector._usage["fake"]["model:None"]["resource_id"] == None
assert collector._usage["fake"]["model:None"]["resource_details"] == "{}"
assert collector._usage["fake"]["model:None"]["api_key_hash"] == "fake"

0 comments on commit b3af5d9

Please sign in to comment.