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

refactor(moment): Replace Moment.js with DayJs #31310

Merged
merged 22 commits into from
Dec 23, 2024

Conversation

msyavuz
Copy link
Contributor

@msyavuz msyavuz commented Dec 5, 2024

SUMMARY

This pr aims to replace Moment.js with Day.js as the former is a legacy project and is in maintenance mode. There were some design decisions made:

  1. Datepicker component in Ant Design v4 accepts Moment object as props. There are couple options to remedy that:

I decided on the last option since other two seemed overkill for a temporary hurdle.

  1. Extensions
    Day.js object needed to be extended to support our usages. I centralized that in extendedDayjs to prevent confusion. Alternative to this is to extend where its used.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

Run the testing suite.

ADDITIONAL INFORMATION

  • Has associated issue: Fixes Remove Moment.js in favor of Day.js #31178
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@rusackas rusackas requested a review from geido December 18, 2024 16:50
@rusackas
Copy link
Member

It sounds like Ant Design v4 is one hangup here. Pinging @geido since we're in the process of upgrading from v4 to v5, one component at a time. Would v5's Datepicker component mitigate this issue? If so, perhaps we can prioritize that, to get this PR through more easily.

@msyavuz
Copy link
Contributor Author

msyavuz commented Dec 18, 2024

@rusackas This PR and the Datepicker PR kind of depend on each other since Ant design v4 uses Moment and v5 uses Dayjs. In my opinion finishing the Moment to Dayjs migration on datepicker PR is the most straightforward way.

@msyavuz msyavuz marked this pull request as ready for review December 18, 2024 17:02
@dosubot dosubot bot added change:frontend Requires changing the frontend frontend:refactor Related to refactoring the frontend labels Dec 18, 2024
Copy link

@korbit-ai korbit-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.

Review by Korbit AI

Category Issue
Functionality Incorrect Time Unit Documentation ▹ view
Functionality Missing Day.js Locale Plugin ▹ view
Functionality Incorrect Duration Calculation Method ▹ view
Functionality Redundant Timezone Conversion ▹ view
Files scanned
File Path Reviewed
superset-frontend/src/components/CachedLabel/TooltipContent.tsx
superset-frontend/src/components/Timer/Timer.stories.tsx
superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/utils.ts
superset-frontend/src/components/ListView/Filters/DateRange.tsx
superset-frontend/src/preamble.ts
superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx
superset-frontend/src/explore/components/controls/DateFilterControl/utils/dateParser.ts
superset-frontend/src/components/LastUpdated/index.tsx
superset-frontend/src/utils/dates.ts
superset-frontend/src/features/allEntities/AllEntitiesTable.tsx
superset-frontend/src/pages/ExecutionLogList/index.tsx
superset-frontend/src/visualizations/TimeTable/SparklineCell.tsx
superset-frontend/plugins/plugin-chart-echarts/src/BigNumber/BigNumberPeriodOverPeriod/transformProps.ts
superset-frontend/src/features/home/ActivityTable.tsx
superset-frontend/src/dashboard/components/OverwriteConfirm/OverwriteConfirmModal.tsx
superset-frontend/src/features/datasets/AddDataset/EditDataset/UsageTab/index.tsx
superset-frontend/src/pages/AllEntities/index.tsx
superset-frontend/src/explore/components/controls/TimeOffsetControl.tsx
superset-frontend/src/components/TimezoneSelector/index.tsx
superset-frontend/src/features/annotations/AnnotationModal.tsx
superset-frontend/src/pages/AnnotationList/index.tsx
superset-frontend/src/SqlLab/components/QueryTable/index.tsx
superset-frontend/src/SqlLab/components/SaveDatasetModal/index.tsx
superset-frontend/src/pages/QueryHistoryList/index.tsx
superset-frontend/src/dashboard/components/SliceHeaderControls/index.tsx
superset-frontend/src/pages/AlertReportList/index.tsx
superset-frontend/webpack.config.js
superset-frontend/src/components/Chart/chartAction.js
superset-frontend/src/dashboard/components/Header/index.jsx
superset-frontend/plugins/legacy-preset-chart-nvd3/src/NVD3Vis.js

Explore our documentation to understand the languages and file types we support and the files we ignore.

Need a new review? Comment /korbit-review on this PR and I'll review your latest changes.

Korbit Guide: Usage and Customization

Interacting with Korbit

  • You can manually ask Korbit to review your PR using the /korbit-review command in a comment at the root of your PR.
  • You can ask Korbit to generate a new PR description using the /korbit-generate-pr-description command in any comment on your PR.
  • Too many Korbit comments? I can resolve all my comment threads if you use the /korbit-resolve command in any comment on your PR.
  • Chat with Korbit on issues we post by tagging @korbit-ai in your reply.
  • Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.

Customizing Korbit

  • Check out our docs on how you can make Korbit work best for you and your team.
  • Customize Korbit for your organization through the Korbit Console.

Current Korbit Configuration

General Settings
Setting Value
Review Schedule Automatic excluding drafts
Max Issue Count 10
Automatic PR Descriptions
Issue Categories
Category Enabled
Naming
Database Operations
Documentation
Logging
Error Handling
Systems and Environment
Objects and Data Structures
Readability and Maintainability
Asynchronous Processing
Design Patterns
Third-Party Libraries
Performance
Security
Functionality

Feedback and Support

Note

Korbit Pro is free for open source projects 🎉

Looking to add Korbit to your team? Get started with a free 2 week trial here

Comment on lines +48 to +51
export const now = function (): number {
// seconds from EPOCH as a float
return dayjs().utc().valueOf();
};
Copy link

Choose a reason for hiding this comment

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

Incorrect Time Unit Documentation category Functionality

Tell me more
What is the issue?

The comment is incorrect as valueOf() returns milliseconds since epoch, not seconds

Why this matters

This misleading comment could cause integration issues if other developers rely on the comment's description of the return value being in seconds.

Suggested change

export const now = function (): number {
// milliseconds from EPOCH as a number
return dayjs().utc().valueOf();
};

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

@@ -44,7 +44,7 @@ const bootstrapData = getBootstrapData();
// Configure translation
if (typeof window !== 'undefined') {
configure({ languagePack: bootstrapData.common.language_pack });
moment.locale(bootstrapData.common.locale);
dayjs.locale(bootstrapData.common.locale);
Copy link

Choose a reason for hiding this comment

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

Missing Day.js Locale Plugin category Functionality

Tell me more
What is the issue?

Day.js locale support requires loading the appropriate locale plugin before setting the locale.

Why this matters

Without loading the locale plugin first, the locale setting will have no effect, causing date formatting to fallback to default English locale regardless of user preferences.

Suggested change

Import and load the Day.js locale plugin before setting the locale:

import dayjs from 'dayjs';
import LocalizedFormat from 'dayjs/plugin/localizedFormat';

dayjs.extend(LocalizedFormat);
Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

Comment on lines +38 to +46
export const fDuration = function (
t1: number,
t2: number,
format = 'HH:mm:ss.SSS',
): string {
const diffSec = t2 - t1;
const duration = dayjs(new Date(diffSec));
return duration.utc().format(format);
};
Copy link

Choose a reason for hiding this comment

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

Incorrect Duration Calculation Method category Functionality

Tell me more
What is the issue?

The fDuration function incorrectly calculates duration by creating a date from the difference in milliseconds instead of using dayjs.duration()

Why this matters

This will produce incorrect duration formatting as it treats the millisecond difference as an epoch timestamp rather than a duration, leading to unexpected results when formatting the time difference.

Suggested change

export const fDuration = function (
t1: number,
t2: number,
format = 'HH:mm:ss.SSS',
): string {
return dayjs.duration(t2 - t1).format(format);
};

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

@@ -82,43 +70,40 @@ export default function TimezoneSelector({
const getTimezoneName = (name: string) => {
const offsets = getOffsetKey(name);
return (
(currentDate.tz(name).isDST()
(isDST(currentDate.tz(name), name)
Copy link

Choose a reason for hiding this comment

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

Redundant Timezone Conversion category Functionality

Tell me more
What is the issue?

Double timezone conversion might occur as the date is already converted to the timezone before being passed to isDST, which might also perform a timezone conversion.

Why this matters

Unnecessary timezone conversions can lead to edge case bugs around DST boundaries and increase the chance of timezone-related calculation errors.

Suggested change

Pass the unconverted currentDate to isDST and let it handle the timezone conversion: isDST(currentDate, name)

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

@geido geido added preset:bounty Issues that have been selected by Preset and have a bounty attached. hold! On hold labels Dec 18, 2024
Copy link
Member

@geido geido left a comment

Choose a reason for hiding this comment

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

Great job! A few nits

superset-frontend/src/pages/AnnotationList/index.tsx Outdated Show resolved Hide resolved
superset-frontend/src/pages/AnnotationList/index.tsx Outdated Show resolved Hide resolved
@@ -119,7 +119,7 @@ function ExecutionLog({
original: { scheduled_dttm: scheduledDttm },
},
}: any) =>
Copy link
Member

Choose a reason for hiding this comment

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

Can we now use a better type than any?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For this one i think keeping any is ok still

superset-frontend/src/pages/ExecutionLogList/index.tsx Outdated Show resolved Hide resolved
@geido geido removed the hold! On hold label Dec 23, 2024
@geido geido merged commit a193d79 into apache:master Dec 23, 2024
35 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change:frontend Requires changing the frontend dependencies:npm frontend:refactor Related to refactoring the frontend plugins preset:bounty Issues that have been selected by Preset and have a bounty attached. size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove Moment.js in favor of Day.js
5 participants