def client(): c = ActivityWatchClient("client-test", testing=True) yield c # Delete test buckets after all tests needing the fixture have been run buckets = c.get_buckets() for bucket_id in buckets: if bucket_id.startswith("test-"): c.delete_bucket(bucket_id)
def aw_client(): # TODO: Could it be possible to write a sisterclass of ActivityWatchClient # which calls aw_server.api directly? Would it be of use? Would add another # layer of integration tests that are actually more like unit tests. c = ActivityWatchClient("client-test", testing=True) yield c # Delete test buckets after all tests needing the fixture have been run buckets = c.get_buckets() for bucket_id in buckets: if bucket_id.startswith("test-"): c.delete_bucket(bucket_id)
def delete_prev_buckets(): logging.info("Deleting old buckets") client = ActivityWatchClient("aw-fake-client", testing=True) client.client_hostname = hostname client.connect() try: client.delete_bucket(window_bucket_name) except HTTPError: pass try: client.delete_bucket(afk_bucket_name) except HTTPError: pass
def setup_client() -> ActivityWatchClient: logger.info("Setting up client") # Default is to run in testing mode, can be run in prod mode if set to exactly 'false' testing = os.getenv("AW_TESTING", "true").lower() not in ["false"] if testing: logger.info( "Using testing parameters (set the env var AW_TESTING to false to run in prod mode)" ) client = ActivityWatchClient(client_name, testing=testing) client.client_hostname = hostname buckets = client.get_buckets() logger.info("Deleting old buckets") buckets_all = [ bucket_afk, bucket_window, bucket_browser_chrome, bucket_browser_firefox, ] if not testing: ans = input( f"Running in prod, are you sure you want to delete all existing buckets?\n{buckets_all}\nAre you sure? (y/N)" ) if ans != "y": print("Exiting") sys.exit(0) for bucket in [ bucket_window, bucket_afk, bucket_browser_chrome, bucket_browser_firefox, ]: if bucket in buckets: client.delete_bucket(bucket, force=True) client.create_bucket(bucket_window, "currentwindow") client.create_bucket(bucket_afk, "afkstatus") client.create_bucket(bucket_browser_chrome, "web.tab.current") client.create_bucket(bucket_browser_firefox, "web.tab.current") client.connect() return client
e = Event(timestamp=now, duration=timedelta(seconds=1), data={"label": label}) events.append(e) now = now + timedelta(seconds=1) client.insert_events(bucket_id, events) insert_events("a", 5) query = "RETURN = query_bucket('{}');".format(bucket_id) res = client.query(query, "1970", "2100") print(res) # Should print the last 5 events res = client.query(query, start + timedelta(seconds=1), now - timedelta(seconds=2)) print(res) # Should print three events insert_events("b", 10) query = """ events = query_bucket('{}'); merged_events = merge_events_by_keys(events, 'label'); RETURN=merged_events; """.format(bucket_id) res = client.query(query, "1970", "2100") # Should print two merged events # Event "a" with a duration of 5s and event "b" with a duration of 10s print(res) client.delete_bucket(bucket_id)
# print(title) # print(start, duration) # if attendees: # for attendee in attendees: # print(attendee) # print(e) # print(component) # print(80 * "-") return events if __name__ == "__main__": filename = sys.argv.pop() events = load_events(filename) print(f"Loaded {len(events)} events") aw = ActivityWatchClient(testing=True) bucket_name = 'ical-import' if bucket_name in aw.get_buckets(): aw.delete_bucket(bucket_name) aw.create_bucket(bucket_name, 'calendar') aw.insert_events(bucket_name, events) """ Basic query: events = query_bucket(find_bucket('ical-import')); RETURN = filter_keyvals_regex(events, 'title', '\<\>')); """
def create_unique_event(): return Event(timestamp=datetime.now(timezone.utc), duration=timedelta(), data={"label": str(random())}) client = ActivityWatchClient("aw-test-client", testing=True) client.connect() bucket_name = "test-bucket" bucket_etype = "test" # Delete bucket before creating it, and handle error if it doesn't already exist try: client.delete_bucket(bucket_name) except HTTPError as e: pass client.create_bucket(bucket_name, bucket_etype) e1 = create_unique_event() client.insert_event(bucket_name, e1) print("Getting events") events = client.get_events(bucket_name) print("Asserting events") assert events[0]['data']['label'] == e1['data']['label'] print("Getting eventcount") eventcount = client.get_eventcount(bucket_name)