Skip to content

Commit 4c90850

Browse files
authored
Merge pull request #104 from tharropoulos/v30-api-updates
V30 api updates
2 parents 9777059 + 60fda95 commit 4c90850

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2824
-569
lines changed

.github/workflows/test-and-lint.yml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@ jobs:
1212
strategy:
1313
matrix:
1414
python-version: ["3.9", "3.10", "3.11", "3.12"]
15-
services:
16-
typesense:
17-
image: typesense/typesense:28.0
18-
ports:
19-
- 8108:8108
20-
volumes:
21-
- /tmp/typesense-data:/data
22-
- /tmp/typesense-analytics:/analytics
23-
env:
24-
TYPESENSE_API_KEY: xyz
25-
TYPESENSE_DATA_DIR: /data
26-
TYPESENSE_ENABLE_CORS: true
27-
TYPESENSE_ANALYTICS_DIR: /analytics
28-
TYPESENSE_ENABLE_SEARCH_ANALYTICS: true
2915

3016
steps:
17+
- name: Start Typesense
18+
run: |
19+
docker run -d \
20+
-p 8108:8108 \
21+
--name typesense \
22+
-v /tmp/typesense-data:/data \
23+
-v /tmp/typesense-analytics-data:/analytics-data \
24+
typesense/typesense:30.0.alpha1 \
25+
--api-key=xyz \
26+
--data-dir=/data \
27+
--enable-search-analytics=true \
28+
--analytics-dir=/analytics-data \
29+
--analytics-flush-interval=60 \
30+
--analytics-minute-rate-limit=50 \
31+
--enable-cors
32+
3133
- name: Wait for Typesense
3234
run: |
3335
timeout 20 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:8108/health)" != "200" ]]; do sleep 1; done' || false

examples/analytics_operations.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
# Drop pre-existing rule if any
1414
try:
15-
client.analytics.rules['top_queries'].delete()
15+
client.analyticsV1.rules['top_queries'].delete()
1616
except Exception as e:
1717
pass
1818

1919
# Create a new rule
20-
create_response = client.analytics.rules.create({
20+
create_response = client.analyticsV1.rules.create({
2121
"name": "top_queries",
2222
"type": "popular_queries",
2323
"params": {
@@ -33,10 +33,10 @@
3333
print(create_response)
3434

3535
# Try to fetch it back
36-
print(client.analytics.rules['top_queries'].retrieve())
36+
print(client.analyticsV1.rules['top_queries'].retrieve())
3737

3838
# Update the rule
39-
update_response = client.analytics.rules.upsert('top_queries', {
39+
update_response = client.analyticsV1.rules.upsert('top_queries', {
4040
"name": "top_queries",
4141
"type": "popular_queries",
4242
"params": {
@@ -52,7 +52,7 @@
5252
print(update_response)
5353

5454
# List all rules
55-
print(client.analytics.rules.retrieve())
55+
print(client.analyticsV1.rules.retrieve())
5656

5757
# Delete the rule
58-
print(client.analytics.rules['top_queries'].delete())
58+
print(client.analyticsV1.rules['top_queries'].delete())

src/typesense/analytics.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,14 @@
1-
"""
2-
This module provides functionality for managing analytics in Typesense.
3-
4-
Classes:
5-
- Analytics: Handles operations related to analytics, including access to analytics rules.
6-
7-
Methods:
8-
- __init__: Initializes the Analytics object.
9-
10-
The Analytics class serves as an entry point for analytics-related operations in Typesense,
11-
currently providing access to AnalyticsRules.
12-
13-
For more information on analytics, refer to the Analytics & Query Suggestion
14-
[documentation](https://typesense.org/docs/27.0/api/analytics-query-suggestions.html)
15-
16-
This module uses type hinting and is compatible with Python 3.11+ as well as earlier
17-
versions through the use of the typing_extensions library.
18-
"""
1+
"""Client for Typesense Analytics module."""
192

3+
from typesense.analytics_events import AnalyticsEvents
204
from typesense.analytics_rules import AnalyticsRules
215
from typesense.api_call import ApiCall
226

237

24-
class Analytics(object):
25-
"""
26-
Class for managing analytics in Typesense.
27-
28-
This class provides access to analytics-related functionalities,
29-
currently including operations on analytics rules.
30-
31-
Attributes:
32-
rules (AnalyticsRules): An instance of AnalyticsRules for managing analytics rules.
33-
"""
8+
class Analytics:
9+
"""Client for v30 Analytics endpoints."""
3410

3511
def __init__(self, api_call: ApiCall) -> None:
36-
"""
37-
Initialize the Analytics object.
38-
39-
Args:
40-
api_call (ApiCall): The API call object for making requests.
41-
"""
12+
self.api_call = api_call
4213
self.rules = AnalyticsRules(api_call)
14+
self.events = AnalyticsEvents(api_call)

src/typesense/analytics_events.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Client for Analytics events and status operations."""
2+
3+
import sys
4+
5+
if sys.version_info >= (3, 11):
6+
import typing
7+
else:
8+
import typing_extensions as typing
9+
10+
from typesense.api_call import ApiCall
11+
from typesense.types.analytics import (
12+
AnalyticsEvent as AnalyticsEventSchema,
13+
AnalyticsEventCreateResponse,
14+
AnalyticsEventsResponse,
15+
AnalyticsStatus,
16+
)
17+
18+
19+
class AnalyticsEvents:
20+
events_path: typing.Final[str] = "/analytics/events"
21+
flush_path: typing.Final[str] = "/analytics/flush"
22+
status_path: typing.Final[str] = "/analytics/status"
23+
24+
def __init__(self, api_call: ApiCall) -> None:
25+
self.api_call = api_call
26+
27+
def create(self, event: AnalyticsEventSchema) -> AnalyticsEventCreateResponse:
28+
response: AnalyticsEventCreateResponse = self.api_call.post(
29+
AnalyticsEvents.events_path,
30+
body=event,
31+
as_json=True,
32+
entity_type=AnalyticsEventCreateResponse,
33+
)
34+
return response
35+
36+
def retrieve(
37+
self,
38+
*,
39+
user_id: str,
40+
name: str,
41+
n: int,
42+
) -> AnalyticsEventsResponse:
43+
params: typing.Dict[str, typing.Union[str, int]] = {
44+
"user_id": user_id,
45+
"name": name,
46+
"n": n,
47+
}
48+
response: AnalyticsEventsResponse = self.api_call.get(
49+
AnalyticsEvents.events_path,
50+
params=params,
51+
as_json=True,
52+
entity_type=AnalyticsEventsResponse,
53+
)
54+
return response
55+
56+
def flush(self) -> AnalyticsEventCreateResponse:
57+
response: AnalyticsEventCreateResponse = self.api_call.post(
58+
AnalyticsEvents.flush_path,
59+
body={},
60+
as_json=True,
61+
entity_type=AnalyticsEventCreateResponse,
62+
)
63+
return response
64+
65+
def status(self) -> AnalyticsStatus:
66+
response: AnalyticsStatus = self.api_call.get(
67+
AnalyticsEvents.status_path,
68+
as_json=True,
69+
entity_type=AnalyticsStatus,
70+
)
71+
return response
72+
73+

src/typesense/analytics_rule.py

Lines changed: 17 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,31 @@
1-
"""
2-
This module provides functionality for managing individual analytics rules in Typesense.
3-
4-
Classes:
5-
- AnalyticsRule: Handles operations related to a specific analytics rule.
6-
7-
Methods:
8-
- __init__: Initializes the AnalyticsRule object.
9-
- _endpoint_path: Constructs the API endpoint path for this specific analytics rule.
10-
- retrieve: Retrieves the details of this specific analytics rule.
11-
- delete: Deletes this specific analytics rule.
12-
13-
The AnalyticsRule class interacts with the Typesense API to manage operations on a
14-
specific analytics rule. It provides methods to retrieve and delete individual rules.
15-
16-
For more information on analytics, refer to the Analytics & Query Suggestion
17-
[documentation](https://typesense.org/docs/27.0/api/analytics-query-suggestions.html)
18-
19-
This module uses type hinting and is compatible with Python 3.11+ as well as earlier
20-
versions through the use of the typing_extensions library.
21-
"""
22-
23-
import sys
24-
25-
if sys.version_info >= (3, 11):
26-
import typing
27-
else:
28-
import typing_extensions as typing
1+
"""Per-rule client for Analytics rules operations."""
292

303
from typesense.api_call import ApiCall
31-
from typesense.types.analytics_rule import (
32-
RuleDeleteSchema,
33-
RuleSchemaForCounters,
34-
RuleSchemaForQueries,
35-
)
4+
from typesense.types.analytics import AnalyticsRuleSchema
365

376

387
class AnalyticsRule:
39-
"""
40-
Class for managing individual analytics rules in Typesense.
41-
42-
This class provides methods to interact with a specific analytics rule,
43-
including retrieving and deleting it.
44-
45-
Attributes:
46-
api_call (ApiCall): The API call object for making requests.
47-
rule_id (str): The ID of the analytics rule.
48-
"""
49-
50-
def __init__(self, api_call: ApiCall, rule_id: str):
51-
"""
52-
Initialize the AnalyticsRule object.
53-
54-
Args:
55-
api_call (ApiCall): The API call object for making requests.
56-
rule_id (str): The ID of the analytics rule.
57-
"""
8+
def __init__(self, api_call: ApiCall, rule_name: str) -> None:
589
self.api_call = api_call
59-
self.rule_id = rule_id
10+
self.rule_name = rule_name
11+
12+
@property
13+
def _endpoint_path(self) -> str:
14+
from typesense.analytics_rules import AnalyticsRules
6015

61-
def retrieve(
62-
self,
63-
) -> typing.Union[RuleSchemaForQueries, RuleSchemaForCounters]:
64-
"""
65-
Retrieve this specific analytics rule.
16+
return "/".join([AnalyticsRules.resource_path, self.rule_name])
6617

67-
Returns:
68-
Union[RuleSchemaForQueries, RuleSchemaForCounters]:
69-
The schema containing the rule details.
70-
"""
71-
response: typing.Union[RuleSchemaForQueries, RuleSchemaForCounters] = (
72-
self.api_call.get(
73-
self._endpoint_path,
74-
entity_type=typing.Union[RuleSchemaForQueries, RuleSchemaForCounters],
75-
as_json=True,
76-
)
18+
def retrieve(self) -> AnalyticsRuleSchema:
19+
response: AnalyticsRule = self.api_call.get(
20+
self._endpoint_path,
21+
as_json=True,
22+
entity_type=AnalyticsRule,
7723
)
7824
return response
7925

80-
def delete(self) -> RuleDeleteSchema:
81-
"""
82-
Delete this specific analytics rule.
83-
84-
Returns:
85-
RuleDeleteSchema: The schema containing the deletion response.
86-
"""
87-
response: RuleDeleteSchema = self.api_call.delete(
26+
def delete(self) -> AnalyticsRuleSchema:
27+
response: AnalyticsRule = self.api_call.delete(
8828
self._endpoint_path,
89-
entity_type=RuleDeleteSchema,
29+
entity_type=AnalyticsRule,
9030
)
91-
9231
return response
93-
94-
@property
95-
def _endpoint_path(self) -> str:
96-
"""
97-
Construct the API endpoint path for this specific analytics rule.
98-
99-
Returns:
100-
str: The constructed endpoint path.
101-
"""
102-
from typesense.analytics_rules import AnalyticsRules
103-
104-
return "/".join([AnalyticsRules.resource_path, self.rule_id])

0 commit comments

Comments
 (0)