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

Convert interval seconds to days, hours, minutes, and seconds #5220

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

Conversation

Vivek-Py
Copy link

@Vivek-Py Vivek-Py commented Oct 18, 2024

⚠️⚠️⚠️ Since we do not accept all types of pull requests and do not want to waste your time. Please be sure that you have read pull request rules:
https://github.com/louislam/uptime-kuma/blob/master/CONTRIBUTING.md#can-i-create-a-pull-request-for-uptime-kuma

Tick the checkbox if you understand [x]:

  • I have read and understand the pull request rules.

Description

Converts the interval from seconds to "days, hours, minutes and seconds" (depending if it is larger than each) and display the result under the form input. Have ensured it works for both locale directions as well.

Fixes #3601

Type of change

Please delete any options that are not relevant.

  • User interface (UI)

Checklist

  • My code follows the style guidelines of this project
  • I ran ESLint and other linters for modified files
  • I have performed a self-review of my own code and tested it
  • I have commented my code, particularly in hard-to-understand areas (including JSDoc for methods)
  • My changes generates no new warnings
  • My code needed automated testing. I have added them (this is optional task)

Screenshots (if any)

sample1
sample2

Please do not use any external image service. Instead, just paste in or drag and drop the image here, and it will be uploaded automatically.

Copy link
Collaborator

@CommanderStorm CommanderStorm left a comment

Choose a reason for hiding this comment

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

Looks reasonable, I have left a few suggestions below.
Especially the unittests would be appreciated.

src/util-frontend.js Outdated Show resolved Hide resolved
src/util-frontend.js Outdated Show resolved Hide resolved
src/util-frontend.js Outdated Show resolved Hide resolved
})
.join("");
formattedString = formattedString.trim();
parts.push(formattedString);
Copy link
Collaborator

Choose a reason for hiding this comment

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

The controllflow in this function is quite non-obvious. Please refactor this to not abuse scoping as heavily ^^

Copy link
Author

Choose a reason for hiding this comment

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

I have refactored the control flow, made it easy and intuitive to understand.

const toFormattedPart = (value, unitOfTime) => {
const res = this.getInstance().formatToParts(value, unitOfTime);
console.log(res);
let formattedString = res
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is happening here? Is this and following lines necessary?

Copy link
Author

Choose a reason for hiding this comment

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

Refactored, and added comments for better understanding.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I asked because the filtering you are doing seems ineffective (literal and number are the only alowed values for type).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts

Also, I don't think that the trimming is nessesary.
Also the index > 0 is likely incorrect in at least some languages.

I think you are doing this to get rid of the in prefix.
Given that the proper way (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DurationFormat/format) is not yet supported in firefox (https://bugzilla.mozilla.org/show_bug.cgi?id=1648139)

=> I think the best change to do here (after looking into this a bit) would be to switch to the new api and wait for merging until firefox supports it.

src/util-frontend.js Outdated Show resolved Hide resolved
src/util-frontend.js Outdated Show resolved Hide resolved
* @param {number} seconds Receive value in seconds.
* @returns {string} String converted to Days Mins Seconds Format
*/
secondsToHumanReadableFormat(seconds) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a few quick unit tests to ensure that this works as expected?

Copy link
Author

Choose a reason for hiding this comment

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

I'm seeing e2e tests but no unit tests for frontend, do we have the setup for it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like the (previously existing) frontend tests such as the one for currentLocale() (#4692) has been removed without re-adding them.

You can add a folder frontend-test and add a testcase there.
This might need to be added to the npm run .. commands to be executed in CI.

Comment on lines 241 to 246
updateLocale(locale, options = {}) {
this.locale = locale;
this.options = {
...this.options,
...options,
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see why we would want to change the options on this. It is shared => would apply globally, right?

Suggested change
updateLocale(locale, options = {}) {
this.locale = locale;
this.options = {
...this.options,
...options,
};
updateLocale(locale) {
this.locale = locale;

Copy link
Author

@Vivek-Py Vivek-Py Oct 19, 2024

Choose a reason for hiding this comment

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

Yes it is shared and would be applied globally. Have removed it. Thanks!

* Default locale and options for Relative Time Formatter
*/
constructor() {
this.locale = currentLocale();
Copy link
Collaborator

Choose a reason for hiding this comment

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

As used, I don't think we need to store the locale.
It is always directly used to construct the RelativeTimeFormat below

=> Let's make this unit a local variable instead

Copy link
Author

Choose a reason for hiding this comment

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

Right, refactored to get locale while initializing instance.

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.

Text that convert interval seconds to days, hours, minutes, seconds
2 participants