def test_invalidurl(self): try: EventClient(access_key=access_key, url="invalid") self.fail() except InvalidArgumentError as ex: print("The expected error: {0}".format(ex))
def test_eventclient_channel(self): subprocess.call(['pio', 'app', 'channel-new', app_name, channel]) client = EventClient(access_key=access_key, url="http://127.0.0.1:7070", channel=channel) # Check status print("Check status") print(client.get_status()) self.assertEqual(client.get_status(), {'status': 'alive'}) # First event first_event_properties = { "prop1": 1, "prop2": "value2", "prop3": [1, 2, 3], "prop4": True, "prop5": ["a", "b", "c"], "prop6": 4.56, } first_event_time = datetime(2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain')) first_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", properties=first_event_properties, event_time=first_event_time, ) print("First Event response") print(first_event_response) self.assertEqual(first_event_response.status, 201) # Second event second_event_properties = { "someProperty": "value1", "anotherProperty": "value2", } second_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", target_entity_type="item", target_entity_id="iid", properties=second_event_properties, event_time=datetime(2014, 12, 13, 21, 38, 45, 618000, pytz.utc)) print("Second Event response") print(second_event_response) self.assertEqual(second_event_response.status, 201) # Get the first event from Event Server first_event_id = first_event_response.json_body["eventId"] print("Get Event") event = client.get_event(first_event_id) print(event) self.assertEqual(event.get('eventId'), first_event_id) # Delete the first event from Event Server print("Delete Event") delete_response = client.delete_event(first_event_id) print(delete_response) self.assertEqual(delete_response.decode('utf-8'), '{"message":"Found"}') # Delete the first event from Event Server again should yield exception. print("Delete Event Again") try: delete_response = client.delete_event(first_event_id) self.fail() except NotFoundError as ex: print("The expected error: {0}".format(ex))
def test_eventclient(self): client = EventClient(access_key=access_key, url="http://127.0.0.1:7070") # Check status print("Check status") print(client.get_status()) self.assertEqual(client.get_status(), {'status': 'alive'}) # First event first_event_properties = { "prop1": 1, "prop2": "value2", "prop3": [1, 2, 3], "prop4": True, "prop5": ["a", "b", "c"], "prop6": 4.56, } first_event_time = datetime(2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain')) first_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", properties=first_event_properties, event_time=first_event_time, ) print("First Event response") print(first_event_response) self.assertEqual(first_event_response.status, 201) # Second event second_event_properties = { "someProperty": "value1", "anotherProperty": "value2", } second_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", target_entity_type="item", target_entity_id="iid", properties=second_event_properties, event_time=datetime(2014, 12, 13, 21, 38, 45, 618000, pytz.utc)) print("Second Event response") print(second_event_response) self.assertEqual(second_event_response.status, 201) # Get the first event from Event Server first_event_id = first_event_response.json_body["eventId"] print("Get Event") event = client.get_event(first_event_id) print(event) self.assertEqual(event.get('eventId'), first_event_id) # Delete the first event from Event Server print("Delete Event") delete_response = client.delete_event(first_event_id) print(delete_response) self.assertEqual(delete_response.decode('utf-8'), '{"message":"Found"}') # Delete the first event from Event Server again should yield exception. print("Delete Event Again") try: delete_response = client.delete_event(first_event_id) self.fail() except NotFoundError as ex: print("The expected error: {0}".format(ex)) # "user"-helper methods # Set user properties implicitly create a user # This call creates a user "foo", and set the properties of "foo". print("Create user foo") foo_properties = {"city": "sunnyvale", "car": "honda fit"} response = client.set_user("foo", properties=foo_properties) print(response) self.assertEqual(response.status, 201) # This call overrides the existing properties for user "foo", setting "car" to # a new "honda odyssey" and create a new property "food" to "seafood". print("Set new properties") foo_properties = {"car": "honda odyssey", "food": "seafood"} response = client.set_user("foo", properties=foo_properties) print(response) self.assertEqual(response.status, 201) # This call removes the specified properties. It ignores the value of the dict. # After this call, the "city" will become an unset field. print("Unset properties") foo_properties = {"city": "x"} response = client.unset_user("foo", properties=foo_properties) print(response) self.assertEqual(response.status, 201) # This call deletes a user print("Delete user") response = client.delete_user("foo") print(response) self.assertEqual(response.status, 201) # The SDK also support specifying the eventTime. It is useful for importing # events happened in the past. foo_time = datetime(2014, 8, 31, 4, 56, tzinfo=pytz.timezone('US/Pacific')) print("Create user at " + str(foo_time)) response = client.set_user("Jarvis", {}, foo_time) print(response) self.assertEqual(response.status, 201) # "item"-helper methods # Set item properties implicitly create a item # This call creates a item "bar", and set the properties of "bar". print("Create item bar") bar_properties = {"city": "santa clara", "weight": 6.9} response = client.set_item("bar", properties=bar_properties) print(response) self.assertEqual(response.status, 201) # Similar to user-methods, we can do the same thing with item print("Set new properties") bar_properties = {"weight": 6.2} response = client.set_item("bar", properties=bar_properties) print(response) self.assertEqual(response.status, 201) # This call removes the specified properties. It ignores the value of the dict. # After this call, the "city" will become an unset field. print("Unset properties") bar_properties = {"city": None} response = client.unset_item("bar", properties=bar_properties) print(response) self.assertEqual(response.status, 201) # This call deletes a item print("Delete item") response = client.delete_item("bar") print(response) self.assertEqual(response.status, 201) # "record" action helper functions # This call creates a event between a user and an item. In particular, this set # the price of the action print("Record user action") action_properties = {"price": 10.0} response = client.record_user_action_on_item("buy", "foo", "bar", action_properties) print(response) self.assertEqual(response.status, 201)
def test_eventclient(self): client = EventClient(access_key=access_key, url="http://127.0.0.1:7070") # Check status print("Check status") print(client.get_status()) self.assertEqual(client.get_status(), {'status': 'alive'}) # First event first_event_properties = { "prop1": 1, "prop2": "value2", "prop3": [1, 2, 3], "prop4": True, "prop5": ["a", "b", "c"], "prop6": 4.56, } first_event_time = datetime( 2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain')) first_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", properties=first_event_properties, event_time=first_event_time, ) print("First Event response") print(first_event_response) self.assertEqual(first_event_response.status, 201) # Second event second_event_properties = { "someProperty": "value1", "anotherProperty": "value2", } second_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", target_entity_type="item", target_entity_id="iid", properties=second_event_properties, event_time=datetime(2014, 12, 13, 21, 38, 45, 618000, pytz.utc)) print("Second Event response") print(second_event_response) self.assertEqual(second_event_response.status, 201) # Get the first event from Event Server first_event_id = first_event_response.json_body["eventId"] print("Get Event") event = client.get_event(first_event_id) print(event) self.assertEqual(event.get('eventId'), first_event_id) # Delete the first event from Event Server print("Delete Event") delete_response = client.delete_event(first_event_id) print(delete_response) self.assertEqual(delete_response.decode('utf-8'), '{"message":"Found"}') # Delete the first event from Event Server again should yield exception. print("Delete Event Again") try: delete_response = client.delete_event(first_event_id) self.fail() except NotFoundError as ex: print("The expected error: {0}".format(ex)) # "user"-helper methods # Set user properties implicitly create a user # This call creates a user "foo", and set the properties of "foo". print("Create user foo") foo_properties = {"city": "sunnyvale", "car": "honda fit"} response = client.set_user("foo", properties=foo_properties) print(response) self.assertEqual(response.status, 201) # This call overrides the existing properties for user "foo", setting "car" to # a new "honda odyssey" and create a new property "food" to "seafood". print("Set new properties") foo_properties = {"car": "honda odyssey", "food": "seafood"} response = client.set_user("foo", properties=foo_properties) print(response) self.assertEqual(response.status, 201) # This call removes the specified properties. It ignores the value of the dict. # After this call, the "city" will become an unset field. print("Unset properties") foo_properties = {"city": "x"} response = client.unset_user("foo", properties=foo_properties) print(response) self.assertEqual(response.status, 201) # This call deletes a user print("Delete user") response = client.delete_user("foo") print(response) self.assertEqual(response.status, 201) # The SDK also support specifying the eventTime. It is useful for importing # events happened in the past. foo_time = datetime(2014, 8, 31, 4, 56, tzinfo=pytz.timezone('US/Pacific')) print("Create user at " + str(foo_time)) response = client.set_user("Jarvis", {}, foo_time) print(response) self.assertEqual(response.status, 201) # "item"-helper methods # Set item properties implicitly create a item # This call creates a item "bar", and set the properties of "bar". print("Create item bar") bar_properties = {"city": "santa clara", "weight": 6.9} response = client.set_item("bar", properties=bar_properties) print(response) self.assertEqual(response.status, 201) # Similar to user-methods, we can do the same thing with item print("Set new properties") bar_properties = {"weight": 6.2} response = client.set_item("bar", properties=bar_properties) print(response) self.assertEqual(response.status, 201) # This call removes the specified properties. It ignores the value of the dict. # After this call, the "city" will become an unset field. print("Unset properties") bar_properties = {"city": None} response = client.unset_item("bar", properties=bar_properties) print(response) self.assertEqual(response.status, 201) # This call deletes a item print("Delete item") response = client.delete_item("bar") print(response) self.assertEqual(response.status, 201) # "record" action helper functions # This call creates a event between a user and an item. In particular, this set # the price of the action print("Record user action") action_properties = {"price": 10.0} response = client.record_user_action_on_item("buy", "foo", "bar", action_properties) print(response) self.assertEqual(response.status, 201)
from predictionio import EventClient from predictionio import NotFoundError from datetime import datetime import pytz import sys client = EventClient(app_id=4, url="http://localhost:7070") # Check status print("Check status") print(client.get_status()) # First event first_event_properties = { "prop1" : 1, "prop2" : "value2", "prop3" : [1, 2, 3], "prop4" : True, "prop5" : ["a", "b", "c"], "prop6" : 4.56 , } first_event_time = datetime( 2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain')) first_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", properties=first_event_properties, event_time=first_event_time, ) print("First Event response")
def test_query(self): random.seed() client = EventClient(access_key=access_key, url="http://127.0.0.1:7070") # Check status print("Check status") print(client.get_status()) self.assertEqual(client.get_status(), {'status': 'alive'}) user_ids = [str(i) for i in range(1, 3)] for user_id in user_ids: print("Set user", user_id) client.set_user(user_id) # TODO assert item_ids = [str(i) for i in range(1, 5)] for item_id in item_ids: print("Set item", item_id) client.set_item(item_id, {"itypes": ['1']}) # TODO assert # each user randomly views 10 items for user_id in user_ids: for viewed_item in random.sample(item_ids, 2): print("User", user_id, "views item", viewed_item) client.record_user_action_on_item("view", user_id, viewed_item) # TODO assert client.close()
def test_eventclient_channel(self): subprocess.call(['pio', 'app', 'channel-new', app_name, channel]) client = EventClient(access_key=access_key, url="http://127.0.0.1:7070", channel=channel) # Check status print("Check status") print(client.get_status()) self.assertEqual(client.get_status(), {'status': 'alive'}) # First event first_event_properties = { "prop1" : 1, "prop2" : "value2", "prop3" : [1, 2, 3], "prop4" : True, "prop5" : ["a", "b", "c"], "prop6" : 4.56 , } first_event_time = datetime( 2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain')) first_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", properties=first_event_properties, event_time=first_event_time, ) print("First Event response") print(first_event_response) self.assertEqual(first_event_response.status, 201) # Second event second_event_properties = { "someProperty" : "value1", "anotherProperty" : "value2", } second_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", target_entity_type="item", target_entity_id="iid", properties=second_event_properties, event_time=datetime(2014, 12, 13, 21, 38, 45, 618000, pytz.utc)) print("Second Event response") print(second_event_response) self.assertEqual(second_event_response.status, 201) # Get the first event from Event Server first_event_id = first_event_response.json_body["eventId"] print("Get Event") event = client.get_event(first_event_id) print(event) self.assertEqual(event.get('eventId'), first_event_id) # Delete the first event from Event Server print("Delete Event") delete_response = client.delete_event(first_event_id) print(delete_response) self.assertEqual(delete_response.decode('utf-8'), '{"message":"Found"}') # Delete the first event from Event Server again should yield exception. print("Delete Event Again") try: delete_response = client.delete_event(first_event_id) self.fail() except NotFoundError as ex: print("The expected error: {0}".format(ex))
from datetime import datetime import pytz from predictionio import EventClient from predictionio import NotFoundError access_key = "gDx1XuMUC9vu1YWWPRZkLRTftoq7m73mlj2MtnZEjncPlZ1JxUS2s7oajwP9xrZQ" assert access_key is not None, "Please create an access key with 'pio app new'" client = EventClient(access_key=access_key, url="http://localhost:7070") # Check status print("Check status") print(client.get_status()) # First event first_event_properties = { "prop1": 1, "prop2": "value2", "prop3": [1, 2, 3], "prop4": True, "prop5": ["a", "b", "c"], "prop6": 4.56, } first_event_time = datetime(2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain')) first_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid",
from predictionio import EventClient from datetime import datetime import pytz client = EventClient('FDUakPq4PpcPUVWrEF-3AsEDRjeXq2k3LWO_5kM1CjxayPmKzXlqlgHRtO5Wh4Xo', "http://10.228.81.5:7070") print(client) first_event_properties = { "prop1" : 1, "prop2" : "value2", "prop3" : [1, 2, 3], "prop4" : True, "prop5" : ["a", "b", "c"], "prop6" : 4.56 , } first_event_time = datetime( 2004, 12, 13, 21, 39, 45, 618000, pytz.timezone('US/Mountain')) first_event_response = client.create_event( event="my_event", entity_type="user", entity_id="uid", properties=first_event_properties, event_time=first_event_time, ) print(first_event_response) second_event_properties = { "someProperty" : "value1", "anotherProperty" : "value2", } # second_event_response = client.create_event( # event="my_event", # entity_type="user", # entity_id="uid",
from predictionio import EventClient from predictionio import NotFoundError from datetime import datetime import pytz import sys access_key = None channel = None assert access_key is not None, "Please create an access key with 'pio app new'" # Need to create channel first before assert channel is not None, "Please create new channel with 'pio app channel-new'" client = EventClient(access_key=access_key, url="http://localhost:7070", channel=channel) # Check status print("Check status") print(client.get_status()) # First event first_event_properties = { "prop1": 1, "prop2": "value2", "prop3": [1, 2, 3], "prop4": True, "prop5": ["a", "b", "c"], "prop6": 4.56, }
from predictionio import EventClient import datetime import pytz client = EventClient( '5h5crmbnZl4_hd-Ob0Ms9Qgm67ConOJWfoxAxtbGo2nKfTELgV0wj7WW0ho3iN7b', "http://localhost:7070", threads=5, qsize=500) first_event_properties = { "prop1": 1, "prop2": "value2", "prop3": [1, 2, 3], "prop4": True, "prop5": ["a", "b", "c"], "prop6": 4.56 } now_date = datetime.datetime.now(pytz.utc) # - datetime.timedelta(days=2.7) current_date = now_date event_time_increment = datetime.timedelta(days=-0.8) first_event_response = client.create_event( event="buy", entity_type="user", entity_id="10001", target_entity_id="2", target_entity_type="item" # properties=first_event_properties, # event_time = current_date )
# this is example of import events to a specific channel of an App from predictionio import EventClient from predictionio import NotFoundError from datetime import datetime import pytz import sys access_key = None channel = None assert access_key is not None, "Please create an access key with 'pio app new'" # Need to create channel first before assert channel is not None, "Please create new channel with 'pio app channel-new'" client = EventClient(access_key=access_key, url="http://localhost:7070", channel=channel) # Check status print("Check status") print(client.get_status()) # First event first_event_properties = { "prop1" : 1, "prop2" : "value2", "prop3" : [1, 2, 3], "prop4" : True, "prop5" : ["a", "b", "c"], "prop6" : 4.56 , } first_event_time = datetime(