async def test_session_exists_profile_exists_conflict(): # Create Profile for Tracker tracker_profile = Profile.new() await tracker_profile.storage().save() # Create Profile for Session session_profile = Profile.new() await session_profile.storage().save() # Create Session session_id = str(uuid4()) session = Session(id=session_id) session.profile = session_profile await session.storage().save() tracker_payload = TrackerPayload(session=Entity(id=session.id), profile=Entity(id=tracker_profile.id), source=Entity(id="scope")) profile, session = await tracker_payload._get_profile_and_session() assert isinstance(profile, Profile) assert isinstance(session, Session) assert profile.id == session_profile.id # profile id must be form session assert session.id == session_id # Remove await session.storage().delete() await tracker_profile.storage().delete() await session_profile.storage().delete()
async def test_save_events(): tracker_payload = TrackerPayload( session=Entity(id="12345"), profile=Profile.new(), source=Entity(id="scope"), events=[ EventPayload(type="click", properties={"btn": [1, 2, 3]}, options={"save": True}), EventPayload(type="click", properties={"btn": [3, 4, 5]}, options={"save": True}) ]) events = tracker_payload.get_events(Session.new(), Profile.new()) result = await events.bulk().save() assert result.saved == 2 # profile id must be form session for id in result.ids: entity = Entity(id=id) await entity.storage("event").delete()
async def main(): source = NamedEntity(id="mobile-app", name="mobile-app") # Create profile, rule needs it p = Profile.new() await p.storage().save() profile = await p.storage().load() assert p.id == profile.id assert isinstance(profile, Profile) flow_record = FlowRecord(id="1", name="flow-1") await flow_record.storage().save() rule = Rule(id="string", source=source, name="my-rule", event=Type(type="xxx1"), flow=flow_record) await rule.storage().save() saved_rule = await rule.storage().load() assert saved_rule.id == rule.id x = await RulesEngine.load_rules("xxx2") pprint(x) payload = { "id": str(uuid4()), "event_server": Context(), "source": source.dict(), "profile": p.dict(), "context": {}, "session": { "id": "0e6121a5-3ea0-45ed-a9ad-552e1765167f", "event_server": { "page": { "url": "http://localhost:8002/tracker/", "path": "/tracker/", "hash": "", "title": "My title", "referer": { "host": None, "query": None }, "history": { "length": 2 } } }, }, "properties": { "a": "tak" }, "type": "xxx1", "user": { "id": "user-id-2" } } event = Event(**payload) events = Events() events.append(event) events.append(event) session = Session(id="session-id") profile = Profile(id="profile-id") rules_engine = RulesEngine(session, profile, events) flow_result, segmentation_result = await rules_engine.execute(source.id ) # print(stats.to_json()) print(flow_result, segmentation_result) await rule.storage().delete()