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

adding time in calculations of yesterday and today #66

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

vedansh-5
Copy link
Contributor

@vedansh-5 vedansh-5 commented Mar 27, 2025

Fixes issue #65
Changes: Added time in calculation logic of yesterday and today, earlier it was not given setting it to default at 00:00 causing the issue.

Summary by Sourcery

Improve date calculation logic to correctly handle time zones and date ranges in GitHub data retrieval

Bug Fixes:

  • Fix date calculation for yesterday and today by properly setting time components to ensure accurate data filtering

Enhancements:

  • Modify date handling to use UTC time and set precise start and end times for date ranges
  • Add a new filter function to accurately select GitHub data within specified date ranges

Copy link

sourcery-ai bot commented Mar 27, 2025

Reviewer's Guide by Sourcery

This pull request addresses issue #65 by incorporating time information into the date calculations for 'yesterday' and 'today'. The changes ensure that date ranges are accurately calculated by setting specific times (00:00:00 for the start date and 23:59:59 for the end date). Additionally, a filtering mechanism has been added to apply these date ranges to the fetched GitHub data.

Sequence diagram for fetching and filtering data by date

sequenceDiagram
  participant User
  participant scrumHelper.js
  participant GitHub API
  participant filterDataByDate

  User->>scrumHelper.js: Initiates data fetch
  scrumHelper.js->>GitHub API: Fetches data (issues, PRs, user data)
  activate GitHub API
  GitHub API-->>scrumHelper.js: Returns data
  deactivate GitHub API
  scrumHelper.js->>filterDataByDate: Filters data by date range
  activate filterDataByDate
  filterDataByDate-->>scrumHelper.js: Returns filtered data
  deactivate filterDataByDate
  scrumHelper.js->>scrumHelper.js: Stores filtered data
  scrumHelper.js-->>User: Updates UI with filtered data
Loading

File-Level Changes

Change Details Files
Modified getLastWeek and getToday functions to include time information, setting hours, minutes, seconds, and milliseconds to ensure accurate date range calculations.
  • Set the time to 00:00:00 for getLastWeek.
  • Set the time to 23:59:59.999 for getToday.
  • Adjusted the date objects to UTC format before converting to ISO strings.
  • Return ISO strings for date representation.
src/scripts/scrumHelper.js
Added a filterDataByDate function to filter data based on the updated date ranges and applied this filtering to the data fetched from GitHub.
  • Created filterDataByDate to filter items based on updated_at within the startingDate and endingDate range.
  • Created filterAndStoreData to apply the date filter to githubIssuesData, githubPrsReviewData, and githubUserData.
  • Call filterAndStoreData after fetching data from GitHub.
src/scripts/scrumHelper.js
Modified getLastWeek and getToday functions in main.js to include time information, setting hours, minutes, seconds, and milliseconds to ensure accurate date range calculations.
  • Set the time to 00:00:00 for getLastWeek.
  • Set the time to 23:59:59.999 for getToday.
src/scripts/main.js

Possibly linked issues

  • Wrong calculation of yesterday and today #65: The PR fixes the issue mentioned by adding time in the calculation logic of yesterday and today. The issue was caused due to the time being set to default 00:00. The PR fixes this.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @vedansh-5 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using a library like Moment.js or date-fns to simplify date manipulation and formatting.
  • The duplication in date formatting logic between getLastWeek and getToday could be extracted into a separate helper function.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@vedansh-5
Copy link
Contributor Author

Hey @vedansh-5 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using a library like Moment.js or date-fns to simplify date manipulation and formatting.
  • The duplication in date formatting logic between getLastWeek and getToday could be extracted into a separate helper function.

Here's what I looked at during the review
Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Since this is a very small change a helper function is not necessary.

@vedansh-5
Copy link
Contributor Author

@hongquan This would fix your issue.

var WeekYear = Week.getFullYear();
today.setHours(23, 59, 59, 999);
// var Week = new Date(today.getFullYear(), today.getMonth(), today.getDate());
var WeekMonth = today.getMonth() + 1;
Copy link
Member

Choose a reason for hiding this comment

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

Please update the variables name to make them follow the same style (camelCase).

Signed-off-by: Vedansh Saini <[email protected]>
@vedansh-5
Copy link
Contributor Author

@hongquan In the latest commit, I have worked on the changes you requested, implementing camelCase, better readability in functions and date calculations, also I am using a dual approach in time addition, we are adding time at the end of string for the github API to have the complete information and we are using setHours() to set time so that nternal date calculations use correct times

@vedansh-5 vedansh-5 requested a review from hongquan March 28, 2025 06:26
Copy link
Member

@hongquan hongquan left a comment

Choose a reason for hiding this comment

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

Have you tested?
It seems that your change still have the same bug as before.
The bug comes from ignoring timezone difference.

The new Date() returns the datetime at user timezone, but when you constructing the API request, you assume that the datetime is in UTC.

var WeekDisplayPadded =
('0000' + WeekYear.toString()).slice(-4) +
today.setHours(23, 59, 59, 999);
var week = new Date(today.getFullYear(), today.getMonth(), today.getDate());
Copy link
Member

Choose a reason for hiding this comment

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

Your variable name is confusing. Its name is week, but the value seems to be a single date.

@vedansh-5
Copy link
Contributor Author

Have you tested? It seems that your change still have the same bug as before. The bug comes from ignoring timezone difference.

The new Date() returns the datetime at user timezone, but when you constructing the API request, you assume that the datetime is in UTC.

Thanks for pointing that out, I now see the issue comes from assuming the local timezone while constructing the API req, I will update the implementation to properly convet the datetime.
Since I haven't had enough PRs to thoroughly test it, the output appeared correct based on my timezone. I'll make the corrections now.

Signed-off-by: Vedansh Saini <[email protected]>
@vedansh-5
Copy link
Contributor Author

In the latest changes I have implemented to convert the local time to UTC, but this doesn't work either, because it is checked against GitHub timestamps which are stored in UTC so now, i'll add the code to convert github timestamps to local time

Signed-off-by: Vedansh Saini <[email protected]>
Signed-off-by: Vedansh Saini <[email protected]>
@vedansh-5
Copy link
Contributor Author

@hongquan I made some changes in the code, can you please review it again as of now, the extended UTC+- 14 ensures no data is missed, but it might have overlapping data, I'll refine my filtering logic more, I'd appreciate any feedback from you. Thanks.

@vedansh-5 vedansh-5 requested a review from hongquan April 1, 2025 17:15
@vedansh-5
Copy link
Contributor Author

@sourcery-ai review

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @vedansh-5 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider consolidating the date calculation logic in src/scripts/scrumHelper.js and src/scripts/main.js to avoid duplication.
  • The date filtering logic seems to be duplicated, consider creating a shared utility function.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +117 to +119
today.setHours(23, 59, 59, 999);

const utc = new Date(today.getTime() + (14 * 60 * 60 * 1000));
Copy link

Choose a reason for hiding this comment

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

suggestion: Review the repetitive pattern of applying a timezone offset.

Both getLastWeek() and getToday() apply the same 14-hour offset adjustment. It might be worthwhile to refactor this repeated logic into a shared helper function for clarity and maintainability.

Suggested implementation:

// Helper function to apply a 14-hour timezone offset and format the date as ISO string
function applyTimezoneOffset(date) {
    const offsetInMilliseconds = 14 * 60 * 60 * 1000;
    const utcDate = new Date(date.getTime() + offsetInMilliseconds);
    return utcDate.toISOString().slice(0, 19) + "Z";
}
		today.setHours(23, 59, 59, 999);
		return applyTimezoneOffset(today);
	}	

You should also update the getLastWeek() function (if it uses the same pattern) in a similar fashion by replacing its 14-hour offset logic with a call to applyTimezoneOffset().

'-' +
('00' + WeekDay.toString()).slice(-2);
return WeekDisplayPadded;
var today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
Copy link

Choose a reason for hiding this comment

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

suggestion: Reconsider duplicated date formatting logic in getToday().

getToday() in main.js manually formats the date after adjusting with the offset, while scrumHelper.js uses toISOString slicing. Aligning these approaches (potentially via a common utility function) could improve consistency and reduce potential formatting discrepancies.


const utc = new Date(lastWeek.getTime() + (14 * 60* 60* 1000));

return utc.toISOString().slice(0,19) + "Z";
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider extracting the date transformation logic into a shared helper function to avoid repetition and improve code maintainability

Extract a shared helper to handle the date transformation so you don’t repeat the setHours and offset logic. For example:

function formatDateWithOffset(date, isStart) {
  if (isStart) {
    date.setHours(0, 0, 0, 0);
  } else {
    date.setHours(23, 59, 59, 999);
  }
  const offsetHours = 14;
  const utc = new Date(date.getTime() + (offsetHours * 60 * 60 * 1000));
  return utc.toISOString().slice(0, 19) + "Z";
}

Then update your functions as follows:

function getLastWeek() {
  const today = new Date();
  const daysBack = gsoc === 0 ? 7 : 1;
  const lastWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - daysBack);
  return formatDateWithOffset(lastWeek, true);
}

function getToday() {
  const today = new Date();
  return formatDateWithOffset(today, false);
}

This refactoring reduces duplication while keeping the functionality identical.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is used in only two functions, also its a single line, I don't think a function is necessary.

Copy link

Choose a reason for hiding this comment

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

I'll respond to the comment by the developer:

While I understand your perspective that the code is used in only two functions and appears to be a single line, creating a helper function offers several benefits:

  1. Code Readability: The extracted function makes the date transformation logic more explicit and easier to understand at a glance.

  2. Maintainability: If the date transformation logic needs to change in the future, you'll only need to modify it in one place.

  3. Reusability: Even if it's currently used in just two functions, having a dedicated helper makes it easier to reuse the logic elsewhere if needed.

  4. Consistency: The helper ensures that date transformations are handled consistently across different parts of the code.

The proposed refactoring is lightweight and doesn't add unnecessary complexity. It actually simplifies the existing code by:

  • Removing manual string formatting
  • Centralizing the time zone and offset logic
  • Making the intent of the code clearer

I recommend considering the proposed refactoring, but if you feel strongly against it, that's okay too. The most important thing is maintaining clean, understandable code.

Would you like to discuss this further or share your thoughts on the suggested approach?

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.

2 participants