feat: snap analytics query dates to month/week boundaries#44
Merged
Conversation
The YouTube Analytics API rejects unaligned start/end dates for the month dimension (both ends must be the first of a month) and does not snap on its own, so -d month --days N or any mid-month bound returned HTTP 400. Add _align_date_range to snap query dates to the required boundaries before the request: month snaps both ends down to the first of their month; week snaps both ends back to the preceding Sunday. day and other dimensions are unchanged.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #44 +/- ##
==========================================
+ Coverage 83.81% 83.90% +0.08%
==========================================
Files 15 15
Lines 2447 2460 +13
Branches 354 356 +2
==========================================
+ Hits 2051 2064 +13
Misses 264 264
Partials 132 132 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Problem
The YouTube Analytics API requires aligned start/end dates for the
monthdimension: both ends must be the first day of a month, and the API rejects any other day rather than snapping. Thequerycommand built dates from--days/-s/-ewith no dimension-aware alignment, so-d month --days 365or any non-first-of-month bound produced an HTTP 400.Fix
Add
_align_date_range(dimension_names, start, end)incommands/analytics.pyand call it after the query dates are built:month: snap both ends DOWN to the first of their month (inclusive range; degenerate same-month range stays valid).week: snap both ends back to the preceding Sunday. YouTube Analytics weeks begin on Sunday. Note: the current modern Analytics API exposes onlydayandmonthas time-period dimensions (per https://developers.google.com/youtube/analytics/dimensions), soweekis handled defensively for forward-compat.dayand all other dimensions are returned unchanged.Tests
New
TestAlignDateRangeunit tests cover: month start snap, month end snap,--days-derived non-aligned dates, already-aligned unchanged, same-month range,dayuntouched, no-dimension untouched, week snap to Sunday, and Sunday unchanged. Full suite: 292 passed; ruff clean.Closes #39