Skip to content

feat(ui): Convert interval seconds to days, hours, minutes, and seconds#5220

Merged
CommanderStorm merged 11 commits intolouislam:masterfrom
Vivek-Py:human-readable-format-for-intervals
Jul 26, 2025
Merged

feat(ui): Convert interval seconds to days, hours, minutes, and seconds#5220
CommanderStorm merged 11 commits intolouislam:masterfrom
Vivek-Py:human-readable-format-for-intervals

Conversation

@Vivek-Py
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
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.

})
.join("");
formattedString = formattedString.trim();
parts.push(formattedString);
Copy link
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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.

* @param {number} seconds Receive value in seconds.
* @returns {string} String converted to Days Mins Seconds Format
*/
secondsToHumanReadableFormat(seconds) {
Copy link
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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.

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

Think this is uncontroversial enough to be merged into v2.0, even though we are still past beta-2

@CommanderStorm CommanderStorm modified the milestones: 2.0.0-beta.3, Pending Apr 26, 2025
@CommanderStorm
Copy link
Copy Markdown
Collaborator

@Vivek-Py The testcaseses are failing. Could you have a look at this

We can re-group into v2.0 if this is fixed. ^^

@Vivek-Py
Copy link
Copy Markdown
Contributor Author

@CommanderStorm I have fixed the test, it was related to humanReadableFormat being sent to the DB.

@CommanderStorm CommanderStorm changed the title Convert interval seconds to days, hours, minutes, and seconds feat(ui): Convert interval seconds to days, hours, minutes, and seconds Jul 26, 2025
@CommanderStorm CommanderStorm modified the milestones: Pending, 2.0.0-beta.4 Jul 26, 2025
@CommanderStorm CommanderStorm merged commit c1adcfb into louislam:master Jul 26, 2025
19 checks passed
@CommanderStorm
Copy link
Copy Markdown
Collaborator

Thanks a ton <3

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