def main() -> None: now = datetime.now(tz=timezone.utc) td30d = timedelta(days=7) aw = ActivityWatchClient() print("Querying...") data = aw.query(_query, [(now - td30d, now)]) events = [{ "timestamp": e["timestamp"], "duration": timedelta(seconds=e["duration"]), **e["data"], } for e in data[0]["events"]] df = pd.json_normalize(events) df["timestamp"] = pd.to_datetime(df["timestamp"], infer_datetime_format=True) df.set_index("timestamp", inplace=True) print(df) answer = input("Do you want to export to CSV? (y/N): ") if answer == "y": filename = "output.csv" df.to_csv(filename) print(f"Wrote to {filename}")
def bucket_report(bucket_name,start_date,end_date): client = ActivityWatchClient("aw-watcher-cli", testing=False) bucket_id = "{}_{}".format(bucket_name, client.hostname) query = "RETURN = query_bucket('{}');".format(bucket_id) events = client.query(query,start_date,end_date)[0] if len(events) == 0: print("No events") exit() events.reverse() #oder from oldest to newest # first date date = parser.parse(events[0]["timestamp"],default=datetime.now()) - timedelta(hours=6) #- CST print(date.date()) #print first date day_duration = 0 total_duration = 0 cnt = 1 for event in events: total_duration += event["duration"] timestamp = parser.parse(event["timestamp"],default=datetime.now()) - timedelta(hours=6) #- CST if date.date() != timestamp.date(): date = timestamp print("sum:\t{}".format(format_duration(day_duration))) #duration from prev block print("") day_duration = event["duration"] #reset duration print(date.date()) #print next date else: day_duration += event["duration"] print("{}.\t{}\t{}".format(cnt,format_duration(event["duration"]),event["data"]["label"])) cnt += 1 #print last day_duration when last event is reached if event == events[-1]: print("sum:\t{}".format(format_duration(day_duration))) #Print total report duration print("total:\t{}".format(format_duration(total_duration)))
def get_events( hostname: str, since: datetime, end: datetime, include_smartertime="auto", include_toggl=None, testing: bool = False, ) -> List[Event]: awc = ActivityWatchClient("test", testing=testing) query = build_query(hostname) logger.debug(f"Query:\n{query}") result = awc.query(query, timeperiods=[(since, end)]) events = [Event(**e) for e in result[0]] if include_smartertime: events = union_no_overlap( events, _get_events_smartertime(since, filepath=include_smartertime)) events = sorted(events, key=lambda e: e.timestamp) if include_toggl: events = union_no_overlap( events, _get_events_toggl(since, filepath=include_toggl)) events = sorted(events, key=lambda e: e.timestamp) # Filter by time events = [ e for e in events if since.astimezone(timezone.utc) < e.timestamp and e.timestamp + e.duration < end.astimezone(timezone.utc) ] assert all(since.astimezone(timezone.utc) < e.timestamp for e in events) assert all(e.timestamp + e.duration < end.astimezone(timezone.utc) for e in events) # Filter out events without data (which sometimes happens for whatever reason) events = [e for e in events if e.data] for event in events: if "app" not in event.data: if "url" in event.data: event.data["app"] = urlparse(event.data["url"]).netloc else: print("Unexpected event: ", event) events = [e for e in events if e.data] return events
def get_activity( project: str, begin: Optional[datetime.datetime] = None, end: Optional[datetime.datetime] = None, activity_buckets: Sequence[str] = DEFAULT_ACTIVITY_BUCKETS ) -> Activity: if begin is None: begin = get_begin_time() if end is None: end = get_default_end_time() time_periods = [(begin, end)] client = ActivityWatchClient() query = _get_activity_query(project, activity_buckets) events: List[ActivityData] = client.query(query, time_periods) return Activity(events[0]['aw_watcher_window_events'])
def get_events(hostname: str, since: datetime, end: datetime, include_smartertime='auto', include_toggl=None) -> List[Event]: awc = ActivityWatchClient("test", testing=False) # print(query_complete) query = build_query(hostname) result = awc.query(query, start=since, end=end) events = [Event(**e) for e in result[0]] if include_smartertime: events = _union_no_overlap( events, _get_events_smartertime(since, filepath=include_smartertime)) events = sorted(events, key=lambda e: e.timestamp) if include_toggl: events = _union_no_overlap( events, _get_events_toggl(since, filepath=include_toggl)) events = sorted(events, key=lambda e: e.timestamp) # Filter by time events = [ e for e in events if since.astimezone(timezone.utc) < e.timestamp and e.timestamp + e.duration < end.astimezone(timezone.utc) ] assert all(since.astimezone(timezone.utc) < e.timestamp for e in events) assert all(e.timestamp + e.duration < end.astimezone(timezone.utc) for e in events) # Filter out events without data (which sometimes happens for whatever reason) events = [e for e in events if e.data] for event in events: if 'app' not in event.data: if 'url' in event.data: event.data['app'] = urlparse(event.data['url']).netloc else: print('Unexpected event: ', event) events = [e for e in events if e.data] return events
#!/usr/bin/env python3 from time import sleep from datetime import datetime, timedelta, timezone from aw_core.models import Event from aw_client import ActivityWatchClient client = ActivityWatchClient("test-client", testing=True) now = datetime.now(timezone.utc) start = now query = "RETURN=0;" res = client.query(query, "1970-01-01", "2100-01-01") print(res) # Should print 0 bucket_id = "{}_{}".format("test-client-bucket", client.hostname) event_type = "dummydata" client.create_bucket(bucket_id, event_type="test") def insert_events(label: str, count: int): global now events = [] for i in range(count): 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)
microsecond=0) DATE_TO = datetime.now() # aw settings BUCKET_WEB = f"aw-watcher-web-firefox" BUCKET_GIT = f"aw-git-hooks_{socket.gethostname()}" client = ActivityWatchClient("report-client") # extract different projects from issues or git origins if no issues available # get all git hook events query = f""" events = query_bucket('{BUCKET_GIT}'); RETURN = sort_by_timestamp(events); """ git = client.query(query, [(DATE_FROM, DATE_TO)]) hooks = hooks_to_dataframe([GitHook(**e["data"]) for e in git[0]]) issues = hooks["issue"] \ .drop_duplicates() \ .dropna() projects = issues.loc[issues.str.contains(r"[A-Z]+-[0-9]+")] \ .apply(lambda r: r.split('-')[0]) # get all window titles, project titles, etc. query = f""" events = query_bucket('{BUCKET_WEB}'); RETURN = sort_by_timestamp(events); """ web = client.query(query, [(DATE_FROM, DATE_TO)]) visits = visits_to_dataframe([WebVisit(**e["data"]) for e in web[0]]) # save title as list of words additionally