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.

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

Choose a reason for hiding this comment

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

Please do a bit more thorough self-review in the future

Suggested change
console.log(res);

Copy link
Author

Choose a reason for hiding this comment

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

Ack, have removed the log.

* Method to get the singleton instance
* @returns {object} Intl.RelativeTimeFormat instance
*/
getInstance() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's inline this. As it is being used, you can just use the field as is.

Also I don't think this is a singleton

Copy link
Author

Choose a reason for hiding this comment

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

Yes, makes sense. Have removed this function, instance can be accessed directly.

}
}

export const rtf = new RelativeTimeFormatter();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please export it under a non-hamming-shortend name, so future devs don't need to look up the definition (this goes for a few other variables as well)

For common names like res this is fine, but not for instances like here

Copy link
Author

Choose a reason for hiding this comment

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

Understood, have updated the naming. Lmk if it aligns with the naming convention.

})
.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.


const result =
parts.length > 0
? `~ ${parts.join(" ")}`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why approximately? All time parts are added exactly

Copy link
Author

Choose a reason for hiding this comment

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

Removed, not required.

const result =
parts.length > 0
? `~ ${parts.join(" ")}`
: this.getInstance().format(0, "second"); // Handle case for 0 seconds
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's use if .. return here instead

Copy link
Author

Choose a reason for hiding this comment

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

Updated to use if .. return

* @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?

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