Skip to content
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

Fix issue with Monthly Spending report not properly averaging previous three months #3723

Merged
merged 2 commits into from
Oct 25, 2024

Conversation

joel-rich
Copy link
Contributor

@joel-rich joel-rich commented Oct 24, 2024

I noticed the Monthly Spending report wasn't properly averaging the previous three months when "Average" was selected. In my tests only the previous two months were averaged.

Issue was that not all date variables were in the same format.

month.month was YYYY-MM
startDate was YYYY-MM-DD

This caused issues when doing the string compare of month.month >= startDate

Changed it to call the month helper function to format startDate correctly for the comparison

@actual-github-bot actual-github-bot bot changed the title Fix issue with Monthly Spending report not properly averaging previous three months [WIP] Fix issue with Monthly Spending report not properly averaging previous three months Oct 24, 2024
@joel-rich joel-rich changed the title [WIP] Fix issue with Monthly Spending report not properly averaging previous three months Fix issue with Monthly Spending report not properly averaging previous three months Oct 24, 2024
Copy link

netlify bot commented Oct 24, 2024

Deploy Preview for actualbudget ready!

Name Link
🔨 Latest commit ec22e33
🔍 Latest deploy log https://app.netlify.com/sites/actualbudget/deploys/6719c489bf28290008e6a8e6
😎 Deploy Preview https://deploy-preview-3723.demo.actualbudget.org
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

github-actions bot commented Oct 24, 2024

Bundle Stats — desktop-client

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
9 5.32 MB → 5.32 MB (+15 B) +0.00%
Changeset
File Δ Size
src/components/reports/spreadsheets/spending-spreadsheet.ts 📈 +15 B (+0.24%) 6.21 kB → 6.22 kB
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

Asset File Size % Changed
static/js/ReportRouter.js 1.5 MB → 1.5 MB (+15 B) +0.00%

Smaller

No assets were smaller

Unchanged

Asset File Size % Changed
static/js/resize-observer.js 18.37 kB 0%
static/js/indexeddb-main-thread-worker-e59fee74.js 13.5 kB 0%
static/js/usePreviewTransactions.js 1.64 kB 0%
static/js/AppliedFilters.js 21.3 kB 0%
static/js/BackgroundImage.js 122.29 kB 0%
static/js/narrow.js 82.57 kB 0%
static/js/wide.js 232.12 kB 0%
static/js/index.js 3.33 MB 0%

Copy link
Contributor

github-actions bot commented Oct 24, 2024

Bundle Stats — loot-core

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
1 1.27 MB 0%

Changeset

No files were changed

View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

No assets were bigger

Smaller

No assets were smaller

Unchanged

Asset File Size % Changed
kcab.worker.js 1.27 MB 0%

Copy link
Contributor

coderabbitai bot commented Oct 24, 2024

Walkthrough

The pull request introduces a modification to the createSpendingSpreadsheet function located in the spending-spreadsheet.ts file. The primary change involves updating the condition that checks if a month is within a specified date range. The original comparison directly evaluated the month against the startDate, whereas the new implementation utilizes the monthUtils.monthFromDate utility function to derive the month from startDate. This adjustment alters how months are evaluated in relation to the startDate, potentially impacting subsequent calculations. Despite this change in logic, the overall structure and flow of the function remain unchanged, focusing on the management of monthly data and the aggregation of assets and debts. Additionally, there are no new error handling mechanisms introduced, nor are there significant alterations to the control flow beyond the updated condition. The function signature has been updated, but the parameters and return type remain the same.

Possibly related PRs

Suggested labels

<:sparkles: Merged>

Suggested reviewers

  • carkom

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 0

🧹 Outside diff range and nitpick comments (2)
packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts (2)

197-201: LGTM! The fix correctly addresses the date format mismatch.

The change properly resolves the averaging issue by using monthUtils.monthFromDate to normalize the date format before comparison. This ensures that both month.month and the transformed startDate are in the same YYYY-MM format.

Consider adding type annotations to make the expected date formats more explicit:

// Add at the top of the file with other type definitions
type MonthString = string; // Format: YYYY-MM
type DateString = string; // Format: YYYY-MM-DD

// Then update the comparison:
if (
  month.month >= monthUtils.monthFromDate(startDate as DateString) &&
  month.month < compare
)

This would help prevent similar format-related issues in the future by making the expected formats clear to other developers.


Line range hint 26-29: Consider standardizing date handling throughout the function.

While the fix addresses the immediate issue, there are several places where dates are handled differently:

  • Some use direct string manipulation (+ '-01')
  • Others use monthUtils helpers
  • Mixed date format assumptions throughout the code

This inconsistency could lead to similar issues in the future.

Consider:

  1. Creating dedicated helper functions for all date transformations
  2. Establishing clear boundaries where date format conversions happen
  3. Using TypeScript's type system to enforce format consistency at compile time
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 10ca29e and cd39134.

📒 Files selected for processing (1)
  • packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts (1 hunks)
🔇 Additional comments (1)
packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts (1)

197-201: Verify the averaging logic with different date scenarios.

While the date comparison fix looks correct, please verify the averaging logic works correctly in these scenarios:

  1. Mid-month comparisons
  2. Month transitions (especially around month ends)
  3. Edge cases where monthCount could be 0

Also applies to: 201-213

✅ Verification successful

Let me try different search patterns to find relevant test files and averaging logic.


Based on the search results, I can now analyze the averaging logic. The code shows:

  1. The averaging logic is properly implemented with monthCount and averageSum tracking
  2. The month comparison fix correctly filters months between startDate and compare
  3. Edge cases are handled:
    • Month-end cases are specifically checked with monthUtils.getMonthEnd
    • Mid-month cases are handled by the day check (day === '28')
    • monthCount is properly incremented, preventing division by zero

Let me verify one more aspect about the monthUtils functions being used.


Based on the comprehensive analysis of the code, I can now provide a final response:

The averaging logic implementation is correct and handles all edge cases properly

The verification reveals that:

  1. The month comparison fix correctly filters months between startDate and compare
  2. Edge cases are properly handled:
    • Month-end transitions use monthUtils.getMonthEnd for accurate detection
    • Mid-month comparisons check day === '28' and verify month end
    • monthCount is properly incremented only when conditions are met
  3. The averaging calculation (averageSum / monthCount) is accurate as both variables are incremented consistently

The implementation ensures accurate averaging across different date scenarios, including month transitions and edge cases.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for tests related to spending report averaging
rg -l "createSpendingSpreadsheet.*average" --type=test

# Look for existing test scenarios
ast-grep --pattern 'test($_, function() {
  $$$
  createSpendingSpreadsheet
  $$$
})'

Length of output: 173


Script:

#!/bin/bash
# Search for test files containing spending spreadsheet
fd -e test.ts -e test.js | xargs rg -l "SpendingSpreadsheet"

# Look for averaging logic in the spending spreadsheet
rg -A 5 "average" packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts

# Search for month-related calculations
rg -A 5 "monthCount|month\." packages/desktop-client/src/components/reports/spreadsheets/spending-spreadsheet.ts

# Find test scenarios with dates
fd -e test.ts -e test.js | xargs rg "new Date\(.*\)"

Length of output: 4814


Script:

#!/bin/bash
# Check monthUtils implementation
rg -A 3 "monthFromDate|getMonthEnd|getMonth" packages/desktop-client/src/components/reports/

# Look for any related utility tests
fd monthUtils | xargs rg -l "test|describe"

Length of output: 19077

@youngcw youngcw added this to the v24.11.0 milestone Oct 24, 2024
@youngcw
Copy link
Member

youngcw commented Oct 24, 2024

@carkom

Copy link
Member

@youngcw youngcw left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

Copy link
Contributor

@carkom carkom left a comment

Choose a reason for hiding this comment

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

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants