-
Notifications
You must be signed in to change notification settings - Fork 4
Harvest rounder #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zimbatm
wants to merge
2
commits into
main
Choose a base branch
from
harvest-rounder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Harvest rounder #110
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| watch_file shell.nix | ||
| source_env_if_exists .envrc.local | ||
| use flake | ||
| watch_file .envrc.local shell.nix | ||
| [[ -f .envrc.local ]] && source_env .envrc.local |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| pkgs ? import <nixpkgs> { }, | ||
| lib ? pkgs.lib, | ||
| }: | ||
| pkgs.python3.pkgs.buildPythonApplication { | ||
| pname = "harvest-rounder"; | ||
| version = "0.0.1"; | ||
| src = lib.fileset.toSource { | ||
| root = ./.; | ||
| fileset = lib.fileset.unions [ | ||
| ./pyproject.toml | ||
| ./README.md | ||
| ./harvest | ||
| ./harvest_exporter | ||
| ./harvest_rounder | ||
| ./kimai | ||
| ./kimai_exporter | ||
| ./rest | ||
| ]; | ||
| }; | ||
|
|
||
| pyproject = true; | ||
| build-system = [ pkgs.python3.pkgs.hatchling ]; | ||
|
|
||
| doCheck = false; | ||
|
|
||
| # Rich is a dependency of the shared pyproject.toml even though | ||
| # harvest-rounder doesn't use it directly | ||
| dependencies = [ pkgs.python3.pkgs.rich ]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| """Round Harvest time entries to the nearest increment (default: 15 minutes).""" | ||
|
|
||
| from dataclasses import dataclass | ||
| from fractions import Fraction | ||
| from typing import Any | ||
|
|
||
| from rest import http_request | ||
|
|
||
|
|
||
| @dataclass | ||
| class TimeEntry: | ||
| """Represents a Harvest time entry with rounding information.""" | ||
|
|
||
| id: int | ||
| date: str | ||
| hours: Fraction | ||
| rounded_hours: Fraction | ||
| notes: str | ||
| project: str | ||
| task: str | ||
| client: str | ||
| user: str | ||
|
|
||
| @property | ||
| def needs_rounding(self) -> bool: | ||
| """Check if this entry needs to be rounded.""" | ||
| return self.hours != self.rounded_hours | ||
|
|
||
| @property | ||
| def difference(self) -> Fraction: | ||
| """Return the difference between rounded and original hours.""" | ||
| return self.rounded_hours - self.hours | ||
|
|
||
|
|
||
| def round_to_increment(hours: Fraction, increment_minutes: int = 15) -> Fraction: | ||
| """Round hours up to the next increment. | ||
|
|
||
| Args: | ||
| hours: The number of hours as a Fraction | ||
| increment_minutes: The increment in minutes (default: 15) | ||
|
|
||
| Returns: | ||
| Hours rounded up to the next increment as a Fraction | ||
| """ | ||
| # Convert increment from minutes to hours as a fraction | ||
| increment_hours = Fraction(increment_minutes, 60) | ||
|
|
||
| # If hours is zero, return zero | ||
| if hours == 0: | ||
| return Fraction(0) | ||
|
|
||
| # Calculate how many increments fit into the hours | ||
| # We use ceiling division to round up | ||
| increments = hours / increment_hours | ||
|
|
||
| # If it's already an exact multiple, return as-is | ||
| if increments.denominator == 1: | ||
| return hours | ||
|
|
||
| # Otherwise, round up to next increment | ||
| rounded_increments = int(increments) + 1 | ||
| return increment_hours * rounded_increments | ||
|
|
||
|
|
||
| def parse_time_entry(entry: dict[str, Any], increment_minutes: int = 15) -> TimeEntry: | ||
| """Parse a Harvest API time entry into a TimeEntry object. | ||
|
|
||
| Args: | ||
| entry: Raw time entry from the Harvest API | ||
| increment_minutes: The increment in minutes for rounding | ||
|
|
||
| Returns: | ||
| A TimeEntry object with original and rounded hours | ||
| """ | ||
| hours = Fraction(entry["hours"]).limit_denominator(1000) | ||
| rounded_hours = round_to_increment(hours, increment_minutes) | ||
|
|
||
| return TimeEntry( | ||
| id=entry["id"], | ||
| date=entry["spent_date"], | ||
| hours=hours, | ||
| rounded_hours=rounded_hours, | ||
| notes=entry.get("notes") or "", | ||
| project=entry["project"]["name"], | ||
| task=entry["task"]["name"], | ||
| client=entry["client"]["name"], | ||
| user=entry["user"]["name"], | ||
| ) | ||
|
|
||
|
|
||
| def get_time_entries( | ||
| account_id: str, | ||
| access_token: str, | ||
| from_date: int, | ||
| to_date: int, | ||
| increment_minutes: int = 15, | ||
| ) -> list[TimeEntry]: | ||
| """Fetch time entries from Harvest and parse them. | ||
|
|
||
| Args: | ||
| account_id: Harvest account ID | ||
| access_token: Harvest bearer token | ||
| from_date: Start date as YYYYMMDD integer | ||
| to_date: End date as YYYYMMDD integer | ||
| increment_minutes: The increment in minutes for rounding | ||
|
|
||
| Returns: | ||
| List of TimeEntry objects | ||
| """ | ||
| headers = { | ||
| "Authorization": f"Bearer {access_token}", | ||
| "Harvest-Account-id": account_id, | ||
| } | ||
| url = f"https://api.harvestapp.com/v2/time_entries?from={from_date}&to={to_date}" | ||
| entries: list[TimeEntry] = [] | ||
| while url is not None: | ||
| resp = http_request(url, headers=headers) | ||
| entries.extend( | ||
| parse_time_entry(entry, increment_minutes) for entry in resp["time_entries"] | ||
| ) | ||
| url = resp["links"]["next"] | ||
| return entries | ||
|
|
||
|
|
||
| def update_time_entry( | ||
| account_id: str, | ||
| access_token: str, | ||
| entry_id: int, | ||
| hours: Fraction, | ||
| ) -> dict[str, Any]: | ||
| """Update a time entry's hours in Harvest. | ||
|
|
||
| Args: | ||
| account_id: Harvest account ID | ||
| access_token: Harvest bearer token | ||
| entry_id: The ID of the time entry to update | ||
| hours: The new hours value | ||
|
|
||
| Returns: | ||
| The updated time entry from the API | ||
| """ | ||
| headers = { | ||
| "Authorization": f"Bearer {access_token}", | ||
| "Harvest-Account-id": account_id, | ||
| "Content-Type": "application/json", | ||
| } | ||
| url = f"https://api.harvestapp.com/v2/time_entries/{entry_id}" | ||
|
|
||
| return http_request( | ||
| url, | ||
| method="PATCH", | ||
| headers=headers, | ||
| data={"hours": float(hours)}, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quiet understand yet, why this is a separate cli as opposed to a flag in the same script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exporter is read-only. I didn't want to mix it with a tool that writes back to Harvest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the harvest-exporter is not writing anything and it also uses the rounding that harvest reports?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use-case is different. Here the goal is to update the recorded hours, with the rounding. The Harvest rounding has been turned off because it creates confusion with the customers when the reports don't match the invoiced hours.