Skip to content

fix(dashboard): incorrect time conversion in MySQL#1058

Merged
looplj merged 1 commit intolooplj:release/v0.9.xfrom
imlonghao:fix/dashboard
Mar 14, 2026
Merged

fix(dashboard): incorrect time conversion in MySQL#1058
looplj merged 1 commit intolooplj:release/v0.9.xfrom
imlonghao:fix/dashboard

Conversation

@imlonghao
Copy link
Copy Markdown
Contributor

在 MySQL 中,CONVERT_TZ 函数的 from_tzto_tz 可以使用 +08:00 以及 Asia/Shanghai 形式

但是如果使用命名时区,需要提前将时区文件导入进来,这并非默认配置

我所使用的是类似 MySQL As A Service 的服务,我没有 root 权限,服务器也没有预设这个时区文件,因此我不能使用命名时区的写法

所以我将其改为了更具有兼容性的写法

internal/server/gql/dashboard_helpers.go 中我把他从 DATE 改成了 DATE_FORMAT 形式,虽然最后这个语句运行得到的都是 2026-03-11,但是前者的类型是 Date,Go 会先转换为 time.Time 再转换为字符串,最后会得到 2026-03-11T00:00:00+08:00 这样一个东西。

因而在进行「最佳渠道性能」的展示时就会有问题,在 buildChannelPerformanceResponse 方法中,把 statsMap 的 key 用 2026-03-11T00:00:00+08:002026-03-11 进行等于比较,显然不存在


修复前

PixPin_2026-03-14_13-12-11 PixPin_2026-03-14_13-12-27 PixPin_2026-03-14_13-13-01

修复后 每日概览 最佳模型性能 最佳渠道性能 均有数据

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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!

此拉取请求旨在解决 MySQL 数据库中时间转换不准确导致仪表盘数据展示异常的问题。通过优化 MySQL 查询中 CONVERT_TZ 函数的时区参数,使其使用更具兼容性的 +HH:MM 格式,并统一日期输出为 YYYY-MM-DD 字符串格式,确保了跨环境的正确性和数据比较的一致性,从而修复了仪表盘中关键性能指标的显示错误。

Highlights

  • MySQL 时区转换兼容性改进: 将 CONVERT_TZ 函数中的时区参数从命名时区(如 Asia/Shanghai)改为更具兼容性的 +HH:MM 格式偏移字符串。这解决了在没有预设时区文件的 MySQL 服务(如 MySQL As A Service)中因权限限制导致的问题。
  • 日期格式化一致性: 在 internal/server/gql/dashboard_helpers.go 中,将 MySQL 的 DATE 函数替换为 DATE_FORMAT,确保日期始终以 YYYY-MM-DD 字符串形式返回。这避免了 Go 语言将 DATE 类型转换为 time.Time 时可能引入的时间部分,从而解决了日期字符串比较不匹配的问题。
  • 修复仪表盘数据展示问题: 通过上述更改,修复了仪表盘中“每日概览”、“最佳模型性能”和“最佳渠道性能”等模块因日期格式不一致导致的数据无法正确显示的问题。
Changelog
  • internal/server/gql/dashboard.resolvers.go
    • 为 MySQL 数据库的 CONVERT_TZ 函数计算并应用了 +HH:MM 格式的时区偏移字符串,替换了原有的 loc.String()
  • internal/server/gql/dashboard_helpers.go
    • 为 MySQL 数据库的 CONVERT_TZ 函数计算并应用了 +HH:MM 格式的时区偏移字符串。
    • DATE(CONVERT_TZ(...)) 表达式更改为 DATE_FORMAT(CONVERT_TZ(...), '%Y-%m-%d'),以确保输出为标准日期字符串。
  • internal/server/gql/qb/throughput_daily.go
    • 为 MySQL 数据库的 CONVERT_TZ 函数计算并应用了 +HH:MM 格式的时区偏移字符串,替换了原有的 timezone 变量。
Activity
  • 未检测到具体的评论、审查或进度更新活动。
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
Copy Markdown
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

这个 PR 通过从命名时区切换到偏移格式,正确地解决了 MySQL 的时区问题。它还通过使用 DATE_FORMAT 修复了类型不匹配的问题。然而,在三个不同的文件中,计算时区偏移字符串的代码存在大量重复。这应该被重构为一个单独的辅助函数,以提高可维护性。此外,当前的偏移计算逻辑对于具有分钟的负时区存在一个 bug,我已经在具体的评论中指出了这一点。

@imlonghao
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
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

这个 PR 修复了在某些 MySQL 环境下因缺少时区文件而导致的 CONVERT_TZ 函数问题,通过将命名时区(如 Asia/Shanghai)改为使用 UTC 偏移量(如 +08:00)来提高兼容性。同时,还将 DATE() 函数改为 DATE_FORMAT() 以确保返回的是格式化后的日期字符串,避免了 Go 在类型转换时产生非预期的带有时区信息的时间戳。整体修改是正确的,并且与 PR 描述的目标一致。我有一个关于 FormatUTCOffset 函数实现的改进建议,可以让代码更简洁和地道。

@imlonghao
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
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

此拉取请求通过将命名时区(例如 'Asia/Shanghai')替换为 UTC 偏移格式(例如 '+08:00'),解决了 MySQL 中的时区转换问题。这提高了与未安装时区文件的 MySQL 环境的兼容性。为此,引入了一个新的辅助函数 xtime.FormatUTCOffset 并附有相应的测试。此更改还统一了 MySQL 查询的日期格式字符串,以防止在 Go 中出现与类型相关的问题,具体细节已在拉取请求描述中说明。这些更改实现得很好,有效地解决了问题。我没有其他意见。

@imlonghao
Copy link
Copy Markdown
Contributor Author

AI 真好用啊 (x

@looplj
Copy link
Copy Markdown
Owner

looplj commented Mar 14, 2026

ci 挂了

@imlonghao
Copy link
Copy Markdown
Contributor Author

CI 好了

@looplj looplj merged commit 4fc5ef7 into looplj:release/v0.9.x Mar 14, 2026
2 checks passed
@imlonghao imlonghao deleted the fix/dashboard branch March 14, 2026 06:55
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