|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from typing import TextIO |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | + |
| 7 | +from compiler_admin.services.google import user_info as google_user_info |
| 8 | +import compiler_admin.services.files as files |
| 9 | + |
| 10 | +# cache of previously seen project information, keyed on Toggl project name |
| 11 | +PROJECT_INFO = {} |
| 12 | + |
| 13 | +# cache of previously seen user information, keyed on email |
| 14 | +USER_INFO = {} |
| 15 | +NOT_FOUND = "NOT FOUND" |
| 16 | + |
| 17 | +# input CSV columns needed for conversion |
| 18 | +INPUT_COLUMNS = ["Email", "Task", "Client", "Start date", "Start time", "Duration", "Description"] |
| 19 | + |
| 20 | +# default output CSV columns |
| 21 | +OUTPUT_COLUMNS = ["Date", "Client", "Project", "Task", "Notes", "Hours", "First Name", "Last Name"] |
| 22 | + |
| 23 | + |
| 24 | +def _harvest_client_name(): |
| 25 | + """Gets the value of the HARVEST_CLIENT_NAME env var.""" |
| 26 | + return os.environ.get("HARVEST_CLIENT_NAME") |
| 27 | + |
| 28 | + |
| 29 | +def _get_info(obj: dict, key: str, env_key: str): |
| 30 | + """Read key from obj, populating obj once from a file path at env_key.""" |
| 31 | + if obj == {}: |
| 32 | + file_path = os.environ.get(env_key) |
| 33 | + if file_path: |
| 34 | + file_info = files.read_json(file_path) |
| 35 | + obj.update(file_info) |
| 36 | + return obj.get(key) |
| 37 | + |
| 38 | + |
| 39 | +def _toggl_project_info(project: str): |
| 40 | + """Return the cached project for the given project key.""" |
| 41 | + return _get_info(PROJECT_INFO, project, "TOGGL_PROJECT_INFO") |
| 42 | + |
| 43 | + |
| 44 | +def _toggl_user_info(email: str): |
| 45 | + """Return the cached user for the given email.""" |
| 46 | + return _get_info(USER_INFO, email, "TOGGL_USER_INFO") |
| 47 | + |
| 48 | + |
| 49 | +def _get_first_name(email: str) -> str: |
| 50 | + """Get cached first name or derive from email.""" |
| 51 | + user = _toggl_user_info(email) |
| 52 | + first_name = user.get("First Name") if user else None |
| 53 | + if first_name is None: |
| 54 | + parts = email.split("@") |
| 55 | + first_name = parts[0].capitalize() |
| 56 | + data = {"First Name": first_name} |
| 57 | + if email in USER_INFO: |
| 58 | + USER_INFO[email].update(data) |
| 59 | + else: |
| 60 | + USER_INFO[email] = data |
| 61 | + return first_name |
| 62 | + |
| 63 | + |
| 64 | +def _get_last_name(email: str): |
| 65 | + """Get cached last name or query from Google.""" |
| 66 | + user = _toggl_user_info(email) |
| 67 | + last_name = user.get("Last Name") if user else None |
| 68 | + if last_name is None: |
| 69 | + user = google_user_info(email) |
| 70 | + last_name = user.get("Last Name") if user else None |
| 71 | + if email in USER_INFO: |
| 72 | + USER_INFO[email].update(user) |
| 73 | + else: |
| 74 | + USER_INFO[email] = user |
| 75 | + return last_name |
| 76 | + |
| 77 | + |
| 78 | +def _str_timedelta(td): |
| 79 | + """Convert a string formatted duration (e.g. 01:30) to a timedelta.""" |
| 80 | + return pd.to_timedelta(pd.to_datetime(td, format="%H:%M:%S").strftime("%H:%M:%S")) |
| 81 | + |
| 82 | + |
| 83 | +def convert_to_harvest( |
| 84 | + source_path: str | TextIO = sys.stdin, |
| 85 | + output_path: str | TextIO = sys.stdout, |
| 86 | + client_name: str = None, |
| 87 | + output_cols: list[str] = OUTPUT_COLUMNS, |
| 88 | +): |
| 89 | + """Convert Toggl formatted entries in source_path to equivalent Harvest formatted entries. |
| 90 | +
|
| 91 | + Args: |
| 92 | + source_path: The path to a readable CSV file of Toggl time entries; or a readable buffer of the same. |
| 93 | +
|
| 94 | + client_name (str): The value to assign in the output "Client" field |
| 95 | +
|
| 96 | + output_cols (list[str]): A list of column names for the output |
| 97 | +
|
| 98 | + output_path: The path to a CSV file where Harvest time entries will be written; or a writeable buffer for the same. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + None. Either prints the resulting CSV data or writes to output_path. |
| 102 | + """ |
| 103 | + if client_name is None: |
| 104 | + client_name = _harvest_client_name() |
| 105 | + |
| 106 | + # read CSV file, parsing dates and times |
| 107 | + source = files.read_csv(source_path, usecols=INPUT_COLUMNS, parse_dates=["Start date"], cache_dates=True) |
| 108 | + source["Start time"] = source["Start time"].apply(_str_timedelta) |
| 109 | + source["Duration"] = source["Duration"].apply(_str_timedelta) |
| 110 | + source.sort_values(["Start date", "Start time", "Email"], inplace=True) |
| 111 | + |
| 112 | + # rename columns that can be imported as-is |
| 113 | + source.rename(columns={"Task": "Project", "Description": "Notes", "Start date": "Date"}, inplace=True) |
| 114 | + |
| 115 | + # update static calculated columns |
| 116 | + source["Client"] = client_name |
| 117 | + source["Task"] = "Project Consulting" |
| 118 | + |
| 119 | + # get cached project name if any |
| 120 | + source["Project"] = source["Project"].apply(lambda x: _toggl_project_info(x) or x) |
| 121 | + |
| 122 | + # assign First and Last Name |
| 123 | + source["First Name"] = source["Email"].apply(_get_first_name) |
| 124 | + source["Last Name"] = source["Email"].apply(_get_last_name) |
| 125 | + |
| 126 | + # calculate hours as a decimal from duration timedelta |
| 127 | + source["Hours"] = (source["Duration"].dt.total_seconds() / 3600).round(2) |
| 128 | + |
| 129 | + if output_path is None: |
| 130 | + output_path = sys.stdout |
| 131 | + |
| 132 | + files.write_csv(output_path, source, columns=output_cols) |
0 commit comments