Skip to content

Conversation

@NeatGuyCoding
Copy link
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Extract parse_time_range for console app stats related queries

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Signed-off-by: NeatGuyCoding <15627489+NeatGuyCoding@users.noreply.github.com>
Copilot AI review requested due to automatic review settings October 30, 2025 01:52
@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Oct 30, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @NeatGuyCoding, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant refactoring of date and time parsing logic within the console application's backend. The core change involves extracting a new parse_time_range utility function to centralize the conversion of user-provided start and end time strings into UTC datetime objects, while intelligently managing timezone conversions and Daylight Saving Time edge cases. This refactoring is applied consistently across several API endpoints responsible for conversation and application statistics, leading to cleaner, more robust, and reusable code for handling time-based queries.

Highlights

  • Centralized Time Range Parsing: A new utility function, parse_time_range, has been extracted into libs/datetime_utils.py. This function centralizes the logic for parsing start and end time strings, converting them to UTC, and robustly handling timezone complexities, including Daylight Saving Time (DST) ambiguities and non-existent times.
  • Code Refactoring and Reusability: The new parse_time_range function is now utilized across multiple API endpoints in api/controllers/console/app/conversation.py, api/controllers/console/app/statistic.py, and api/controllers/console/app/workflow_statistic.py. This significantly reduces code duplication and improves maintainability for time-based data queries.
  • Improved Error Handling: The parse_time_range function includes robust error handling for invalid time formats and ensures that the start time is not after the end time, raising ValueError or using flask.abort for API responses.
  • Comprehensive Unit Tests: Extensive unit tests have been added for the parse_time_range function, covering various scenarios such as basic parsing, None values, different timezones, invalid inputs, DST transitions (ambiguous and non-existent times), and edge cases.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request does a great job of refactoring the time range parsing logic into a reusable parse_time_range utility function. This significantly reduces code duplication across multiple controller files. The addition of comprehensive unit tests for the new utility, including edge cases for timezones and DST, is also excellent. I've left a few comments for improvement. The main one is a bug fix and refactoring suggestion for the new parse_time_range function to make it more robust and maintainable. I've also pointed out some inconsistencies in how end times for date ranges are handled, which could lead to unexpected behavior for users.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors datetime parsing logic by introducing a centralized parse_time_range utility function to replace duplicated timezone conversion code across multiple controller files.

  • Adds a new parse_time_range function in datetime_utils.py that handles timezone conversion, DST transitions, and input validation
  • Replaces repetitive datetime parsing code in workflow statistics, app statistics, and conversation controllers
  • Adds comprehensive unit tests covering various scenarios including DST handling, timezone conversions, and edge cases

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
api/libs/datetime_utils.py Implements new parse_time_range function with DST handling and timezone conversion
api/tests/unit_tests/libs/test_datetime_utils.py Adds 26 comprehensive test cases for the new utility function
api/controllers/console/app/workflow_statistic.py Refactors 4 endpoints to use the new centralized utility function
api/controllers/console/app/statistic.py Refactors 7 endpoints to use the new centralized utility function
api/controllers/console/app/conversation.py Refactors 2 endpoints to use the new centralized utility function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant