Skip to content

Commit fba5eb3

Browse files
committed
add: analytics API
1 parent 9777059 commit fba5eb3

28 files changed

+1350
-536
lines changed

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: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
1-
"""
2-
This module provides functionality for managing analytics in Typesense.
1+
"""Client for Typesense Analytics module."""
32

4-
Classes:
5-
- Analytics: Handles operations related to analytics, including access to analytics rules.
3+
import sys
64

7-
Methods:
8-
- __init__: Initializes the Analytics object.
5+
if sys.version_info >= (3, 11):
6+
import typing
7+
else:
8+
import typing_extensions as typing
99

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-
"""
19-
20-
from typesense.analytics_rules import AnalyticsRules
2110
from typesense.api_call import ApiCall
11+
from typesense.analytics_events import AnalyticsEvents
12+
from typesense.analytics_rules import AnalyticsRules
2213

2314

24-
class Analytics(object):
25-
"""
26-
Class for managing analytics in Typesense.
15+
class Analytics:
16+
"""Client for v30 Analytics endpoints."""
2717

28-
This class provides access to analytics-related functionalities,
29-
currently including operations on analytics rules.
18+
def __init__(self, api_call: ApiCall) -> None:
19+
self.api_call = api_call
20+
self.rules = AnalyticsRules(api_call)
21+
self.events = AnalyticsEvents(api_call)
3022

31-
Attributes:
32-
rules (AnalyticsRules): An instance of AnalyticsRules for managing analytics rules.
33-
"""
3423

35-
def __init__(self, api_call: ApiCall) -> None:
36-
"""
37-
Initialize the Analytics object.
3824

39-
Args:
40-
api_call (ApiCall): The API call object for making requests.
41-
"""
42-
self.rules = AnalyticsRules(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 & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,4 @@
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-
"""
1+
"""Per-rule client for Analytics rules operations."""
222

233
import sys
244

@@ -28,77 +8,33 @@
288
import typing_extensions as typing
299

3010
from typesense.api_call import ApiCall
31-
from typesense.types.analytics_rule import (
32-
RuleDeleteSchema,
33-
RuleSchemaForCounters,
34-
RuleSchemaForQueries,
35-
)
11+
from typesense.types.analytics import AnalyticsRule
3612

3713

3814
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-
"""
15+
def __init__(self, api_call: ApiCall, rule_name: str) -> None:
5816
self.api_call = api_call
59-
self.rule_id = rule_id
17+
self.rule_name = rule_name
18+
19+
@property
20+
def _endpoint_path(self) -> str:
21+
from typesense.analytics_rules import AnalyticsRules
6022

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

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-
)
25+
def retrieve(self) -> AnalyticsRule:
26+
response: AnalyticsRule = self.api_call.get(
27+
self._endpoint_path,
28+
as_json=True,
29+
entity_type=AnalyticsRule,
7730
)
7831
return response
7932

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(
33+
def delete(self) -> AnalyticsRule:
34+
response: AnalyticsRule = self.api_call.delete(
8835
self._endpoint_path,
89-
entity_type=RuleDeleteSchema,
36+
entity_type=AnalyticsRule,
9037
)
91-
9238
return response
9339

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
10340

104-
return "/".join([AnalyticsRules.resource_path, self.rule_id])

src/typesense/analytics_rule_v1.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
This module provides functionality for managing individual analytics rules in Typesense (V1).
3+
4+
Classes:
5+
- AnalyticsRuleV1: Handles operations related to a specific analytics rule.
6+
7+
Methods:
8+
- __init__: Initializes the AnalyticsRuleV1 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 AnalyticsRuleV1 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
29+
30+
from typesense.api_call import ApiCall
31+
from typesense.types.analytics_rule_v1 import (
32+
RuleDeleteSchema,
33+
RuleSchemaForCounters,
34+
RuleSchemaForQueries,
35+
)
36+
37+
38+
class AnalyticsRuleV1:
39+
"""
40+
Class for managing individual analytics rules in Typesense (V1).
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 AnalyticsRuleV1 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+
"""
58+
self.api_call = api_call
59+
self.rule_id = rule_id
60+
61+
def retrieve(
62+
self,
63+
) -> typing.Union[RuleSchemaForQueries, RuleSchemaForCounters]:
64+
"""
65+
Retrieve this specific analytics rule.
66+
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+
)
77+
)
78+
return response
79+
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(
88+
self._endpoint_path,
89+
entity_type=RuleDeleteSchema,
90+
)
91+
92+
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_v1 import AnalyticsRulesV1
103+
104+
return "/".join([AnalyticsRulesV1.resource_path, self.rule_id])
105+
106+

0 commit comments

Comments
 (0)