Skip to content

Commit bb2a15c

Browse files
committed
Add analytics rules resource.
1 parent 765c996 commit bb2a15c

4 files changed

Lines changed: 100 additions & 1 deletion

File tree

examples/analytics_operations.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import typesense
2+
3+
client = typesense.Client({
4+
'api_key': 'abcd',
5+
'nodes': [{
6+
'host': 'localhost',
7+
'port': '8108',
8+
'protocol': 'http'
9+
}],
10+
'connection_timeout_seconds': 2
11+
})
12+
13+
# Drop pre-existing rule if any
14+
try:
15+
client.analytics_rules['top_queries'].delete()
16+
except Exception as e:
17+
pass
18+
19+
# Create a new rule
20+
create_response = client.analytics_rules.create({
21+
"name": "top_queries",
22+
"type": "popular_queries",
23+
"params": {
24+
"source": {
25+
"collections": ["products"]
26+
},
27+
"destination": {
28+
"collection": "top_queries"
29+
},
30+
"limit": 1000
31+
}
32+
})
33+
print(create_response)
34+
35+
# Try to fetch it back
36+
print(client.analytics_rules['top_queries'].retrieve())
37+
38+
# Update the rule
39+
update_response = client.analytics_rules.upsert('top_queries', {
40+
"name": "top_queries",
41+
"type": "popular_queries",
42+
"params": {
43+
"source": {
44+
"collections": ["products"]
45+
},
46+
"destination": {
47+
"collection": "top_queries"
48+
},
49+
"limit": 100
50+
}
51+
})
52+
print(update_response)
53+
54+
# List all rules
55+
print(client.analytics_rules.retrieve())
56+
57+
# Delete the rule
58+
print(client.analytics_rules['top_queries'].delete())

typesense/analytics_rule.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class AnalyticsRule(object):
2+
def __init__(self, api_call, rule_id):
3+
self.api_call = api_call
4+
self.rule_id = rule_id
5+
6+
def _endpoint_path(self):
7+
from .analytics_rules import AnalyticsRules
8+
return u"{0}/{1}".format(AnalyticsRules.RESOURCE_PATH, self.rule_id)
9+
10+
def retrieve(self):
11+
return self.api_call.get(self._endpoint_path())
12+
13+
def delete(self):
14+
return self.api_call.delete(self._endpoint_path())

typesense/analytics_rules.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from .analytics_rule import AnalyticsRule
2+
3+
4+
class AnalyticsRules(object):
5+
RESOURCE_PATH = '/analytics/rules'
6+
7+
def __init__(self, api_call):
8+
self.api_call = api_call
9+
self.rules = {}
10+
11+
def __getitem__(self, rule_id):
12+
if rule_id not in self.rules:
13+
self.rules[rule_id] = AnalyticsRule(self.api_call, rule_id)
14+
15+
return self.rules[rule_id]
16+
17+
def create(self, rule, params=None):
18+
params = params or {}
19+
return self.api_call.post(AnalyticsRules.RESOURCE_PATH, rule, params)
20+
21+
def upsert(self, id, rule):
22+
return self.api_call.put(u"{0}/{1}".format(AnalyticsRules.RESOURCE_PATH, id), rule)
23+
24+
def retrieve(self):
25+
return self.api_call.get(AnalyticsRules.RESOURCE_PATH)
26+

typesense/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .operations import Operations
77
from .configuration import Configuration
88
from .api_call import ApiCall
9-
9+
from .analytics_rules import AnalyticsRules
1010

1111
class Client(object):
1212
def __init__(self, config_dict):
@@ -16,5 +16,6 @@ def __init__(self, config_dict):
1616
self.multi_search = MultiSearch(self.api_call)
1717
self.keys = Keys(self.api_call)
1818
self.aliases = Aliases(self.api_call)
19+
self.analytics_rules = AnalyticsRules(self.api_call)
1920
self.operations = Operations(self.api_call)
2021
self.debug = Debug(self.api_call)

0 commit comments

Comments
 (0)