Skip to content

Commit 3c3b3f0

Browse files
committed
add fallback for vector space ID in usage
1 parent a0e8726 commit 3c3b3f0

2 files changed

Lines changed: 22 additions & 15 deletions

File tree

tests/test_management.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,13 @@ def test_delete_vector_space():
164164

165165

166166
@pytest.mark.metrics
167-
class TestMetrics:
168-
169-
# Test getting monthly usage from Vecto
170-
def test_usage(self):
171-
from datetime import datetime
172-
today = datetime.now()
173-
usage_response = user_vecto.usage(user_vecto.vector_space_id, today.year, today.month)
174-
logger.info("Checking that usage returns a valid response.")
175-
assert usage_response is not None
176-
logger.info("Checking that usage for today is not empty.")
177-
assert usage_response.usage.lookups.dailyMetrics[today.day-1].count > 0
178-
assert usage_response.usage.indexing.dailyMetrics[today.day-1].count > 0
167+
def test_usage():
168+
from datetime import datetime
169+
today = datetime.now()
170+
usage_response = user_vecto.usage(today.year, today.month)
171+
logger.info("Checking that usage returns a valid response.")
172+
assert usage_response is not None
173+
logger.info("Checking that usage for today is not empty.")
174+
assert usage_response.usage.lookups.dailyMetrics[today.day-1].count > 0
175+
assert usage_response.usage.indexing.dailyMetrics[today.day-1].count > 0
179176

vecto/vecto_requests.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -941,15 +941,25 @@ def _parse_vecto_usage_metrics(self, u: Dict[str, Any]) -> VectoUsageMetrics:
941941
indexing=self._parse_usage_metric(u['indexing'])
942942
)
943943

944-
def usage(self, vector_space_id: int, year: int, month: int, **kwargs) -> MonthlyUsageResponse:
944+
def usage(self, year: int, month: int, vector_space_id: int = None, **kwargs) -> MonthlyUsageResponse:
945945
'''Return the usage metrics for the selected month
946946
947947
Args:
948-
vector_space_id (int): The ID of the vector space.
949948
year (int): The year for the usage data.
950949
month (int): The month for the usage data.
950+
vector_space_id (int, optional): The ID of the vector space. Defaults to None.
951951
**kwargs: Other keyword arguments for clients other than `requests`
952+
Returns:
953+
MonthlyUsageResponse: Named tuple that contains the usage metrics for a specified vector space and month.
952954
'''
955+
956+
# Use provided vector_space_id or fallback to self.vector_space_id
957+
vector_space_id = vector_space_id or getattr(self, 'vector_space_id', None)
958+
959+
# Raise an error if vector_space_id is still not available
960+
if vector_space_id is None:
961+
raise ValueError("A vector space ID must be provided either as a parameter or set in the instance.")
962+
953963
url = f"/api/v0/space/{vector_space_id}/usage/{year}/{month}"
954964
response = self._client.get(url, **kwargs)
955965
response_data = response.json()
@@ -962,4 +972,4 @@ def usage(self, vector_space_id: int, year: int, month: int, **kwargs) -> Monthl
962972
usage=usage_metrics
963973
)
964974

965-
return monthly_usage_response
975+
return monthly_usage_response

0 commit comments

Comments
 (0)