1- __author__ = 'dkador'
1+ import os
2+ from keen .client import KeenClient
3+ from keen .exceptions import InvalidEnvironmentError
4+
5+ __author__ = 'dkador'
6+
7+ _client = None
8+ project_id = None
9+ write_key = None
10+ read_key = None
11+
12+
13+ def _initialize_client_from_environment ():
14+ global _client , project_id , write_key , read_key
15+
16+ if _client is None :
17+ # check environment for project ID and keys
18+ project_id = project_id or os .environ .get ("KEEN_PROJECT_ID" )
19+ write_key = write_key or os .environ .get ("KEEN_WRITE_KEY" )
20+ read_key = read_key or os .environ .get ("KEEN_READ_KEY" )
21+
22+ if not project_id :
23+ raise InvalidEnvironmentError ("Please set the KEEN_PROJECT_ID environment variable or set keen.project_id!" )
24+
25+ _client = KeenClient (project_id ,
26+ write_key = write_key ,
27+ read_key = read_key )
28+
29+
30+ def add_event (event_collection , body , timestamp = None ):
31+ _initialize_client_from_environment ()
32+ _client .add_event (event_collection , body , timestamp = timestamp )
33+
34+
35+ def count (event_collection , timeframe = None , timezone = None , interval = None , filters = None , group_by = None ):
36+ """ Performs a count query
37+
38+ Counts the number of events that meet the given criteria.
39+
40+ :param event_collection: string, the name of the collection to query
41+ :param timeframe: string or dict, the timeframe in which the events
42+ happened example: "previous_7_days"
43+ :param timezone: int, the timezone you'd like to use for the timeframe
44+ and interval in seconds
45+ :param interval: string, the time interval used for measuring data over
46+ time example: "daily"
47+ :param filters: array of dict, contains the filters you'd like to apply to the data
48+ example: {["property_name":"device", "operator":"eq", "property_value":"iPhone"}]
49+ :param group_by: string or array of strings, the name(s) of the properties you would
50+ like to group you results by. example: "customer.id" or ["browser","operating_system"]
51+
52+ """
53+ _initialize_client_from_environment ()
54+ return _client .count (event_collection = event_collection , timeframe = timeframe , timezone = timezone ,
55+ interval = interval , filters = filters , group_by = group_by )
56+
57+
58+ def sum (event_collection , target_property , timeframe = None , timezone = None , interval = None , filters = None ,
59+ group_by = None ):
60+ """ Performs a sum query
61+
62+ Adds the values of a target property for events that meet the given criteria.
63+
64+ :param event_collection: string, the name of the collection to query
65+ :param target_property: string, the name of the event property you would like use
66+ :param timeframe: string or dict, the timeframe in which the events
67+ happened example: "previous_7_days"
68+ :param timezone: int, the timezone you'd like to use for the timeframe
69+ and interval in seconds
70+ :param interval: string, the time interval used for measuring data over
71+ time example: "daily"
72+ :param filters: array of dict, contains the filters you'd like to apply to the data
73+ example: {["property_name":"device", "operator":"eq", "property_value":"iPhone"}]
74+ :param group_by: string or array of strings, the name(s) of the properties you would
75+ like to group you results by. example: "customer.id" or ["browser","operating_system"]
76+
77+ """
78+ _initialize_client_from_environment ()
79+ return _client .sum (event_collection = event_collection , timeframe = timeframe , timezone = timezone ,
80+ interval = interval , filters = filters , group_by = group_by , target_property = target_property )
81+
82+
83+ def minimum (event_collection , target_property , timeframe = None , timezone = None , interval = None , filters = None ,
84+ group_by = None ):
85+ """ Performs a minimum query
86+
87+ Finds the minimum value of a target property for events that meet the given criteria.
88+
89+ :param event_collection: string, the name of the collection to query
90+ :param target_property: string, the name of the event property you would like use
91+ :param timeframe: string or dict, the timeframe in which the events
92+ happened example: "previous_7_days"
93+ :param timezone: int, the timezone you'd like to use for the timeframe
94+ and interval in seconds
95+ :param interval: string, the time interval used for measuring data over
96+ time example: "daily"
97+ :param filters: array of dict, contains the filters you'd like to apply to the data
98+ example: {["property_name":"device", "operator":"eq", "property_value":"iPhone"}]
99+ :param group_by: string or array of strings, the name(s) of the properties you would
100+ like to group you results by. example: "customer.id" or ["browser","operating_system"]
101+
102+ """
103+ _initialize_client_from_environment ()
104+ return _client .minimum (event_collection = event_collection , timeframe = timeframe , timezone = timezone ,
105+ interval = interval , filters = filters , group_by = group_by , target_property = target_property )
106+
107+
108+ def maximum (event_collection , target_property , timeframe = None , timezone = None , interval = None , filters = None ,
109+ group_by = None ):
110+ """ Performs a maximum query
111+
112+ Finds the maximum value of a target property for events that meet the given criteria.
113+
114+ :param event_collection: string, the name of the collection to query
115+ :param target_property: string, the name of the event property you would like use
116+ :param timeframe: string or dict, the timeframe in which the events
117+ happened example: "previous_7_days"
118+ :param timezone: int, the timezone you'd like to use for the timeframe
119+ and interval in seconds
120+ :param interval: string, the time interval used for measuring data over
121+ time example: "daily"
122+ :param filters: array of dict, contains the filters you'd like to apply to the data
123+ example: {["property_name":"device", "operator":"eq", "property_value":"iPhone"}]
124+ :param group_by: string or array of strings, the name(s) of the properties you would
125+ like to group you results by. example: "customer.id" or ["browser","operating_system"]
126+
127+ """
128+ _initialize_client_from_environment ()
129+ return _client .maximum (event_collection = event_collection , timeframe = timeframe , timezone = timezone ,
130+ interval = interval , filters = filters , group_by = group_by , target_property = target_property )
131+
132+
133+ def average (event_collection , target_property , timeframe = None , timezone = None , interval = None , filters = None ,
134+ group_by = None ):
135+ """ Performs a average query
136+
137+ Finds the average of a target property for events that meet the given criteria.
138+
139+ :param event_collection: string, the name of the collection to query
140+ :param target_property: string, the name of the event property you would like use
141+ :param timeframe: string or dict, the timeframe in which the events
142+ happened example: "previous_7_days"
143+ :param timezone: int, the timezone you'd like to use for the timeframe
144+ and interval in seconds
145+ :param interval: string, the time interval used for measuring data over
146+ time example: "daily"
147+ :param filters: array of dict, contains the filters you'd like to apply to the data
148+ example: {["property_name":"device", "operator":"eq", "property_value":"iPhone"}]
149+ :param group_by: string or array of strings, the name(s) of the properties you would
150+ like to group you results by. example: "customer.id" or ["browser","operating_system"]
151+
152+ """
153+ _initialize_client_from_environment ()
154+ return _client .average (event_collection = event_collection , timeframe = timeframe , timezone = timezone ,
155+ interval = interval , filters = filters , group_by = group_by , target_property = target_property )
156+
157+
158+ def count_unique (event_collection , target_property , timeframe = None , timezone = None , interval = None ,
159+ filters = None , group_by = None ):
160+ """ Performs a count unique query
161+
162+ Counts the unique values of a target property for events that meet the given criteria.
163+
164+ :param event_collection: string, the name of the collection to query
165+ :param target_property: string, the name of the event property you would like use
166+ :param timeframe: string or dict, the timeframe in which the events
167+ happened example: "previous_7_days"
168+ :param timezone: int, the timezone you'd like to use for the timeframe
169+ and interval in seconds
170+ :param interval: string, the time interval used for measuring data over
171+ time example: "daily"
172+ :param filters: array of dict, contains the filters you'd like to apply to the data
173+ example: {["property_name":"device", "operator":"eq", "property_value":"iPhone"}]
174+ :param group_by: string or array of strings, the name(s) of the properties you would
175+ like to group you results by. example: "customer.id" or ["browser","operating_system"]
176+
177+ """
178+ _initialize_client_from_environment ()
179+ return _client .count_unique (event_collection = event_collection , timeframe = timeframe , timezone = timezone ,
180+ interval = interval , filters = filters , group_by = group_by , target_property = target_property )
181+
182+
183+ def select_unique (event_collection , target_property , timeframe = None , timezone = None , interval = None ,
184+ filters = None , group_by = None ):
185+ """ Performs a select unique query
186+
187+ Returns an array of the unique values of a target property for events that meet the given criteria.
188+
189+ :param event_collection: string, the name of the collection to query
190+ :param target_property: string, the name of the event property you would like use
191+ :param timeframe: string or dict, the timeframe in which the events
192+ happened example: "previous_7_days"
193+ :param timezone: int, the timezone you'd like to use for the timeframe
194+ and interval in seconds
195+ :param interval: string, the time interval used for measuring data over
196+ time example: "daily"
197+ :param filters: array of dict, contains the filters you'd like to apply to the data
198+ example: {["property_name":"device", "operator":"eq", "property_value":"iPhone"}]
199+ :param group_by: string or array of strings, the name(s) of the properties you would
200+ like to group you results by. example: "customer.id" or ["browser","operating_system"]
201+
202+ """
203+ _initialize_client_from_environment ()
204+ return _client .select_unique (event_collection = event_collection , timeframe = timeframe , timezone = timezone ,
205+ interval = interval , filters = filters , group_by = group_by , target_property = target_property )
206+
207+
208+ def extraction (event_collection , timeframe = None , timezone = None , filters = None , latest = None , email = None ):
209+ """ Performs a data extraction
210+
211+ Returns either a JSON object of events or a response
212+ indicating an email will be sent to you with data.
213+
214+ :param event_collection: string, the name of the collection to query
215+ :param timeframe: string or dict, the timeframe in which the events
216+ happened example: "previous_7_days"
217+ :param timezone: int, the timezone you'd like to use for the timeframe
218+ and interval in seconds
219+ :param filters: array of dict, contains the filters you'd like to apply to the data
220+ example: {["property_name":"device", "operator":"eq", "property_value":"iPhone"}]
221+ :param latest: int, the number of most recent records you'd like to return
222+ :param email: string, optional string containing an email address to email results to
223+
224+ """
225+ _initialize_client_from_environment ()
226+ return _client .extraction (event_collection = event_collection , timeframe = timeframe , timezone = timezone ,
227+ filters = filters , latest = latest , email = email )
228+
229+
230+ def funnel (steps , timeframe = None , timezone = None ):
231+ """ Performs a Funnel query
232+
233+ Returns an object containing the results for each step of the funnel.
234+
235+ :param steps: array of dictionaries, one for each step. example:
236+ [{"event_collection":"signup","actor_property":"user.id"},
237+ {"event_collection":"purchase","actor_property:"user.id"}]
238+ :param timeframe: string or dict, the timeframe in which the events
239+ happened example: "previous_7_days"
240+ :param timezone: int, the timezone you'd like to use for the timeframe
241+ and interval in seconds
242+
243+ """
244+ _initialize_client_from_environment ()
245+ return _client .funnel (steps = steps , timeframe = timeframe , timezone = timezone )
246+
247+
248+ def multi_analysis (event_collection , analyses , timeframe = None , timezone = None , filters = None , group_by = None ):
249+ """ Performs a multi-analysis query
250+
251+ Returns a dictionary of analysis results.
252+
253+ :param event_collection: string, the name of the collection to query
254+ :param analyses: dict, the types of analyses you'd like to run. example:
255+ {"total money made":{"analysis_type":"sum","target_property":"purchase.price",
256+ "average price":{"analysis_type":"average","target_property":"purchase.price"}
257+ :param timeframe: string or dict, the timeframe in which the events
258+ happened example: "previous_7_days"
259+ :param timezone: int, the timezone you'd like to use for the timeframe
260+ and interval in seconds
261+ :param filters: array of dict, contains the filters you'd like to apply to the data
262+ example: {["property_name":"device", "operator":"eq", "property_value":"iPhone"}]
263+ :param group_by: string or array of strings, the name(s) of the properties you would
264+ like to group you results by. example: "customer.id" or ["browser","operating_system"]
265+
266+ """
267+ _initialize_client_from_environment ()
268+ return _client .multi_analysis (event_collection = event_collection , timeframe = timeframe , timezone = timezone ,
269+ filters = filters , group_by = group_by , analyses = analyses )
0 commit comments