Skip to content

fix: align weekly chart buckets from end to match npm downloads#2052

Merged
graphieros merged 7 commits intonpmx-dev:mainfrom
jycouet:fix/weekly-buckets-from-end
Mar 13, 2026
Merged

fix: align weekly chart buckets from end to match npm downloads#2052
graphieros merged 7 commits intonpmx-dev:mainfrom
jycouet:fix/weekly-buckets-from-end

Conversation

@jycouet
Copy link
Contributor

@jycouet jycouet commented Mar 12, 2026

Closes #2041

Problem

Weekly buckets were aligned from the start of the date range, so the last bucket often covered a different 7-day window than the npm rolling week.

Solution

  • Build weekly buckets from the end date backwards so the last bucket aligns exactly with the npm "last week" window
  • Extract bucketing logic into app/utils/chart-data-buckets.ts with fillPartialBucket for the (now partial) first bucket
  • Extract date helpers into app/utils/date.ts (parseIsoDate, toIsoDate, addDays, etc.)
  • Handle single-point series edge case
  • Add comprehensive tests for both buckets and date utilities

Verification

On /compare?packages=svelte,@sveltejs/kit, table and chart now show identical values (3.3M / 1.4M).

@vercel
Copy link

vercel bot commented Mar 12, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
npmx.dev Ready Ready Preview, Comment Mar 12, 2026 8:51pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs.npmx.dev Ignored Ignored Preview Mar 12, 2026 8:51pm
npmx-lunaria Ignored Ignored Mar 12, 2026 8:51pm

Request Review

@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 50.94340% with 52 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
app/utils/chart-data-buckets.ts 46.47% 30 Missing and 8 partials ⚠️
app/composables/useCharts.ts 52.38% 9 Missing and 1 partial ⚠️
app/utils/date.ts 69.23% 3 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 12, 2026

📝 Walkthrough

Walkthrough

This pull request extracts date utilities into app/utils/date.ts and chart bucketing/evolution builders into app/utils/chart-data-buckets.ts, then updates app/composables/useCharts.ts to use those new helpers (replacing removed internal evolution-from-daily exports). app/components/Package/TrendsChart.vue adjusts estimation eligibility (excluding both daily and weekly granularities for estimation, and still excluding the contributors metric), changes weekly predictionPoints handling (sets predictionPoints to 0 for weekly), and tweaks anomalies UI/checkbox behaviour. Tests were added/updated for the new date and bucket utilities and useCharts exports; Vitest config gains a ~ alias.

Possibly related PRs

Suggested reviewers

  • graphieros
  • danielroe
🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ❓ Inconclusive All changes are scoped to fixing weekly chart bucket alignment and refactoring related code. The anomalies UI tweaks in TrendsChart.vue appear unrelated to the core objective but are minor and well-contained. Clarify whether the anomalies toggle behaviour changes in TrendsChart.vue are related to the weekly bucket alignment fix or if they should be in a separate PR.
✅ Passed checks (2 passed)
Check name Status Explanation
Description check ✅ Passed The pull request description clearly relates to the changeset, describing the weekly bucket alignment fix and extraction of utility functions that match the file changes.
Linked Issues check ✅ Passed All code changes implement the requirements from issue #2041: weekly buckets are now built from end backwards, bucketing logic is extracted with partial bucket handling, date utilities are extracted, and comprehensive tests are added.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

Migrating from UI to YAML configuration.

Use the @coderabbitai configuration command in a PR comment to get a dump of all your UI settings in YAML format. You can then edit this YAML file and upload it to the root of your repository to configure CodeRabbit programmatically.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
app/utils/chart-data-buckets.ts (1)

94-107: Consider defensive handling for malformed date strings.

The type assertion as [number, number] on line 95 assumes the split will always yield two numeric elements. While this is safe for well-formed ISO dates from internal sources, a malformed string would produce NaN values.

Given this is internal utility code processing known-format npm API data, this is acceptable, but worth noting.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 534a7ede-1f37-4844-abb4-17238285f660

📥 Commits

Reviewing files that changed from the base of the PR and between eda2657 and 1f5b832.

📒 Files selected for processing (8)
  • app/components/Package/TrendsChart.vue
  • app/composables/useCharts.ts
  • app/utils/chart-data-buckets.ts
  • app/utils/date.ts
  • test/unit/app/composables/use-charts.spec.ts
  • test/unit/app/utils/chart-data-buckets.spec.ts
  • test/unit/app/utils/date.spec.ts
  • vitest.config.ts

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 74616625-2d65-438e-8ed0-2a30b6a6275a

📥 Commits

Reviewing files that changed from the base of the PR and between 1f5b832 and 410b4f6.

📒 Files selected for processing (2)
  • app/components/Package/TrendsChart.vue
  • app/composables/useCharts.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/components/Package/TrendsChart.vue

Comment on lines 311 to 317
const endDateOnly = toDateOnly(evolutionOptions.endDate)
const end = endDateOnly ? parseIsoDateOnly(endDateOnly) : yesterday
const end = endDateOnly ? parseIsoDate(endDateOnly) : yesterday

const startDateOnly = toDateOnly(evolutionOptions.startDate)
if (startDateOnly) {
const start = parseIsoDateOnly(startDateOnly)
const start = parseIsoDate(startDateOnly)
return { start, end }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Handle invalid calendar dates before using parsed values.

toDateOnly checks format only. A value like 2026-02-31 still passes, then parseIsoDate can produce an invalid date and propagate NaN behaviour. Please guard parsed dates and fall back to defaults.

Proposed fix
   const endDateOnly = toDateOnly(evolutionOptions.endDate)
-  const end = endDateOnly ? parseIsoDate(endDateOnly) : yesterday
+  const parsedEnd = endDateOnly ? parseIsoDate(endDateOnly) : null
+  const end = parsedEnd && !Number.isNaN(parsedEnd.getTime()) ? parsedEnd : yesterday

   const startDateOnly = toDateOnly(evolutionOptions.startDate)
   if (startDateOnly) {
-    const start = parseIsoDate(startDateOnly)
-    return { start, end }
+    const parsedStart = parseIsoDate(startDateOnly)
+    if (!Number.isNaN(parsedStart.getTime())) {
+      return { start: parsedStart, end }
+    }
   }

As per coding guidelines "Use error handling patterns consistently".

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const endDateOnly = toDateOnly(evolutionOptions.endDate)
const end = endDateOnly ? parseIsoDateOnly(endDateOnly) : yesterday
const end = endDateOnly ? parseIsoDate(endDateOnly) : yesterday
const startDateOnly = toDateOnly(evolutionOptions.startDate)
if (startDateOnly) {
const start = parseIsoDateOnly(startDateOnly)
const start = parseIsoDate(startDateOnly)
return { start, end }
const endDateOnly = toDateOnly(evolutionOptions.endDate)
const parsedEnd = endDateOnly ? parseIsoDate(endDateOnly) : null
const end = parsedEnd && !Number.isNaN(parsedEnd.getTime()) ? parsedEnd : yesterday
const startDateOnly = toDateOnly(evolutionOptions.startDate)
if (startDateOnly) {
const parsedStart = parseIsoDate(startDateOnly)
if (!Number.isNaN(parsedStart.getTime())) {
return { start: parsedStart, end }
}
}

Copy link
Contributor

@graphieros graphieros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great !

Just one thing, the Known Anomalies checkbox is now togglable when there are no recorded corrections. I think this might be a regression (?).

@jycouet
Copy link
Contributor Author

jycouet commented Mar 13, 2026

This is great !

Just one thing, the Known Anomalies checkbox is now togglable when there are no recorded corrections. I think this might be a regression (?).

Haaa, yes I did this change. It's actually bugging me that sometime I can click and sometime not. We have the tooltip on top to know if it will have wrong data, so having this conditional doesn't feel correct.

It's a bit like "disabling smooting if if's the same value"

What do you think ?

@graphieros
Copy link
Contributor

graphieros commented Mar 13, 2026

What do you think ?

As a user, if I can click but it does not change anything, it might feel as a bug.
I don't think we can rely on users reading the tooltip ;)

I wonder if the tooltip, however, could also say: 'No recorded anomalies' ?

@jycouet
Copy link
Contributor Author

jycouet commented Mar 13, 2026

On a flat chart (a lot of small libraries with almost no download) all sliders have also no affect on the trend.

I have a half strong opinion on this 😜
But you decide, no worries, I can remove this change 👍
I'm waiting for you final word 👀

@graphieros
Copy link
Contributor

graphieros commented Mar 13, 2026

Yeah, you're right about the sliders...
Can we compromise, just adding 'No recorded anomalies' in the tooltip ?

(And I prefer that we decide together ;)

@jycouet
Copy link
Contributor Author

jycouet commented Mar 13, 2026

Yeah, you're right about the sliders...
Can we compromise, just adding 'No recorded anomalies' in the tooltip ?

(And I prefer that we decide together ;)

) <- closing your open parentesis ;)

Me too, I would like to decide together, just wanted to say that we could remove it. I would not have hard feelings, you will still be a great OSS friend 😉 BTW we have to meet one day !🧡

Your suggestion just before looks perfect to me. I'll update that after coffee ☺️

@graphieros
Copy link
Contributor

Me too, I would like to decide together, just wanted to say that we could remove it. I would not have hard feelings, you will still be a great OSS friend 😉 BTW we have to meet one day !🧡

Yes, you must let know the next time you're in Paris ^^

@npmx-dev npmx-dev deleted a comment from jycouet Mar 13, 2026
@graphieros
Copy link
Contributor

I'm sorry @jycouet I edited your previous message instead of answering it.
You are right, even I don't read tooltips apparently!

Copy link
Contributor

@graphieros graphieros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🏆

@graphieros graphieros added this pull request to the merge queue Mar 13, 2026
Merged via the queue into npmx-dev:main with commit 8976969 Mar 13, 2026
20 checks passed
eryue0220 added a commit to eryue0220/npmx.dev that referenced this pull request Mar 13, 2026
…ix/issue-2044

* 'fix/issue-2044' of github.com:eryue0220/npmx.dev:
  fix: align weekly chart buckets from end to match npm downloads (npmx-dev#2052)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Weekly chart: align buckets from end to match npm trends

2 participants