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

fix: Made timestamp consistant #46

Merged
merged 10 commits into from Aug 21, 2020

Conversation

atiqueahmedziad
Copy link
Contributor

Fixes #41

  • log.timeStamp contains timestamp (number).
  • While viewing logs, the timestamp is being formatted into human-readable format i.e. 8:49:31 PM (hh:mm:ss am/pm)
  • When hover the timestamp in log-view, it shows tooltip in the format 17-8-2020 8:49:31 PM
  • The timestamp filter texts also show the same format as tooltip i.e. 17-8-2020 8:49:31 PM

Screenshot

Screenshot 2020-08-17 at 8 52 49 PM

const selectedRow = selectedEl.closest('tr');
if (!selectedEl) {
return;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we have multiple activity log page opened at a time, multiple context menu event listeners are being assigned but we get only one selectedEl (from where the context event has been called)

Update: opened another PR #47 , as this change is unrelated to this PR. Removed this change from here.

export function dateTimeFormat(timestamp) {
const date = new Date(timestamp);
const month = date.getMonth() + 1;
return `${date.getDate()}-${month}-${date.getFullYear()} ${date.toLocaleTimeString()}`;
Copy link
Member

Choose a reason for hiding this comment

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

dd-MM-YYYY looks reasonable to me because the format is the same as where I am from (The Netherlands), but it is not universally true.

Since you're trying to have locale-aware time strings, I suggest to be consistent.

Use the Intl.DateTimeFormat API to format the date in a locale-aware way - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

@@ -33,14 +35,15 @@ export class FilterTimestamp extends HTMLElement {

const timeStamp = this.timeStamp || {};

const chosenTimestamp = selectedRow.querySelector('.timestamp').textContent;
const chosenTimestamp = selectedRow.querySelector('.timestamp')?._timestamp;
Copy link
Member

Choose a reason for hiding this comment

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

Use selectedRow._log.timeStamp instead.

I would rather not have an external class using an internal _log property, but it is preferable to your current approach, of adding a _timeStamp property to an unrelated DOM element.

To reduce the risk of future regressions, it would be nice to have a unit test that verifies that the time labels are rendered as expected.

@atiqueahmedziad atiqueahmedziad force-pushed the fix-timestamp branch 2 times, most recently from 105def4 to 511fd3f Compare August 18, 2020 14:24

export function dateTimeFormat(timestamp, options) {
const dateTime = new Date(timestamp);
const time = dateTime.toLocaleTimeString();
Copy link
Member

Choose a reason for hiding this comment

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

DateTimeFormat also supports parameters to include the time in the generated string. Do not use toLocaleTimeString, but only DateTimeFormat.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done :-)

});

document.body.innerHTML = activityLogBody;
const dateTimeFormatFn = jest.spyOn(ExtListen, 'dateTimeFormat');
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't mock your function, but the API that you're using. The only changes should be:

  • fix 'en-US' as the locale instead of undefined.
  • fix the timeZone option.

By doing both, the generated time strings become fully deterministic / predictable, and you can put the expected times in strings below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done :-) Thanks

expect(dateTimeFormatFn).toHaveBeenCalled();

const dateTime = new Date(logTimestamp);
const expectedTime = dateTime.toLocaleTimeString();
Copy link
Member

Choose a reason for hiding this comment

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

Hard-code the expected strings instead of generating it based on the dynamic objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done :-)

@@ -31,3 +31,19 @@ export function openActivityLogPage() {
url: getActivityLogPageURL(),
});
}

export function dateTimeFormat(timestamp, options) {
Copy link
Member

Choose a reason for hiding this comment

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

Why did you put this function in ext-listen.js instead of a new file called, say, formatters.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, made a new file formatters.js

dateTime
);

return `${date} ${time}`;
Copy link
Member

Choose a reason for hiding this comment

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

Don't create two DateTimeFormat instances followed by string concatenation. Let the value of timeFormatOptions be conditional based on options?.timeOnly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done :-)

// To have consistant date time format, we choose "en-US" date time formatting and UTC timezone.
IntlDateTimeFormatFn.mockImplementation(function (zone, options) {
Object.assign(options, timeZone);
this.format = (date) =>
Copy link
Member

Choose a reason for hiding this comment

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

Don't replace the format method; just let the constructor return new originalIntlDateTimeFormat('en-US', options);.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done :-)

const { activityLog } = new ActivityLog();
// To have consistant date time format, we choose "en-US" date time formatting and UTC timezone.
IntlDateTimeFormatFn.mockImplementation((zone, options) => {
Object.assign(options, timeZone);
Copy link
Member

Choose a reason for hiding this comment

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

Mocks should not modify the input object, as that may have side effects.
Do this instead:

options = {...options, timeZone: 'UTC'}

@@ -34,6 +34,9 @@ export default class ExtensionMonitor {

createLogListener() {
return async (details) => {
// set a timestamp(number) as the value of timeStamp property
details.timeStamp = Date.parse(details.timeStamp);
Copy link
Member

Choose a reason for hiding this comment

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

Could you add the next comment:

// TODO: Stop using `Date.parse` when `details.timeStamp` is a numeric timestamp.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1660460

@atiqueahmedziad atiqueahmedziad merged commit 68d5194 into mozilla:master Aug 21, 2020
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.

timestamp should be consistant
2 participants