# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Tutorial
#
# We can illustrate how to use hourly by looking at the hourly repo itself.
#
#     git clone https://github.com/asherp/hourly.git
#     cd hourly

from hourly import get_work_commits

# ```get_work_commits``` gathers all commits into a pandas array

work, repo = get_work_commits('..')
work.head()

# ```get_labor``` calculates hours worked by differencing commit timestamps and raises an error if clock in and clock out are of different lengths.

# ### Getting time card
#
# First we filter by commit messages containing "clock"

from hourly import get_clocks, get_labor

work.head(8)

# Choose start and end dates (timezones have to match)

start_date, end_date = work.index[[0, 25]]
start_date, end_date

# Filter by clocks statements

clocks = get_clocks(work, start_date = start_date, end_date = end_date)
clocks

# ### Handling errant clock in/out messages
# If you mistakenly put "clock out" in a message, hourly will interpret the message as a legitimate end time. This will likely raise an error when computing the labor. For example, there is a problematic commit in the this repo's history:

clocks[clocks.hash == 'd9ec537b36475b565df6b28d0cab6edc3a89f2da']

# When we include this in our labor calculation, we get the following error:

try:
    get_labor(clocks)
except ValueError as e:
    print(e)

# We can skip this errant commit by setting ```errant_clocks```

clocks = get_clocks(work,
                    start_date = start_date, 
                    end_date = end_date,
                    errant_clocks = ['d9ec537b36475b565df6b28d0cab6edc3a89f2da'],
                   )

# Finally we can generate a timesheet:

labor = get_labor(clocks)
labor

# ### Filtering work session keywords
#
# Use the "ignore" key word to skip any work you don't want to include in your invoices.

labor = get_labor(clocks, ignore = 'pro bono')
labor

# ## Get total earnings
#
# Total earnings can be found using this function. Currency is just a string for printing, but in the future we can add unit conversion.

from hourly import get_hours_worked, get_earnings

get_hours_worked(labor)

get_earnings(get_hours_worked(labor), wage = 80, currency = 'USD')

# # Time adjustment
#
# If you forget to clock in or clock out, you can correct your time sheet by adjusting your clock time.

work, repo = get_work_commits('..') # reports actual work time, according to commit message
work = work[work.message.str.contains('T-')].drop(['name', 'email'], axis = 1)
work

# The above work commits contain time adjustments. By default, `get_clocks` adjusts time stamps automatically when a commit message contains the keyword `T-`.

clocks = get_clocks(work)
clocks

# We can verify that the time adjustment works:

clocks['adjustment'] = (work.index - clocks.index)
clocks

get_labor(clocks, match_logs = False)

# The default behavior can be changed by setting `adjust_clocks` to `False`.

clocks_raw = get_clocks(work, adjust_clocks=False)
clocks_raw['adjustment'] = (work.index - clocks_raw.index)
clocks_raw

# !!! Note
#     Only time subtractions are supported for now: it's easy to forget to clock in/out, but I can't think of a reason to clock-in sometime in the future.
#
# !!! Tip Time delta formats
#     Hourly uses pandas' Timedelta format to modify times. This allows for syntax like ```clock in T-1hr45```

StarcoderdataJupyterScriptsDedupFiltered