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

Fixing time zone problems #1624

Merged
merged 5 commits into from
Sep 17, 2019
Merged

Conversation

lancewf
Copy link
Contributor

@lancewf lancewf commented Sep 16, 2019

πŸ”© Description: What code changed, and why?

Fixing a bug where the compliance reporting page would not allow the user in UTC+1 view the current day. The problem was the reportQuery state was being changed unintentionally. There was also some sections of code that when converting from moment to date was switching the current hour because of time zones.

πŸ‘ Definition of Done

When in timezone UTC+1 the current date can be selected.

πŸ‘Ÿ How to Build and Test the Change

  1. Switch your local computer to London time (UTC+1)
  2. build components/automate-ui && start_all_services
  3. Open the https://a2-dev.test/compliance/reports/overview
  4. Ensure today's UTC date is selected.
  5. Ensure the date can be switched to yesterday back to today.

βœ… Checklist

  • Tests added/updated?
  • Docs added/updated?

πŸ“· Screenshots, if applicable

Lance Finfrock added 2 commits September 16, 2019 14:10
Signed-off-by: Lance Finfrock <lfinfrock@chef.io>
Signed-off-by: Lance Finfrock <lfinfrock@chef.io>
endDate: reportQuery.endDate.clone(),
interval: reportQuery.interval,
filters: Object.assign([], reportQuery.filters)
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Returning a clone of the current state to avoid the state changing unintentionally


apiFilters.push({type: 'start_time', values: [value.format()]});
}

if (reportQuery.endDate) {
const now = moment();
const value = reportQuery.endDate
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the real bug. The hour for the state was being updated here.

@@ -445,7 +446,7 @@ export class ReportingComponent implements OnInit, OnDestroy {
const foundFilter = urlFilters.find( (filter: Chicklet) => filter.type === 'end_time');

if (foundFilter !== undefined) {
const endDate = moment(foundFilter.text, 'YYYY-MM-DD');
const endDate = moment(foundFilter.text + 'GMT+00:00', 'YYYY-MM-DDZ');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

parsing the date as UTC. Before the date would come in as the local users timezone.

@@ -180,7 +180,8 @@ export class ReportingComponent implements OnInit, OnDestroy {
const allUrlParameters$ = this.getAllUrlParameters();

this.endDate$ = this.reportQuery.state.pipe(map((reportQuery: ReportQuery) =>
reportQuery.endDate.toDate()));
new Date(reportQuery.endDate.year(), reportQuery.endDate.month(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Converting the moment to a date object without any time zone conversion. Where before the date's hours would be offset by the user's local timezone.

Lance Finfrock added 2 commits September 16, 2019 15:29
Signed-off-by: Lance Finfrock <lfinfrock@chef.io>
Signed-off-by: Lance Finfrock <lfinfrock@chef.io>
component.applyParamFilters([
{type: 'chef_tags', text: '123'},
{type: 'end_time', text: '2019-09-05'}]);
expect(reportQueryService.setState).toHaveBeenCalledWith(
Copy link
Contributor Author

@lancewf lancewf Sep 16, 2019

Choose a reason for hiding this comment

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

This test would not pass anymore even though the same objects were displayed in the error. The 'getEndDate' test covers the same test case.

@lancewf lancewf added automate-ui bug πŸ› Something isn't working ui labels Sep 16, 2019
@lancewf lancewf marked this pull request as ready for review September 16, 2019 23:40
@lancewf lancewf requested a review from a team September 16, 2019 23:40
startDate: reportQuery.startDate.clone(),
endDate: reportQuery.endDate.clone(),
interval: reportQuery.interval,
filters: Object.assign([], reportQuery.filters)
Copy link
Contributor

Choose a reason for hiding this comment

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

Cleaner pattern for cloning that we (auth team) have been introducing in recent months--use the spread operator:

Suggested change
filters: Object.assign([], reportQuery.filters)
filters: [...portQuery.filters]

@@ -68,6 +68,18 @@ describe('ReportingComponent', () => {
expect(qe).not.toBeNull();
});

it('convertMomentToDateWithoutTimezone', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

For consistency should be a verb-phrase rather than a noun, perhaps for this test:

Suggested change
it('convertMomentToDateWithoutTimezone', () => {
it('maps hour correctly without timezone', () => {

and for the next test, looks like with timezone...?

@@ -68,6 +68,18 @@ describe('ReportingComponent', () => {
expect(qe).not.toBeNull();
});

it('convertMomentToDateWithoutTimezone', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

These new tests are, I understand, specifically to cover the bug. But consider expanding them with a few other key values, typically around boundaries. There's a test helper using in spec-helpers.ts, that makes it easy to test all equivalence classes:

  using([
    [23, 'an hour before end of day'],
    [0, 'exactly end of day'],
    [1, 'an hour after end of day']
  ], function (hour: number, description: string) {
    it(`maps hour correctly without timezone for ${description}`, () => {
      const dateBefore = moment(`20191023-${hour}00`, 'YYYYMMDD-HHMM');
      const dateAfter = component.convertMomentToDateWithoutTimezone(dateBefore);
      expect(dateBefore.hour()).toEqual(dateAfter.getHours());
    });
  });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. The using block is a nice tool.

@@ -253,7 +253,7 @@ export class ReportingComponent implements OnInit, OnDestroy {
this.downloadOptsVisible = false;

const reportQuery = this.reportQuery.getReportQuery();
const filename = `${moment(reportQuery.endDate).format('YYYY-M-D')}.${format}`;
const filename = `${reportQuery.endDate.format('YYYY-M-D')}.${format}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest constant width for easy filename sorting at the command line:

Suggested change
const filename = `${reportQuery.endDate.format('YYYY-M-D')}.${format}`;
const filename = `${reportQuery.endDate.format('YYYY-MM-DD')}.${format}`;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't feel comfortable making this change with this PR.

second: now.get('second')
}).utc().endOf('day');

const value = reportQuery.endDate.clone().utc().endOf('day');
Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty sure you do not need the clone() there as you are evaluating a functional sequence.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is what I thought from my original PR, but the endOf('day') function changes the original object. https://momentjs.com/docs/#/manipulating/end-of/

Copy link

@vjeffrey vjeffrey left a comment

Choose a reason for hiding this comment

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

pulled the change down locally and ensure the correct date was selectable for my timezone as well as those across the pond

Signed-off-by: Lance Finfrock <lfinfrock@chef.io>
@lancewf lancewf merged commit f74a640 into master Sep 17, 2019
@chef-expeditor chef-expeditor bot deleted the lancewf/compliance_reporting_fix_timezone branch September 17, 2019 17:08
@SEAjamieD SEAjamieD mentioned this pull request Jul 2, 2020
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automate-ui bug πŸ› Something isn't working ui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants