Skip to content

Commit

Permalink
Merge pull request #14 from upstash/change_stats_to_info
Browse files Browse the repository at this point in the history
change stats to info
  • Loading branch information
burak-upstash committed Jan 30, 2024
2 parents f56e7c1 + e4f0bc4 commit ee4b819
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 52 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ res = index.delete("id1")
index.reset()
```

### Index Stats
### Index Info
```python
stats = index.stats()
# stats.vector_count: total number of vectors in the index
# stats.pending_vector_count: total number of vectors waiting to be indexed
# stats.index_size: total size of the index on disk in bytes
info = index.info()
# info.vector_count: total number of vectors in the index
# info.pending_vector_count: total number of vectors waiting to be indexed
# info.index_size: total size of the index on disk in bytes
# info.dimension: how many dimensions the index has
# info.similarity_function: similarity function chosen for the index
```

# Contributing
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "upstash-vector"
version = "0.1.6"
version = "0.2.0"
description = "Serverless Vector SDK from Upstash"
license = "MIT"
authors = ["Upstash <[email protected]>"]
Expand Down
37 changes: 37 additions & 0 deletions tests/core/test_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest

from tests import assert_eventually, assert_eventually_async
from upstash_vector import Index, AsyncIndex


def test_info(index: Index):
info = index.info()

assert info.vector_count == 0
assert info.pending_vector_count == 0
assert info.dimension == 2
assert info.similarity_function == "COSINE"

index.upsert([{"id": "foo", "vector": [0, 1]}])

def assertion():
assert index.info().vector_count == 1

assert_eventually(assertion)


@pytest.mark.asyncio
async def test_info_async(async_index: AsyncIndex):
info = await async_index.info()

assert info.vector_count == 0
assert info.pending_vector_count == 0
assert info.dimension == 2
assert info.similarity_function == "COSINE"

await async_index.upsert([{"id": "foo", "vector": [0, 1]}])

async def assertion():
assert (await async_index.info()).vector_count == 1

await assert_eventually_async(assertion)
33 changes: 0 additions & 33 deletions tests/core/test_stats.py

This file was deleted.

2 changes: 1 addition & 1 deletion upstash_vector/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.6"
__version__ = "0.2.0"

from upstash_vector.client import Index, AsyncIndex
from upstash_vector.types import Vector
Expand Down
24 changes: 14 additions & 10 deletions upstash_vector/core/index_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from upstash_vector.types import (
DeleteResult,
RangeResult,
StatsResult,
InfoResult,
SupportsToList,
Vector,
FetchResult,
Expand All @@ -21,7 +21,7 @@
RESET_PATH = "/reset"
RANGE_PATH = "/range"
FETCH_PATH = "/fetch"
STATS_PATH = "/stats"
INFO_PATH = "/info"


class IndexOperations:
Expand Down Expand Up @@ -213,16 +213,18 @@ def fetch(
for vector in self._execute_request(payload=payload, path=FETCH_PATH)
]

def stats(self) -> StatsResult:
def info(self) -> InfoResult:
"""
Returns the index statistics, including:
Returns the index info, including:
* total number of vectors
* total number of vectors waiting to be indexed
* total size of the index on disk in bytes
* dimension count for the index
* similarity function selected for the index
"""
return StatsResult._from_json(
self._execute_request(payload=None, path=STATS_PATH)
return InfoResult._from_json(
self._execute_request(payload=None, path=INFO_PATH)
)


Expand Down Expand Up @@ -420,14 +422,16 @@ async def fetch(
)
]

async def stats(self) -> StatsResult:
async def info(self) -> InfoResult:
"""
Returns the index statistics asynchronously, including:
Returns the index info asynchronously, including:
* total number of vectors
* total number of vectors waiting to be indexed
* total size of the index on disk in bytes
* dimension count for the index
* similarity function selected for the index
"""
return StatsResult._from_json(
await self._execute_request_async(payload=None, path=STATS_PATH)
return InfoResult._from_json(
await self._execute_request_async(payload=None, path=INFO_PATH)
)
9 changes: 7 additions & 2 deletions upstash_vector/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,20 @@ def _from_json(cls, obj: dict) -> "RangeResult":


@dataclass
class StatsResult:
class InfoResult:
vector_count: int
pending_vector_count: int
index_size: int
dimension: int
similarity_function: str

@classmethod
def _from_json(cls, obj: dict) -> "StatsResult":
def _from_json(cls, obj: dict) -> "InfoResult":
print(obj)
return cls(
vector_count=obj["vectorCount"],
pending_vector_count=obj["pendingVectorCount"],
index_size=obj["indexSize"],
dimension=obj["dimension"],
similarity_function=obj["similarityFunction"],
)

0 comments on commit ee4b819

Please sign in to comment.