Add timesheetTotals endpoint for hours by day or week - #15
Draft
turegjorup wants to merge 4 commits into
Draft
Conversation
New /apidata/api/timesheetTotals endpoint returns SUM(hours) grouped by day or week (groupBy), filterable by workDate range (from/to) and projectIds. It applies the same filters as the timesheets endpoint so consumers can sum their synced hours per period and reconcile against the source to detect drift.
Accepts workDate as "2026" or "2026-06" and matches timesheet.workDate by prefix, so consumers can scope totals to a year or month without computing unix timestamps. Invalid values are ignored.
Filter timesheetTotals by workYear and/or workMonth, resolved to a half-open workDate range (>= start AND < end). When only workMonth is given the current year is assumed. The range form avoids wrapping workDate in a function and is index-friendly if a workDate index is ever added.
tuj
approved these changes
Aug 3, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Link to ticket
https://leantime.itkdev.dk/#/tickets/showTicket/7893
Description
Adds a sanity-check endpoint so API consumers can detect when their synced timesheet data has drifted from the source.
GET/POST/apidata/api/timesheetTotalsreturnsSUM(hours)grouped by day or week:groupBy:day(default) orweek(ISO-8601, Monday based).from/to: optional unix timestamps boundingworkDate.workYear/workMonth: optional. Select a year or month onworkDate, resolved to a half-open range (>= start AND < end). If onlyworkMonthis given, the current year is assumed.projectIds: optional, mirrors the timesheets endpoint's filter.It applies the same row filters as the
timesheetsendpoint (entries without hours excluded,projectIdsvia the ticket join), so the totals reconcile against the synced data. Each result is{period, hours, count}—hoursis rounded to 2 decimals andperiodisYYYY-MM-DD(day) orYYYY-Www(week). The consumer sums its own synced hours per period and compares; a mismatch flags a period to re-pull.SUM(hours)is the metric that catches value drift (e.g. an hours change that did not sync), which a row count cannot. Broader reconciliation (content checksums, other entity types) is intentionally left for later.Implemented as a plain grouped SQL aggregate (cheap — single scan, intended for ~daily off-peak calls). The workYear/workMonth filter uses a half-open range rather than
LIKE/YEAR()/MONTH()so it doesn't wrap the column in a function and stays index-friendly.Steps to reproduce
Not a bug fix. To verify:
curl '.../apidata/api/timesheetTotals' -H 'x-api-key: lt_...' -d '{"groupBy":"day","workYear":2025,"workMonth":6}'.SELECT DATE(workDate), ROUND(SUM(hours),2), COUNT(*) FROM zp_timesheets WHERE hours IS NOT NULL AND workDate >= '2025-06-01' AND workDate < '2025-07-01' GROUP BY DATE(workDate)— output matches row for row.{"workMonth":6}alone scopes to June of the current year.Checklist